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