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