author | Claus Gittinger <cg@exept.de> |
Sun, 18 Sep 2011 13:05:14 +0200 | |
changeset 13711 | d3fe511856fc |
parent 13361 | e9e42eb53f34 |
child 14601 | c470d058140c |
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 */ |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
239 |
if (__isStringLike(aString) && __isSmallInteger(startIndex)) { |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
240 |
char *cp = (char *)(__stringVal(aString)); |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
241 |
int idx = __intVal(startIndex) - 1; |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
242 |
double atof(); |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
243 |
double val; |
4297 | 244 |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
245 |
if ((unsigned)idx < __stringSize(aString)) { |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
246 |
val = atof(cp + idx); |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
247 |
RETURN (__MKSFLOAT(val)); |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
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:[ |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
268 |
100000 timesRepeat:[ |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
269 |
ShortFloat readFrom:'123.45' |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
270 |
] |
4297 | 271 |
] |
272 |
" |
|
273 |
||
274 |
" |
|
275 |
Time millisecondsToRun:[ |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
276 |
100000 timesRepeat:[ |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
277 |
ShortFloat fastFromString:'123.45' at:1 |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
278 |
] |
4297 | 279 |
] |
280 |
" |
|
281 |
! |
|
282 |
||
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
283 |
readFrom:aStringOrStream |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
284 |
"read a shortFloat from a string" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
285 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
286 |
|num| |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
287 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
288 |
num := super readFrom:aStringOrStream onError:[self error:'conversion error for: ' , self name]. |
11945 | 289 |
num notNil ifTrue:[ |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
290 |
num := num asShortFloat |
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
291 |
]. |
11945 | 292 |
^ num |
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
293 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
294 |
" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
295 |
ShortFloat readFrom:'0.1' |
11945 | 296 |
ShortFloat readFrom:'0' |
297 |
ShortFloat readFrom:'.123' |
|
298 |
ShortFloat readFrom:'-.123' |
|
299 |
ShortFloat readFrom:'1e4' |
|
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
300 |
" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
301 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
302 |
"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
|
303 |
! |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
304 |
|
3158 | 305 |
readFrom:aStringOrStream onError:exceptionBlock |
306 |
"read a shortFloat from a string" |
|
307 |
||
308 |
|num| |
|
309 |
||
310 |
num := super readFrom:aStringOrStream onError:nil. |
|
11945 | 311 |
num isNil ifTrue:[ |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
312 |
^ exceptionBlock value |
3158 | 313 |
]. |
314 |
^ num asShortFloat |
|
315 |
||
316 |
" |
|
317 |
ShortFloat readFrom:'0.1' |
|
318 |
ShortFloat readFrom:'0' |
|
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
319 |
ShortFloat readFrom:'.123' |
3158 | 320 |
" |
321 |
||
322 |
"Modified: / 7.1.1998 / 16:17:59 / cg" |
|
1207 | 323 |
! ! |
324 |
||
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
325 |
!ShortFloat class methodsFor:'accessing'! |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
326 |
|
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
327 |
defaultPrintFormat |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
328 |
"this is fixed in ShortFloat" |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
329 |
|
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
330 |
^ '.6' |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
331 |
! ! |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
332 |
|
7356 | 333 |
!ShortFloat class methodsFor:'binary storage'! |
334 |
||
335 |
readBinaryIEEESingleFrom:aStream |
|
336 |
"read a float value from the binary stream, aStream, |
|
337 |
interpreting the next bytes as an IEEE formatted 4-byte float" |
|
1197 | 338 |
|
7356 | 339 |
|f| |
1197 | 340 |
|
7356 | 341 |
f := ShortFloat basicNew. |
342 |
self readBinaryIEEESingleFrom:aStream into:f. |
|
343 |
^ f |
|
1197 | 344 |
|
9636 | 345 |
"not part of libboss, as this is also used by others (TIFFReader)" |
346 |
||
347 |
"Created: / 16-04-1996 / 21:00:35 / cg" |
|
348 |
"Modified: / 23-08-2006 / 16:01:47 / cg" |
|
1197 | 349 |
! |
350 |
||
7356 | 351 |
readBinaryIEEESingleFrom:aStream into:aFloat |
352 |
"read a float value from the binary stream, aStream, |
|
353 |
interpreting the next bytes as an IEEE formatted 4-byte float" |
|
354 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
355 |
aFloat class == self ifFalse:[self error:'no a ShortFloat']. |
6574 | 356 |
|
7356 | 357 |
" |
358 |
this implementation is wrong: does not work on non-IEEE machines |
|
359 |
(to date all machines where ST/X is running on use |
|
360 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
361 |
" |
|
362 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
363 |
||
364 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
12629 | 365 |
"swap the bytes" |
366 |
4 to:1 by:-1 do:[:i | |
|
367 |
aFloat basicAt:i put:(aStream next) |
|
368 |
]. |
|
369 |
^ self |
|
7356 | 370 |
]. |
371 |
1 to:4 do:[:i | |
|
12629 | 372 |
aFloat basicAt:i put:aStream next |
7356 | 373 |
] |
9636 | 374 |
|
375 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
376 |
||
377 |
"Modified: / 23-08-2006 / 16:01:52 / cg" |
|
6574 | 378 |
! |
379 |
||
7356 | 380 |
storeBinaryIEEESingle:aFloat on:aStream |
381 |
"store aFloat as an IEEE formatted 4-byte float |
|
382 |
onto the binary stream, aStream" |
|
383 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
384 |
|float| |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
385 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
386 |
float := aFloat asShortFloat. |
7356 | 387 |
|
388 |
" |
|
389 |
this implementation is wrong: does not work on non-IEEE machines |
|
390 |
(to date all machines where ST/X is running on use |
|
391 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
392 |
" |
|
393 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
6574 | 394 |
|
7356 | 395 |
UninterpretedBytes isBigEndian ifFalse:[ |
12629 | 396 |
"swap the bytes" |
397 |
4 to:1 by:-1 do:[:i | |
|
398 |
aStream nextPut:(float basicAt:i). |
|
399 |
]. |
|
400 |
^ self |
|
7356 | 401 |
]. |
402 |
1 to:4 do:[:i | |
|
12629 | 403 |
aStream nextPut:(float basicAt:i). |
7356 | 404 |
] |
9636 | 405 |
|
406 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
407 |
||
408 |
"Modified: / 23-08-2006 / 16:01:55 / cg" |
|
7356 | 409 |
! ! |
410 |
||
411 |
!ShortFloat class methodsFor:'constants'! |
|
6574 | 412 |
|
7392 | 413 |
e |
414 |
"return the constant e as ShortFloat" |
|
415 |
||
416 |
^ Float e asShortFloat |
|
417 |
! |
|
418 |
||
419 |
pi |
|
420 |
"return the constant pi as ShortFloat" |
|
421 |
||
422 |
^ Float pi asShortFloat |
|
423 |
! |
|
424 |
||
1197 | 425 |
unity |
1260 | 426 |
"return the neutral element for multiplication (1.0) as ShortFloat" |
1197 | 427 |
|
428 |
^ 1.0 asShortFloat |
|
429 |
||
1260 | 430 |
"Modified: 23.4.1996 / 09:26:51 / cg" |
1197 | 431 |
! |
432 |
||
433 |
zero |
|
1260 | 434 |
"return the neutral element for addition (0.0) as ShortFloat" |
1197 | 435 |
|
436 |
^ 0.0 asShortFloat |
|
437 |
||
1260 | 438 |
"Modified: 23.4.1996 / 09:26:45 / cg" |
1197 | 439 |
! ! |
440 |
||
1880 | 441 |
!ShortFloat class methodsFor:'queries'! |
1197 | 442 |
|
6898 | 443 |
exponentCharacter |
444 |
^ $e |
|
445 |
! |
|
446 |
||
1197 | 447 |
isBuiltInClass |
1264 | 448 |
"return true if this class is known by the run-time-system. |
449 |
Here, true is returned for myself, false for subclasses." |
|
1197 | 450 |
|
451 |
^ self == ShortFloat |
|
452 |
||
1264 | 453 |
"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
|
454 |
! |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
455 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
456 |
isIEEEFormat |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
457 |
"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
|
458 |
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
|
459 |
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
|
460 |
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
|
461 |
machine). |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
462 |
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
|
463 |
(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
|
464 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
465 |
^ true "/ this may be a lie |
7356 | 466 |
! |
467 |
||
468 |
numBitsInExponent |
|
469 |
"answer the number of bits in the exponent |
|
470 |
This is an IEEE float, where 8 bits are available: |
|
11945 | 471 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
7356 | 472 |
" |
473 |
||
474 |
^ 8 |
|
475 |
! |
|
476 |
||
477 |
numBitsInMantissa |
|
478 |
"answer the number of bits in the mantissa. |
|
479 |
This is an IEEE float, where 23 bits (the hidden one is not counted here) are available: |
|
11945 | 480 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
7356 | 481 |
" |
482 |
||
483 |
^ 23 |
|
484 |
! |
|
485 |
||
486 |
precision |
|
487 |
"answer the precision of a ShortFloat (in bits) |
|
11945 | 488 |
This is an IEEE float, where only the fraction from the normalized mantissa is stored |
9149 | 489 |
and so there is a hidden bit and the mantissa is actually represented by 24 binary digits |
490 |
(although only 23 are needed in the binary representation)" |
|
7356 | 491 |
|
492 |
^ 24 |
|
493 |
! |
|
494 |
||
495 |
radix |
|
496 |
"answer the radix of a ShortFloats exponent |
|
497 |
This is an IEEE float, which is represented as binary" |
|
498 |
||
9149 | 499 |
^ 2 "must be careful here, whenever ST/X is used on a VAX or a 370" |
1197 | 500 |
! ! |
501 |
||
502 |
!ShortFloat methodsFor:'arithmetic'! |
|
503 |
||
504 |
* aNumber |
|
11730 | 505 |
"return the product of the receiver and the argument." |
1197 | 506 |
|
507 |
%{ /* NOCONTEXT */ |
|
508 |
||
509 |
OBJ newFloat; |
|
510 |
float result; |
|
511 |
double dResult; |
|
512 |
||
513 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 514 |
result = __shortFloatVal(self) * (float)(__intVal(aNumber)); |
1197 | 515 |
retResult: |
11945 | 516 |
__qMKSFLOAT(newFloat, result); |
517 |
RETURN ( newFloat ); |
|
518 |
} |
|
7356 | 519 |
if (__isShortFloat(aNumber)) { |
11945 | 520 |
result = __shortFloatVal(self) * __shortFloatVal(aNumber); |
521 |
goto retResult; |
|
522 |
} |
|
7356 | 523 |
if (__isFloatLike(aNumber)) { |
11945 | 524 |
dResult = (double) __shortFloatVal(self)* __floatVal(aNumber); |
525 |
__qMKFLOAT(newFloat, dResult); |
|
526 |
RETURN ( newFloat ); |
|
1197 | 527 |
} |
528 |
%}. |
|
529 |
^ aNumber productFromShortFloat:self |
|
530 |
! |
|
531 |
||
532 |
+ aNumber |
|
533 |
"return the sum of the receiver and the argument, aNumber" |
|
534 |
||
535 |
%{ /* NOCONTEXT */ |
|
536 |
||
537 |
OBJ newFloat; |
|
538 |
float result; |
|
539 |
double dResult; |
|
540 |
||
541 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 542 |
result = __shortFloatVal(self) + (float)(__intVal(aNumber)); |
1197 | 543 |
retResult: |
11945 | 544 |
__qMKSFLOAT(newFloat, result); |
545 |
RETURN ( newFloat ); |
|
546 |
} |
|
7356 | 547 |
if (__isShortFloat(aNumber)) { |
11945 | 548 |
result = __shortFloatVal(self) + __shortFloatVal(aNumber); |
549 |
goto retResult; |
|
550 |
} |
|
7356 | 551 |
if (__isFloatLike(aNumber)) { |
11945 | 552 |
dResult = (double) __shortFloatVal(self) + __floatVal(aNumber); |
553 |
__qMKFLOAT(newFloat, dResult); |
|
554 |
RETURN ( newFloat ); |
|
1197 | 555 |
} |
556 |
%}. |
|
557 |
^ aNumber sumFromShortFloat:self |
|
558 |
! |
|
559 |
||
560 |
- aNumber |
|
561 |
"return the difference of the receiver and the argument, aNumber" |
|
562 |
||
563 |
%{ /* NOCONTEXT */ |
|
564 |
||
565 |
OBJ newFloat; |
|
566 |
float result; |
|
567 |
double dResult; |
|
568 |
||
569 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 570 |
result = __shortFloatVal(self) - (float)(__intVal(aNumber)); |
1197 | 571 |
retResult: |
11945 | 572 |
__qMKSFLOAT(newFloat, result); |
573 |
RETURN ( newFloat ); |
|
574 |
} |
|
7356 | 575 |
if (__isShortFloat(aNumber)) { |
11945 | 576 |
result = __shortFloatVal(self) - __shortFloatVal(aNumber); |
577 |
goto retResult; |
|
578 |
} |
|
7356 | 579 |
if (__isFloatLike(aNumber)) { |
11945 | 580 |
dResult = (double) __shortFloatVal(self) - __floatVal(aNumber); |
581 |
__qMKFLOAT(newFloat, dResult); |
|
582 |
RETURN ( newFloat ); |
|
1197 | 583 |
} |
584 |
%}. |
|
585 |
^ aNumber differenceFromShortFloat:self |
|
586 |
! |
|
587 |
||
588 |
/ aNumber |
|
589 |
"return the quotient of the receiver and the argument, aNumber" |
|
590 |
||
591 |
%{ /* NOCONTEXT */ |
|
592 |
||
593 |
OBJ newFloat; |
|
594 |
float result, val; |
|
595 |
double dResult, dVal; |
|
596 |
||
597 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 598 |
if (aNumber != __mkSmallInteger(0)) { |
599 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
1197 | 600 |
retResult: |
11945 | 601 |
__qMKSFLOAT(newFloat, result); |
602 |
RETURN ( newFloat ); |
|
603 |
} |
|
604 |
} |
|
7356 | 605 |
if (__isShortFloat(aNumber)) { |
11945 | 606 |
val = __shortFloatVal(aNumber); |
607 |
if (val != 0.0) { |
|
608 |
result = __shortFloatVal(self) / val; |
|
609 |
goto retResult; |
|
610 |
} |
|
611 |
} |
|
7356 | 612 |
if (__isFloatLike(aNumber)) { |
11945 | 613 |
dVal = __floatVal(aNumber); |
614 |
if (dVal != 0.0) { |
|
615 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
616 |
__qMKFLOAT(newFloat, dResult); |
|
617 |
RETURN ( newFloat ); |
|
618 |
} |
|
1197 | 619 |
} |
620 |
%}. |
|
621 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
11945 | 622 |
" |
623 |
No, you shalt not divide by zero |
|
624 |
" |
|
625 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
1197 | 626 |
]. |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
627 |
^ aNumber quotientFromShortFloat:self |
1197 | 628 |
! |
629 |
||
7725 | 630 |
abs |
631 |
"return the absolute value of the receiver |
|
632 |
reimplemented here for speed" |
|
633 |
||
634 |
%{ /* NOCONTEXT */ |
|
635 |
||
636 |
OBJ newFloat; |
|
637 |
float val = __shortFloatVal(self); |
|
638 |
||
639 |
if (val < 0.0) { |
|
11945 | 640 |
__qMKSFLOAT(newFloat, -val); |
641 |
RETURN ( newFloat ); |
|
7725 | 642 |
} |
643 |
RETURN (self); |
|
644 |
%}. |
|
645 |
||
646 |
" |
|
647 |
3.0 asShortFloat abs |
|
648 |
-3.0 asShortFloat abs |
|
649 |
" |
|
650 |
! |
|
651 |
||
1197 | 652 |
negated |
653 |
"return myself negated" |
|
654 |
||
655 |
%{ /* NOCONTEXT */ |
|
656 |
OBJ newFloat; |
|
657 |
float rslt = - __shortFloatVal(self); |
|
658 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
659 |
__qMKSFLOAT(newFloat, rslt); |
1197 | 660 |
RETURN ( newFloat ); |
5411 | 661 |
%}. |
662 |
^ 0.0 - self |
|
1197 | 663 |
|
3147 | 664 |
! |
665 |
||
666 |
uncheckedDivide:aNumber |
|
7356 | 667 |
"return the quotient of the receiver and the argument, aNumber. |
7471 | 668 |
Do not check for divide by zero (return NaN or Infinity). |
7356 | 669 |
This operation is provided for emulators of other languages/semantics, |
670 |
where no exception is raised for these results (i.e. Java). |
|
671 |
Its only defined if the arguments type is the same as the receivers." |
|
3147 | 672 |
|
673 |
%{ /* NOCONTEXT */ |
|
674 |
||
675 |
OBJ newFloat; |
|
676 |
float result, val; |
|
677 |
double dResult, dVal; |
|
678 |
||
679 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 680 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
3147 | 681 |
retResult: |
11945 | 682 |
__qMKSFLOAT(newFloat, result); |
683 |
RETURN ( newFloat ); |
|
684 |
} |
|
7356 | 685 |
if (__isShortFloat(aNumber)) { |
11945 | 686 |
val = __shortFloatVal(aNumber); |
687 |
result = __shortFloatVal(self) / val; |
|
688 |
goto retResult; |
|
689 |
} |
|
7356 | 690 |
if (__isFloatLike(aNumber)) { |
11945 | 691 |
dVal = __floatVal(aNumber); |
692 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
693 |
__qMKFLOAT(newFloat, dResult); |
|
694 |
RETURN ( newFloat ); |
|
3147 | 695 |
} |
696 |
%}. |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
697 |
^ aNumber quotientFromShortFloat:self |
3147 | 698 |
|
699 |
" |
|
700 |
0.0 asShortFloat uncheckedDivide:0 |
|
701 |
1.0 asShortFloat uncheckedDivide:0.0 |
|
702 |
" |
|
1197 | 703 |
! ! |
704 |
||
5257 | 705 |
!ShortFloat methodsFor:'coercing & converting'! |
1197 | 706 |
|
707 |
asFloat |
|
11238 | 708 |
"return a Float with same value as the receiver. |
709 |
Redefined for performance (machine can do it faster)" |
|
1197 | 710 |
|
711 |
%{ /* NOCONTEXT */ |
|
712 |
||
713 |
OBJ newFloat; |
|
714 |
double dVal = (double)__shortFloatVal(self); |
|
715 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
716 |
__qMKFLOAT(newFloat, dVal); |
1197 | 717 |
RETURN ( newFloat ); |
718 |
%} |
|
719 |
||
720 |
" |
|
11945 | 721 |
1.0 asShortFloat asFloat |
1197 | 722 |
" |
723 |
! |
|
724 |
||
725 |
asInteger |
|
726 |
"return an integer with same value - might truncate" |
|
727 |
||
728 |
%{ /* NOCONTEXT */ |
|
1670 | 729 |
float fVal; |
1197 | 730 |
|
1670 | 731 |
fVal = __shortFloatVal(self); |
7419 | 732 |
#ifdef WIN32 |
11945 | 733 |
if (! isnanf(fVal)) |
7419 | 734 |
#endif |
735 |
{ |
|
11945 | 736 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
737 |
RETURN ( __mkSmallInteger( (INT)fVal) ); |
|
738 |
} |
|
1197 | 739 |
} |
740 |
%}. |
|
741 |
^ super asInteger |
|
742 |
||
743 |
" |
|
744 |
12345.0 asShortFloat asInteger |
|
745 |
1e15 asShortFloat asInteger |
|
746 |
" |
|
747 |
! |
|
748 |
||
7437
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
749 |
asLongFloat |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
750 |
^ LongFloat fromShortFloat:self |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
751 |
! |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
752 |
|
1197 | 753 |
asShortFloat |
754 |
"return a ShortFloat with same value as the receiver - thats me" |
|
755 |
||
756 |
^ self |
|
1200 | 757 |
! |
758 |
||
7356 | 759 |
coerce:aNumber |
11720 | 760 |
"convert the argument aNumber into an instance of the receivers class and return it." |
7356 | 761 |
|
762 |
^ aNumber asShortFloat |
|
763 |
! |
|
764 |
||
1200 | 765 |
generality |
766 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
767 |
||
768 |
^ 70 |
|
769 |
||
770 |
||
1197 | 771 |
! ! |
772 |
||
773 |
!ShortFloat methodsFor:'comparing'! |
|
774 |
||
775 |
< aNumber |
|
776 |
"return true, if the argument is greater" |
|
777 |
||
778 |
%{ /* NOCONTEXT */ |
|
779 |
||
780 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 781 |
RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false ); |
1197 | 782 |
} |
7356 | 783 |
if (aNumber != nil) { |
11945 | 784 |
if (__qIsFloatLike(aNumber)) { |
785 |
RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false ); |
|
786 |
} |
|
787 |
if (__qIsShortFloat(aNumber)) { |
|
788 |
RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false ); |
|
789 |
} |
|
1197 | 790 |
} |
791 |
%}. |
|
792 |
^ aNumber lessFromShortFloat:self |
|
793 |
||
1200 | 794 |
" |
795 |
1.0 asShortFloat > (1/3) |
|
796 |
" |
|
1197 | 797 |
! |
798 |
||
799 |
<= aNumber |
|
800 |
"return true, if the argument is greater or equal" |
|
801 |
||
802 |
%{ /* NOCONTEXT */ |
|
803 |
||
804 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 805 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 806 |
} |
7356 | 807 |
if (aNumber != nil) { |
11945 | 808 |
if (__qIsFloatLike(aNumber)) { |
809 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
|
810 |
} |
|
811 |
if (__qIsShortFloat(aNumber)) { |
|
812 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
|
813 |
} |
|
814 |
} |
|
1197 | 815 |
%}. |
816 |
^ self retry:#<= coercing:aNumber |
|
817 |
! |
|
818 |
||
819 |
= aNumber |
|
7356 | 820 |
"return true, if the argument represents the same numeric value |
821 |
as the receiver, false otherwise" |
|
1197 | 822 |
|
823 |
%{ /* NOCONTEXT */ |
|
824 |
||
825 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 826 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
7356 | 827 |
} |
828 |
if (aNumber == nil) { |
|
11945 | 829 |
RETURN (false); |
1197 | 830 |
} |
7356 | 831 |
if (__qIsFloatLike(aNumber)) { |
11945 | 832 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
1197 | 833 |
} |
7356 | 834 |
if (__qIsShortFloat(aNumber)) { |
11945 | 835 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
1197 | 836 |
} |
837 |
%}. |
|
7356 | 838 |
^ aNumber equalFromShortFloat:self |
1197 | 839 |
! |
840 |
||
841 |
> aNumber |
|
842 |
"return true, if the argument is less" |
|
843 |
||
844 |
%{ /* NOCONTEXT */ |
|
845 |
||
846 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 847 |
RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false ); |
1197 | 848 |
} |
7356 | 849 |
if (aNumber != nil) { |
11945 | 850 |
if (__qIsFloatLike(aNumber)) { |
851 |
RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false ); |
|
852 |
} |
|
853 |
if (__qIsShortFloat(aNumber)) { |
|
854 |
RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false ); |
|
855 |
} |
|
1197 | 856 |
} |
857 |
%}. |
|
858 |
^ self retry:#> coercing:aNumber |
|
859 |
! |
|
860 |
||
861 |
>= aNumber |
|
862 |
"return true, if the argument is less or equal" |
|
863 |
||
864 |
%{ /* NOCONTEXT */ |
|
865 |
||
866 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 867 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 868 |
} |
7356 | 869 |
if (aNumber != nil) { |
11945 | 870 |
if (__qIsFloatLike(aNumber)) { |
871 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
|
872 |
} |
|
873 |
if (__qIsShortFloat(aNumber)) { |
|
874 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
|
875 |
} |
|
1197 | 876 |
} |
877 |
%}. |
|
878 |
^ self retry:#>= coercing:aNumber |
|
879 |
! |
|
880 |
||
881 |
hash |
|
882 |
"return a number for hashing; redefined, since floats compare |
|
883 |
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
|
884 |
as 3 hash." |
1197 | 885 |
|
886 |
|i| |
|
887 |
||
888 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
889 |
i := self asInteger. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
890 |
self = i ifTrue:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
891 |
^ i hash |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
892 |
]. |
1197 | 893 |
]. |
894 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
895 |
^ self asFloat hash |
1197 | 896 |
|
897 |
" |
|
11945 | 898 |
1.2345 hash |
899 |
1.2345 asShortFloat hash |
|
900 |
1.0 hash |
|
901 |
1.0 asShortFloat hash |
|
902 |
0.5 asShortFloat hash |
|
903 |
0.25 asShortFloat hash |
|
904 |
0.5 hash |
|
905 |
0.25 hash |
|
1197 | 906 |
" |
907 |
! |
|
908 |
||
909 |
~= aNumber |
|
910 |
"return true, if the arguments value are not equal" |
|
911 |
||
912 |
%{ /* NOCONTEXT */ |
|
913 |
||
7356 | 914 |
if (aNumber != nil) { |
11945 | 915 |
if (__isSmallInteger(aNumber)) { |
916 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
|
917 |
} |
|
918 |
if (__qIsFloatLike(aNumber)) { |
|
919 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
|
920 |
} |
|
921 |
if (__qIsShortFloat(aNumber)) { |
|
922 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
|
923 |
} |
|
7356 | 924 |
} else { |
11945 | 925 |
RETURN ( true ); |
1197 | 926 |
} |
927 |
%}. |
|
7356 | 928 |
^ super ~= aNumber |
1197 | 929 |
! ! |
930 |
||
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
931 |
!ShortFloat methodsFor:'mathematical functions'! |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
932 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
933 |
fastInverseSqrt |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
934 |
"return a rough but fast approximation of (1 / self sqrt). |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
935 |
The error is some 1%, which is ok for many 3D computations or physics simulations. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
936 |
Do not use this for now: it is non-portable and probably not speeding things up |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
937 |
much, unless inlined into the sender code. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
938 |
The code is here as a reminder and might be later used as a hint for the inliner |
13360
1aac241c9981
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13359
diff
changeset
|
939 |
(to speed up 3D computations, for example). |
1aac241c9981
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13359
diff
changeset
|
940 |
see: http://betterexplained.com/articles/understanding-quakes-fast-inverse-square-root/" |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
941 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
942 |
%{ /* NOCONTEXT */ |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
943 |
float x, rslt; |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
944 |
OBJ newFloat; |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
945 |
|
13361 | 946 |
if (sizeof(float) == 4) { |
947 |
x = __shortFloatVal(self); |
|
948 |
{ |
|
949 |
float xhalf = 0.5f * x; |
|
950 |
int i = *(int*)&x; // store floating-point bits in integer |
|
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
951 |
|
13361 | 952 |
i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method |
953 |
x = *(float*)&i; // convert new bits into float |
|
954 |
x = x*(1.5f - xhalf*x*x); // One round of Newton's method |
|
955 |
__qMKSFLOAT(newFloat, x); |
|
956 |
RETURN ( newFloat ); |
|
957 |
} |
|
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
958 |
} |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
959 |
%}. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
960 |
^ 1 / self sqrt |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
961 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
962 |
" |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
963 |
10.0 asShortFloat fastInverseSqrt |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
964 |
(1 / 10.0 asShortFloat sqrt) |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
965 |
" |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
966 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
967 |
" |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
968 |
|a b t0 t1 t2| |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
969 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
970 |
a := 345 asShortFloat. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
971 |
t0 := Time millisecondsToRun:[ |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
972 |
1000000 timesRepeat:[ |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
973 |
] |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
974 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
975 |
t1 := Time millisecondsToRun:[ |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
976 |
1000000 timesRepeat:[ |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
977 |
a fastInverseSqrt |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
978 |
] |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
979 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
980 |
t2 := Time millisecondsToRun:[ |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
981 |
1000000 timesRepeat:[ |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
982 |
(1 / a sqrt) |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
983 |
] |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
984 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
985 |
Transcript show:'empty: '; showCR:t0. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
986 |
Transcript show:'fast: '; showCR:t1. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
987 |
Transcript show:'regular: '; showCR:t2. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
988 |
" |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
989 |
! ! |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
990 |
|
1197 | 991 |
!ShortFloat methodsFor:'printing & storing'! |
992 |
||
993 |
printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
994 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
995 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
996 |
#printOn: as basic print mechanism." |
1197 | 997 |
|
998 |
%{ /* NOCONTEXT */ |
|
999 |
||
1000 |
char buffer[64]; |
|
1001 |
REGISTER char *cp; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1002 |
int len; |
1197 | 1003 |
OBJ s; |
1004 |
||
1005 |
/* |
|
1006 |
* actually only needed on sparc: since thisContext is |
|
1007 |
* in a global register, which gets destroyed by printf, |
|
1008 |
* manually save it here - very stupid ... |
|
1009 |
*/ |
|
1010 |
__BEGIN_PROTECT_REGISTERS__ |
|
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1011 |
len = snprintf(buffer, sizeof(buffer), "%.6g", (float)__shortFloatVal(self)); |
1197 | 1012 |
__END_PROTECT_REGISTERS__ |
1013 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1014 |
if (len >= 0 && len <= sizeof(buffer)-3) { |
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1015 |
/* |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1016 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1017 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1018 |
*/ |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1019 |
for (cp = buffer; *cp; cp++) { |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1020 |
if ((*cp == '.') || (*cp == 'E') || (*cp == 'e')) break; |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1021 |
} |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1022 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1023 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1024 |
*cp++ = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1025 |
} else { |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1026 |
*cp++ = '.'; |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1027 |
} |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1028 |
*cp++ = '0'; |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1029 |
*cp = '\0'; |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1030 |
} else { |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1031 |
if (cp && (*cp == '.')) { |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1032 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1033 |
*cp = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1034 |
} |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1035 |
} |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1036 |
} |
1197 | 1037 |
|
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1038 |
s = __MKSTRING(buffer); |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1039 |
if (s != nil) { |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1040 |
RETURN (s); |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1041 |
} |
1197 | 1042 |
} |
1043 |
%}. |
|
5432 | 1044 |
^ self asFloat printString |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1045 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1046 |
" |
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1047 |
1.234 asShortFloat printString. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1048 |
1.0 asShortFloat printString. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1049 |
1e10 asShortFloat printString. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1050 |
1.2e3 asShortFloat printString. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1051 |
1.2e30 asShortFloat printString. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1052 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1053 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1054 |
self pi printString. |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1055 |
|
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1056 |
DecimalPointCharacterForPrinting := $,. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1057 |
1.234 asShortFloat printString. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1058 |
1.0 asShortFloat printString. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1059 |
1e10 asShortFloat printString. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1060 |
1.2e3 asShortFloat printString. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1061 |
1.2e30 asShortFloat printString. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1062 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1063 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1064 |
DecimalPointCharacterForPrinting := $. |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1065 |
" |
5718 | 1066 |
! |
1067 |
||
1068 |
printfPrintString:formatString |
|
7746 | 1069 |
"non-standard: return a printed representation of the receiver |
5718 | 1070 |
as specified by formatString, which is defined by printf. |
1071 |
If you use this, be aware, that specifying doubles differs on |
|
11945 | 1072 |
systems; on SYSV machines you have to give something like %lf, |
5718 | 1073 |
while on BSD systems the format string has to be %F. |
1074 |
Also, the resulting string may not be longer than 255 bytes - |
|
1075 |
since thats the (static) size of the buffer. |
|
11924
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1076 |
This method is NONSTANDARD and may be removed without notice. |
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1077 |
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
|
1078 |
Please use the printf: method, which is safe as it is completely implemented in Smalltalk." |
5718 | 1079 |
|
1080 |
%{ /* STACK: 400 */ |
|
1081 |
char buffer[256]; |
|
1082 |
OBJ s; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1083 |
int len; |
5718 | 1084 |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1085 |
if (__isStringLike(formatString)) { |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1086 |
/* |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1087 |
* actually only needed on sparc: since thisContext is |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1088 |
* in a global register, which gets destroyed by printf, |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1089 |
* manually save it here - very stupid ... |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1090 |
*/ |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1091 |
__BEGIN_PROTECT_REGISTERS__ |
5718 | 1092 |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1093 |
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
|
1094 |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1095 |
__END_PROTECT_REGISTERS__ |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1096 |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1097 |
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
|
1098 |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1099 |
s = __MKSTRING_L(buffer, len); |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1100 |
if (s != nil) { |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1101 |
RETURN (s); |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1102 |
} |
5718 | 1103 |
} |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1104 |
fail: ; |
5718 | 1105 |
%}. |
1106 |
self primitiveFailed |
|
1107 |
||
1108 |
"ShortFloat pi printfPrintString:'%%lg -> %lg'" |
|
1109 |
"ShortFloat pi printfPrintString:'%%lf -> %lf'" |
|
1110 |
"ShortFloat pi printfPrintString:'%%7.5lg -> %7.5lg'" |
|
1111 |
"ShortFloat pi printfPrintString:'%%G -> %G'" |
|
1112 |
"ShortFloat pi printfPrintString:'%%F -> %F'" |
|
1113 |
"ShortFloat pi printfPrintString:'%%7.5G -> %7.5G'" |
|
1114 |
"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
|
1115 |
! |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1116 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1117 |
storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1118 |
"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
|
1119 |
all valid digits are printed. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1120 |
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
|
1121 |
#storeOn: as basic print mechanism." |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1122 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1123 |
%{ /* NOCONTEXT */ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1124 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1125 |
char buffer[64]; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1126 |
REGISTER char *cp; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1127 |
OBJ s; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1128 |
int len; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1129 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1130 |
/* |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1131 |
* build a printf format string |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1132 |
*/ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1133 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1134 |
__BEGIN_PROTECT_REGISTERS__ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1135 |
#ifdef SYSV |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1136 |
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
|
1137 |
#else |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1138 |
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
|
1139 |
#endif |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1140 |
__END_PROTECT_REGISTERS__ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1141 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1142 |
if (len >= 0 && len < sizeof(buffer)-3) { |
11945 | 1143 |
/* |
1144 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
|
1145 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
|
1146 |
*/ |
|
1147 |
for (cp = buffer; *cp; cp++) { |
|
1148 |
if ((*cp == '.') || (*cp == 'E') || (*cp == 'e')) break; |
|
1149 |
} |
|
1150 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
|
1151 |
*cp++ = '.'; |
|
1152 |
*cp++ = '0'; |
|
1153 |
*cp = '\0'; |
|
1154 |
} |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1155 |
|
11945 | 1156 |
s = __MKSTRING(buffer); |
1157 |
if (s != nil) { |
|
1158 |
RETURN (s); |
|
1159 |
} |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1160 |
} |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1161 |
%}. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1162 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1163 |
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
|
1164 |
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
|
1165 |
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
|
1166 |
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
|
1167 |
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
|
1168 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1169 |
^ ObjectMemory allocationFailureSignal raise. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1170 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1171 |
" |
11945 | 1172 |
1.0 asShortFloat storeString |
1173 |
1.234 asShortFloat storeString |
|
1174 |
1e10 asShortFloat storeString |
|
1175 |
1.2e3 asShortFloat storeString |
|
1176 |
1.2e30 asShortFloat storeString |
|
1177 |
Float pi asShortFloat storeString |
|
1178 |
(1.0 uncheckedDivide:0) asShortFloat storeString |
|
1179 |
(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
|
1180 |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1181 |
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
|
1182 |
|
11945 | 1183 |
DecimalPointCharacterForPrinting := $,. |
1184 |
1.234 asShortFloat storeString. |
|
1185 |
1.0 asShortFloat storeString. |
|
1186 |
1e10 asShortFloat storeString. |
|
1187 |
1.2e3 asShortFloat storeString. |
|
1188 |
1.2e30 asShortFloat storeString. |
|
1189 |
(1.0 uncheckedDivide:0) asShortFloat storeString. |
|
1190 |
(0.0 uncheckedDivide:0) asShortFloat storeString. |
|
1191 |
DecimalPointCharacterForPrinting := $. |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1192 |
" |
1197 | 1193 |
! ! |
1194 |
||
12628 | 1195 |
!ShortFloat methodsFor:'private accessing'! |
1196 |
||
1197 |
basicAt:index |
|
1198 |
"return an internal byte of the float. |
|
1199 |
The value returned here depends on byte order, float representation etc. |
|
1200 |
Therefore, this method should be used strictly private. |
|
1201 |
||
1202 |
Notice: |
|
1203 |
the need to redefine this method here is due to the |
|
1204 |
inability of many machines to store floats in non-double aligned memory. |
|
1205 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
|
1206 |
and the actual float is stored at index 5 .. 12. |
|
1207 |
To hide this at one place, this method knows about that, and returns |
|
1208 |
values as if this filler wasnt present." |
|
1209 |
||
1210 |
%{ /* NOCONTEXT */ |
|
1211 |
||
1212 |
register int indx; |
|
1213 |
unsigned char *cp; |
|
1214 |
||
1215 |
/* |
|
1216 |
* notice the missing test for self being a nonNilObject - |
|
1217 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
1218 |
* and SmallInteger |
|
1219 |
*/ |
|
1220 |
if (__isSmallInteger(index)) { |
|
1221 |
indx = __intVal(index) - 1; |
|
1222 |
if (((unsigned)(indx)) < sizeof(float)) { |
|
1223 |
cp = (unsigned char *)(& (__ShortFloatInstPtr(self)->f_floatvalue)); |
|
1224 |
RETURN ( __mkSmallInteger(cp[indx] & 0xFF) ); |
|
1225 |
} |
|
1226 |
} |
|
1227 |
%}. |
|
1228 |
^ self indexNotIntegerOrOutOfBounds:index |
|
1229 |
! |
|
1230 |
||
1231 |
basicAt:index put:value |
|
1232 |
"set an internal byte of the float. |
|
1233 |
The value to be stored here depends on byte order, float representation etc. |
|
1234 |
Therefore, this method should be used strictly private. |
|
1235 |
||
1236 |
Notice: |
|
1237 |
the need to redefine this method here is due to the |
|
1238 |
inability of many machines to store floats in non-double aligned memory. |
|
1239 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
|
1240 |
and the actual float is stored at index 5 .. 12. |
|
1241 |
To hide this at one place, this method knows about that, and returns |
|
1242 |
values as if this filler wasnt present." |
|
1243 |
||
1244 |
%{ /* NOCONTEXT */ |
|
1245 |
register int indx, val; |
|
1246 |
unsigned char *cp; |
|
1247 |
||
1248 |
/* |
|
1249 |
* notice the missing test for self being a nonNilObject - |
|
1250 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
1251 |
* and SmallInteger |
|
1252 |
*/ |
|
1253 |
if (__bothSmallInteger(index, value)) { |
|
1254 |
val = __intVal(value); |
|
1255 |
if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) { |
|
1256 |
indx = __intVal(index) - 1; |
|
1257 |
if (((unsigned)(indx)) < sizeof(float)) { |
|
1258 |
cp = (unsigned char *)(& (__ShortFloatInstPtr(self)->f_floatvalue)); |
|
1259 |
cp[indx] = val; |
|
1260 |
RETURN ( value ); |
|
1261 |
} |
|
1262 |
} |
|
1263 |
} |
|
1264 |
%}. |
|
1265 |
value isInteger ifFalse:[ |
|
1266 |
" |
|
1267 |
the object to store should be an integer number |
|
1268 |
" |
|
1269 |
^ self elementNotInteger |
|
1270 |
]. |
|
1271 |
(value between:0 and:255) ifFalse:[ |
|
1272 |
" |
|
1273 |
the object to store must be a bytes value |
|
1274 |
" |
|
1275 |
^ self elementBoundsError:value |
|
1276 |
]. |
|
1277 |
^ self indexNotIntegerOrOutOfBounds:index |
|
1278 |
! ! |
|
1279 |
||
3897 | 1280 |
!ShortFloat methodsFor:'special access'! |
1281 |
||
1282 |
exponent |
|
1283 |
"extract a normalized floats exponent. |
|
1284 |
The returned value depends on the float-representation of |
|
1285 |
the underlying machine and is therefore highly unportable. |
|
1286 |
This is not for general use. |
|
1287 |
This assumes that the mantissa is normalized to |
|
1288 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
1289 |
||
1290 |
%{ /* NOCONTEXT */ |
|
1291 |
||
1292 |
double frexp(); |
|
8913 | 1293 |
int exp; |
3897 | 1294 |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1295 |
__threadErrno = 0; |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1296 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1297 |
frexpf( __shortFloatVal(self), &exp); |
1298 |
#else |
|
1299 |
frexp( (double)(__shortFloatVal(self)), &exp); |
|
1300 |
#endif |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1301 |
if (__threadErrno == 0) { |
11945 | 1302 |
RETURN (__mkSmallInteger(exp)); |
3897 | 1303 |
} |
1304 |
%}. |
|
7387 | 1305 |
^ super exponent |
3897 | 1306 |
|
1307 |
" |
|
11945 | 1308 |
4.0 asShortFloat exponent |
1309 |
2.0 asShortFloat exponent |
|
1310 |
1.0 asShortFloat exponent |
|
1311 |
0.5 asShortFloat exponent |
|
1312 |
0.25 asShortFloat exponent |
|
1313 |
0.00000011111 asShortFloat exponent |
|
3897 | 1314 |
" |
1315 |
! |
|
1316 |
||
1317 |
mantissa |
|
1318 |
"extract a normalized floats mantissa. |
|
1319 |
The returned value depends on the float-representation of |
|
1320 |
the underlying machine and is therefore highly unportable. |
|
1321 |
This is not for general use. |
|
1322 |
This assumes that the mantissa is normalized to |
|
1323 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
1324 |
||
1325 |
%{ /* NOCONTEXT */ |
|
8913 | 1326 |
int exp; |
3897 | 1327 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1328 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1329 |
float frac; |
1330 |
||
1331 |
__threadErrno = 0; |
|
1332 |
frac = frexpf( (double)__shortFloatVal(self), &exp); |
|
1333 |
if (__threadErrno == 0) { |
|
11945 | 1334 |
RETURN (__MKSFLOAT(frac)); |
7356 | 1335 |
} |
1336 |
#else |
|
3897 | 1337 |
double frexp(); |
1338 |
double frac; |
|
1339 |
||
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1340 |
__threadErrno = 0; |
3897 | 1341 |
frac = frexp( (double)(__shortFloatVal(self)), &exp); |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1342 |
if (__threadErrno == 0) { |
11945 | 1343 |
RETURN (__MKFLOAT(frac)); |
3897 | 1344 |
} |
7356 | 1345 |
#endif |
3897 | 1346 |
%}. |
1347 |
^ self primitiveFailed |
|
1348 |
||
1349 |
" |
|
11945 | 1350 |
1.0 asShortFloat exponent |
3897 | 1351 |
1.0 asShortFloat mantissa |
1352 |
||
11945 | 1353 |
0.5 asShortFloat exponent |
1354 |
0.5 asShortFloat mantissa |
|
3897 | 1355 |
|
11945 | 1356 |
0.25 asShortFloat exponent |
1357 |
0.25 asShortFloat mantissa |
|
3897 | 1358 |
|
11945 | 1359 |
0.00000011111 asShortFloat exponent |
1360 |
0.00000011111 asShortFloat mantissa |
|
3897 | 1361 |
" |
1362 |
! ! |
|
1363 |
||
1197 | 1364 |
!ShortFloat methodsFor:'testing'! |
1365 |
||
3147 | 1366 |
isFinite |
11945 | 1367 |
"return true, if the receiver is a finite float |
3147 | 1368 |
i.e. not NaN and not infinite." |
1369 |
||
1370 |
%{ /* NOCONTEXT */ |
|
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
1371 |
float fV = __shortFloatVal(self); |
3147 | 1372 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1373 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1374 |
* notice: on machines which do not provide |
11945 | 1375 |
* a isfinite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
1376 |
* 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
|
1377 |
*/ |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1378 |
if (isfinitef(fV)) { RETURN (true); } |
3147 | 1379 |
%}. |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
1380 |
^ false |
3147 | 1381 |
|
1382 |
" |
|
11945 | 1383 |
1.0 asShortFloat isFinite |
1384 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
1385 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
3147 | 1386 |
" |
1387 |
! |
|
1388 |
||
2387 | 1389 |
isNaN |
1390 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
1391 |
These are not created by ST/X float operations (they raise an exception); |
|
1392 |
however, inline C-code could produce them ..." |
|
1393 |
||
1394 |
%{ /* NOCONTEXT */ |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1395 |
float sV = __shortFloatVal(self); |
3139 | 1396 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1397 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1398 |
* notice: on machines which do not provide |
11945 | 1399 |
* a isnan() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
1400 |
* 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
|
1401 |
*/ |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1402 |
if (isnanf(sV)) { RETURN (true); } |
2392 | 1403 |
%}. |
1404 |
^ false |
|
2387 | 1405 |
|
3147 | 1406 |
" |
7356 | 1407 |
1.0 asShortFloat isNaN |
11945 | 1408 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
1409 |
(0.0 asShortFloat uncheckedDivide: 0.0 asShortFloat) isNaN |
|
3147 | 1410 |
" |
2387 | 1411 |
! |
1412 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1413 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1414 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1415 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1416 |
%{ |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1417 |
#if defined(__i386__) && defined(linux) |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1418 |
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
|
1419 |
#endif |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1420 |
%}. |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1421 |
^ super isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1422 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1423 |
" |
11945 | 1424 |
0.0 asShortFloat isNegativeZero |
1425 |
-0.0 asShortFloat isNegativeZero |
|
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1426 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1427 |
! |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1428 |
|
1197 | 1429 |
negative |
1430 |
"return true if the receiver is less than zero" |
|
1431 |
||
1432 |
%{ /* NOCONTEXT */ |
|
1433 |
||
1434 |
RETURN ( (__shortFloatVal(self) < 0.0) ? true : false ); |
|
5411 | 1435 |
%}. |
1197 | 1436 |
! |
1437 |
||
5357 | 1438 |
numberOfBits |
1439 |
"return the size (in bits) of the real; |
|
1440 |
typically, 32 is returned here, |
|
1441 |
but who knows ..." |
|
1442 |
||
1443 |
%{ /* NOCONTEXT */ |
|
1444 |
||
8913 | 1445 |
RETURN (__mkSmallInteger (sizeof(float) * 8)); |
5357 | 1446 |
%} |
1447 |
||
1448 |
" |
|
11945 | 1449 |
1.2 numberOfBits |
1450 |
1.2 asShortFloat numberOfBits |
|
5357 | 1451 |
" |
1452 |
||
1453 |
! |
|
1454 |
||
1197 | 1455 |
positive |
1456 |
"return true if the receiver is greater or equal to zero" |
|
1457 |
||
1458 |
%{ /* NOCONTEXT */ |
|
1459 |
||
1460 |
RETURN ( (__shortFloatVal(self) >= 0.0) ? true : false ); |
|
1461 |
%} |
|
1880 | 1462 |
! |
1463 |
||
1464 |
strictlyPositive |
|
1465 |
"return true if the receiver is greater than zero" |
|
1466 |
||
1467 |
%{ /* NOCONTEXT */ |
|
1468 |
||
1469 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
1470 |
%} |
|
1197 | 1471 |
! ! |
1472 |
||
5552 | 1473 |
!ShortFloat methodsFor:'truncation & rounding'! |
3897 | 1474 |
|
7356 | 1475 |
ceiling |
1476 |
"return the smallest integer which is greater or equal to the receiver." |
|
1477 |
||
1478 |
|val| |
|
1479 |
||
1480 |
%{ |
|
1481 |
float fVal; |
|
1482 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1483 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1484 |
fVal = ceilf(__shortFloatVal(self)); |
1485 |
#else |
|
1486 |
fVal = (float)(ceil((double)__shortFloatVal(self))); |
|
1487 |
#endif |
|
1488 |
/* |
|
1489 |
* ST-80 (and X3J20) returns integer. |
|
1490 |
*/ |
|
1491 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
11945 | 1492 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1493 |
} |
1494 |
__qMKSFLOAT(val, fVal); |
|
1495 |
%}. |
|
1496 |
^ val asInteger |
|
1497 |
||
1498 |
" |
|
11945 | 1499 |
0.5 asShortFloat ceiling |
1500 |
-0.5 asShortFloat ceiling |
|
7356 | 1501 |
" |
1502 |
! |
|
1503 |
||
1504 |
ceilingAsFloat |
|
1505 |
"return the smallest integer-valued float greater or equal to the receiver. |
|
11945 | 1506 |
This is much like #ceiling, but avoids a (possibly expensive) conversion |
7356 | 1507 |
of the result to an integer. |
1508 |
It may be useful, if the result is to be further used in another float-operation." |
|
1509 |
||
1510 |
%{ /* NOCONTEXT */ |
|
1511 |
float fVal; |
|
1512 |
OBJ v; |
|
1513 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1514 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1515 |
fVal = ceilf(__shortFloatVal(self)); |
1516 |
#else |
|
1517 |
fVal = (float) ceil((double)__shortFloatVal(self)); |
|
1518 |
#endif |
|
1519 |
__qMKSFLOAT(v, fVal); |
|
1520 |
RETURN (v); |
|
1521 |
%} |
|
1522 |
" |
|
11945 | 1523 |
0.5 asShortFloat ceilingAsFloat |
1524 |
-0.5 asShortFloat ceilingAsFloat |
|
1525 |
-1.5 asShortFloat ceilingAsFloat |
|
7356 | 1526 |
" |
1527 |
! |
|
1528 |
||
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1529 |
floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1530 |
"return the integer nearest the receiver towards negative infinity." |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1531 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1532 |
|val| |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1533 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1534 |
%{ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1535 |
float fVal; |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1536 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1537 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1538 |
fVal = floorf(__shortFloatVal(self)); |
1539 |
#else |
|
1540 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
1541 |
#endif |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1542 |
/* |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1543 |
* ST-80 (and X3J20) returns integer. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1544 |
*/ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1545 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
11945 | 1546 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1547 |
} |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1548 |
__qMKSFLOAT(val, fVal); |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1549 |
%}. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1550 |
^ val asInteger |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1551 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1552 |
" |
11945 | 1553 |
0.5 asShortFloat floor |
1554 |
-0.5 asShortFloat floor |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1555 |
" |
7356 | 1556 |
! |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1557 |
|
7356 | 1558 |
floorAsFloat |
1559 |
"return the float which represents the next lower |
|
1560 |
integer nearest the receiver towards negative infinity. |
|
1561 |
Much like floor, but returns a float result - useful if the result |
|
1562 |
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
|
1563 |
|
7356 | 1564 |
%{ /* NOCONTEXT */ |
1565 |
float fVal; |
|
1566 |
OBJ v; |
|
1567 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1568 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1569 |
fVal = floorf(__shortFloatVal(self)); |
1570 |
#else |
|
1571 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
1572 |
#endif |
|
1573 |
__qMKSFLOAT(v, fVal); |
|
1574 |
RETURN ( v ); |
|
1575 |
%} |
|
1576 |
||
1577 |
" |
|
11945 | 1578 |
0.5 asShortFloat floorAsFloat |
1579 |
-0.5 asShortFloat floorAsFloat |
|
7356 | 1580 |
" |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1581 |
! |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1582 |
|
3897 | 1583 |
fractionPart |
1584 |
"extract the after-decimal fraction part. |
|
7356 | 1585 |
such that (self truncated + self fractionPart) = self" |
3897 | 1586 |
|
1587 |
%{ /* NOCONTEXT */ |
|
1588 |
||
1589 |
double modf(); |
|
1590 |
double frac, trunc; |
|
1591 |
||
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1592 |
__threadErrno = 0; |
3897 | 1593 |
frac = modf((double)(__shortFloatVal(self)), &trunc); |
11945 | 1594 |
if (! isnan(frac)) { |
1595 |
if (__threadErrno == 0) { |
|
1596 |
RETURN (__MKSFLOAT((float)frac)); |
|
1597 |
} |
|
3897 | 1598 |
} |
1599 |
%}. |
|
7403 | 1600 |
^ self class |
11945 | 1601 |
raise:#domainErrorSignal |
1602 |
receiver:self |
|
1603 |
selector:#fractionPart |
|
1604 |
arguments:#() |
|
1605 |
errorString:'bad receiver in fractionPart' |
|
3897 | 1606 |
|
1607 |
" |
|
11945 | 1608 |
1.6 asShortFloat fractionPart + 1.6 asShortFloat truncated |
1609 |
-1.6 asShortFloat fractionPart + -1.6 asShortFloat truncated |
|
7356 | 1610 |
|
11945 | 1611 |
1.0 asShortFloat fractionalPart |
1612 |
0.5 asShortFloat fractionalPart |
|
1613 |
0.25 asShortFloat fractionalPart |
|
1614 |
3.14159 asShortFloat fractionalPart |
|
1615 |
12345673.14159 asShortFloat fractionalPart |
|
1616 |
123456731231231231.14159 asShortFloat fractionalPart |
|
3897 | 1617 |
" |
7356 | 1618 |
! |
3897 | 1619 |
|
7356 | 1620 |
rounded |
1621 |
"return the receiver rounded to the nearest integer" |
|
1622 |
||
1623 |
|val| |
|
1624 |
||
11945 | 1625 |
%{ |
7356 | 1626 |
float fVal; |
1627 |
||
1628 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1629 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1630 |
if (fVal < 0.0) { |
11945 | 1631 |
fVal = ceilf(fVal - (float)0.5); |
7356 | 1632 |
} else { |
11945 | 1633 |
fVal = floorf(fVal + (float)0.5); |
7356 | 1634 |
} |
1635 |
#else |
|
1636 |
if (fVal < 0.0) { |
|
11945 | 1637 |
fVal = (float)ceil((double)fVal - 0.5); |
7356 | 1638 |
} else { |
11945 | 1639 |
fVal = (float)floor((double)fVal + 0.5); |
7356 | 1640 |
} |
1641 |
#endif |
|
1642 |
/* |
|
1643 |
* ST-80 (and X3J20) returns integer. |
|
1644 |
*/ |
|
1645 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
11945 | 1646 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1647 |
} |
1648 |
__qMKSFLOAT(val, fVal); |
|
1649 |
%}. |
|
1650 |
^ val asInteger |
|
1651 |
||
1652 |
" |
|
11945 | 1653 |
0.4 asShortFloat rounded |
1654 |
0.5 asShortFloat rounded |
|
1655 |
0.6 asShortFloat rounded |
|
1656 |
-0.4 asShortFloat rounded |
|
1657 |
-0.5 asShortFloat rounded |
|
1658 |
-0.6 asShortFloat rounded |
|
7356 | 1659 |
" |
1660 |
! |
|
1661 |
||
1662 |
roundedAsFloat |
|
1663 |
"return the receiver rounded to the nearest integer as a float. |
|
11945 | 1664 |
This is much like #rounded, but avoids a (possibly expensive) conversion |
7356 | 1665 |
of the result to an integer. |
1666 |
It may be useful, if the result is to be further used in another float-operation." |
|
1667 |
||
1668 |
%{ /* NOCONTEXT */ |
|
1669 |
float fVal; |
|
1670 |
OBJ v; |
|
1671 |
||
1672 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1673 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1674 |
if (fVal < 0.0) { |
11945 | 1675 |
fVal = ceilf(fVal - (float)0.5); |
7356 | 1676 |
} else { |
11945 | 1677 |
fVal = floorf(fVal + (float)0.5); |
7356 | 1678 |
} |
1679 |
#else |
|
1680 |
if (fVal < 0.0) { |
|
11945 | 1681 |
fVal = (float)ceil((double)fVal - 0.5); |
7356 | 1682 |
} else { |
11945 | 1683 |
fVal = (float)floor((double)fVal + 0.5); |
7356 | 1684 |
} |
1685 |
#endif |
|
1686 |
__qMKSFLOAT(v, fVal); |
|
1687 |
RETURN (v); |
|
1688 |
%} |
|
1689 |
||
1690 |
" |
|
11945 | 1691 |
0.4 asShortFloat rounded |
1692 |
0.5 asShortFloat rounded |
|
1693 |
0.6 asShortFloat rounded |
|
1694 |
-0.4 asShortFloat rounded |
|
1695 |
-0.5 asShortFloat rounded |
|
1696 |
-0.6 asShortFloat rounded |
|
7356 | 1697 |
|
11945 | 1698 |
0.4 asShortFloat roundedAsFloat |
1699 |
0.5 asShortFloat roundedAsFloat |
|
1700 |
0.6 asShortFloat roundedAsFloat |
|
1701 |
-0.4 asShortFloat roundedAsFloat |
|
1702 |
-0.5 asShortFloat roundedAsFloat |
|
1703 |
-0.6 asShortFloat roundedAsFloat |
|
7356 | 1704 |
" |
1705 |
! |
|
1706 |
||
1707 |
truncated |
|
1708 |
"return the receiver truncated towards zero as an integer" |
|
1709 |
||
1710 |
|val| |
|
1711 |
||
11945 | 1712 |
%{ |
7356 | 1713 |
float fVal; |
1714 |
||
1715 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1716 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1717 |
if (fVal < 0.0) { |
11945 | 1718 |
fVal = ceilf(fVal); |
7356 | 1719 |
} else { |
11945 | 1720 |
fVal = floorf(fVal); |
7356 | 1721 |
} |
1722 |
#else |
|
1723 |
if (fVal < 0.0) { |
|
11945 | 1724 |
fVal = (float)ceil((double)fVal); |
7356 | 1725 |
} else { |
11945 | 1726 |
fVal = (float)floor((double)fVal); |
7356 | 1727 |
} |
1728 |
#endif |
|
1729 |
/* |
|
1730 |
* ST-80 (and X3J20) returns integer. |
|
1731 |
*/ |
|
1732 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
11945 | 1733 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1734 |
} |
1735 |
__qMKSFLOAT(val, fVal); |
|
1736 |
%}. |
|
1737 |
^ val asInteger |
|
1738 |
||
1739 |
" |
|
11945 | 1740 |
0.5 asShortFloat truncated |
1741 |
-0.5 asShortFloat truncated |
|
1742 |
0.5 asShortFloat truncatedAsFloat |
|
1743 |
-0.5 asShortFloat truncatedAsFloat |
|
7356 | 1744 |
" |
1745 |
! |
|
1746 |
||
1747 |
truncatedAsFloat |
|
1748 |
"return the receiver truncated towards zero as a float. |
|
11945 | 1749 |
This is much like #truncated, but avoids a (possibly expensive) conversion |
7356 | 1750 |
of the result to an integer. |
11945 | 1751 |
It may be useful, if the result is to be further used in another |
7356 | 1752 |
float-operation." |
1753 |
||
1754 |
%{ /* NOCONTEXT */ |
|
1755 |
float fVal; |
|
1756 |
OBJ v; |
|
1757 |
||
1758 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1759 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1760 |
if (fVal < 0.0) { |
11945 | 1761 |
fVal = ceilf(fVal); |
7356 | 1762 |
} else { |
11945 | 1763 |
fVal = floorf(fVal); |
7356 | 1764 |
} |
1765 |
#else |
|
1766 |
if (fVal < 0.0) { |
|
11945 | 1767 |
fVal = (float)ceil((double)fVal); |
7356 | 1768 |
} else { |
11945 | 1769 |
fVal = (float)floor((double)fVal); |
7356 | 1770 |
} |
1771 |
#endif |
|
1772 |
__qMKSFLOAT(v, fVal); |
|
1773 |
RETURN (v); |
|
1774 |
%} |
|
1775 |
||
1776 |
" |
|
11945 | 1777 |
0.5 asShortFloat truncated |
1778 |
-0.5 asShortFloat truncated |
|
1779 |
0.5 asShortFloat truncatedAsFloat |
|
1780 |
-0.5 asShortFloat truncatedAsFloat |
|
7356 | 1781 |
" |
3897 | 1782 |
! ! |
1783 |
||
1880 | 1784 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 1785 |
|
1786 |
version |
|
13361 | 1787 |
^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.108 2011-05-05 08:57:20 mb Exp $' |
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1788 |
! |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1789 |
|
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1790 |
version_CVS |
13361 | 1791 |
^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.108 2011-05-05 08:57:20 mb Exp $' |
1197 | 1792 |
! ! |