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