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