author | Claus Gittinger <cg@exept.de> |
Thu, 22 Oct 2009 19:34:44 +0200 | |
changeset 12331 | 02c0437d7817 |
parent 11945 | 8697e2333da3 |
child 12486 | f1ef886832f1 |
permissions | -rw-r--r-- |
1197 | 1 |
" |
2 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3158 | 3 |
All Rights Reserved |
1197 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5411 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1197 | 14 |
LimitedPrecisionReal variableByteSubclass:#ShortFloat |
15 |
instanceVariableNames:'' |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
16 |
classVariableNames:'' |
1197 | 17 |
poolDictionaries:'' |
18 |
category:'Magnitude-Numbers' |
|
19 |
! |
|
20 |
||
2394 | 21 |
!ShortFloat primitiveDefinitions! |
22 |
%{ |
|
23 |
||
3471 | 24 |
#include <errno.h> |
25 |
||
26 |
#ifndef __OPTIMIZE__ |
|
27 |
# define __OPTIMIZE__ |
|
28 |
#endif |
|
4825 | 29 |
|
30 |
#define __USE_ISOC9X 1 |
|
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
31 |
#define __USE_ISOC99 1 |
3471 | 32 |
#include <math.h> |
33 |
||
34 |
/* |
|
35 |
* on some systems errno is a macro ... check for it here |
|
36 |
*/ |
|
37 |
#ifndef errno |
|
38 |
extern errno; |
|
39 |
#endif |
|
40 |
||
2394 | 41 |
#if defined (_AIX) |
42 |
# include <float.h> |
|
43 |
#endif |
|
4825 | 44 |
|
2395 | 45 |
#if defined(IRIX) |
46 |
# include <nan.h> |
|
47 |
#endif |
|
4825 | 48 |
|
2397 | 49 |
#if defined(LINUX) |
4825 | 50 |
# ifndef NAN |
5822
4e56f8aadda5
Get nan.h from bits/nan.h for Linux
Stefan Vogel <sv@exept.de>
parents:
5718
diff
changeset
|
51 |
# include <bits/nan.h> |
4825 | 52 |
# endif |
2397 | 53 |
#endif |
4825 | 54 |
|
2400 | 55 |
#if defined(solaris) || defined(sunos) |
2398 | 56 |
# include <nan.h> |
57 |
#endif |
|
2394 | 58 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
59 |
#ifdef WIN32 |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
60 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
61 |
* no finite(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
62 |
* no isnan(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
63 |
*/ |
7419 | 64 |
# ifndef isnan |
65 |
# define isnan(x) \ |
|
11945 | 66 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
67 |
(((unsigned int *)(&x))[1] == 0xFFF80000)) |
|
7419 | 68 |
# endif |
69 |
||
70 |
# ifndef isPositiveInfinity |
|
71 |
# define isPositiveInfinity(x) \ |
|
11945 | 72 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
73 |
(((unsigned int *)(&x))[1] == 0x7FF00000)) |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
74 |
# endif |
7419 | 75 |
|
76 |
# ifndef isNegativeInfinity |
|
77 |
# define isPositiveInfinity(x) \ |
|
11945 | 78 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
79 |
(((unsigned int *)(&x))[1] == 0xFFF00000)) |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
80 |
# endif |
7419 | 81 |
|
82 |
# ifndef isinf |
|
83 |
# define isinf(x) \ |
|
11945 | 84 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
85 |
((((unsigned int *)(&x))[1] & 0x7FF00000) == 0x7FF00000)) |
|
7419 | 86 |
# endif |
87 |
||
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
88 |
# ifndef isfinite |
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
89 |
# define isfinite(x) (!isinf(x) && !isnan(x)) |
7419 | 90 |
# endif |
91 |
||
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
92 |
# ifndef isnanf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
93 |
# define isnanf(x) \ |
11945 | 94 |
(((unsigned int *)(&x))[0] == 0xFFC00000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
95 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
96 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
97 |
# ifndef isPositiveInfinityf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
98 |
# define isPositiveInfinityf(x) \ |
11945 | 99 |
(((unsigned int *)(&x))[0] == 0x7F800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
100 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
101 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
102 |
# ifndef isNegativeInfinityf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
103 |
# define isNegativeInfinityf(x) \ |
11945 | 104 |
(((unsigned int *)(&x))[0] == 0xFF800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
105 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
106 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
107 |
# ifndef isinff |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
108 |
# define isinff(x) \ |
11945 | 109 |
((((unsigned int *)(&x))[0] & 0x7FFFFFFF) == 0x7F800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
110 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
111 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
112 |
# ifndef isfinitef |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
113 |
# define isfinitef(x) (!isinff(x) && !isnanf(x)) |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
114 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
115 |
|
7419 | 116 |
# define NO_ASINH |
117 |
# define NO_ACOSH |
|
118 |
# define NO_ATANH |
|
8706 | 119 |
#endif /* WIN32 */ |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
120 |
|
8984 | 121 |
#ifdef solaris |
122 |
# ifndef isfinite |
|
123 |
# define isfinite(f) finite((double)(f)) |
|
124 |
# endif |
|
125 |
#endif |
|
126 |
||
3414 | 127 |
#ifdef realIX |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
128 |
# ifndef isfinite |
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
129 |
# define isfinite(x) 1 |
3414 | 130 |
# endif |
8706 | 131 |
#endif /* realIX */ |
132 |
||
9896 | 133 |
#ifndef isfinitef |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
134 |
# define isfinitef(x) isfinite(x) |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
135 |
#endif |
9896 | 136 |
#ifndef isnanf |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
137 |
# define isnanf(x) isnan(x) |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
138 |
#endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
139 |
|
2394 | 140 |
%} |
141 |
! ! |
|
142 |
||
1880 | 143 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 144 |
|
145 |
copyright |
|
146 |
" |
|
147 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3158 | 148 |
All Rights Reserved |
1197 | 149 |
|
150 |
This software is furnished under a license and may be used |
|
151 |
only in accordance with the terms of that license and with the |
|
152 |
inclusion of the above copyright notice. This software may not |
|
153 |
be provided or otherwise made available to, or used by, any |
|
154 |
other person. No title to or ownership of the software is |
|
155 |
hereby transferred. |
|
156 |
" |
|
157 |
||
158 |
||
159 |
! |
|
160 |
||
161 |
documentation |
|
162 |
" |
|
11945 | 163 |
ShortFloats represent rational numbers with limited precision. |
7380 | 164 |
They use the C-compilers 'float' format, which is usually the IEE single float format. |
165 |
||
166 |
In contrast to Floats (which use the C-compilers 64bit 'double' format), |
|
167 |
ShortFloats give you 32 bit floats. |
|
1197 | 168 |
|
169 |
Notice, that ST/X Floats are what Doubles are in ST-80 and ShortFloats are |
|
170 |
ST-80's Floats respectively. |
|
11945 | 171 |
This may change in one of the next versions (at least on machines, which |
1197 | 172 |
provide different float and double types in their C-compiler. |
173 |
||
174 |
WARNING: |
|
11945 | 175 |
The layout of shortFloat instances is known by the runtime system and the compiler; |
176 |
you may not add instance variables here. |
|
177 |
Also, subclassing is complicated by the fact, that the VM creates floats/shortFloats, |
|
178 |
and does some of its float-checks by an identity compare with the ShortFloat-class. |
|
179 |
(i.e. your subclasses instances may not be recognized as float-like objects, |
|
180 |
thus mixed mode arithmetic will always coerce them, effectively slowing things down). |
|
181 |
This may be changed, to use a flag bit in the class. |
|
1197 | 182 |
|
7380 | 183 |
Mixed mode arithmetic: |
11945 | 184 |
shortFloat op shortFloat -> shortFloat |
185 |
shortFloat op fix -> shortFloat |
|
186 |
shortFloat op fraction -> shortFloat |
|
187 |
shortFloat op integer -> shortFloat |
|
188 |
shortFloat op longFloat -> longFloat |
|
189 |
shortFloat op float -> float |
|
190 |
shortFloat op complex -> complex |
|
1295 | 191 |
|
7405 | 192 |
Representation: |
11945 | 193 |
32bit single precision IEE floats |
194 |
23 bit mantissa, |
|
195 |
8 bit exponent, |
|
196 |
6 decimal digits (approx) |
|
7405 | 197 |
|
198 |
Range and Precision of Storage Formats: see LimitedPrecisionReal >> documentation |
|
199 |
||
1295 | 200 |
[author:] |
11945 | 201 |
Claus Gittinger |
1556 | 202 |
|
203 |
[see also:] |
|
11945 | 204 |
Number |
205 |
Float LongFloat Fraction FixedPoint Integer Complex |
|
206 |
FloatArray DoubleArray |
|
1197 | 207 |
" |
208 |
! ! |
|
209 |
||
1880 | 210 |
!ShortFloat class methodsFor:'instance creation'! |
1207 | 211 |
|
212 |
basicNew |
|
213 |
"return a new shortFloat - here we return 0.0 |
|
214 |
- shortFloats are usually NOT created this way ... |
|
215 |
Its implemented here to allow things like binary store & load |
|
216 |
of shortFloats. (but even this support will go away eventually, its not |
|
217 |
a good idea to store the bits of a float - the reader might have a |
|
11945 | 218 |
totally different representation - so floats will eventually be |
1207 | 219 |
binary stored in a device independent format." |
220 |
||
221 |
%{ /* NOCONTEXT */ |
|
222 |
OBJ newFloat; |
|
223 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
224 |
__qMKSFLOAT(newFloat, 0.0); |
1207 | 225 |
RETURN (newFloat); |
226 |
%} |
|
3158 | 227 |
! |
228 |
||
4297 | 229 |
fastFromString:aString at:startIndex |
11945 | 230 |
"return the next ShortFloat from the string starting at startIndex. |
4297 | 231 |
No spaces are skipped. |
232 |
||
233 |
This is a specially tuned entry (using a low-level C-call), which |
|
234 |
returns garbage if the argument string is not a valid float number. |
|
11945 | 235 |
It has been added to allow higher speed string decomposition into numbers, |
236 |
especially for mass-data." |
|
237 |
||
238 |
%{ /* NOCONTEXT */ |
|
4297 | 239 |
if (__isString(aString) && __isSmallInteger(startIndex)) { |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
240 |
char *cp = (char *)(__stringVal(aString)); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
241 |
int idx = __intVal(startIndex) - 1; |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
242 |
double atof(); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
243 |
double val; |
4297 | 244 |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
245 |
if ((unsigned)idx < __stringSize(aString)) { |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
246 |
val = atof(cp + idx); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
247 |
RETURN (__MKSFLOAT(val)); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
248 |
} |
4297 | 249 |
} |
250 |
%}. |
|
251 |
self primitiveFailed. |
|
252 |
||
253 |
" |
|
11945 | 254 |
ShortFloat fastFromString:'123.45' at:1 |
255 |
ShortFloat fastFromString:'123.45' at:2 |
|
256 |
ShortFloat fastFromString:'123.45E4' at:1 |
|
257 |
ShortFloat fastFromString:'hello123.45E4' at:6 |
|
258 |
ShortFloat fastFromString:'12345' at:1 |
|
259 |
ShortFloat fastFromString:'12345' at:2 |
|
260 |
ShortFloat fastFromString:'12345' at:3 |
|
261 |
ShortFloat fastFromString:'12345' at:4 |
|
262 |
ShortFloat fastFromString:'12345' at:5 |
|
263 |
ShortFloat fastFromString:'12345' at:6 |
|
264 |
ShortFloat fastFromString:'12345' at:0 |
|
265 |
ShortFloat fastFromString:'hello123.45E4' at:1 |
|
4297 | 266 |
|
267 |
Time millisecondsToRun:[ |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
268 |
100000 timesRepeat:[ |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
269 |
ShortFloat readFrom:'123.45' |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
270 |
] |
4297 | 271 |
] |
272 |
" |
|
273 |
||
274 |
" |
|
275 |
Time millisecondsToRun:[ |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
276 |
100000 timesRepeat:[ |
11945 | 277 |
ShortFloat fastFromString:'123.45' at:1 |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
278 |
] |
4297 | 279 |
] |
280 |
" |
|
281 |
||
282 |
||
283 |
||
284 |
! |
|
285 |
||
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
286 |
readFrom:aStringOrStream |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
287 |
"read a shortFloat from a string" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
288 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
289 |
|num| |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
290 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
291 |
num := super readFrom:aStringOrStream onError:[self error:'conversion error for: ' , self name]. |
11945 | 292 |
num notNil ifTrue:[ |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
293 |
num := num asShortFloat |
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
294 |
]. |
11945 | 295 |
^ num |
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
296 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
297 |
" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
298 |
ShortFloat readFrom:'0.1' |
11945 | 299 |
ShortFloat readFrom:'0' |
300 |
ShortFloat readFrom:'.123' |
|
301 |
ShortFloat readFrom:'-.123' |
|
302 |
ShortFloat readFrom:'1e4' |
|
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
303 |
" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
304 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
305 |
"Modified: / 7.1.1998 / 16:17:59 / cg" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
306 |
! |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
307 |
|
3158 | 308 |
readFrom:aStringOrStream onError:exceptionBlock |
309 |
"read a shortFloat from a string" |
|
310 |
||
311 |
|num| |
|
312 |
||
313 |
num := super readFrom:aStringOrStream onError:nil. |
|
11945 | 314 |
num isNil ifTrue:[ |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
315 |
^ exceptionBlock value |
3158 | 316 |
]. |
317 |
^ num asShortFloat |
|
318 |
||
319 |
" |
|
320 |
ShortFloat readFrom:'0.1' |
|
321 |
ShortFloat readFrom:'0' |
|
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
322 |
ShortFloat readFrom:'.123' |
3158 | 323 |
" |
324 |
||
325 |
"Modified: / 7.1.1998 / 16:17:59 / cg" |
|
1207 | 326 |
! ! |
327 |
||
7356 | 328 |
!ShortFloat class methodsFor:'binary storage'! |
329 |
||
330 |
readBinaryIEEESingleFrom:aStream |
|
331 |
"read a float value from the binary stream, aStream, |
|
332 |
interpreting the next bytes as an IEEE formatted 4-byte float" |
|
1197 | 333 |
|
7356 | 334 |
|f| |
1197 | 335 |
|
7356 | 336 |
f := ShortFloat basicNew. |
337 |
self readBinaryIEEESingleFrom:aStream into:f. |
|
338 |
^ f |
|
1197 | 339 |
|
9636 | 340 |
"not part of libboss, as this is also used by others (TIFFReader)" |
341 |
||
342 |
"Created: / 16-04-1996 / 21:00:35 / cg" |
|
343 |
"Modified: / 23-08-2006 / 16:01:47 / cg" |
|
1197 | 344 |
! |
345 |
||
7356 | 346 |
readBinaryIEEESingleFrom:aStream into:aFloat |
347 |
"read a float value from the binary stream, aStream, |
|
348 |
interpreting the next bytes as an IEEE formatted 4-byte float" |
|
349 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
350 |
aFloat class == self ifFalse:[self error:'no a ShortFloat']. |
6574 | 351 |
|
7356 | 352 |
" |
353 |
this implementation is wrong: does not work on non-IEEE machines |
|
354 |
(to date all machines where ST/X is running on use |
|
355 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
356 |
" |
|
357 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
358 |
||
359 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
11945 | 360 |
"swap the bytes" |
361 |
8 to:4 by:-1 do:[:i | |
|
362 |
aFloat basicAt:i put:(aStream next) |
|
363 |
]. |
|
364 |
^ self |
|
7356 | 365 |
]. |
366 |
1 to:4 do:[:i | |
|
11945 | 367 |
aFloat basicAt:i put:aStream next |
7356 | 368 |
] |
9636 | 369 |
|
370 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
371 |
||
372 |
"Modified: / 23-08-2006 / 16:01:52 / cg" |
|
6574 | 373 |
! |
374 |
||
7356 | 375 |
storeBinaryIEEESingle:aFloat on:aStream |
376 |
"store aFloat as an IEEE formatted 4-byte float |
|
377 |
onto the binary stream, aStream" |
|
378 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
379 |
|float| |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
380 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
381 |
float := aFloat asShortFloat. |
7356 | 382 |
|
383 |
" |
|
384 |
this implementation is wrong: does not work on non-IEEE machines |
|
385 |
(to date all machines where ST/X is running on use |
|
386 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
387 |
" |
|
388 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
6574 | 389 |
|
7356 | 390 |
UninterpretedBytes isBigEndian ifFalse:[ |
11945 | 391 |
"swap the bytes" |
392 |
8 to:4 by:-1 do:[:i | |
|
393 |
aStream nextPut:(float basicAt:i). |
|
394 |
]. |
|
395 |
^ self |
|
7356 | 396 |
]. |
397 |
1 to:4 do:[:i | |
|
11945 | 398 |
aStream nextPut:(float basicAt:i). |
7356 | 399 |
] |
9636 | 400 |
|
401 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
402 |
||
403 |
"Modified: / 23-08-2006 / 16:01:55 / cg" |
|
7356 | 404 |
! ! |
405 |
||
406 |
!ShortFloat class methodsFor:'constants'! |
|
6574 | 407 |
|
7392 | 408 |
e |
409 |
"return the constant e as ShortFloat" |
|
410 |
||
411 |
^ Float e asShortFloat |
|
412 |
! |
|
413 |
||
414 |
pi |
|
415 |
"return the constant pi as ShortFloat" |
|
416 |
||
417 |
^ Float pi asShortFloat |
|
418 |
! |
|
419 |
||
1197 | 420 |
unity |
1260 | 421 |
"return the neutral element for multiplication (1.0) as ShortFloat" |
1197 | 422 |
|
423 |
^ 1.0 asShortFloat |
|
424 |
||
1260 | 425 |
"Modified: 23.4.1996 / 09:26:51 / cg" |
1197 | 426 |
! |
427 |
||
428 |
zero |
|
1260 | 429 |
"return the neutral element for addition (0.0) as ShortFloat" |
1197 | 430 |
|
431 |
^ 0.0 asShortFloat |
|
432 |
||
1260 | 433 |
"Modified: 23.4.1996 / 09:26:45 / cg" |
1197 | 434 |
! ! |
435 |
||
1880 | 436 |
!ShortFloat class methodsFor:'queries'! |
1197 | 437 |
|
6898 | 438 |
exponentCharacter |
439 |
^ $e |
|
440 |
! |
|
441 |
||
1197 | 442 |
isBuiltInClass |
1264 | 443 |
"return true if this class is known by the run-time-system. |
444 |
Here, true is returned for myself, false for subclasses." |
|
1197 | 445 |
|
446 |
^ self == ShortFloat |
|
447 |
||
1264 | 448 |
"Modified: 23.4.1996 / 16:00:23 / cg" |
3427
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
449 |
! |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
450 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
451 |
isIEEEFormat |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
452 |
"return true, if this machine represents floats in IEEE format. |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
453 |
Currently, no support is provided for non-ieee machines |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
454 |
to convert their floats into this (which is only relevant, |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
455 |
if such a machine wants to send floats as binary to some other |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
456 |
machine). |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
457 |
Machines with non-IEEE format are VAXed and IBM370-type systems |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
458 |
(among others). Today, most systems use IEEE format floats." |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
459 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
460 |
^ true "/ this may be a lie |
7356 | 461 |
! |
462 |
||
463 |
numBitsInExponent |
|
464 |
"answer the number of bits in the exponent |
|
465 |
This is an IEEE float, where 8 bits are available: |
|
11945 | 466 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
7356 | 467 |
" |
468 |
||
469 |
^ 8 |
|
470 |
! |
|
471 |
||
472 |
numBitsInMantissa |
|
473 |
"answer the number of bits in the mantissa. |
|
474 |
This is an IEEE float, where 23 bits (the hidden one is not counted here) are available: |
|
11945 | 475 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
7356 | 476 |
" |
477 |
||
478 |
^ 23 |
|
479 |
! |
|
480 |
||
481 |
precision |
|
482 |
"answer the precision of a ShortFloat (in bits) |
|
11945 | 483 |
This is an IEEE float, where only the fraction from the normalized mantissa is stored |
9149 | 484 |
and so there is a hidden bit and the mantissa is actually represented by 24 binary digits |
485 |
(although only 23 are needed in the binary representation)" |
|
7356 | 486 |
|
487 |
^ 24 |
|
488 |
! |
|
489 |
||
490 |
radix |
|
491 |
"answer the radix of a ShortFloats exponent |
|
492 |
This is an IEEE float, which is represented as binary" |
|
493 |
||
9149 | 494 |
^ 2 "must be careful here, whenever ST/X is used on a VAX or a 370" |
1197 | 495 |
! ! |
496 |
||
497 |
!ShortFloat methodsFor:'arithmetic'! |
|
498 |
||
499 |
* aNumber |
|
11730 | 500 |
"return the product of the receiver and the argument." |
1197 | 501 |
|
502 |
%{ /* NOCONTEXT */ |
|
503 |
||
504 |
OBJ newFloat; |
|
505 |
float result; |
|
506 |
double dResult; |
|
507 |
||
508 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 509 |
result = __shortFloatVal(self) * (float)(__intVal(aNumber)); |
1197 | 510 |
retResult: |
11945 | 511 |
__qMKSFLOAT(newFloat, result); |
512 |
RETURN ( newFloat ); |
|
513 |
} |
|
7356 | 514 |
if (__isShortFloat(aNumber)) { |
11945 | 515 |
result = __shortFloatVal(self) * __shortFloatVal(aNumber); |
516 |
goto retResult; |
|
517 |
} |
|
7356 | 518 |
if (__isFloatLike(aNumber)) { |
11945 | 519 |
dResult = (double) __shortFloatVal(self)* __floatVal(aNumber); |
520 |
__qMKFLOAT(newFloat, dResult); |
|
521 |
RETURN ( newFloat ); |
|
1197 | 522 |
} |
523 |
%}. |
|
524 |
^ aNumber productFromShortFloat:self |
|
525 |
! |
|
526 |
||
527 |
+ aNumber |
|
528 |
"return the sum of the receiver and the argument, aNumber" |
|
529 |
||
530 |
%{ /* NOCONTEXT */ |
|
531 |
||
532 |
OBJ newFloat; |
|
533 |
float result; |
|
534 |
double dResult; |
|
535 |
||
536 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 537 |
result = __shortFloatVal(self) + (float)(__intVal(aNumber)); |
1197 | 538 |
retResult: |
11945 | 539 |
__qMKSFLOAT(newFloat, result); |
540 |
RETURN ( newFloat ); |
|
541 |
} |
|
7356 | 542 |
if (__isShortFloat(aNumber)) { |
11945 | 543 |
result = __shortFloatVal(self) + __shortFloatVal(aNumber); |
544 |
goto retResult; |
|
545 |
} |
|
7356 | 546 |
if (__isFloatLike(aNumber)) { |
11945 | 547 |
dResult = (double) __shortFloatVal(self) + __floatVal(aNumber); |
548 |
__qMKFLOAT(newFloat, dResult); |
|
549 |
RETURN ( newFloat ); |
|
1197 | 550 |
} |
551 |
%}. |
|
552 |
^ aNumber sumFromShortFloat:self |
|
553 |
! |
|
554 |
||
555 |
- aNumber |
|
556 |
"return the difference of the receiver and the argument, aNumber" |
|
557 |
||
558 |
%{ /* NOCONTEXT */ |
|
559 |
||
560 |
OBJ newFloat; |
|
561 |
float result; |
|
562 |
double dResult; |
|
563 |
||
564 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 565 |
result = __shortFloatVal(self) - (float)(__intVal(aNumber)); |
1197 | 566 |
retResult: |
11945 | 567 |
__qMKSFLOAT(newFloat, result); |
568 |
RETURN ( newFloat ); |
|
569 |
} |
|
7356 | 570 |
if (__isShortFloat(aNumber)) { |
11945 | 571 |
result = __shortFloatVal(self) - __shortFloatVal(aNumber); |
572 |
goto retResult; |
|
573 |
} |
|
7356 | 574 |
if (__isFloatLike(aNumber)) { |
11945 | 575 |
dResult = (double) __shortFloatVal(self) - __floatVal(aNumber); |
576 |
__qMKFLOAT(newFloat, dResult); |
|
577 |
RETURN ( newFloat ); |
|
1197 | 578 |
} |
579 |
%}. |
|
580 |
^ aNumber differenceFromShortFloat:self |
|
581 |
! |
|
582 |
||
583 |
/ aNumber |
|
584 |
"return the quotient of the receiver and the argument, aNumber" |
|
585 |
||
586 |
%{ /* NOCONTEXT */ |
|
587 |
||
588 |
OBJ newFloat; |
|
589 |
float result, val; |
|
590 |
double dResult, dVal; |
|
591 |
||
592 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 593 |
if (aNumber != __mkSmallInteger(0)) { |
594 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
1197 | 595 |
retResult: |
11945 | 596 |
__qMKSFLOAT(newFloat, result); |
597 |
RETURN ( newFloat ); |
|
598 |
} |
|
599 |
} |
|
7356 | 600 |
if (__isShortFloat(aNumber)) { |
11945 | 601 |
val = __shortFloatVal(aNumber); |
602 |
if (val != 0.0) { |
|
603 |
result = __shortFloatVal(self) / val; |
|
604 |
goto retResult; |
|
605 |
} |
|
606 |
} |
|
7356 | 607 |
if (__isFloatLike(aNumber)) { |
11945 | 608 |
dVal = __floatVal(aNumber); |
609 |
if (dVal != 0.0) { |
|
610 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
611 |
__qMKFLOAT(newFloat, dResult); |
|
612 |
RETURN ( newFloat ); |
|
613 |
} |
|
1197 | 614 |
} |
615 |
%}. |
|
616 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
11945 | 617 |
" |
618 |
No, you shalt not divide by zero |
|
619 |
" |
|
620 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
1197 | 621 |
]. |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
622 |
^ aNumber quotientFromShortFloat:self |
1197 | 623 |
! |
624 |
||
7725 | 625 |
abs |
626 |
"return the absolute value of the receiver |
|
627 |
reimplemented here for speed" |
|
628 |
||
629 |
%{ /* NOCONTEXT */ |
|
630 |
||
631 |
OBJ newFloat; |
|
632 |
float val = __shortFloatVal(self); |
|
633 |
||
634 |
if (val < 0.0) { |
|
11945 | 635 |
__qMKSFLOAT(newFloat, -val); |
636 |
RETURN ( newFloat ); |
|
7725 | 637 |
} |
638 |
RETURN (self); |
|
639 |
%}. |
|
640 |
||
641 |
" |
|
642 |
3.0 asShortFloat abs |
|
643 |
-3.0 asShortFloat abs |
|
644 |
" |
|
645 |
! |
|
646 |
||
1197 | 647 |
negated |
648 |
"return myself negated" |
|
649 |
||
650 |
%{ /* NOCONTEXT */ |
|
651 |
OBJ newFloat; |
|
652 |
float rslt = - __shortFloatVal(self); |
|
653 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
654 |
__qMKSFLOAT(newFloat, rslt); |
1197 | 655 |
RETURN ( newFloat ); |
5411 | 656 |
%}. |
657 |
^ 0.0 - self |
|
1197 | 658 |
|
3147 | 659 |
! |
660 |
||
661 |
uncheckedDivide:aNumber |
|
7356 | 662 |
"return the quotient of the receiver and the argument, aNumber. |
7471 | 663 |
Do not check for divide by zero (return NaN or Infinity). |
7356 | 664 |
This operation is provided for emulators of other languages/semantics, |
665 |
where no exception is raised for these results (i.e. Java). |
|
666 |
Its only defined if the arguments type is the same as the receivers." |
|
3147 | 667 |
|
668 |
%{ /* NOCONTEXT */ |
|
669 |
||
670 |
OBJ newFloat; |
|
671 |
float result, val; |
|
672 |
double dResult, dVal; |
|
673 |
||
674 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 675 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
3147 | 676 |
retResult: |
11945 | 677 |
__qMKSFLOAT(newFloat, result); |
678 |
RETURN ( newFloat ); |
|
679 |
} |
|
7356 | 680 |
if (__isShortFloat(aNumber)) { |
11945 | 681 |
val = __shortFloatVal(aNumber); |
682 |
result = __shortFloatVal(self) / val; |
|
683 |
goto retResult; |
|
684 |
} |
|
7356 | 685 |
if (__isFloatLike(aNumber)) { |
11945 | 686 |
dVal = __floatVal(aNumber); |
687 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
688 |
__qMKFLOAT(newFloat, dResult); |
|
689 |
RETURN ( newFloat ); |
|
3147 | 690 |
} |
691 |
%}. |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
692 |
^ aNumber quotientFromShortFloat:self |
3147 | 693 |
|
694 |
" |
|
695 |
0.0 asShortFloat uncheckedDivide:0 |
|
696 |
1.0 asShortFloat uncheckedDivide:0.0 |
|
697 |
" |
|
1197 | 698 |
! ! |
699 |
||
5257 | 700 |
!ShortFloat methodsFor:'coercing & converting'! |
1197 | 701 |
|
702 |
asFloat |
|
11238 | 703 |
"return a Float with same value as the receiver. |
704 |
Redefined for performance (machine can do it faster)" |
|
1197 | 705 |
|
706 |
%{ /* NOCONTEXT */ |
|
707 |
||
708 |
OBJ newFloat; |
|
709 |
double dVal = (double)__shortFloatVal(self); |
|
710 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
711 |
__qMKFLOAT(newFloat, dVal); |
1197 | 712 |
RETURN ( newFloat ); |
713 |
%} |
|
714 |
||
715 |
" |
|
11945 | 716 |
1.0 asShortFloat asFloat |
1197 | 717 |
" |
718 |
! |
|
719 |
||
720 |
asInteger |
|
721 |
"return an integer with same value - might truncate" |
|
722 |
||
723 |
%{ /* NOCONTEXT */ |
|
1670 | 724 |
float fVal; |
1197 | 725 |
|
1670 | 726 |
fVal = __shortFloatVal(self); |
7419 | 727 |
#ifdef WIN32 |
11945 | 728 |
if (! isnanf(fVal)) |
7419 | 729 |
#endif |
730 |
{ |
|
11945 | 731 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
732 |
RETURN ( __mkSmallInteger( (INT)fVal) ); |
|
733 |
} |
|
1197 | 734 |
} |
735 |
%}. |
|
736 |
^ super asInteger |
|
737 |
||
738 |
" |
|
739 |
12345.0 asShortFloat asInteger |
|
740 |
1e15 asShortFloat asInteger |
|
741 |
" |
|
742 |
! |
|
743 |
||
7437
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
744 |
asLongFloat |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
745 |
^ LongFloat fromShortFloat:self |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
746 |
! |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
747 |
|
1197 | 748 |
asShortFloat |
749 |
"return a ShortFloat with same value as the receiver - thats me" |
|
750 |
||
751 |
^ self |
|
1200 | 752 |
! |
753 |
||
7356 | 754 |
coerce:aNumber |
11720 | 755 |
"convert the argument aNumber into an instance of the receivers class and return it." |
7356 | 756 |
|
757 |
^ aNumber asShortFloat |
|
758 |
! |
|
759 |
||
1200 | 760 |
generality |
761 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
762 |
||
763 |
^ 70 |
|
764 |
||
765 |
||
1197 | 766 |
! ! |
767 |
||
768 |
!ShortFloat methodsFor:'comparing'! |
|
769 |
||
770 |
< aNumber |
|
771 |
"return true, if the argument is greater" |
|
772 |
||
773 |
%{ /* NOCONTEXT */ |
|
774 |
||
775 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 776 |
RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false ); |
1197 | 777 |
} |
7356 | 778 |
if (aNumber != nil) { |
11945 | 779 |
if (__qIsFloatLike(aNumber)) { |
780 |
RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false ); |
|
781 |
} |
|
782 |
if (__qIsShortFloat(aNumber)) { |
|
783 |
RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false ); |
|
784 |
} |
|
1197 | 785 |
} |
786 |
%}. |
|
787 |
^ aNumber lessFromShortFloat:self |
|
788 |
||
1200 | 789 |
" |
790 |
1.0 asShortFloat > (1/3) |
|
791 |
" |
|
1197 | 792 |
! |
793 |
||
794 |
<= aNumber |
|
795 |
"return true, if the argument is greater or equal" |
|
796 |
||
797 |
%{ /* NOCONTEXT */ |
|
798 |
||
799 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 800 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 801 |
} |
7356 | 802 |
if (aNumber != nil) { |
11945 | 803 |
if (__qIsFloatLike(aNumber)) { |
804 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
|
805 |
} |
|
806 |
if (__qIsShortFloat(aNumber)) { |
|
807 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
|
808 |
} |
|
809 |
} |
|
1197 | 810 |
%}. |
811 |
^ self retry:#<= coercing:aNumber |
|
812 |
! |
|
813 |
||
814 |
= aNumber |
|
7356 | 815 |
"return true, if the argument represents the same numeric value |
816 |
as the receiver, false otherwise" |
|
1197 | 817 |
|
818 |
%{ /* NOCONTEXT */ |
|
819 |
||
820 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 821 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
7356 | 822 |
} |
823 |
if (aNumber == nil) { |
|
11945 | 824 |
RETURN (false); |
1197 | 825 |
} |
7356 | 826 |
if (__qIsFloatLike(aNumber)) { |
11945 | 827 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
1197 | 828 |
} |
7356 | 829 |
if (__qIsShortFloat(aNumber)) { |
11945 | 830 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
1197 | 831 |
} |
832 |
%}. |
|
7356 | 833 |
^ aNumber equalFromShortFloat:self |
1197 | 834 |
! |
835 |
||
836 |
> aNumber |
|
837 |
"return true, if the argument is less" |
|
838 |
||
839 |
%{ /* NOCONTEXT */ |
|
840 |
||
841 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 842 |
RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false ); |
1197 | 843 |
} |
7356 | 844 |
if (aNumber != nil) { |
11945 | 845 |
if (__qIsFloatLike(aNumber)) { |
846 |
RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false ); |
|
847 |
} |
|
848 |
if (__qIsShortFloat(aNumber)) { |
|
849 |
RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false ); |
|
850 |
} |
|
1197 | 851 |
} |
852 |
%}. |
|
853 |
^ self retry:#> coercing:aNumber |
|
854 |
! |
|
855 |
||
856 |
>= aNumber |
|
857 |
"return true, if the argument is less or equal" |
|
858 |
||
859 |
%{ /* NOCONTEXT */ |
|
860 |
||
861 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 862 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 863 |
} |
7356 | 864 |
if (aNumber != nil) { |
11945 | 865 |
if (__qIsFloatLike(aNumber)) { |
866 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
|
867 |
} |
|
868 |
if (__qIsShortFloat(aNumber)) { |
|
869 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
|
870 |
} |
|
1197 | 871 |
} |
872 |
%}. |
|
873 |
^ self retry:#>= coercing:aNumber |
|
874 |
! |
|
875 |
||
876 |
hash |
|
877 |
"return a number for hashing; redefined, since floats compare |
|
878 |
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
879 |
as 3 hash." |
1197 | 880 |
|
881 |
|i| |
|
882 |
||
883 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
884 |
i := self asInteger. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
885 |
self = i ifTrue:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
886 |
^ i hash |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
887 |
]. |
1197 | 888 |
]. |
889 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
890 |
^ self asFloat hash |
1197 | 891 |
|
892 |
" |
|
11945 | 893 |
1.2345 hash |
894 |
1.2345 asShortFloat hash |
|
895 |
1.0 hash |
|
896 |
1.0 asShortFloat hash |
|
897 |
0.5 asShortFloat hash |
|
898 |
0.25 asShortFloat hash |
|
899 |
0.5 hash |
|
900 |
0.25 hash |
|
1197 | 901 |
" |
902 |
! |
|
903 |
||
904 |
~= aNumber |
|
905 |
"return true, if the arguments value are not equal" |
|
906 |
||
907 |
%{ /* NOCONTEXT */ |
|
908 |
||
7356 | 909 |
if (aNumber != nil) { |
11945 | 910 |
if (__isSmallInteger(aNumber)) { |
911 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
|
912 |
} |
|
913 |
if (__qIsFloatLike(aNumber)) { |
|
914 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
|
915 |
} |
|
916 |
if (__qIsShortFloat(aNumber)) { |
|
917 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
|
918 |
} |
|
7356 | 919 |
} else { |
11945 | 920 |
RETURN ( true ); |
1197 | 921 |
} |
922 |
%}. |
|
7356 | 923 |
^ super ~= aNumber |
1197 | 924 |
! ! |
925 |
||
926 |
!ShortFloat methodsFor:'printing & storing'! |
|
927 |
||
928 |
printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
929 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
930 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
931 |
#printOn: as basic print mechanism." |
1197 | 932 |
|
933 |
%{ /* NOCONTEXT */ |
|
934 |
||
935 |
char buffer[64]; |
|
936 |
REGISTER char *cp; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
937 |
int len; |
1197 | 938 |
OBJ s; |
939 |
||
940 |
/* |
|
941 |
* actually only needed on sparc: since thisContext is |
|
942 |
* in a global register, which gets destroyed by printf, |
|
943 |
* manually save it here - very stupid ... |
|
944 |
*/ |
|
945 |
__BEGIN_PROTECT_REGISTERS__ |
|
946 |
||
947 |
#ifdef SYSV |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
948 |
len = snprintf(buffer, sizeof(buffer), "%.6lg", (double)__shortFloatVal(self)); |
1197 | 949 |
#else |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
950 |
len = snprintf(buffer, sizeof(buffer), "%.6G", (double)__shortFloatVal(self)); |
1197 | 951 |
#endif |
952 |
||
953 |
__END_PROTECT_REGISTERS__ |
|
954 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
955 |
if (len >= 0 && len <= sizeof(buffer)-3) { |
11945 | 956 |
/* |
957 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
|
958 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
|
959 |
*/ |
|
960 |
for (cp = buffer; *cp; cp++) { |
|
961 |
if ((*cp == '.') || (*cp == 'E') || (*cp == 'e')) break; |
|
962 |
} |
|
963 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
|
964 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
|
965 |
*cp++ = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
|
966 |
} else { |
|
967 |
*cp++ = '.'; |
|
968 |
} |
|
969 |
*cp++ = '0'; |
|
970 |
*cp = '\0'; |
|
971 |
} else { |
|
972 |
if (cp && (*cp == '.')) { |
|
973 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
|
974 |
*cp = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
|
975 |
} |
|
976 |
} |
|
977 |
} |
|
1197 | 978 |
|
11945 | 979 |
s = __MKSTRING(buffer); |
980 |
if (s != nil) { |
|
981 |
RETURN (s); |
|
982 |
} |
|
1197 | 983 |
} |
984 |
%}. |
|
5432 | 985 |
^ self asFloat printString |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
986 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
987 |
" |
11945 | 988 |
1.234 asShortFloat printString. |
989 |
1.0 asShortFloat printString. |
|
990 |
1e10 asShortFloat printString. |
|
991 |
1.2e3 asShortFloat printString. |
|
992 |
1.2e30 asShortFloat printString. |
|
993 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
|
994 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
995 |
|
11945 | 996 |
DecimalPointCharacterForPrinting := $,. |
997 |
1.234 asShortFloat printString. |
|
998 |
1.0 asShortFloat printString. |
|
999 |
1e10 asShortFloat printString. |
|
1000 |
1.2e3 asShortFloat printString. |
|
1001 |
1.2e30 asShortFloat printString. |
|
1002 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
|
1003 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
|
1004 |
DecimalPointCharacterForPrinting := $. |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1005 |
" |
5718 | 1006 |
! |
1007 |
||
1008 |
printfPrintString:formatString |
|
7746 | 1009 |
"non-standard: return a printed representation of the receiver |
5718 | 1010 |
as specified by formatString, which is defined by printf. |
1011 |
If you use this, be aware, that specifying doubles differs on |
|
11945 | 1012 |
systems; on SYSV machines you have to give something like %lf, |
5718 | 1013 |
while on BSD systems the format string has to be %F. |
1014 |
Also, the resulting string may not be longer than 255 bytes - |
|
1015 |
since thats the (static) size of the buffer. |
|
11924
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1016 |
This method is NONSTANDARD and may be removed without notice. |
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1017 |
WARNNG: this goes directly to the C-printf function and may therefore me inherently unsafe. |
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1018 |
Please use the printf: method, which is safe as it is completely implemented in Smalltalk." |
5718 | 1019 |
|
1020 |
%{ /* STACK: 400 */ |
|
1021 |
char buffer[256]; |
|
1022 |
OBJ s; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1023 |
int len; |
5718 | 1024 |
|
1025 |
if (__isString(formatString)) { |
|
11945 | 1026 |
/* |
1027 |
* actually only needed on sparc: since thisContext is |
|
1028 |
* in a global register, which gets destroyed by printf, |
|
1029 |
* manually save it here - very stupid ... |
|
1030 |
*/ |
|
1031 |
__BEGIN_PROTECT_REGISTERS__ |
|
5718 | 1032 |
|
11945 | 1033 |
len = snprintf(buffer, sizeof(buffer), __stringVal(formatString), __shortFloatVal(self)); |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1034 |
|
11945 | 1035 |
__END_PROTECT_REGISTERS__ |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1036 |
|
11945 | 1037 |
if (len < 0) goto fail; |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1038 |
|
11945 | 1039 |
s = __MKSTRING_L(buffer, len); |
1040 |
if (s != nil) { |
|
1041 |
RETURN (s); |
|
1042 |
} |
|
5718 | 1043 |
} |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1044 |
fail: ; |
5718 | 1045 |
%}. |
1046 |
self primitiveFailed |
|
1047 |
||
1048 |
"ShortFloat pi printfPrintString:'%%lg -> %lg'" |
|
1049 |
"ShortFloat pi printfPrintString:'%%lf -> %lf'" |
|
1050 |
"ShortFloat pi printfPrintString:'%%7.5lg -> %7.5lg'" |
|
1051 |
"ShortFloat pi printfPrintString:'%%G -> %G'" |
|
1052 |
"ShortFloat pi printfPrintString:'%%F -> %F'" |
|
1053 |
"ShortFloat pi printfPrintString:'%%7.5G -> %7.5G'" |
|
1054 |
"ShortFloat pi printfPrintString:'%%7.5F -> %7.5F'" |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1055 |
! |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1056 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1057 |
storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1058 |
"return a printed representation of the receiver; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1059 |
all valid digits are printed. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1060 |
LimitedPrecisonReal and its subclasses use #storeString instead of |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1061 |
#storeOn: as basic print mechanism." |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1062 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1063 |
%{ /* NOCONTEXT */ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1064 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1065 |
char buffer[64]; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1066 |
REGISTER char *cp; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1067 |
OBJ s; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1068 |
int len; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1069 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1070 |
/* |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1071 |
* build a printf format string |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1072 |
*/ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1073 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1074 |
__BEGIN_PROTECT_REGISTERS__ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1075 |
#ifdef SYSV |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1076 |
len = snprintf(buffer, sizeof(buffer), "%.6lg", (double)__shortFloatVal(self)); |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1077 |
#else |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1078 |
len = snprintf(buffer, sizeof(buffer), "%.6G", (double)__shortFloatVal(self)); |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1079 |
#endif |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1080 |
__END_PROTECT_REGISTERS__ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1081 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1082 |
if (len >= 0 && len < sizeof(buffer)-3) { |
11945 | 1083 |
/* |
1084 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
|
1085 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
|
1086 |
*/ |
|
1087 |
for (cp = buffer; *cp; cp++) { |
|
1088 |
if ((*cp == '.') || (*cp == 'E') || (*cp == 'e')) break; |
|
1089 |
} |
|
1090 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
|
1091 |
*cp++ = '.'; |
|
1092 |
*cp++ = '0'; |
|
1093 |
*cp = '\0'; |
|
1094 |
} |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1095 |
|
11945 | 1096 |
s = __MKSTRING(buffer); |
1097 |
if (s != nil) { |
|
1098 |
RETURN (s); |
|
1099 |
} |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1100 |
} |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1101 |
%}. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1102 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1103 |
memory allocation (for the new string) failed. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1104 |
When we arrive here, there was no memory, even after a garbage collect. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1105 |
This means, that the VM wanted to get some more memory from the |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1106 |
OS, which was not kind enough to give it. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1107 |
Bad luck - you should increase the swap space on your machine. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1108 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1109 |
^ ObjectMemory allocationFailureSignal raise. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1110 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1111 |
" |
11945 | 1112 |
1.0 asShortFloat storeString |
1113 |
1.234 asShortFloat storeString |
|
1114 |
1e10 asShortFloat storeString |
|
1115 |
1.2e3 asShortFloat storeString |
|
1116 |
1.2e30 asShortFloat storeString |
|
1117 |
Float pi asShortFloat storeString |
|
1118 |
(1.0 uncheckedDivide:0) asShortFloat storeString |
|
1119 |
(0.0 uncheckedDivide:0) asShortFloat storeString |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1120 |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1121 |
notice that the storeString is NOT affected by DecimalPointCharacterForPrinting: |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1122 |
|
11945 | 1123 |
DecimalPointCharacterForPrinting := $,. |
1124 |
1.234 asShortFloat storeString. |
|
1125 |
1.0 asShortFloat storeString. |
|
1126 |
1e10 asShortFloat storeString. |
|
1127 |
1.2e3 asShortFloat storeString. |
|
1128 |
1.2e30 asShortFloat storeString. |
|
1129 |
(1.0 uncheckedDivide:0) asShortFloat storeString. |
|
1130 |
(0.0 uncheckedDivide:0) asShortFloat storeString. |
|
1131 |
DecimalPointCharacterForPrinting := $. |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1132 |
" |
1197 | 1133 |
! ! |
1134 |
||
3897 | 1135 |
!ShortFloat methodsFor:'special access'! |
1136 |
||
1137 |
exponent |
|
1138 |
"extract a normalized floats exponent. |
|
1139 |
The returned value depends on the float-representation of |
|
1140 |
the underlying machine and is therefore highly unportable. |
|
1141 |
This is not for general use. |
|
1142 |
This assumes that the mantissa is normalized to |
|
1143 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
1144 |
||
1145 |
%{ /* NOCONTEXT */ |
|
1146 |
||
1147 |
double frexp(); |
|
8913 | 1148 |
int exp; |
3897 | 1149 |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1150 |
__threadErrno = 0; |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1151 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1152 |
frexpf( __shortFloatVal(self), &exp); |
1153 |
#else |
|
1154 |
frexp( (double)(__shortFloatVal(self)), &exp); |
|
1155 |
#endif |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1156 |
if (__threadErrno == 0) { |
11945 | 1157 |
RETURN (__mkSmallInteger(exp)); |
3897 | 1158 |
} |
1159 |
%}. |
|
7387 | 1160 |
^ super exponent |
3897 | 1161 |
|
1162 |
" |
|
11945 | 1163 |
4.0 asShortFloat exponent |
1164 |
2.0 asShortFloat exponent |
|
1165 |
1.0 asShortFloat exponent |
|
1166 |
0.5 asShortFloat exponent |
|
1167 |
0.25 asShortFloat exponent |
|
1168 |
0.00000011111 asShortFloat exponent |
|
3897 | 1169 |
" |
1170 |
! |
|
1171 |
||
1172 |
mantissa |
|
1173 |
"extract a normalized floats mantissa. |
|
1174 |
The returned value depends on the float-representation of |
|
1175 |
the underlying machine and is therefore highly unportable. |
|
1176 |
This is not for general use. |
|
1177 |
This assumes that the mantissa is normalized to |
|
1178 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
1179 |
||
1180 |
%{ /* NOCONTEXT */ |
|
8913 | 1181 |
int exp; |
3897 | 1182 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1183 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1184 |
float frac; |
1185 |
||
1186 |
__threadErrno = 0; |
|
1187 |
frac = frexpf( (double)__shortFloatVal(self), &exp); |
|
1188 |
if (__threadErrno == 0) { |
|
11945 | 1189 |
RETURN (__MKSFLOAT(frac)); |
7356 | 1190 |
} |
1191 |
#else |
|
3897 | 1192 |
double frexp(); |
1193 |
double frac; |
|
1194 |
||
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1195 |
__threadErrno = 0; |
3897 | 1196 |
frac = frexp( (double)(__shortFloatVal(self)), &exp); |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1197 |
if (__threadErrno == 0) { |
11945 | 1198 |
RETURN (__MKFLOAT(frac)); |
3897 | 1199 |
} |
7356 | 1200 |
#endif |
3897 | 1201 |
%}. |
1202 |
^ self primitiveFailed |
|
1203 |
||
1204 |
" |
|
11945 | 1205 |
1.0 asShortFloat exponent |
3897 | 1206 |
1.0 asShortFloat mantissa |
1207 |
||
11945 | 1208 |
0.5 asShortFloat exponent |
1209 |
0.5 asShortFloat mantissa |
|
3897 | 1210 |
|
11945 | 1211 |
0.25 asShortFloat exponent |
1212 |
0.25 asShortFloat mantissa |
|
3897 | 1213 |
|
11945 | 1214 |
0.00000011111 asShortFloat exponent |
1215 |
0.00000011111 asShortFloat mantissa |
|
3897 | 1216 |
" |
1217 |
! ! |
|
1218 |
||
1197 | 1219 |
!ShortFloat methodsFor:'testing'! |
1220 |
||
3147 | 1221 |
isFinite |
11945 | 1222 |
"return true, if the receiver is a finite float |
3147 | 1223 |
i.e. not NaN and not infinite." |
1224 |
||
1225 |
%{ /* NOCONTEXT */ |
|
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
1226 |
float fV = __shortFloatVal(self); |
3147 | 1227 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1228 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1229 |
* notice: on machines which do not provide |
11945 | 1230 |
* a isfinite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
1231 |
* this may always ret true here ... |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1232 |
*/ |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1233 |
if (isfinitef(fV)) { RETURN (true); } |
3147 | 1234 |
%}. |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
1235 |
^ false |
3147 | 1236 |
|
1237 |
" |
|
11945 | 1238 |
1.0 asShortFloat isFinite |
1239 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
1240 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
3147 | 1241 |
" |
1242 |
! |
|
1243 |
||
2387 | 1244 |
isNaN |
1245 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
1246 |
These are not created by ST/X float operations (they raise an exception); |
|
1247 |
however, inline C-code could produce them ..." |
|
1248 |
||
1249 |
%{ /* NOCONTEXT */ |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1250 |
float sV = __shortFloatVal(self); |
3139 | 1251 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1252 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1253 |
* notice: on machines which do not provide |
11945 | 1254 |
* a isnan() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
1255 |
* this may always ret false here ... |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1256 |
*/ |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1257 |
if (isnanf(sV)) { RETURN (true); } |
2392 | 1258 |
%}. |
1259 |
^ false |
|
2387 | 1260 |
|
3147 | 1261 |
" |
7356 | 1262 |
1.0 asShortFloat isNaN |
11945 | 1263 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
1264 |
(0.0 asShortFloat uncheckedDivide: 0.0 asShortFloat) isNaN |
|
3147 | 1265 |
" |
2387 | 1266 |
! |
1267 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1268 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1269 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1270 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1271 |
%{ |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1272 |
#if defined(__i386__) && defined(linux) |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1273 |
RETURN ((__ByteArrayInstPtr(self)->ba_element[sizeof(float)-1] == 128) ? true : false ); |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1274 |
#endif |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1275 |
%}. |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1276 |
^ super isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1277 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1278 |
" |
11945 | 1279 |
0.0 asShortFloat isNegativeZero |
1280 |
-0.0 asShortFloat isNegativeZero |
|
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1281 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1282 |
! |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1283 |
|
1197 | 1284 |
negative |
1285 |
"return true if the receiver is less than zero" |
|
1286 |
||
1287 |
%{ /* NOCONTEXT */ |
|
1288 |
||
1289 |
RETURN ( (__shortFloatVal(self) < 0.0) ? true : false ); |
|
5411 | 1290 |
%}. |
1197 | 1291 |
! |
1292 |
||
5357 | 1293 |
numberOfBits |
1294 |
"return the size (in bits) of the real; |
|
1295 |
typically, 32 is returned here, |
|
1296 |
but who knows ..." |
|
1297 |
||
1298 |
%{ /* NOCONTEXT */ |
|
1299 |
||
8913 | 1300 |
RETURN (__mkSmallInteger (sizeof(float) * 8)); |
5357 | 1301 |
%} |
1302 |
||
1303 |
" |
|
11945 | 1304 |
1.2 numberOfBits |
1305 |
1.2 asShortFloat numberOfBits |
|
5357 | 1306 |
" |
1307 |
||
1308 |
! |
|
1309 |
||
1197 | 1310 |
positive |
1311 |
"return true if the receiver is greater or equal to zero" |
|
1312 |
||
1313 |
%{ /* NOCONTEXT */ |
|
1314 |
||
1315 |
RETURN ( (__shortFloatVal(self) >= 0.0) ? true : false ); |
|
1316 |
%} |
|
1880 | 1317 |
! |
1318 |
||
1319 |
strictlyPositive |
|
1320 |
"return true if the receiver is greater than zero" |
|
1321 |
||
1322 |
%{ /* NOCONTEXT */ |
|
1323 |
||
1324 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
1325 |
%} |
|
1197 | 1326 |
! ! |
1327 |
||
5552 | 1328 |
!ShortFloat methodsFor:'truncation & rounding'! |
3897 | 1329 |
|
7356 | 1330 |
ceiling |
1331 |
"return the smallest integer which is greater or equal to the receiver." |
|
1332 |
||
1333 |
|val| |
|
1334 |
||
1335 |
%{ |
|
1336 |
float fVal; |
|
1337 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1338 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1339 |
fVal = ceilf(__shortFloatVal(self)); |
1340 |
#else |
|
1341 |
fVal = (float)(ceil((double)__shortFloatVal(self))); |
|
1342 |
#endif |
|
1343 |
/* |
|
1344 |
* ST-80 (and X3J20) returns integer. |
|
1345 |
*/ |
|
1346 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
11945 | 1347 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1348 |
} |
1349 |
__qMKSFLOAT(val, fVal); |
|
1350 |
%}. |
|
1351 |
^ val asInteger |
|
1352 |
||
1353 |
" |
|
11945 | 1354 |
0.5 asShortFloat ceiling |
1355 |
-0.5 asShortFloat ceiling |
|
7356 | 1356 |
" |
1357 |
! |
|
1358 |
||
1359 |
ceilingAsFloat |
|
1360 |
"return the smallest integer-valued float greater or equal to the receiver. |
|
11945 | 1361 |
This is much like #ceiling, but avoids a (possibly expensive) conversion |
7356 | 1362 |
of the result to an integer. |
1363 |
It may be useful, if the result is to be further used in another float-operation." |
|
1364 |
||
1365 |
%{ /* NOCONTEXT */ |
|
1366 |
float fVal; |
|
1367 |
OBJ v; |
|
1368 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1369 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1370 |
fVal = ceilf(__shortFloatVal(self)); |
1371 |
#else |
|
1372 |
fVal = (float) ceil((double)__shortFloatVal(self)); |
|
1373 |
#endif |
|
1374 |
__qMKSFLOAT(v, fVal); |
|
1375 |
RETURN (v); |
|
1376 |
%} |
|
1377 |
" |
|
11945 | 1378 |
0.5 asShortFloat ceilingAsFloat |
1379 |
-0.5 asShortFloat ceilingAsFloat |
|
1380 |
-1.5 asShortFloat ceilingAsFloat |
|
7356 | 1381 |
" |
1382 |
! |
|
1383 |
||
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1384 |
floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1385 |
"return the integer nearest the receiver towards negative infinity." |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1386 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1387 |
|val| |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1388 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1389 |
%{ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1390 |
float fVal; |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1391 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1392 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1393 |
fVal = floorf(__shortFloatVal(self)); |
1394 |
#else |
|
1395 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
1396 |
#endif |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1397 |
/* |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1398 |
* ST-80 (and X3J20) returns integer. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1399 |
*/ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1400 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
11945 | 1401 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1402 |
} |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1403 |
__qMKSFLOAT(val, fVal); |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1404 |
%}. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1405 |
^ val asInteger |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1406 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1407 |
" |
11945 | 1408 |
0.5 asShortFloat floor |
1409 |
-0.5 asShortFloat floor |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1410 |
" |
7356 | 1411 |
! |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1412 |
|
7356 | 1413 |
floorAsFloat |
1414 |
"return the float which represents the next lower |
|
1415 |
integer nearest the receiver towards negative infinity. |
|
1416 |
Much like floor, but returns a float result - useful if the result |
|
1417 |
will be used in another float operation, to avoid costy int-conversion." |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1418 |
|
7356 | 1419 |
%{ /* NOCONTEXT */ |
1420 |
float fVal; |
|
1421 |
OBJ v; |
|
1422 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1423 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1424 |
fVal = floorf(__shortFloatVal(self)); |
1425 |
#else |
|
1426 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
1427 |
#endif |
|
1428 |
__qMKSFLOAT(v, fVal); |
|
1429 |
RETURN ( v ); |
|
1430 |
%} |
|
1431 |
||
1432 |
" |
|
11945 | 1433 |
0.5 asShortFloat floorAsFloat |
1434 |
-0.5 asShortFloat floorAsFloat |
|
7356 | 1435 |
" |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1436 |
! |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1437 |
|
3897 | 1438 |
fractionPart |
1439 |
"extract the after-decimal fraction part. |
|
7356 | 1440 |
such that (self truncated + self fractionPart) = self" |
3897 | 1441 |
|
1442 |
%{ /* NOCONTEXT */ |
|
1443 |
||
1444 |
double modf(); |
|
1445 |
double frac, trunc; |
|
1446 |
||
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1447 |
__threadErrno = 0; |
3897 | 1448 |
frac = modf((double)(__shortFloatVal(self)), &trunc); |
11945 | 1449 |
if (! isnan(frac)) { |
1450 |
if (__threadErrno == 0) { |
|
1451 |
RETURN (__MKSFLOAT((float)frac)); |
|
1452 |
} |
|
3897 | 1453 |
} |
1454 |
%}. |
|
7403 | 1455 |
^ self class |
11945 | 1456 |
raise:#domainErrorSignal |
1457 |
receiver:self |
|
1458 |
selector:#fractionPart |
|
1459 |
arguments:#() |
|
1460 |
errorString:'bad receiver in fractionPart' |
|
3897 | 1461 |
|
1462 |
" |
|
11945 | 1463 |
1.6 asShortFloat fractionPart + 1.6 asShortFloat truncated |
1464 |
-1.6 asShortFloat fractionPart + -1.6 asShortFloat truncated |
|
7356 | 1465 |
|
11945 | 1466 |
1.0 asShortFloat fractionalPart |
1467 |
0.5 asShortFloat fractionalPart |
|
1468 |
0.25 asShortFloat fractionalPart |
|
1469 |
3.14159 asShortFloat fractionalPart |
|
1470 |
12345673.14159 asShortFloat fractionalPart |
|
1471 |
123456731231231231.14159 asShortFloat fractionalPart |
|
3897 | 1472 |
" |
7356 | 1473 |
! |
3897 | 1474 |
|
7356 | 1475 |
rounded |
1476 |
"return the receiver rounded to the nearest integer" |
|
1477 |
||
1478 |
|val| |
|
1479 |
||
11945 | 1480 |
%{ |
7356 | 1481 |
float fVal; |
1482 |
||
1483 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1484 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1485 |
if (fVal < 0.0) { |
11945 | 1486 |
fVal = ceilf(fVal - (float)0.5); |
7356 | 1487 |
} else { |
11945 | 1488 |
fVal = floorf(fVal + (float)0.5); |
7356 | 1489 |
} |
1490 |
#else |
|
1491 |
if (fVal < 0.0) { |
|
11945 | 1492 |
fVal = (float)ceil((double)fVal - 0.5); |
7356 | 1493 |
} else { |
11945 | 1494 |
fVal = (float)floor((double)fVal + 0.5); |
7356 | 1495 |
} |
1496 |
#endif |
|
1497 |
/* |
|
1498 |
* ST-80 (and X3J20) returns integer. |
|
1499 |
*/ |
|
1500 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
11945 | 1501 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1502 |
} |
1503 |
__qMKSFLOAT(val, fVal); |
|
1504 |
%}. |
|
1505 |
^ val asInteger |
|
1506 |
||
1507 |
" |
|
11945 | 1508 |
0.4 asShortFloat rounded |
1509 |
0.5 asShortFloat rounded |
|
1510 |
0.6 asShortFloat rounded |
|
1511 |
-0.4 asShortFloat rounded |
|
1512 |
-0.5 asShortFloat rounded |
|
1513 |
-0.6 asShortFloat rounded |
|
7356 | 1514 |
" |
1515 |
! |
|
1516 |
||
1517 |
roundedAsFloat |
|
1518 |
"return the receiver rounded to the nearest integer as a float. |
|
11945 | 1519 |
This is much like #rounded, but avoids a (possibly expensive) conversion |
7356 | 1520 |
of the result to an integer. |
1521 |
It may be useful, if the result is to be further used in another float-operation." |
|
1522 |
||
1523 |
%{ /* NOCONTEXT */ |
|
1524 |
float fVal; |
|
1525 |
OBJ v; |
|
1526 |
||
1527 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1528 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1529 |
if (fVal < 0.0) { |
11945 | 1530 |
fVal = ceilf(fVal - (float)0.5); |
7356 | 1531 |
} else { |
11945 | 1532 |
fVal = floorf(fVal + (float)0.5); |
7356 | 1533 |
} |
1534 |
#else |
|
1535 |
if (fVal < 0.0) { |
|
11945 | 1536 |
fVal = (float)ceil((double)fVal - 0.5); |
7356 | 1537 |
} else { |
11945 | 1538 |
fVal = (float)floor((double)fVal + 0.5); |
7356 | 1539 |
} |
1540 |
#endif |
|
1541 |
__qMKSFLOAT(v, fVal); |
|
1542 |
RETURN (v); |
|
1543 |
%} |
|
1544 |
||
1545 |
" |
|
11945 | 1546 |
0.4 asShortFloat rounded |
1547 |
0.5 asShortFloat rounded |
|
1548 |
0.6 asShortFloat rounded |
|
1549 |
-0.4 asShortFloat rounded |
|
1550 |
-0.5 asShortFloat rounded |
|
1551 |
-0.6 asShortFloat rounded |
|
7356 | 1552 |
|
11945 | 1553 |
0.4 asShortFloat roundedAsFloat |
1554 |
0.5 asShortFloat roundedAsFloat |
|
1555 |
0.6 asShortFloat roundedAsFloat |
|
1556 |
-0.4 asShortFloat roundedAsFloat |
|
1557 |
-0.5 asShortFloat roundedAsFloat |
|
1558 |
-0.6 asShortFloat roundedAsFloat |
|
7356 | 1559 |
" |
1560 |
! |
|
1561 |
||
1562 |
truncated |
|
1563 |
"return the receiver truncated towards zero as an integer" |
|
1564 |
||
1565 |
|val| |
|
1566 |
||
11945 | 1567 |
%{ |
7356 | 1568 |
float fVal; |
1569 |
||
1570 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1571 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1572 |
if (fVal < 0.0) { |
11945 | 1573 |
fVal = ceilf(fVal); |
7356 | 1574 |
} else { |
11945 | 1575 |
fVal = floorf(fVal); |
7356 | 1576 |
} |
1577 |
#else |
|
1578 |
if (fVal < 0.0) { |
|
11945 | 1579 |
fVal = (float)ceil((double)fVal); |
7356 | 1580 |
} else { |
11945 | 1581 |
fVal = (float)floor((double)fVal); |
7356 | 1582 |
} |
1583 |
#endif |
|
1584 |
/* |
|
1585 |
* ST-80 (and X3J20) returns integer. |
|
1586 |
*/ |
|
1587 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
11945 | 1588 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1589 |
} |
1590 |
__qMKSFLOAT(val, fVal); |
|
1591 |
%}. |
|
1592 |
^ val asInteger |
|
1593 |
||
1594 |
" |
|
11945 | 1595 |
0.5 asShortFloat truncated |
1596 |
-0.5 asShortFloat truncated |
|
1597 |
0.5 asShortFloat truncatedAsFloat |
|
1598 |
-0.5 asShortFloat truncatedAsFloat |
|
7356 | 1599 |
" |
1600 |
! |
|
1601 |
||
1602 |
truncatedAsFloat |
|
1603 |
"return the receiver truncated towards zero as a float. |
|
11945 | 1604 |
This is much like #truncated, but avoids a (possibly expensive) conversion |
7356 | 1605 |
of the result to an integer. |
11945 | 1606 |
It may be useful, if the result is to be further used in another |
7356 | 1607 |
float-operation." |
1608 |
||
1609 |
%{ /* NOCONTEXT */ |
|
1610 |
float fVal; |
|
1611 |
OBJ v; |
|
1612 |
||
1613 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1614 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1615 |
if (fVal < 0.0) { |
11945 | 1616 |
fVal = ceilf(fVal); |
7356 | 1617 |
} else { |
11945 | 1618 |
fVal = floorf(fVal); |
7356 | 1619 |
} |
1620 |
#else |
|
1621 |
if (fVal < 0.0) { |
|
11945 | 1622 |
fVal = (float)ceil((double)fVal); |
7356 | 1623 |
} else { |
11945 | 1624 |
fVal = (float)floor((double)fVal); |
7356 | 1625 |
} |
1626 |
#endif |
|
1627 |
__qMKSFLOAT(v, fVal); |
|
1628 |
RETURN (v); |
|
1629 |
%} |
|
1630 |
||
1631 |
" |
|
11945 | 1632 |
0.5 asShortFloat truncated |
1633 |
-0.5 asShortFloat truncated |
|
1634 |
0.5 asShortFloat truncatedAsFloat |
|
1635 |
-0.5 asShortFloat truncatedAsFloat |
|
7356 | 1636 |
" |
3897 | 1637 |
! ! |
1638 |
||
1880 | 1639 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 1640 |
|
1641 |
version |
|
11945 | 1642 |
^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.100 2009-09-16 17:08:24 cg Exp $' |
1197 | 1643 |
! ! |