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