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