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