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