author | Claus Gittinger <cg@exept.de> |
Wed, 25 Mar 2015 16:41:55 +0100 | |
changeset 17654 | d3505c4f8a57 |
parent 17396 | b587ca0a385f |
child 17656 | bc8eb3c144f1 |
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 |
||
17382 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1197 | 16 |
LimitedPrecisionReal variableByteSubclass:#ShortFloat |
17 |
instanceVariableNames:'' |
|
16670 | 18 |
classVariableNames:'DefaultPrintFormat Epsilon' |
1197 | 19 |
poolDictionaries:'' |
20 |
category:'Magnitude-Numbers' |
|
21 |
! |
|
22 |
||
2394 | 23 |
!ShortFloat primitiveDefinitions! |
24 |
%{ |
|
25 |
||
14744 | 26 |
#include <stdio.h> |
3471 | 27 |
#include <errno.h> |
28 |
||
29 |
#ifndef __OPTIMIZE__ |
|
30 |
# define __OPTIMIZE__ |
|
31 |
#endif |
|
4825 | 32 |
|
33 |
#define __USE_ISOC9X 1 |
|
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
34 |
#define __USE_ISOC99 1 |
3471 | 35 |
#include <math.h> |
36 |
||
16670 | 37 |
#ifndef INT32 |
38 |
# define INT32 int |
|
39 |
#endif |
|
40 |
||
3471 | 41 |
/* |
42 |
* on some systems errno is a macro ... check for it here |
|
43 |
*/ |
|
44 |
#ifndef errno |
|
45 |
extern errno; |
|
46 |
#endif |
|
47 |
||
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
48 |
#if !defined (WIN32) |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
49 |
# include <locale.h> |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
50 |
#endif |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
51 |
|
2394 | 52 |
#if defined (_AIX) |
53 |
# include <float.h> |
|
54 |
#endif |
|
4825 | 55 |
|
2395 | 56 |
#if defined(IRIX) |
57 |
# include <nan.h> |
|
58 |
#endif |
|
4825 | 59 |
|
2397 | 60 |
#if defined(LINUX) |
4825 | 61 |
# ifndef NAN |
5822
4e56f8aadda5
Get nan.h from bits/nan.h for Linux
Stefan Vogel <sv@exept.de>
parents:
5718
diff
changeset
|
62 |
# include <bits/nan.h> |
4825 | 63 |
# endif |
2397 | 64 |
#endif |
4825 | 65 |
|
2400 | 66 |
#if defined(solaris) || defined(sunos) |
2398 | 67 |
# include <nan.h> |
68 |
#endif |
|
2394 | 69 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
70 |
#ifdef WIN32 |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
71 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
72 |
* no finite(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
73 |
* no isnan(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
74 |
*/ |
7419 | 75 |
# ifndef isnan |
76 |
# define isnan(x) \ |
|
16670 | 77 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
78 |
(((unsigned int *)(&x))[1] == 0xFFF80000)) |
|
7419 | 79 |
# endif |
80 |
||
81 |
# ifndef isPositiveInfinity |
|
82 |
# define isPositiveInfinity(x) \ |
|
16670 | 83 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
84 |
(((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
|
85 |
# endif |
7419 | 86 |
|
87 |
# ifndef isNegativeInfinity |
|
14931 | 88 |
# define isNegativeInfinity(x) \ |
16670 | 89 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
90 |
(((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
|
91 |
# endif |
7419 | 92 |
|
93 |
# ifndef isinf |
|
94 |
# define isinf(x) \ |
|
16670 | 95 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
96 |
((((unsigned int *)(&x))[1] & 0x7FF00000) == 0x7FF00000)) |
|
7419 | 97 |
# endif |
98 |
||
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
99 |
# ifndef isfinite |
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
100 |
# define isfinite(x) (!isinf(x) && !isnan(x)) |
7419 | 101 |
# endif |
102 |
||
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
103 |
# ifndef isnanf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
104 |
# define isnanf(x) \ |
16670 | 105 |
(((unsigned int *)(&x))[0] == 0xFFC00000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
106 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
107 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
108 |
# ifndef isPositiveInfinityf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
109 |
# define isPositiveInfinityf(x) \ |
16670 | 110 |
(((unsigned int *)(&x))[0] == 0x7F800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
111 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
112 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
113 |
# ifndef isNegativeInfinityf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
114 |
# define isNegativeInfinityf(x) \ |
16670 | 115 |
(((unsigned int *)(&x))[0] == 0xFF800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
116 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
117 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
118 |
# ifndef isinff |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
119 |
# define isinff(x) \ |
16670 | 120 |
((((unsigned int *)(&x))[0] & 0x7FFFFFFF) == 0x7F800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
121 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
122 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
123 |
# ifndef isfinitef |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
124 |
# define isfinitef(x) (!isinff(x) && !isnanf(x)) |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
125 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
126 |
|
7419 | 127 |
# define NO_ASINH |
128 |
# define NO_ACOSH |
|
129 |
# define NO_ATANH |
|
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
130 |
# ifdef __BORLANDC__ |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
131 |
# define NO_FMODF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
132 |
# define NO_MODFF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
133 |
# endif |
14931 | 134 |
|
135 |
# ifdef __MINGW__ |
|
136 |
# include <string.h> |
|
137 |
# endif |
|
138 |
||
8706 | 139 |
#endif /* WIN32 */ |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
140 |
|
8984 | 141 |
#ifdef solaris |
142 |
# ifndef isfinite |
|
143 |
# define isfinite(f) finite((double)(f)) |
|
144 |
# endif |
|
145 |
#endif |
|
146 |
||
3414 | 147 |
#ifdef realIX |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
148 |
# ifndef isfinite |
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
149 |
# define isfinite(x) 1 |
3414 | 150 |
# endif |
8706 | 151 |
#endif /* realIX */ |
152 |
||
9896 | 153 |
#ifndef isfinitef |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
154 |
# define isfinitef(x) isfinite(x) |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
155 |
#endif |
9896 | 156 |
#ifndef isnanf |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
157 |
# define isnanf(x) isnan(x) |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
158 |
#endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
159 |
|
2394 | 160 |
%} |
161 |
! ! |
|
162 |
||
1880 | 163 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 164 |
|
165 |
copyright |
|
166 |
" |
|
167 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3158 | 168 |
All Rights Reserved |
1197 | 169 |
|
170 |
This software is furnished under a license and may be used |
|
171 |
only in accordance with the terms of that license and with the |
|
172 |
inclusion of the above copyright notice. This software may not |
|
173 |
be provided or otherwise made available to, or used by, any |
|
174 |
other person. No title to or ownership of the software is |
|
175 |
hereby transferred. |
|
176 |
" |
|
177 |
||
178 |
||
179 |
! |
|
180 |
||
181 |
documentation |
|
182 |
" |
|
11945 | 183 |
ShortFloats represent rational numbers with limited precision. |
17220 | 184 |
They use the C-compilers 'float' format, which is usually the IEEE single float format. |
7380 | 185 |
|
186 |
In contrast to Floats (which use the C-compilers 64bit 'double' format), |
|
187 |
ShortFloats give you 32 bit floats. |
|
1197 | 188 |
|
189 |
Notice, that ST/X Floats are what Doubles are in ST-80 and ShortFloats are |
|
14719 | 190 |
ST-80's Floats respectively. The reason was to make ST/X's floats compatible |
191 |
to bothe visualWorks and other smalltalks, which use C-doubles for the Float class. |
|
192 |
Thus, STX's Float precision is not worse than that of other ST's. |
|
193 |
||
1197 | 194 |
WARNING: |
16633 | 195 |
The layout of shortFloat instances is known by the runtime system and the compiler; |
196 |
you may not add instance variables here. |
|
197 |
Also, subclassing is complicated by the fact, that the VM creates floats/shortFloats, |
|
198 |
and does some of its float-checks by an identity compare with the ShortFloat-class. |
|
199 |
(i.e. your subclasses instances may not be recognized as float-like objects, |
|
200 |
thus mixed mode arithmetic will always coerce them, effectively slowing things down). |
|
201 |
This may be changed, to use a flag bit in the class. |
|
1197 | 202 |
|
7380 | 203 |
Mixed mode arithmetic: |
16633 | 204 |
shortFloat op shortFloat -> shortFloat |
205 |
shortFloat op fix -> shortFloat |
|
206 |
shortFloat op fraction -> shortFloat |
|
207 |
shortFloat op integer -> shortFloat |
|
208 |
shortFloat op longFloat -> longFloat |
|
209 |
shortFloat op float -> float |
|
210 |
shortFloat op complex -> complex |
|
1295 | 211 |
|
7405 | 212 |
Representation: |
17220 | 213 |
32bit single precision IEEE floats |
16633 | 214 |
23 bit mantissa, |
215 |
8 bit exponent, |
|
216 |
6 decimal digits (approx) |
|
7405 | 217 |
|
218 |
Range and Precision of Storage Formats: see LimitedPrecisionReal >> documentation |
|
219 |
||
1295 | 220 |
[author:] |
16633 | 221 |
Claus Gittinger |
1556 | 222 |
|
223 |
[see also:] |
|
16633 | 224 |
Number |
225 |
Float LongFloat Fraction FixedPoint Integer Complex |
|
226 |
FloatArray DoubleArray |
|
1197 | 227 |
" |
228 |
! ! |
|
229 |
||
1880 | 230 |
!ShortFloat class methodsFor:'instance creation'! |
1207 | 231 |
|
232 |
basicNew |
|
233 |
"return a new shortFloat - here we return 0.0 |
|
234 |
- shortFloats are usually NOT created this way ... |
|
235 |
Its implemented here to allow things like binary store & load |
|
236 |
of shortFloats. (but even this support will go away eventually, its not |
|
237 |
a good idea to store the bits of a float - the reader might have a |
|
11945 | 238 |
totally different representation - so floats will eventually be |
1207 | 239 |
binary stored in a device independent format." |
240 |
||
241 |
%{ /* NOCONTEXT */ |
|
242 |
OBJ newFloat; |
|
243 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
244 |
__qMKSFLOAT(newFloat, 0.0); |
1207 | 245 |
RETURN (newFloat); |
246 |
%} |
|
3158 | 247 |
! |
248 |
||
16446 | 249 |
coerce:aNumber |
250 |
^ aNumber asShortFloat. |
|
251 |
! |
|
252 |
||
4297 | 253 |
fastFromString:aString at:startIndex |
11945 | 254 |
"return the next ShortFloat from the string starting at startIndex. |
4297 | 255 |
No spaces are skipped. |
256 |
||
257 |
This is a specially tuned entry (using a low-level C-call), which |
|
258 |
returns garbage if the argument string is not a valid float number. |
|
11945 | 259 |
It has been added to allow higher speed string decomposition into numbers, |
260 |
especially for mass-data." |
|
261 |
||
262 |
%{ /* NOCONTEXT */ |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
263 |
if (__isStringLike(aString) && __isSmallInteger(startIndex)) { |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
264 |
char *cp = (char *)(__stringVal(aString)); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
265 |
int idx = __intVal(startIndex) - 1; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
266 |
double atof(); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
267 |
double val; |
4297 | 268 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
269 |
if ((unsigned)idx < __stringSize(aString)) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
270 |
val = atof(cp + idx); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
271 |
RETURN (__MKSFLOAT(val)); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
272 |
} |
4297 | 273 |
} |
274 |
%}. |
|
275 |
self primitiveFailed. |
|
276 |
||
277 |
" |
|
11945 | 278 |
ShortFloat fastFromString:'123.45' at:1 |
279 |
ShortFloat fastFromString:'123.45' at:2 |
|
280 |
ShortFloat fastFromString:'123.45E4' at:1 |
|
281 |
ShortFloat fastFromString:'hello123.45E4' at:6 |
|
282 |
ShortFloat fastFromString:'12345' at:1 |
|
283 |
ShortFloat fastFromString:'12345' at:2 |
|
284 |
ShortFloat fastFromString:'12345' at:3 |
|
285 |
ShortFloat fastFromString:'12345' at:4 |
|
286 |
ShortFloat fastFromString:'12345' at:5 |
|
287 |
ShortFloat fastFromString:'12345' at:6 |
|
288 |
ShortFloat fastFromString:'12345' at:0 |
|
289 |
ShortFloat fastFromString:'hello123.45E4' at:1 |
|
4297 | 290 |
|
291 |
Time millisecondsToRun:[ |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
292 |
100000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
293 |
ShortFloat readFrom:'123.45' |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
294 |
] |
4297 | 295 |
] |
296 |
" |
|
297 |
||
298 |
" |
|
299 |
Time millisecondsToRun:[ |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
300 |
100000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
301 |
ShortFloat fastFromString:'123.45' at:1 |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
302 |
] |
4297 | 303 |
] |
304 |
" |
|
305 |
! |
|
306 |
||
15015 | 307 |
fromIEEE32Bit: anInteger |
308 |
"creates a float, given the four native float bytes as an integer" |
|
309 |
||
310 |
%{ /* NOCONTEXT */ |
|
311 |
||
312 |
REGISTER union { |
|
313 |
unsigned int i; |
|
314 |
float f; |
|
315 |
} r; |
|
316 |
||
317 |
r.i = __unsignedLongIntVal( anInteger ); |
|
15019 | 318 |
RETURN( __MKSFLOAT(r.f) ); |
15015 | 319 |
%} |
17220 | 320 |
|
321 |
" |
|
322 |
ShortFloat fromIEEE32Bit:(#[64 73 15 219] asInteger) |
|
323 |
" |
|
17654 | 324 |
! |
325 |
||
326 |
fromNumber:aNumber |
|
327 |
^aNumber asShortFloat |
|
1207 | 328 |
! ! |
329 |
||
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
330 |
!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
|
331 |
|
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
332 |
defaultPrintFormat |
16670 | 333 |
^ DefaultPrintFormat |
334 |
! |
|
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
335 |
|
16670 | 336 |
defaultPrintFormat:aString |
337 |
DefaultPrintFormat := aString. |
|
338 |
! |
|
339 |
||
340 |
epsilon |
|
341 |
^ Epsilon |
|
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
342 |
! ! |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
343 |
|
7356 | 344 |
!ShortFloat class methodsFor:'binary storage'! |
345 |
||
346 |
readBinaryIEEESingleFrom:aStream |
|
347 |
"read a float value from the binary stream, aStream, |
|
14924 | 348 |
interpreting the next bytes as an IEEE formatted 4-byte float. |
349 |
The bytes are read in the native byte order (i.e.lsb on intel)" |
|
1197 | 350 |
|
7356 | 351 |
|f| |
1197 | 352 |
|
7356 | 353 |
f := ShortFloat basicNew. |
15012 | 354 |
self readBinaryIEEESingleFrom:aStream into:f MSB:(UninterpretedBytes isBigEndian). |
355 |
^ f |
|
356 |
||
357 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
358 |
||
359 |
"Created: / 16-04-1996 / 21:00:35 / cg" |
|
360 |
"Modified: / 23-08-2006 / 16:01:47 / cg" |
|
361 |
! |
|
362 |
||
363 |
readBinaryIEEESingleFrom:aStream MSB:msbFirst |
|
364 |
"read a float value from the binary stream, aStream, |
|
365 |
interpreting the next bytes as an IEEE formatted 4-byte float. |
|
15014 | 366 |
The bytes are read in the soecified byte order" |
15012 | 367 |
|
368 |
|f| |
|
369 |
||
370 |
f := ShortFloat basicNew. |
|
371 |
self readBinaryIEEESingleFrom:aStream into:f MSB:msbFirst. |
|
7356 | 372 |
^ f |
1197 | 373 |
|
9636 | 374 |
"not part of libboss, as this is also used by others (TIFFReader)" |
375 |
||
376 |
"Created: / 16-04-1996 / 21:00:35 / cg" |
|
377 |
"Modified: / 23-08-2006 / 16:01:47 / cg" |
|
1197 | 378 |
! |
379 |
||
7356 | 380 |
readBinaryIEEESingleFrom:aStream into:aFloat |
381 |
"read a float value from the binary stream, aStream, |
|
14922 | 382 |
interpreting the next bytes as an IEEE formatted 4-byte float. |
14924 | 383 |
The bytes are read in the native byte order (i.e.lsb on intel)" |
14922 | 384 |
|
385 |
^ self readBinaryIEEESingleFrom:aStream into:aFloat MSB:(UninterpretedBytes isBigEndian) |
|
386 |
||
387 |
"Modified: / 23-08-2006 / 16:01:52 / cg" |
|
388 |
! |
|
389 |
||
390 |
readBinaryIEEESingleFrom:aStream into:aFloat MSB:msb |
|
391 |
"read a float value from the binary stream, aStream, |
|
392 |
interpreting the next bytes as an IEEE formatted 4-byte float. |
|
393 |
If msb is true, the stream bytes are most-significant-first." |
|
7356 | 394 |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
395 |
aFloat class == self ifFalse:[self error:'no a ShortFloat']. |
6574 | 396 |
|
7356 | 397 |
" |
398 |
this implementation is wrong: does not work on non-IEEE machines |
|
399 |
(to date all machines where ST/X is running on use |
|
400 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
401 |
" |
|
402 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
403 |
||
14922 | 404 |
(UninterpretedBytes isBigEndian == msb) ifFalse:[ |
14931 | 405 |
"swap the bytes" |
406 |
4 to:1 by:-1 do:[:i | |
|
407 |
aFloat basicAt:i put:(aStream next) |
|
408 |
]. |
|
409 |
^ self |
|
7356 | 410 |
]. |
411 |
1 to:4 do:[:i | |
|
14931 | 412 |
aFloat basicAt:i put:aStream next |
7356 | 413 |
] |
9636 | 414 |
|
415 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
416 |
||
417 |
"Modified: / 23-08-2006 / 16:01:52 / cg" |
|
6574 | 418 |
! |
419 |
||
7356 | 420 |
storeBinaryIEEESingle:aFloat on:aStream |
421 |
"store aFloat as an IEEE formatted 4-byte float |
|
14924 | 422 |
onto the binary stream, aStream. |
423 |
The bytes are written in the native byte order (i.e.lsb on intel)" |
|
424 |
||
425 |
self storeBinaryIEEESingle:aFloat on:aStream MSB:(UninterpretedBytes isBigEndian). |
|
426 |
! |
|
427 |
||
428 |
storeBinaryIEEESingle:aFloat on:aStream MSB:msb |
|
429 |
"store aFloat as an IEEE formatted 4-byte float |
|
430 |
onto the binary stream, aStream. |
|
431 |
If msb is true, the stream bytes are written most-significant-first." |
|
7356 | 432 |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
433 |
|float| |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
434 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
435 |
float := aFloat asShortFloat. |
7356 | 436 |
|
437 |
" |
|
438 |
this implementation is wrong: does not work on non-IEEE machines |
|
439 |
(to date all machines where ST/X is running on use |
|
440 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
441 |
" |
|
442 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
6574 | 443 |
|
14924 | 444 |
(UninterpretedBytes isBigEndian == msb) ifFalse:[ |
14931 | 445 |
"swap the bytes" |
446 |
4 to:1 by:-1 do:[:i | |
|
447 |
aStream nextPut:(float basicAt:i). |
|
448 |
]. |
|
449 |
^ self |
|
7356 | 450 |
]. |
451 |
1 to:4 do:[:i | |
|
14931 | 452 |
aStream nextPut:(float basicAt:i). |
7356 | 453 |
] |
9636 | 454 |
|
455 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
456 |
||
457 |
"Modified: / 23-08-2006 / 16:01:55 / cg" |
|
7356 | 458 |
! ! |
459 |
||
16670 | 460 |
!ShortFloat class methodsFor:'class initialization'! |
461 |
||
462 |
initialize |
|
463 |
Epsilon isNil ifTrue:[ |
|
464 |
DefaultPrintFormat := '.7'. "/ print 7 valid digits |
|
465 |
Epsilon := self computeEpsilon. |
|
466 |
]. |
|
467 |
||
468 |
" |
|
469 |
Epsilon := nil. |
|
470 |
self initialize |
|
471 |
" |
|
472 |
! ! |
|
473 |
||
7356 | 474 |
!ShortFloat class methodsFor:'constants'! |
6574 | 475 |
|
7392 | 476 |
e |
477 |
"return the constant e as ShortFloat" |
|
478 |
||
479 |
^ Float e asShortFloat |
|
480 |
! |
|
481 |
||
482 |
pi |
|
483 |
"return the constant pi as ShortFloat" |
|
484 |
||
485 |
^ Float pi asShortFloat |
|
486 |
! |
|
487 |
||
1197 | 488 |
unity |
1260 | 489 |
"return the neutral element for multiplication (1.0) as ShortFloat" |
1197 | 490 |
|
491 |
^ 1.0 asShortFloat |
|
492 |
||
1260 | 493 |
"Modified: 23.4.1996 / 09:26:51 / cg" |
1197 | 494 |
! |
495 |
||
496 |
zero |
|
1260 | 497 |
"return the neutral element for addition (0.0) as ShortFloat" |
1197 | 498 |
|
499 |
^ 0.0 asShortFloat |
|
500 |
||
1260 | 501 |
"Modified: 23.4.1996 / 09:26:45 / cg" |
1197 | 502 |
! ! |
503 |
||
1880 | 504 |
!ShortFloat class methodsFor:'queries'! |
1197 | 505 |
|
6898 | 506 |
exponentCharacter |
507 |
^ $e |
|
508 |
! |
|
509 |
||
1197 | 510 |
isBuiltInClass |
1264 | 511 |
"return true if this class is known by the run-time-system. |
512 |
Here, true is returned for myself, false for subclasses." |
|
1197 | 513 |
|
514 |
^ self == ShortFloat |
|
515 |
||
1264 | 516 |
"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
|
517 |
! |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
518 |
|
7356 | 519 |
numBitsInExponent |
520 |
"answer the number of bits in the exponent |
|
521 |
This is an IEEE float, where 8 bits are available: |
|
11945 | 522 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
7356 | 523 |
" |
524 |
||
525 |
^ 8 |
|
526 |
! |
|
527 |
||
528 |
numBitsInMantissa |
|
529 |
"answer the number of bits in the mantissa. |
|
530 |
This is an IEEE float, where 23 bits (the hidden one is not counted here) are available: |
|
11945 | 531 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
7356 | 532 |
" |
533 |
||
534 |
^ 23 |
|
535 |
! |
|
536 |
||
537 |
precision |
|
538 |
"answer the precision of a ShortFloat (in bits) |
|
11945 | 539 |
This is an IEEE float, where only the fraction from the normalized mantissa is stored |
9149 | 540 |
and so there is a hidden bit and the mantissa is actually represented by 24 binary digits |
541 |
(although only 23 are needed in the binary representation)" |
|
7356 | 542 |
|
543 |
^ 24 |
|
544 |
! |
|
545 |
||
546 |
radix |
|
547 |
"answer the radix of a ShortFloats exponent |
|
548 |
This is an IEEE float, which is represented as binary" |
|
549 |
||
9149 | 550 |
^ 2 "must be careful here, whenever ST/X is used on a VAX or a 370" |
1197 | 551 |
! ! |
552 |
||
553 |
!ShortFloat methodsFor:'arithmetic'! |
|
554 |
||
555 |
* aNumber |
|
11730 | 556 |
"return the product of the receiver and the argument." |
1197 | 557 |
|
558 |
%{ /* NOCONTEXT */ |
|
559 |
||
560 |
OBJ newFloat; |
|
561 |
float result; |
|
562 |
double dResult; |
|
563 |
||
564 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 565 |
result = __shortFloatVal(self) * (float)(__intVal(aNumber)); |
1197 | 566 |
retResult: |
11945 | 567 |
__qMKSFLOAT(newFloat, result); |
568 |
RETURN ( newFloat ); |
|
569 |
} |
|
7356 | 570 |
if (__isShortFloat(aNumber)) { |
11945 | 571 |
result = __shortFloatVal(self) * __shortFloatVal(aNumber); |
572 |
goto retResult; |
|
573 |
} |
|
7356 | 574 |
if (__isFloatLike(aNumber)) { |
11945 | 575 |
dResult = (double) __shortFloatVal(self)* __floatVal(aNumber); |
576 |
__qMKFLOAT(newFloat, dResult); |
|
577 |
RETURN ( newFloat ); |
|
1197 | 578 |
} |
579 |
%}. |
|
580 |
^ aNumber productFromShortFloat:self |
|
581 |
! |
|
582 |
||
583 |
+ aNumber |
|
584 |
"return the sum of the receiver and the argument, aNumber" |
|
585 |
||
586 |
%{ /* NOCONTEXT */ |
|
587 |
||
588 |
OBJ newFloat; |
|
589 |
float result; |
|
590 |
double dResult; |
|
591 |
||
592 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 593 |
result = __shortFloatVal(self) + (float)(__intVal(aNumber)); |
1197 | 594 |
retResult: |
11945 | 595 |
__qMKSFLOAT(newFloat, result); |
596 |
RETURN ( newFloat ); |
|
597 |
} |
|
7356 | 598 |
if (__isShortFloat(aNumber)) { |
11945 | 599 |
result = __shortFloatVal(self) + __shortFloatVal(aNumber); |
600 |
goto retResult; |
|
601 |
} |
|
7356 | 602 |
if (__isFloatLike(aNumber)) { |
11945 | 603 |
dResult = (double) __shortFloatVal(self) + __floatVal(aNumber); |
604 |
__qMKFLOAT(newFloat, dResult); |
|
605 |
RETURN ( newFloat ); |
|
1197 | 606 |
} |
607 |
%}. |
|
608 |
^ aNumber sumFromShortFloat:self |
|
609 |
! |
|
610 |
||
611 |
- aNumber |
|
612 |
"return the difference of the receiver and the argument, aNumber" |
|
613 |
||
614 |
%{ /* NOCONTEXT */ |
|
615 |
||
616 |
OBJ newFloat; |
|
617 |
float result; |
|
618 |
double dResult; |
|
619 |
||
620 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 621 |
result = __shortFloatVal(self) - (float)(__intVal(aNumber)); |
1197 | 622 |
retResult: |
11945 | 623 |
__qMKSFLOAT(newFloat, result); |
624 |
RETURN ( newFloat ); |
|
625 |
} |
|
7356 | 626 |
if (__isShortFloat(aNumber)) { |
11945 | 627 |
result = __shortFloatVal(self) - __shortFloatVal(aNumber); |
628 |
goto retResult; |
|
629 |
} |
|
7356 | 630 |
if (__isFloatLike(aNumber)) { |
11945 | 631 |
dResult = (double) __shortFloatVal(self) - __floatVal(aNumber); |
632 |
__qMKFLOAT(newFloat, dResult); |
|
633 |
RETURN ( newFloat ); |
|
1197 | 634 |
} |
635 |
%}. |
|
636 |
^ aNumber differenceFromShortFloat:self |
|
637 |
! |
|
638 |
||
639 |
/ aNumber |
|
640 |
"return the quotient of the receiver and the argument, aNumber" |
|
641 |
||
642 |
%{ /* NOCONTEXT */ |
|
643 |
||
644 |
OBJ newFloat; |
|
645 |
float result, val; |
|
646 |
double dResult, dVal; |
|
647 |
||
648 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 649 |
if (aNumber != __mkSmallInteger(0)) { |
650 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
1197 | 651 |
retResult: |
11945 | 652 |
__qMKSFLOAT(newFloat, result); |
653 |
RETURN ( newFloat ); |
|
654 |
} |
|
655 |
} |
|
7356 | 656 |
if (__isShortFloat(aNumber)) { |
11945 | 657 |
val = __shortFloatVal(aNumber); |
658 |
if (val != 0.0) { |
|
659 |
result = __shortFloatVal(self) / val; |
|
660 |
goto retResult; |
|
661 |
} |
|
662 |
} |
|
7356 | 663 |
if (__isFloatLike(aNumber)) { |
11945 | 664 |
dVal = __floatVal(aNumber); |
665 |
if (dVal != 0.0) { |
|
666 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
667 |
__qMKFLOAT(newFloat, dResult); |
|
668 |
RETURN ( newFloat ); |
|
669 |
} |
|
1197 | 670 |
} |
671 |
%}. |
|
672 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
11945 | 673 |
" |
674 |
No, you shalt not divide by zero |
|
675 |
" |
|
676 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
1197 | 677 |
]. |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
678 |
^ aNumber quotientFromShortFloat:self |
1197 | 679 |
! |
680 |
||
14710 | 681 |
abs |
682 |
"return the absolute value of the receiver |
|
683 |
reimplemented here for speed" |
|
684 |
||
685 |
%{ /* NOCONTEXT */ |
|
686 |
||
687 |
OBJ newFloat; |
|
688 |
float val = __shortFloatVal(self); |
|
689 |
||
690 |
if (val < 0.0) { |
|
691 |
__qMKSFLOAT(newFloat, -val); |
|
692 |
RETURN ( newFloat ); |
|
693 |
} |
|
694 |
RETURN (self); |
|
695 |
%}. |
|
696 |
||
697 |
" |
|
698 |
3.0 asShortFloat abs |
|
699 |
-3.0 asShortFloat abs |
|
700 |
" |
|
701 |
! |
|
702 |
||
703 |
negated |
|
704 |
"return myself negated" |
|
705 |
||
706 |
%{ /* NOCONTEXT */ |
|
707 |
OBJ newFloat; |
|
708 |
float rslt = - __shortFloatVal(self); |
|
709 |
||
710 |
__qMKSFLOAT(newFloat, rslt); |
|
711 |
RETURN ( newFloat ); |
|
712 |
%}. |
|
713 |
^ 0.0 - self |
|
714 |
||
715 |
! |
|
716 |
||
14690 | 717 |
rem: aNumber |
718 |
"return the floating point remainder of the receiver and the argument, aNumber" |
|
719 |
||
720 |
%{ /* NOCONTEXT */ |
|
721 |
||
722 |
/* |
|
723 |
* notice: |
|
724 |
* the following inline code handles some common cases, |
|
725 |
* and exists as an optimization, to speed up those cases. |
|
726 |
* |
|
727 |
* Conceptionally, (and for most other argument types), |
|
728 |
* mixed arithmetic is implemented by double dispatching |
|
729 |
* (see the message send at the bottom) |
|
730 |
*/ |
|
731 |
OBJ newFloat; |
|
732 |
float result, val; |
|
733 |
double dResult, dVal; |
|
734 |
||
735 |
if (__isSmallInteger(aNumber)) { |
|
736 |
if (aNumber != __mkSmallInteger(0)) { |
|
737 |
val = (float)__intVal(aNumber); |
|
738 |
computeResult: |
|
14709
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
739 |
#ifdef NO_FMODF |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
740 |
dResult = fmod((double)__shortFloatVal(self), (double)val) ; |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
741 |
result = (float)dResult; |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
742 |
#else |
14690 | 743 |
result = fmodf(__shortFloatVal(self), val) ; |
14709
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
744 |
#endif |
14690 | 745 |
__qMKSFLOAT(newFloat, result); |
746 |
RETURN ( newFloat ); |
|
747 |
} |
|
748 |
} else if (__isFloatLike(aNumber)) { |
|
749 |
dVal = __floatVal(aNumber); |
|
750 |
if (dVal != 0.0) { |
|
751 |
dResult = fmod((double)(__shortFloatVal(self)), dVal) ; |
|
752 |
__qMKFLOAT(newFloat, dResult); |
|
753 |
RETURN ( newFloat ); |
|
754 |
} |
|
755 |
} else if (__isShortFloat(aNumber)) { |
|
756 |
val = __shortFloatVal(aNumber); |
|
757 |
if (val != 0.0) { |
|
758 |
goto computeResult; |
|
759 |
} |
|
760 |
} |
|
761 |
%}. |
|
762 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
763 |
" |
|
764 |
No, you shalt not divide by zero |
|
765 |
" |
|
766 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
767 |
]. |
|
768 |
^ aNumber remainderFromShortFloat:self |
|
769 |
! |
|
770 |
||
3147 | 771 |
uncheckedDivide:aNumber |
7356 | 772 |
"return the quotient of the receiver and the argument, aNumber. |
7471 | 773 |
Do not check for divide by zero (return NaN or Infinity). |
7356 | 774 |
This operation is provided for emulators of other languages/semantics, |
775 |
where no exception is raised for these results (i.e. Java). |
|
776 |
Its only defined if the arguments type is the same as the receivers." |
|
3147 | 777 |
|
778 |
%{ /* NOCONTEXT */ |
|
779 |
||
780 |
OBJ newFloat; |
|
781 |
float result, val; |
|
782 |
double dResult, dVal; |
|
783 |
||
784 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 785 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
3147 | 786 |
retResult: |
11945 | 787 |
__qMKSFLOAT(newFloat, result); |
788 |
RETURN ( newFloat ); |
|
789 |
} |
|
7356 | 790 |
if (__isShortFloat(aNumber)) { |
11945 | 791 |
val = __shortFloatVal(aNumber); |
792 |
result = __shortFloatVal(self) / val; |
|
793 |
goto retResult; |
|
794 |
} |
|
7356 | 795 |
if (__isFloatLike(aNumber)) { |
11945 | 796 |
dVal = __floatVal(aNumber); |
797 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
798 |
__qMKFLOAT(newFloat, dResult); |
|
799 |
RETURN ( newFloat ); |
|
3147 | 800 |
} |
801 |
%}. |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
802 |
^ aNumber quotientFromShortFloat:self |
3147 | 803 |
|
804 |
" |
|
805 |
0.0 asShortFloat uncheckedDivide:0 |
|
806 |
1.0 asShortFloat uncheckedDivide:0.0 |
|
807 |
" |
|
1197 | 808 |
! ! |
809 |
||
5257 | 810 |
!ShortFloat methodsFor:'coercing & converting'! |
1197 | 811 |
|
812 |
asFloat |
|
11238 | 813 |
"return a Float with same value as the receiver. |
814 |
Redefined for performance (machine can do it faster)" |
|
1197 | 815 |
|
816 |
%{ /* NOCONTEXT */ |
|
817 |
||
818 |
OBJ newFloat; |
|
819 |
double dVal = (double)__shortFloatVal(self); |
|
820 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
821 |
__qMKFLOAT(newFloat, dVal); |
1197 | 822 |
RETURN ( newFloat ); |
823 |
%} |
|
824 |
||
825 |
" |
|
11945 | 826 |
1.0 asShortFloat asFloat |
1197 | 827 |
" |
828 |
! |
|
829 |
||
830 |
asInteger |
|
831 |
"return an integer with same value - might truncate" |
|
832 |
||
833 |
%{ /* NOCONTEXT */ |
|
1670 | 834 |
float fVal; |
1197 | 835 |
|
1670 | 836 |
fVal = __shortFloatVal(self); |
7419 | 837 |
#ifdef WIN32 |
11945 | 838 |
if (! isnanf(fVal)) |
7419 | 839 |
#endif |
840 |
{ |
|
11945 | 841 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
842 |
RETURN ( __mkSmallInteger( (INT)fVal) ); |
|
843 |
} |
|
1197 | 844 |
} |
845 |
%}. |
|
846 |
^ super asInteger |
|
847 |
||
848 |
" |
|
849 |
12345.0 asShortFloat asInteger |
|
850 |
1e15 asShortFloat asInteger |
|
851 |
" |
|
852 |
! |
|
853 |
||
7437
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
854 |
asLongFloat |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
855 |
^ LongFloat fromShortFloat:self |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
856 |
! |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
857 |
|
1197 | 858 |
asShortFloat |
15022 | 859 |
"return a ShortFloat with same value as the receiver - that's me" |
1197 | 860 |
|
861 |
^ self |
|
1200 | 862 |
! |
863 |
||
7356 | 864 |
coerce:aNumber |
11720 | 865 |
"convert the argument aNumber into an instance of the receivers class and return it." |
7356 | 866 |
|
867 |
^ aNumber asShortFloat |
|
868 |
! |
|
869 |
||
1200 | 870 |
generality |
871 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
872 |
||
873 |
^ 70 |
|
874 |
||
875 |
||
1197 | 876 |
! ! |
877 |
||
878 |
!ShortFloat methodsFor:'comparing'! |
|
879 |
||
880 |
< aNumber |
|
881 |
"return true, if the argument is greater" |
|
882 |
||
883 |
%{ /* NOCONTEXT */ |
|
884 |
||
885 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 886 |
RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false ); |
1197 | 887 |
} |
7356 | 888 |
if (aNumber != nil) { |
11945 | 889 |
if (__qIsFloatLike(aNumber)) { |
890 |
RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false ); |
|
891 |
} |
|
892 |
if (__qIsShortFloat(aNumber)) { |
|
893 |
RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false ); |
|
894 |
} |
|
1197 | 895 |
} |
896 |
%}. |
|
897 |
^ aNumber lessFromShortFloat:self |
|
898 |
||
1200 | 899 |
" |
900 |
1.0 asShortFloat > (1/3) |
|
901 |
" |
|
1197 | 902 |
! |
903 |
||
904 |
<= aNumber |
|
905 |
"return true, if the argument is greater or equal" |
|
906 |
||
907 |
%{ /* NOCONTEXT */ |
|
908 |
||
909 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 910 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 911 |
} |
7356 | 912 |
if (aNumber != nil) { |
11945 | 913 |
if (__qIsFloatLike(aNumber)) { |
914 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
|
915 |
} |
|
916 |
if (__qIsShortFloat(aNumber)) { |
|
917 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
|
918 |
} |
|
919 |
} |
|
1197 | 920 |
%}. |
921 |
^ self retry:#<= coercing:aNumber |
|
922 |
! |
|
923 |
||
924 |
= aNumber |
|
7356 | 925 |
"return true, if the argument represents the same numeric value |
926 |
as the receiver, false otherwise" |
|
1197 | 927 |
|
928 |
%{ /* NOCONTEXT */ |
|
929 |
||
930 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 931 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
7356 | 932 |
} |
933 |
if (aNumber == nil) { |
|
11945 | 934 |
RETURN (false); |
1197 | 935 |
} |
7356 | 936 |
if (__qIsFloatLike(aNumber)) { |
11945 | 937 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
1197 | 938 |
} |
7356 | 939 |
if (__qIsShortFloat(aNumber)) { |
11945 | 940 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
1197 | 941 |
} |
942 |
%}. |
|
7356 | 943 |
^ aNumber equalFromShortFloat:self |
1197 | 944 |
! |
945 |
||
946 |
> aNumber |
|
947 |
"return true, if the argument is less" |
|
948 |
||
949 |
%{ /* NOCONTEXT */ |
|
950 |
||
951 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 952 |
RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false ); |
1197 | 953 |
} |
7356 | 954 |
if (aNumber != nil) { |
11945 | 955 |
if (__qIsFloatLike(aNumber)) { |
956 |
RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false ); |
|
957 |
} |
|
958 |
if (__qIsShortFloat(aNumber)) { |
|
959 |
RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false ); |
|
960 |
} |
|
1197 | 961 |
} |
962 |
%}. |
|
963 |
^ self retry:#> coercing:aNumber |
|
964 |
! |
|
965 |
||
966 |
>= aNumber |
|
967 |
"return true, if the argument is less or equal" |
|
968 |
||
969 |
%{ /* NOCONTEXT */ |
|
970 |
||
971 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 972 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 973 |
} |
7356 | 974 |
if (aNumber != nil) { |
11945 | 975 |
if (__qIsFloatLike(aNumber)) { |
976 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
|
977 |
} |
|
978 |
if (__qIsShortFloat(aNumber)) { |
|
979 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
|
980 |
} |
|
1197 | 981 |
} |
982 |
%}. |
|
983 |
^ self retry:#>= coercing:aNumber |
|
984 |
! |
|
985 |
||
986 |
hash |
|
987 |
"return a number for hashing; redefined, since floats compare |
|
988 |
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
|
989 |
as 3 hash." |
1197 | 990 |
|
991 |
|i| |
|
992 |
||
993 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
994 |
i := self asInteger. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
995 |
self = i ifTrue:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
996 |
^ i hash |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
997 |
]. |
1197 | 998 |
]. |
999 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
1000 |
^ self asFloat hash |
1197 | 1001 |
|
1002 |
" |
|
11945 | 1003 |
1.2345 hash |
1004 |
1.2345 asShortFloat hash |
|
1005 |
1.0 hash |
|
1006 |
1.0 asShortFloat hash |
|
1007 |
0.5 asShortFloat hash |
|
1008 |
0.25 asShortFloat hash |
|
1009 |
0.5 hash |
|
1010 |
0.25 hash |
|
1197 | 1011 |
" |
1012 |
! |
|
1013 |
||
16670 | 1014 |
isAlmostEqualTo:aNumber nEpsilon:nE |
1015 |
"return true, if the argument, aNumber represents almost the same numeric value |
|
1016 |
as the receiver, false otherwise. |
|
1017 |
||
1018 |
nE is the number of minimal float distances, that the numbers may differ and |
|
1019 |
still be considered equal. |
|
1020 |
||
1021 |
For background information why floats need this |
|
1022 |
read: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ |
|
1023 |
" |
|
1024 |
||
1025 |
%{ /* NOCONTEXT */ |
|
1026 |
||
1027 |
/* |
|
1028 |
* notice: |
|
1029 |
* the following inline code handles some common cases, |
|
1030 |
* and exists as an optimization, to speed up those cases. |
|
1031 |
* |
|
1032 |
* Conceptionally, (and for most other argument types), |
|
1033 |
* mixed arithmetic is implemented by double dispatching |
|
1034 |
* (see the message send at the bottom) |
|
1035 |
*/ |
|
1036 |
||
1037 |
INT32 ulpDiff; |
|
1038 |
union { |
|
1039 |
float f; |
|
1040 |
INT32 i; |
|
1041 |
} myself, otherFloat; |
|
1042 |
int nEpsilon; |
|
1043 |
float scaledEpsilon; |
|
1044 |
||
1045 |
||
1046 |
if (!__isSmallInteger(nE)) { |
|
1047 |
goto tryHarder; |
|
1048 |
} |
|
1049 |
||
1050 |
nEpsilon = __intVal(nE); |
|
1051 |
scaledEpsilon = nEpsilon *__shortFloatVal(@global(Epsilon)); |
|
1052 |
||
1053 |
if (__isSmallInteger(aNumber)) { |
|
1054 |
otherFloat.f = (float)(__intVal(aNumber)); |
|
1055 |
} else if (aNumber == nil) { |
|
1056 |
RETURN(false) |
|
1057 |
} else if (__qIsFloatLike(aNumber)) { |
|
1058 |
otherFloat.f = (float)(__floatVal(aNumber)); |
|
1059 |
} else if (__qIsShortFloat(aNumber)) { |
|
1060 |
otherFloat.f = (double)(__shortFloatVal(aNumber)); |
|
1061 |
} else { |
|
1062 |
goto tryHarder; |
|
1063 |
} |
|
1064 |
||
1065 |
myself.f = __shortFloatVal(self); |
|
1066 |
||
1067 |
// Check if the numbers are really close -- needed |
|
1068 |
// when comparing numbers near zero (ULP method below fails for numbers near 0!). |
|
1069 |
if (fabs(myself.f - otherFloat.f) <= scaledEpsilon) { |
|
1070 |
RETURN(true); |
|
1071 |
} |
|
1072 |
||
1073 |
// if the signs differ, the numbers are different |
|
1074 |
if ((myself.f >= 0) != (otherFloat.f >= 0)) { |
|
1075 |
RETURN(false); |
|
1076 |
} |
|
1077 |
||
1078 |
// compute the difference of the 'units in the last place" ULP |
|
1079 |
// (if ulpDiff == 1, two floats are adjecant) |
|
1080 |
ulpDiff = myself.i - otherFloat.i; |
|
1081 |
if (ulpDiff < 0) ulpDiff = -ulpDiff; |
|
1082 |
if (ulpDiff <= nEpsilon) { |
|
1083 |
RETURN(true); |
|
1084 |
} else { |
|
1085 |
RETURN(false) |
|
1086 |
} |
|
1087 |
||
1088 |
tryHarder:; |
|
1089 |
%}. |
|
1090 |
^ aNumber isAlmostEqualToFromShortFloat:self nEpsilon:nE |
|
1091 |
||
1092 |
" |
|
1093 |
67329.234 asShortFloat isAlmostEqualTo:67329.23400000001 nEpsilon:1 |
|
1094 |
1.0 asShortFloat isAlmostEqualTo:1.0001 nEpsilon:1 |
|
1095 |
1.0 asShortFloat isAlmostEqualTo:-1.0 nEpsilon:1 |
|
1096 |
1.0 asShortFloat isAlmostEqualTo:1 nEpsilon:1 |
|
1097 |
" |
|
1098 |
! |
|
1099 |
||
1197 | 1100 |
~= aNumber |
1101 |
"return true, if the arguments value are not equal" |
|
1102 |
||
1103 |
%{ /* NOCONTEXT */ |
|
1104 |
||
7356 | 1105 |
if (aNumber != nil) { |
11945 | 1106 |
if (__isSmallInteger(aNumber)) { |
1107 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
|
1108 |
} |
|
1109 |
if (__qIsFloatLike(aNumber)) { |
|
1110 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
|
1111 |
} |
|
1112 |
if (__qIsShortFloat(aNumber)) { |
|
1113 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
|
1114 |
} |
|
7356 | 1115 |
} else { |
11945 | 1116 |
RETURN ( true ); |
1197 | 1117 |
} |
1118 |
%}. |
|
7356 | 1119 |
^ super ~= aNumber |
1197 | 1120 |
! ! |
1121 |
||
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1122 |
!ShortFloat methodsFor:'mathematical functions'! |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1123 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1124 |
fastInverseSqrt |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1125 |
"return a rough but fast approximation of (1 / self sqrt). |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1126 |
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
|
1127 |
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
|
1128 |
much, unless inlined into the sender code. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1129 |
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
|
1130 |
(to speed up 3D computations, for example). |
1aac241c9981
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13359
diff
changeset
|
1131 |
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
|
1132 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1133 |
%{ /* NOCONTEXT */ |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1134 |
float x, rslt; |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1135 |
OBJ newFloat; |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1136 |
|
13361 | 1137 |
if (sizeof(float) == 4) { |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1138 |
x = __shortFloatVal(self); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1139 |
{ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1140 |
float xhalf = 0.5f * x; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1141 |
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
|
1142 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1143 |
i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1144 |
x = *(float*)&i; // convert new bits into float |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1145 |
x = x*(1.5f - xhalf*x*x); // One round of Newton's method |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1146 |
__qMKSFLOAT(newFloat, x); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1147 |
RETURN ( newFloat ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1148 |
} |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1149 |
} |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1150 |
%}. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1151 |
^ 1 / self sqrt |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1152 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1153 |
" |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1154 |
10.0 asShortFloat fastInverseSqrt |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1155 |
(1 / 10.0 asShortFloat sqrt) |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1156 |
" |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1157 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1158 |
" |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1159 |
|a b t0 t1 t2| |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1160 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1161 |
a := 345 asShortFloat. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1162 |
t0 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1163 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1164 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1165 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1166 |
t1 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1167 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1168 |
a fastInverseSqrt |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1169 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1170 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1171 |
t2 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1172 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1173 |
(1 / a sqrt) |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1174 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1175 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1176 |
Transcript show:'empty: '; showCR:t0. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1177 |
Transcript show:'fast: '; showCR:t1. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1178 |
Transcript show:'regular: '; showCR:t2. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1179 |
" |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1180 |
! ! |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1181 |
|
1197 | 1182 |
!ShortFloat methodsFor:'printing & storing'! |
1183 |
||
1184 |
printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1185 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1186 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1187 |
#printOn: as basic print mechanism." |
1197 | 1188 |
|
1189 |
%{ /* NOCONTEXT */ |
|
1190 |
||
1191 |
char buffer[64]; |
|
1192 |
REGISTER char *cp; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1193 |
int len; |
1197 | 1194 |
OBJ s; |
16670 | 1195 |
char *fmt; |
1196 |
char fmtBuffer[20]; |
|
1197 |
||
1198 |
if (__isStringLike(@global(DefaultPrintFormat))) { |
|
1199 |
fmt = (char *) __stringVal(@global(DefaultPrintFormat)); |
|
1200 |
} else { |
|
1201 |
/* |
|
1202 |
* in case we get called before #initialize ... |
|
1203 |
*/ |
|
1204 |
fmt = ".7"; |
|
1205 |
} |
|
1206 |
||
1207 |
/* |
|
1208 |
* build a printf format string |
|
1209 |
*/ |
|
1210 |
fmtBuffer[0] = '%'; |
|
1211 |
strncpy(fmtBuffer+1, fmt, 10); |
|
1212 |
strcat(fmtBuffer, "g"); |
|
1197 | 1213 |
|
1214 |
/* |
|
1215 |
* actually only needed on sparc: since thisContext is |
|
1216 |
* in a global register, which gets destroyed by printf, |
|
1217 |
* manually save it here - very stupid ... |
|
1218 |
*/ |
|
1219 |
__BEGIN_PROTECT_REGISTERS__ |
|
16670 | 1220 |
len = snprintf(buffer, sizeof(buffer), fmtBuffer, (float)__shortFloatVal(self)); |
1197 | 1221 |
__END_PROTECT_REGISTERS__ |
1222 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1223 |
if (len >= 0 && len <= sizeof(buffer)-3) { |
16670 | 1224 |
/* |
1225 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
|
1226 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
|
1227 |
*/ |
|
1228 |
for (cp = buffer; *cp; cp++) { |
|
1229 |
if ((*cp == '.') || (*cp == ',') || (*cp == 'E') || (*cp == 'e')) break; |
|
1230 |
} |
|
1231 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
|
1232 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
|
1233 |
*cp++ = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
|
1234 |
} else { |
|
1235 |
*cp++ = '.'; |
|
1236 |
} |
|
1237 |
*cp++ = '0'; |
|
1238 |
*cp = '\0'; |
|
1239 |
} else { |
|
1240 |
if (cp && (*cp == '.')) { |
|
1241 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
|
1242 |
*cp = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
|
1243 |
} |
|
1244 |
} |
|
1245 |
} |
|
1197 | 1246 |
|
16670 | 1247 |
s = __MKSTRING(buffer); |
1248 |
if (s != nil) { |
|
1249 |
RETURN (s); |
|
1250 |
} |
|
1197 | 1251 |
} |
1252 |
%}. |
|
5432 | 1253 |
^ self asFloat printString |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1254 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1255 |
" |
16670 | 1256 |
1.234 asShortFloat printString. |
1257 |
1.0 asShortFloat printString. |
|
1258 |
1e10 asShortFloat printString. |
|
1259 |
1.2e3 asShortFloat printString. |
|
1260 |
1.2e30 asShortFloat printString. |
|
1261 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
|
1262 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
|
1263 |
self pi printString. |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1264 |
|
16670 | 1265 |
self pi printString. |
1266 |
DefaultPrintFormat := '.3'. |
|
1267 |
self pi printString. |
|
1268 |
DefaultPrintFormat := '.7'. |
|
1269 |
||
1270 |
DecimalPointCharacterForPrinting := $,. |
|
1271 |
1.234 asShortFloat printString. |
|
1272 |
1.0 asShortFloat printString. |
|
1273 |
1e10 asShortFloat printString. |
|
1274 |
1.2e3 asShortFloat printString. |
|
1275 |
1.2e30 asShortFloat printString. |
|
1276 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
|
1277 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
|
1278 |
DecimalPointCharacterForPrinting := $. |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1279 |
" |
5718 | 1280 |
! |
1281 |
||
1282 |
printfPrintString:formatString |
|
7746 | 1283 |
"non-standard: return a printed representation of the receiver |
5718 | 1284 |
as specified by formatString, which is defined by printf. |
1285 |
If you use this, be aware, that specifying doubles differs on |
|
11945 | 1286 |
systems; on SYSV machines you have to give something like %lf, |
5718 | 1287 |
while on BSD systems the format string has to be %F. |
1288 |
Also, the resulting string may not be longer than 255 bytes - |
|
1289 |
since thats the (static) size of the buffer. |
|
11924
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1290 |
This method is NONSTANDARD and may be removed without notice. |
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1291 |
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
|
1292 |
Please use the printf: method, which is safe as it is completely implemented in Smalltalk." |
5718 | 1293 |
|
1294 |
%{ /* STACK: 400 */ |
|
1295 |
char buffer[256]; |
|
1296 |
OBJ s; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1297 |
int len; |
5718 | 1298 |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1299 |
if (__isStringLike(formatString)) { |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1300 |
/* |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1301 |
* actually only needed on sparc: since thisContext is |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1302 |
* in a global register, which gets destroyed by printf, |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1303 |
* manually save it here - very stupid ... |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1304 |
*/ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1305 |
__BEGIN_PROTECT_REGISTERS__ |
5718 | 1306 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1307 |
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
|
1308 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1309 |
__END_PROTECT_REGISTERS__ |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1310 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1311 |
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
|
1312 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1313 |
s = __MKSTRING_L(buffer, len); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1314 |
if (s != nil) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1315 |
RETURN (s); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1316 |
} |
5718 | 1317 |
} |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1318 |
fail: ; |
5718 | 1319 |
%}. |
1320 |
self primitiveFailed |
|
1321 |
||
1322 |
"ShortFloat pi printfPrintString:'%%lg -> %lg'" |
|
1323 |
"ShortFloat pi printfPrintString:'%%lf -> %lf'" |
|
1324 |
"ShortFloat pi printfPrintString:'%%7.5lg -> %7.5lg'" |
|
1325 |
"ShortFloat pi printfPrintString:'%%G -> %G'" |
|
1326 |
"ShortFloat pi printfPrintString:'%%F -> %F'" |
|
1327 |
"ShortFloat pi printfPrintString:'%%7.5G -> %7.5G'" |
|
1328 |
"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
|
1329 |
! |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1330 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1331 |
storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1332 |
"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
|
1333 |
all valid digits are printed. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1334 |
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
|
1335 |
#storeOn: as basic print mechanism." |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1336 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1337 |
%{ /* NOCONTEXT */ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1338 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1339 |
char buffer[64]; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1340 |
REGISTER char *cp; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1341 |
OBJ s; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1342 |
int len; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1343 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1344 |
/* |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1345 |
* build a printf format string |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1346 |
*/ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1347 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1348 |
__BEGIN_PROTECT_REGISTERS__ |
16670 | 1349 |
len = snprintf(buffer, sizeof(buffer), "%.8g", (float)__shortFloatVal(self)); |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1350 |
__END_PROTECT_REGISTERS__ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1351 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1352 |
if (len >= 0 && len < sizeof(buffer)-3) { |
16670 | 1353 |
/* |
1354 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
|
1355 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
|
1356 |
*/ |
|
1357 |
for (cp = buffer; *cp; cp++) { |
|
1358 |
if ((*cp == '.') || (*cp == ',') || (*cp == 'E') || (*cp == 'e')) break; |
|
1359 |
} |
|
1360 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
|
1361 |
*cp++ = '.'; |
|
1362 |
*cp++ = '0'; |
|
1363 |
*cp = '\0'; |
|
1364 |
} |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1365 |
|
16670 | 1366 |
s = __MKSTRING(buffer); |
1367 |
if (s != nil) { |
|
1368 |
RETURN (s); |
|
1369 |
} |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1370 |
} |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1371 |
%}. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1372 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1373 |
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
|
1374 |
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
|
1375 |
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
|
1376 |
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
|
1377 |
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
|
1378 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1379 |
^ ObjectMemory allocationFailureSignal raise. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1380 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1381 |
" |
16670 | 1382 |
0.1 asShortFloat storeString |
1383 |
((Array new:10 withAll:0.1 asShortFloat) inject:0 into:[:v :sumSoFar| sumSoFar + v]) storeString |
|
1384 |
1.0 asShortFloat storeString |
|
1385 |
1.234 asShortFloat storeString |
|
1386 |
1e10 asShortFloat storeString |
|
1387 |
1.2e3 asShortFloat storeString |
|
1388 |
1.2e30 asShortFloat storeString |
|
1389 |
Float pi asShortFloat storeString |
|
1390 |
(1.0 uncheckedDivide:0) asShortFloat storeString |
|
1391 |
(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
|
1392 |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1393 |
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
|
1394 |
|
16670 | 1395 |
DecimalPointCharacterForPrinting := $,. |
1396 |
1.234 asShortFloat storeString. |
|
1397 |
1.0 asShortFloat storeString. |
|
1398 |
1e10 asShortFloat storeString. |
|
1399 |
1.2e3 asShortFloat storeString. |
|
1400 |
1.2e30 asShortFloat storeString. |
|
1401 |
(1.0 uncheckedDivide:0) asShortFloat storeString. |
|
1402 |
(0.0 uncheckedDivide:0) asShortFloat storeString. |
|
1403 |
DecimalPointCharacterForPrinting := $. |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1404 |
" |
1197 | 1405 |
! ! |
1406 |
||
12628 | 1407 |
!ShortFloat methodsFor:'private accessing'! |
1408 |
||
1409 |
basicAt:index |
|
1410 |
"return an internal byte of the float. |
|
1411 |
The value returned here depends on byte order, float representation etc. |
|
1412 |
Therefore, this method should be used strictly private. |
|
1413 |
||
1414 |
Notice: |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1415 |
the need to redefine this method here is due to the |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1416 |
inability of many machines to store floats in non-double aligned memory. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1417 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1418 |
and the actual float is stored at index 5 .. 12. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1419 |
To hide this at one place, this method knows about that, and returns |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1420 |
values as if this filler wasnt present." |
12628 | 1421 |
|
1422 |
%{ /* NOCONTEXT */ |
|
1423 |
||
1424 |
register int indx; |
|
1425 |
unsigned char *cp; |
|
1426 |
||
1427 |
/* |
|
1428 |
* notice the missing test for self being a nonNilObject - |
|
1429 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
1430 |
* and SmallInteger |
|
1431 |
*/ |
|
1432 |
if (__isSmallInteger(index)) { |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1433 |
indx = __intVal(index) - 1; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1434 |
if (((unsigned)(indx)) < sizeof(float)) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1435 |
cp = (unsigned char *)(& (__ShortFloatInstPtr(self)->f_floatvalue)); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1436 |
RETURN ( __mkSmallInteger(cp[indx] & 0xFF) ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1437 |
} |
12628 | 1438 |
} |
1439 |
%}. |
|
1440 |
^ self indexNotIntegerOrOutOfBounds:index |
|
1441 |
! |
|
1442 |
||
1443 |
basicAt:index put:value |
|
1444 |
"set an internal byte of the float. |
|
1445 |
The value to be stored here depends on byte order, float representation etc. |
|
1446 |
Therefore, this method should be used strictly private. |
|
1447 |
||
1448 |
Notice: |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1449 |
the need to redefine this method here is due to the |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1450 |
inability of many machines to store floats in non-double aligned memory. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1451 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1452 |
and the actual float is stored at index 5 .. 12. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1453 |
To hide this at one place, this method knows about that, and returns |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1454 |
values as if this filler wasnt present." |
12628 | 1455 |
|
1456 |
%{ /* NOCONTEXT */ |
|
1457 |
register int indx, val; |
|
1458 |
unsigned char *cp; |
|
1459 |
||
1460 |
/* |
|
1461 |
* notice the missing test for self being a nonNilObject - |
|
1462 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
1463 |
* and SmallInteger |
|
1464 |
*/ |
|
1465 |
if (__bothSmallInteger(index, value)) { |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1466 |
val = __intVal(value); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1467 |
if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1468 |
indx = __intVal(index) - 1; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1469 |
if (((unsigned)(indx)) < sizeof(float)) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1470 |
cp = (unsigned char *)(& (__ShortFloatInstPtr(self)->f_floatvalue)); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1471 |
cp[indx] = val; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1472 |
RETURN ( value ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1473 |
} |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1474 |
} |
12628 | 1475 |
} |
1476 |
%}. |
|
1477 |
value isInteger ifFalse:[ |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1478 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1479 |
the object to store should be an integer number |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1480 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1481 |
^ self elementNotInteger |
12628 | 1482 |
]. |
1483 |
(value between:0 and:255) ifFalse:[ |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1484 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1485 |
the object to store must be a bytes value |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1486 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1487 |
^ self elementBoundsError:value |
12628 | 1488 |
]. |
1489 |
^ self indexNotIntegerOrOutOfBounds:index |
|
1490 |
! ! |
|
1491 |
||
16670 | 1492 |
!ShortFloat methodsFor:'queries'! |
1493 |
||
1494 |
nextFloat:count |
|
1495 |
"answer the next float count places after (or before if count is negative) myself" |
|
1496 |
||
1497 |
%{ |
|
1498 |
union u { |
|
1499 |
float d; |
|
1500 |
INT32 i; |
|
1501 |
} this; |
|
1502 |
||
1503 |
if (__isSmallInteger(count)) { |
|
1504 |
this.d = __shortFloatVal(self); |
|
1505 |
if (isfinite(this.d)) |
|
1506 |
this.i += __intVal(count); |
|
1507 |
||
1508 |
RETURN(__MKSFLOAT(this.d)); |
|
1509 |
} |
|
1510 |
%}. |
|
1511 |
self primitiveFailed:#badArgument |
|
1512 |
||
1513 |
" |
|
1514 |
(1.0 asShortFloat nextFloat:2) storeString |
|
1515 |
(67329.234 asShortFloat nextFloat:1) storeString |
|
1516 |
ShortFloat NaN nextFloat:100000 |
|
1517 |
ShortFloat infinity nextFloat:100000 |
|
1518 |
" |
|
1519 |
! ! |
|
1520 |
||
3897 | 1521 |
!ShortFloat methodsFor:'special access'! |
1522 |
||
1523 |
exponent |
|
1524 |
"extract a normalized floats exponent. |
|
1525 |
The returned value depends on the float-representation of |
|
1526 |
the underlying machine and is therefore highly unportable. |
|
1527 |
This is not for general use. |
|
1528 |
This assumes that the mantissa is normalized to |
|
1529 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
1530 |
||
1531 |
%{ /* NOCONTEXT */ |
|
8913 | 1532 |
int exp; |
3897 | 1533 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1534 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1535 |
frexpf( __shortFloatVal(self), &exp); |
1536 |
#else |
|
1537 |
frexp( (double)(__shortFloatVal(self)), &exp); |
|
1538 |
#endif |
|
14744 | 1539 |
RETURN (__mkSmallInteger(exp)); |
3897 | 1540 |
%}. |
1541 |
||
1542 |
" |
|
11945 | 1543 |
4.0 asShortFloat exponent |
1544 |
2.0 asShortFloat exponent |
|
1545 |
1.0 asShortFloat exponent |
|
1546 |
0.5 asShortFloat exponent |
|
1547 |
0.25 asShortFloat exponent |
|
1548 |
0.00000011111 asShortFloat exponent |
|
3897 | 1549 |
" |
1550 |
! |
|
1551 |
||
1552 |
mantissa |
|
1553 |
"extract a normalized floats mantissa. |
|
1554 |
The returned value depends on the float-representation of |
|
1555 |
the underlying machine and is therefore highly unportable. |
|
1556 |
This is not for general use. |
|
1557 |
This assumes that the mantissa is normalized to |
|
1558 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
1559 |
||
1560 |
%{ /* NOCONTEXT */ |
|
8913 | 1561 |
int exp; |
3897 | 1562 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1563 |
#if defined(__i386__) && defined(__GNUC__) |
14744 | 1564 |
float frac = frexpf(__shortFloatVal(self), &exp); |
1565 |
RETURN (__MKSFLOAT(frac)); |
|
7356 | 1566 |
#else |
14744 | 1567 |
double frac = frexp( (double)(__shortFloatVal(self)), &exp); |
1568 |
RETURN (__MKFLOAT(frac)); |
|
7356 | 1569 |
#endif |
3897 | 1570 |
%}. |
1571 |
||
1572 |
" |
|
11945 | 1573 |
1.0 asShortFloat exponent |
3897 | 1574 |
1.0 asShortFloat mantissa |
1575 |
||
11945 | 1576 |
0.5 asShortFloat exponent |
1577 |
0.5 asShortFloat mantissa |
|
3897 | 1578 |
|
11945 | 1579 |
0.25 asShortFloat exponent |
1580 |
0.25 asShortFloat mantissa |
|
3897 | 1581 |
|
11945 | 1582 |
0.00000011111 asShortFloat exponent |
1583 |
0.00000011111 asShortFloat mantissa |
|
3897 | 1584 |
" |
1585 |
! ! |
|
1586 |
||
1197 | 1587 |
!ShortFloat methodsFor:'testing'! |
1588 |
||
3147 | 1589 |
isFinite |
11945 | 1590 |
"return true, if the receiver is a finite float |
3147 | 1591 |
i.e. not NaN and not infinite." |
1592 |
||
1593 |
%{ /* NOCONTEXT */ |
|
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
1594 |
float fV = __shortFloatVal(self); |
3147 | 1595 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1596 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1597 |
* notice: on machines which do not provide |
11945 | 1598 |
* a isfinite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
1599 |
* 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
|
1600 |
*/ |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1601 |
if (isfinitef(fV)) { RETURN (true); } |
3147 | 1602 |
%}. |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
1603 |
^ false |
3147 | 1604 |
|
1605 |
" |
|
11945 | 1606 |
1.0 asShortFloat isFinite |
1607 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
1608 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
3147 | 1609 |
" |
1610 |
! |
|
1611 |
||
2387 | 1612 |
isNaN |
1613 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
1614 |
These are not created by ST/X float operations (they raise an exception); |
|
1615 |
however, inline C-code could produce them ..." |
|
1616 |
||
1617 |
%{ /* NOCONTEXT */ |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1618 |
float sV = __shortFloatVal(self); |
3139 | 1619 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1620 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1621 |
* notice: on machines which do not provide |
11945 | 1622 |
* a isnan() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
1623 |
* 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
|
1624 |
*/ |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1625 |
if (isnanf(sV)) { RETURN (true); } |
2392 | 1626 |
%}. |
1627 |
^ false |
|
2387 | 1628 |
|
3147 | 1629 |
" |
7356 | 1630 |
1.0 asShortFloat isNaN |
11945 | 1631 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
1632 |
(0.0 asShortFloat uncheckedDivide: 0.0 asShortFloat) isNaN |
|
3147 | 1633 |
" |
2387 | 1634 |
! |
1635 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1636 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1637 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1638 |
|
17396 | 1639 |
%{ /* NOCONTEXT */ |
17394 | 1640 |
#if defined(__BORLANDC__) |
1641 |
union { float f; int i; } __u; |
|
1642 |
__u.f = __shortFloatVal(self); |
|
1643 |
RETURN ( (__u.f == 0.0 && __u.i < 0) ? true : false ); |
|
17391 | 1644 |
#else |
17382 | 1645 |
RETURN ( (__shortFloatVal(self) == 0.0 && signbit(__shortFloatVal(self)) != 0) ? true : false ); |
17391 | 1646 |
#endif |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1647 |
%}. |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1648 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1649 |
" |
11945 | 1650 |
0.0 asShortFloat isNegativeZero |
1651 |
-0.0 asShortFloat isNegativeZero |
|
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1652 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1653 |
! |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1654 |
|
1197 | 1655 |
negative |
17382 | 1656 |
"return true if the receiver is less than zero. |
1657 |
-0.0 is positive for now." |
|
1197 | 1658 |
|
1659 |
%{ /* NOCONTEXT */ |
|
1660 |
||
17382 | 1661 |
RETURN ( __shortFloatVal(self) < 0.0 ? true : false ); |
1662 |
// RETURN ( signbit(__shortFloatVal(self)) != 0 ? true : false ); |
|
5411 | 1663 |
%}. |
17382 | 1664 |
|
1665 |
" |
|
1666 |
0.0 asShortFloat negative |
|
1667 |
-0.0 asShortFloat negative |
|
1668 |
1.0 asShortFloat negative |
|
1669 |
-1.0 asShortFloat negative |
|
1670 |
(1.0 uncheckedDivide: 0.0) asShortFloat negative |
|
1671 |
(-1.0 uncheckedDivide: 0.0) asShortFloat negative |
|
1672 |
" |
|
1197 | 1673 |
! |
1674 |
||
5357 | 1675 |
numberOfBits |
1676 |
"return the size (in bits) of the real; |
|
1677 |
typically, 32 is returned here, |
|
1678 |
but who knows ..." |
|
1679 |
||
1680 |
%{ /* NOCONTEXT */ |
|
1681 |
||
8913 | 1682 |
RETURN (__mkSmallInteger (sizeof(float) * 8)); |
5357 | 1683 |
%} |
1684 |
||
1685 |
" |
|
11945 | 1686 |
1.2 numberOfBits |
1687 |
1.2 asShortFloat numberOfBits |
|
5357 | 1688 |
" |
1689 |
||
1690 |
! |
|
1691 |
||
1197 | 1692 |
positive |
17382 | 1693 |
"return true if the receiver is greater or equal to zero |
1694 |
0.0 and -0.0 are positive for now." |
|
1197 | 1695 |
|
1696 |
%{ /* NOCONTEXT */ |
|
1697 |
||
17382 | 1698 |
RETURN ( __shortFloatVal(self) >= 0.0 ? true : false ); |
1699 |
// RETURN ( (signbit(__shortFloatVal(self)) == 0 ? true : false ) ); |
|
1700 |
%}. |
|
1701 |
||
1702 |
" |
|
1703 |
0.0 asShortFloat positive |
|
1704 |
-0.0 asShortFloat positive |
|
1705 |
1.0 asShortFloat positive |
|
1706 |
-1.0 asShortFloat positive |
|
1707 |
(1.0 uncheckedDivide: 0.0) asShortFloat positive |
|
1708 |
(-1.0 uncheckedDivide: 0.0) asShortFloat positive |
|
1709 |
" |
|
1880 | 1710 |
! |
1711 |
||
1712 |
strictlyPositive |
|
1713 |
"return true if the receiver is greater than zero" |
|
1714 |
||
1715 |
%{ /* NOCONTEXT */ |
|
1716 |
||
1717 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
1718 |
%} |
|
1197 | 1719 |
! ! |
1720 |
||
5552 | 1721 |
!ShortFloat methodsFor:'truncation & rounding'! |
3897 | 1722 |
|
7356 | 1723 |
ceiling |
1724 |
"return the smallest integer which is greater or equal to the receiver." |
|
1725 |
||
1726 |
|val| |
|
1727 |
||
1728 |
%{ |
|
1729 |
float fVal; |
|
1730 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1731 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1732 |
fVal = ceilf(__shortFloatVal(self)); |
1733 |
#else |
|
1734 |
fVal = (float)(ceil((double)__shortFloatVal(self))); |
|
1735 |
#endif |
|
1736 |
/* |
|
1737 |
* ST-80 (and X3J20) returns integer. |
|
1738 |
*/ |
|
1739 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
11945 | 1740 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1741 |
} |
1742 |
__qMKSFLOAT(val, fVal); |
|
1743 |
%}. |
|
1744 |
^ val asInteger |
|
1745 |
||
1746 |
" |
|
11945 | 1747 |
0.5 asShortFloat ceiling |
1748 |
-0.5 asShortFloat ceiling |
|
7356 | 1749 |
" |
1750 |
! |
|
1751 |
||
1752 |
ceilingAsFloat |
|
1753 |
"return the smallest integer-valued float greater or equal to the receiver. |
|
11945 | 1754 |
This is much like #ceiling, but avoids a (possibly expensive) conversion |
7356 | 1755 |
of the result to an integer. |
1756 |
It may be useful, if the result is to be further used in another float-operation." |
|
1757 |
||
1758 |
%{ /* NOCONTEXT */ |
|
1759 |
float fVal; |
|
1760 |
OBJ v; |
|
1761 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1762 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1763 |
fVal = ceilf(__shortFloatVal(self)); |
1764 |
#else |
|
1765 |
fVal = (float) ceil((double)__shortFloatVal(self)); |
|
1766 |
#endif |
|
1767 |
__qMKSFLOAT(v, fVal); |
|
1768 |
RETURN (v); |
|
1769 |
%} |
|
1770 |
" |
|
11945 | 1771 |
0.5 asShortFloat ceilingAsFloat |
1772 |
-0.5 asShortFloat ceilingAsFloat |
|
1773 |
-1.5 asShortFloat ceilingAsFloat |
|
7356 | 1774 |
" |
1775 |
! |
|
1776 |
||
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1777 |
floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1778 |
"return the integer nearest the receiver towards negative infinity." |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1779 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1780 |
|val| |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1781 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1782 |
%{ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1783 |
float fVal; |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1784 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1785 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1786 |
fVal = floorf(__shortFloatVal(self)); |
1787 |
#else |
|
1788 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
1789 |
#endif |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1790 |
/* |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1791 |
* ST-80 (and X3J20) returns integer. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1792 |
*/ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1793 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
11945 | 1794 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1795 |
} |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1796 |
__qMKSFLOAT(val, fVal); |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1797 |
%}. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1798 |
^ val asInteger |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1799 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1800 |
" |
11945 | 1801 |
0.5 asShortFloat floor |
1802 |
-0.5 asShortFloat floor |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1803 |
" |
7356 | 1804 |
! |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1805 |
|
7356 | 1806 |
floorAsFloat |
1807 |
"return the float which represents the next lower |
|
1808 |
integer nearest the receiver towards negative infinity. |
|
1809 |
Much like floor, but returns a float result - useful if the result |
|
1810 |
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
|
1811 |
|
7356 | 1812 |
%{ /* NOCONTEXT */ |
1813 |
float fVal; |
|
1814 |
OBJ v; |
|
1815 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1816 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1817 |
fVal = floorf(__shortFloatVal(self)); |
1818 |
#else |
|
1819 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
1820 |
#endif |
|
1821 |
__qMKSFLOAT(v, fVal); |
|
1822 |
RETURN ( v ); |
|
1823 |
%} |
|
1824 |
||
1825 |
" |
|
11945 | 1826 |
0.5 asShortFloat floorAsFloat |
1827 |
-0.5 asShortFloat floorAsFloat |
|
7356 | 1828 |
" |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1829 |
! |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1830 |
|
3897 | 1831 |
fractionPart |
1832 |
"extract the after-decimal fraction part. |
|
7356 | 1833 |
such that (self truncated + self fractionPart) = self" |
3897 | 1834 |
|
1835 |
%{ /* NOCONTEXT */ |
|
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1836 |
#ifdef NO_MODFF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1837 |
double dFrac, dTrunc; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1838 |
#else |
14741 | 1839 |
# ifndef __VISUALC__ |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1840 |
float modff(float, float *); |
14741 | 1841 |
# endif |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1842 |
#endif |
14718 | 1843 |
float frac, trunc; |
3897 | 1844 |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1845 |
__threadErrno = 0; |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1846 |
#ifdef NO_MODFF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1847 |
dFrac = modf((double)__shortFloatVal(self), &dTrunc) ; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1848 |
frac = (float)dFrac; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1849 |
#else |
14718 | 1850 |
frac = modff(__shortFloatVal(self), &trunc); |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1851 |
#endif |
11945 | 1852 |
if (! isnan(frac)) { |
14719 | 1853 |
if (__threadErrno == 0) { |
1854 |
RETURN (__MKSFLOAT(frac)); |
|
1855 |
} |
|
3897 | 1856 |
} |
1857 |
%}. |
|
7403 | 1858 |
^ self class |
14719 | 1859 |
raise:#domainErrorSignal |
1860 |
receiver:self |
|
1861 |
selector:#fractionPart |
|
1862 |
arguments:#() |
|
1863 |
errorString:'bad receiver in fractionPart' |
|
3897 | 1864 |
|
1865 |
" |
|
11945 | 1866 |
1.6 asShortFloat fractionPart + 1.6 asShortFloat truncated |
1867 |
-1.6 asShortFloat fractionPart + -1.6 asShortFloat truncated |
|
7356 | 1868 |
|
14718 | 1869 |
1.0 asShortFloat fractionPart |
1870 |
0.5 asShortFloat fractionPart |
|
1871 |
0.25 asShortFloat fractionPart |
|
1872 |
3.14159 asShortFloat fractionPart |
|
1873 |
12345673.14159 asShortFloat fractionPart |
|
1874 |
123456731231231231.14159 asShortFloat fractionPart |
|
3897 | 1875 |
" |
7356 | 1876 |
! |
3897 | 1877 |
|
7356 | 1878 |
rounded |
1879 |
"return the receiver rounded to the nearest integer" |
|
1880 |
||
1881 |
|val| |
|
1882 |
||
11945 | 1883 |
%{ |
7356 | 1884 |
float fVal; |
1885 |
||
1886 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1887 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1888 |
if (fVal < 0.0) { |
11945 | 1889 |
fVal = ceilf(fVal - (float)0.5); |
7356 | 1890 |
} else { |
11945 | 1891 |
fVal = floorf(fVal + (float)0.5); |
7356 | 1892 |
} |
1893 |
#else |
|
1894 |
if (fVal < 0.0) { |
|
11945 | 1895 |
fVal = (float)ceil((double)fVal - 0.5); |
7356 | 1896 |
} else { |
11945 | 1897 |
fVal = (float)floor((double)fVal + 0.5); |
7356 | 1898 |
} |
1899 |
#endif |
|
1900 |
/* |
|
1901 |
* ST-80 (and X3J20) returns integer. |
|
1902 |
*/ |
|
1903 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
11945 | 1904 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1905 |
} |
1906 |
__qMKSFLOAT(val, fVal); |
|
1907 |
%}. |
|
1908 |
^ val asInteger |
|
1909 |
||
1910 |
" |
|
11945 | 1911 |
0.4 asShortFloat rounded |
1912 |
0.5 asShortFloat rounded |
|
1913 |
0.6 asShortFloat rounded |
|
1914 |
-0.4 asShortFloat rounded |
|
1915 |
-0.5 asShortFloat rounded |
|
1916 |
-0.6 asShortFloat rounded |
|
7356 | 1917 |
" |
1918 |
! |
|
1919 |
||
1920 |
roundedAsFloat |
|
1921 |
"return the receiver rounded to the nearest integer as a float. |
|
11945 | 1922 |
This is much like #rounded, but avoids a (possibly expensive) conversion |
7356 | 1923 |
of the result to an integer. |
1924 |
It may be useful, if the result is to be further used in another float-operation." |
|
1925 |
||
1926 |
%{ /* NOCONTEXT */ |
|
1927 |
float fVal; |
|
1928 |
OBJ v; |
|
1929 |
||
1930 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1931 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1932 |
if (fVal < 0.0) { |
11945 | 1933 |
fVal = ceilf(fVal - (float)0.5); |
7356 | 1934 |
} else { |
11945 | 1935 |
fVal = floorf(fVal + (float)0.5); |
7356 | 1936 |
} |
1937 |
#else |
|
1938 |
if (fVal < 0.0) { |
|
11945 | 1939 |
fVal = (float)ceil((double)fVal - 0.5); |
7356 | 1940 |
} else { |
11945 | 1941 |
fVal = (float)floor((double)fVal + 0.5); |
7356 | 1942 |
} |
1943 |
#endif |
|
1944 |
__qMKSFLOAT(v, fVal); |
|
1945 |
RETURN (v); |
|
1946 |
%} |
|
1947 |
||
1948 |
" |
|
11945 | 1949 |
0.4 asShortFloat rounded |
1950 |
0.5 asShortFloat rounded |
|
1951 |
0.6 asShortFloat rounded |
|
1952 |
-0.4 asShortFloat rounded |
|
1953 |
-0.5 asShortFloat rounded |
|
1954 |
-0.6 asShortFloat rounded |
|
7356 | 1955 |
|
11945 | 1956 |
0.4 asShortFloat roundedAsFloat |
1957 |
0.5 asShortFloat roundedAsFloat |
|
1958 |
0.6 asShortFloat roundedAsFloat |
|
1959 |
-0.4 asShortFloat roundedAsFloat |
|
1960 |
-0.5 asShortFloat roundedAsFloat |
|
1961 |
-0.6 asShortFloat roundedAsFloat |
|
7356 | 1962 |
" |
1963 |
! |
|
1964 |
||
1965 |
truncated |
|
1966 |
"return the receiver truncated towards zero as an integer" |
|
1967 |
||
1968 |
|val| |
|
1969 |
||
11945 | 1970 |
%{ |
7356 | 1971 |
float fVal; |
1972 |
||
1973 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1974 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1975 |
if (fVal < 0.0) { |
11945 | 1976 |
fVal = ceilf(fVal); |
7356 | 1977 |
} else { |
11945 | 1978 |
fVal = floorf(fVal); |
7356 | 1979 |
} |
1980 |
#else |
|
1981 |
if (fVal < 0.0) { |
|
11945 | 1982 |
fVal = (float)ceil((double)fVal); |
7356 | 1983 |
} else { |
11945 | 1984 |
fVal = (float)floor((double)fVal); |
7356 | 1985 |
} |
1986 |
#endif |
|
1987 |
/* |
|
1988 |
* ST-80 (and X3J20) returns integer. |
|
1989 |
*/ |
|
1990 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
11945 | 1991 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1992 |
} |
1993 |
__qMKSFLOAT(val, fVal); |
|
1994 |
%}. |
|
1995 |
^ val asInteger |
|
1996 |
||
1997 |
" |
|
11945 | 1998 |
0.5 asShortFloat truncated |
1999 |
-0.5 asShortFloat truncated |
|
2000 |
0.5 asShortFloat truncatedAsFloat |
|
2001 |
-0.5 asShortFloat truncatedAsFloat |
|
7356 | 2002 |
" |
2003 |
! |
|
2004 |
||
2005 |
truncatedAsFloat |
|
2006 |
"return the receiver truncated towards zero as a float. |
|
11945 | 2007 |
This is much like #truncated, but avoids a (possibly expensive) conversion |
7356 | 2008 |
of the result to an integer. |
11945 | 2009 |
It may be useful, if the result is to be further used in another |
7356 | 2010 |
float-operation." |
2011 |
||
2012 |
%{ /* NOCONTEXT */ |
|
2013 |
float fVal; |
|
2014 |
OBJ v; |
|
2015 |
||
2016 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
2017 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 2018 |
if (fVal < 0.0) { |
11945 | 2019 |
fVal = ceilf(fVal); |
7356 | 2020 |
} else { |
11945 | 2021 |
fVal = floorf(fVal); |
7356 | 2022 |
} |
2023 |
#else |
|
2024 |
if (fVal < 0.0) { |
|
11945 | 2025 |
fVal = (float)ceil((double)fVal); |
7356 | 2026 |
} else { |
11945 | 2027 |
fVal = (float)floor((double)fVal); |
7356 | 2028 |
} |
2029 |
#endif |
|
2030 |
__qMKSFLOAT(v, fVal); |
|
2031 |
RETURN (v); |
|
2032 |
%} |
|
2033 |
||
2034 |
" |
|
11945 | 2035 |
0.5 asShortFloat truncated |
2036 |
-0.5 asShortFloat truncated |
|
2037 |
0.5 asShortFloat truncatedAsFloat |
|
2038 |
-0.5 asShortFloat truncatedAsFloat |
|
7356 | 2039 |
" |
3897 | 2040 |
! ! |
2041 |
||
1880 | 2042 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 2043 |
|
2044 |
version |
|
17654 | 2045 |
^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.137 2015-03-25 15:41:55 cg Exp $' |
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
2046 |
! |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
2047 |
|
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
2048 |
version_CVS |
17654 | 2049 |
^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.137 2015-03-25 15:41:55 cg Exp $' |
1197 | 2050 |
! ! |
15012 | 2051 |
|
16670 | 2052 |
|
2053 |
ShortFloat initialize! |