author | Claus Gittinger <cg@exept.de> |
Sun, 01 Dec 2019 05:18:11 +0100 | |
changeset 25008 | 89196028f729 |
parent 25002 | 34d9021dd872 |
child 25021 | 33a9aa9a5efe |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
24969 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5411 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
17384 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
54 | 16 |
LimitedPrecisionReal variableByteSubclass:#Float |
24969 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'DefaultPrintFormat DefaultPrintfFormat E Epsilon Halfpi |
|
19 |
HalfpiNegative Ln10 Ln2 MaxSmallInteger NaN NegativeInfinity Pi |
|
20 |
PositiveInfinity RadiansPerDegree Sqrt2 Twopi' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Magnitude-Numbers' |
|
1 | 23 |
! |
24 |
||
701 | 25 |
!Float primitiveDefinitions! |
26 |
%{ |
|
14751 | 27 |
#include <stdio.h> |
701 | 28 |
#include <errno.h> |
29 |
||
3127
590c7d2bb4b9
fixes isNAN (on linux); added #isInfinite
Claus Gittinger <cg@exept.de>
parents:
3118
diff
changeset
|
30 |
#ifndef __OPTIMIZE__ |
590c7d2bb4b9
fixes isNAN (on linux); added #isInfinite
Claus Gittinger <cg@exept.de>
parents:
3118
diff
changeset
|
31 |
# define __OPTIMIZE__ |
590c7d2bb4b9
fixes isNAN (on linux); added #isInfinite
Claus Gittinger <cg@exept.de>
parents:
3118
diff
changeset
|
32 |
#endif |
4825 | 33 |
|
34 |
#define __USE_ISOC9X 1 |
|
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
35 |
#define __USE_ISOC99 1 |
3127
590c7d2bb4b9
fixes isNAN (on linux); added #isInfinite
Claus Gittinger <cg@exept.de>
parents:
3118
diff
changeset
|
36 |
#include <math.h> |
25002 | 37 |
#include <float.h> |
3127
590c7d2bb4b9
fixes isNAN (on linux); added #isInfinite
Claus Gittinger <cg@exept.de>
parents:
3118
diff
changeset
|
38 |
|
16669 | 39 |
#ifndef INT64 |
40 |
# ifdef HAS_LONGLONG |
|
41 |
# define INT64 long long int |
|
42 |
# else |
|
43 |
# define INT64 __int64 |
|
44 |
# endif |
|
45 |
#endif |
|
46 |
||
47 |
||
701 | 48 |
/* |
49 |
* on some systems errno is a macro ... check for it here |
|
50 |
*/ |
|
51 |
#ifndef errno |
|
52 |
extern errno; |
|
53 |
#endif |
|
54 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
55 |
#if !defined (__win32__) |
14672
8abd84f0951a
setlocale once only in stxmain
Claus Gittinger <cg@exept.de>
parents:
14630
diff
changeset
|
56 |
# include <locale.h> |
8abd84f0951a
setlocale once only in stxmain
Claus Gittinger <cg@exept.de>
parents:
14630
diff
changeset
|
57 |
#endif |
8abd84f0951a
setlocale once only in stxmain
Claus Gittinger <cg@exept.de>
parents:
14630
diff
changeset
|
58 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
59 |
#if defined (__aix__) |
2394 | 60 |
# include <float.h> |
61 |
#endif |
|
4825 | 62 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
63 |
#if defined(__irix__) |
2395 | 64 |
# include <nan.h> |
65 |
#endif |
|
4825 | 66 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
67 |
#if defined(__linux__) |
4825 | 68 |
# ifndef NAN |
5822
4e56f8aadda5
Get nan.h from bits/nan.h for Linux
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
69 |
# include <bits/nan.h> |
4825 | 70 |
# endif |
2396 | 71 |
#endif |
4825 | 72 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
73 |
#if defined(__solaris__) || defined(__sunos__) |
2398 | 74 |
# include <nan.h> |
75 |
#endif |
|
2394 | 76 |
|
3129 | 77 |
/* |
78 |
* sigh - some systems define that stuff; others dont. |
|
79 |
* (AIX even declares them as macros, so an external decl |
|
80 |
* will not work below ... |
|
81 |
*/ |
|
22578 | 82 |
#if !defined(__aix__) && !defined(__NEXT3__) |
3129 | 83 |
|
84 |
# ifdef acos |
|
85 |
double acos(); |
|
86 |
# endif |
|
87 |
# ifdef asin |
|
88 |
double asin(); |
|
89 |
# endif |
|
90 |
# ifndef atan |
|
91 |
double atan(); |
|
92 |
# endif |
|
93 |
# ifndef cos |
|
94 |
double cos(); |
|
95 |
# endif |
|
96 |
# ifndef sin |
|
97 |
double sin(); |
|
98 |
# endif |
|
99 |
# ifndef pow |
|
100 |
double pow(); |
|
101 |
# endif |
|
102 |
# ifndef log |
|
103 |
double log(); |
|
104 |
# endif |
|
105 |
# ifndef exp |
|
106 |
double exp(); |
|
107 |
# endif |
|
108 |
# ifndef sqrt |
|
109 |
double sqrt(); |
|
110 |
# endif |
|
17199 | 111 |
# ifndef cbrt |
17200
3f919ff62213
cbrt is not avail in win32's math library
Claus Gittinger <cg@exept.de>
parents:
17199
diff
changeset
|
112 |
# ifndef WIN32 |
17199 | 113 |
double cbrt(); |
17200
3f919ff62213
cbrt is not avail in win32's math library
Claus Gittinger <cg@exept.de>
parents:
17199
diff
changeset
|
114 |
# endif |
17199 | 115 |
# endif |
3129 | 116 |
# ifndef tan |
117 |
double tan(); |
|
118 |
# endif |
|
119 |
||
120 |
#endif /* not AIX */ |
|
121 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
122 |
#ifdef __win32__ |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
123 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
124 |
* no finite(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
125 |
* no isnan(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
126 |
*/ |
7420 | 127 |
# ifndef isnan |
128 |
# define isnan(x) \ |
|
24969 | 129 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
130 |
(((unsigned int *)(&x))[1] == 0xFFF80000)) |
|
7420 | 131 |
# endif |
132 |
||
133 |
# ifndef isPositiveInfinity |
|
134 |
# define isPositiveInfinity(x) \ |
|
24969 | 135 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
136 |
(((unsigned int *)(&x))[1] == 0x7FF00000)) |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
137 |
# endif |
7420 | 138 |
|
139 |
# ifndef isNegativeInfinity |
|
14931 | 140 |
# define isNegativeInfinity(x) \ |
24969 | 141 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
142 |
(((unsigned int *)(&x))[1] == 0xFFF00000)) |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
143 |
# endif |
7420 | 144 |
|
145 |
# ifndef isinf |
|
146 |
# define isinf(x) \ |
|
24969 | 147 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
148 |
((((unsigned int *)(&x))[1] & 0x7FF00000) == 0x7FF00000)) |
|
7420 | 149 |
# endif |
150 |
||
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
151 |
# ifndef isfinite |
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
152 |
# define isfinite(x) (!isinf(x) && !isnan(x)) |
7420 | 153 |
# endif |
154 |
||
7411 | 155 |
# define NO_ASINH |
156 |
# define NO_ACOSH |
|
157 |
# define NO_ATANH |
|
14931 | 158 |
|
159 |
# ifdef __MINGW__ |
|
22491 | 160 |
# ifndef _STRING_H_INCLUDED_ |
161 |
# include <string.h> |
|
162 |
# define _STRING_H_INCLUDED_ |
|
163 |
# endif |
|
14931 | 164 |
# endif |
165 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
166 |
#endif /* __win32__ */ |
8984 | 167 |
|
168 |
#ifdef solaris |
|
169 |
# ifndef isfinite |
|
170 |
# define isfinite(f) finite((double)(f)) |
|
171 |
# endif |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
172 |
#endif |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
173 |
|
3414 | 174 |
#ifdef realIX |
11945 | 175 |
# ifndef isfinite |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
176 |
# define isfinite(x) 1 |
3414 | 177 |
# endif |
178 |
#endif |
|
179 |
||
22578 | 180 |
#ifndef __NEXT3__ |
3241 | 181 |
# ifndef ceil |
182 |
double ceil(); |
|
183 |
double floor(); |
|
184 |
# endif |
|
185 |
#endif |
|
186 |
||
701 | 187 |
%} |
188 |
! ! |
|
189 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
190 |
!Float class methodsFor:'documentation'! |
68 | 191 |
|
88 | 192 |
copyright |
193 |
" |
|
194 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
24969 | 195 |
All Rights Reserved |
88 | 196 |
|
197 |
This software is furnished under a license and may be used |
|
198 |
only in accordance with the terms of that license and with the |
|
199 |
inclusion of the above copyright notice. This software may not |
|
200 |
be provided or otherwise made available to, or used by, any |
|
201 |
other person. No title to or ownership of the software is |
|
202 |
hereby transferred. |
|
203 |
" |
|
204 |
! |
|
205 |
||
68 | 206 |
documentation |
207 |
" |
|
15016 | 208 |
Floats represent rational numbers with limited precision. |
25002 | 209 |
They use the C-compiler's 'double' format, |
24461 | 210 |
which is usually the 8byte IEEE double float format. |
7380 | 211 |
|
212 |
Floats give you 64 bit floats. |
|
15016 | 213 |
In contrast to ShortFloats (32bit) and LongFloats (>=64bit). |
7380 | 214 |
|
215 |
WARNING: |
|
25002 | 216 |
The layout of Float instances is known by the runtime system and the compiler; |
217 |
you may not add instance variables here. |
|
218 |
Also, subclassing is complicated by the fact, that the VM creates floats/shortFloats, |
|
219 |
and does some float-checks by an identity compare with the Float-class. |
|
220 |
(i.e. your subclasses instances may not be recognized as float-like objects, |
|
221 |
thus mixed mode arithmetic will always coerce them, effectively slowing things down). |
|
1 | 222 |
|
68 | 223 |
Notice, that Floats are defined as Byte-array to prevent the garbage collector |
224 |
from going into the value ... otherwise I needed a special case in many places. |
|
225 |
||
16632 | 226 |
Also notice, that ST/X Floats are what Doubles are in ST-80. The reason for doing this |
227 |
was to be compatible to both Digitalk, Squeak AND ParcPlace smalltalk implementations |
|
6674 | 228 |
(ParcPlace uses a 4-byte Float and an 8-byte Double class, in contrast to |
15016 | 229 |
Digitalk and Squeak, which have an 8-byte Float class). |
11945 | 230 |
Thus, by providing an 8-byte Float class, code would not loose precicion |
7380 | 231 |
(although some memory is wasted when porting from VW). |
6674 | 232 |
Notice that ST/X provides an alias called Double, and an extra ShortFloat class, which has 4-byte |
233 |
instances. |
|
68 | 234 |
|
1892 | 235 |
Mixed mode arithmetic: |
25002 | 236 |
float op float -> float |
237 |
float op fix -> float |
|
238 |
float op fraction -> float |
|
239 |
float op integer -> float |
|
240 |
float op shortFloat -> float |
|
241 |
float op longFloat -> longFloat |
|
242 |
float op complex -> complex |
|
1892 | 243 |
|
7405 | 244 |
Representation: |
25002 | 245 |
64bit double precision IEEE floats |
246 |
52 bit mantissa + 1 hidden bit providing 53 bits of precision, |
|
247 |
11 bit exponent, |
|
248 |
15 decimal digits (approx.) |
|
7405 | 249 |
|
250 |
Range and Precision of Storage Formats: see LimitedPrecisionReal >> documentation |
|
85 | 251 |
|
7380 | 252 |
[author:] |
25002 | 253 |
Claus Gittinger |
7380 | 254 |
|
1295 | 255 |
[see also:] |
25002 | 256 |
Number |
257 |
ShortFloat LongFloat Fraction FixedPoint Integer Complex |
|
258 |
FloatArray DoubleArray |
|
68 | 259 |
" |
2215 | 260 |
! |
261 |
||
262 |
errorHandling |
|
263 |
" |
|
264 |
Floating point error handling and signalling depends on the systems |
|
265 |
(actually: the C-runtime systems) ability to handle floating point errors. |
|
266 |
Most systems report errors by raising an OS floatingPoint exception, |
|
267 |
which is mapped to a fpExceptionInterrupt in ST/X. |
|
268 |
However, some systems do return NaN as result. |
|
269 |
||
11945 | 270 |
Currently, ST/X does not care specially for these systems - it maybe added |
2215 | 271 |
easily, if there is sufficient customer interest, though. |
272 |
||
273 |
Try: |
|
24969 | 274 |
|f1 f2| |
275 |
||
276 |
f1 := 1.0. |
|
277 |
f2 := 0.0. |
|
278 |
f1 / f2 |
|
2215 | 279 |
|
280 |
or: |
|
24969 | 281 |
2 arcSin |
2215 | 282 |
" |
68 | 283 |
! ! |
1 | 284 |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
285 |
!Float class methodsFor:'instance creation'! |
1 | 286 |
|
287 |
basicNew |
|
288 |
"return a new float - here we return 0.0 |
|
289 |
- floats are usually NOT created this way ... |
|
290 |
Its implemented here to allow things like binary store & load |
|
291 |
of floats. (but even this support will go away eventually, its not |
|
292 |
a good idea to store the bits of a float - the reader might have a |
|
11945 | 293 |
totally different representation - so floats will eventually be |
13 | 294 |
binary stored in a device independent format." |
1 | 295 |
|
296 |
%{ /* NOCONTEXT */ |
|
18296 | 297 |
#ifdef __SCHTEAM__ |
298 |
ERROR("trying to instantiate a float"); |
|
299 |
#else |
|
1206
9d23c1f5e4d3
use quicker MKFLOAT in FLoat>>basicNew
Claus Gittinger <cg@exept.de>
parents:
1198
diff
changeset
|
300 |
OBJ newFloat; |
9d23c1f5e4d3
use quicker MKFLOAT in FLoat>>basicNew
Claus Gittinger <cg@exept.de>
parents:
1198
diff
changeset
|
301 |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
302 |
__qMKFLOAT(newFloat, 0.0); |
1206
9d23c1f5e4d3
use quicker MKFLOAT in FLoat>>basicNew
Claus Gittinger <cg@exept.de>
parents:
1198
diff
changeset
|
303 |
RETURN (newFloat); |
18296 | 304 |
#endif /* not SCHTEAM */ |
1 | 305 |
%} |
3163
212a539ee379
added #readFrom: to avoid returning an int.
Claus Gittinger <cg@exept.de>
parents:
3146
diff
changeset
|
306 |
! |
1206
9d23c1f5e4d3
use quicker MKFLOAT in FLoat>>basicNew
Claus Gittinger <cg@exept.de>
parents:
1198
diff
changeset
|
307 |
|
4295 | 308 |
fastFromString:aString at:startIndex |
11945 | 309 |
"return the next Float from the string starting at startIndex. |
4295 | 310 |
No spaces are skipped. |
23477 | 311 |
Raises an exception, if the startIndex is not valid. |
23943 | 312 |
Returns garbage if the argument string is not a valid double float number. |
23477 | 313 |
|
314 |
This is a specially tuned entry (using a low-level C-call to atof). |
|
24249
17326e1df17f
raise exception on out-of-range in asShortFloat / asFloat
Claus Gittinger <cg@exept.de>
parents:
24230
diff
changeset
|
315 |
It has been added to allow high speed string decomposition |
23477 | 316 |
into numbers, especially for mass-data (reading millions of floats)." |
11945 | 317 |
|
318 |
%{ /* NOCONTEXT */ |
|
23477 | 319 |
int idx = __intVal(startIndex) - 1; |
320 |
||
12478
e8051030cda6
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12423
diff
changeset
|
321 |
if (__isStringLike(aString) && __isSmallInteger(startIndex)) { |
25002 | 322 |
char *cp = (char *)(__stringVal(aString)); |
323 |
double val; |
|
324 |
OBJ newFloat; |
|
325 |
||
326 |
if ((unsigned)idx < __stringSize(aString)) { |
|
24974 | 327 |
#ifndef NO_STRTOD |
25002 | 328 |
double strtod(const char *, char**); |
329 |
val = strtod(cp+idx, NULL); |
|
24974 | 330 |
#else |
25002 | 331 |
double atof(); |
332 |
val = atof(cp + idx); |
|
24974 | 333 |
#endif |
25002 | 334 |
__qMKFLOAT(newFloat, val); |
335 |
RETURN (newFloat); |
|
336 |
} |
|
4295 | 337 |
} |
338 |
%}. |
|
339 |
self primitiveFailed. |
|
340 |
||
341 |
" |
|
11945 | 342 |
Float fastFromString:'123.45' at:1 |
343 |
Float fastFromString:'123.45' at:2 |
|
21870 | 344 |
Float fastFromString:'123.45' at:3 |
345 |
Float fastFromString:'123.45' at:4 |
|
346 |
Float fastFromString:'123.45' at:5 |
|
347 |
Float fastFromString:'123.45' at:6 |
|
348 |
||
11945 | 349 |
Float fastFromString:'123.45E4' at:1 |
350 |
Float fastFromString:'hello123.45E4' at:6 |
|
351 |
Float fastFromString:'12345' at:1 |
|
352 |
Float fastFromString:'12345' at:2 |
|
353 |
Float fastFromString:'12345' at:3 |
|
354 |
Float fastFromString:'12345' at:4 |
|
355 |
Float fastFromString:'12345' at:5 |
|
21870 | 356 |
|
357 |
Float fastFromString:'12345' at:6 -> error |
|
358 |
Float fastFromString:'12345' at:0 -> error |
|
359 |
||
360 |
Float fastFromString:'hello123.45E4' at:1 -> 0 |
|
4295 | 361 |
|
23477 | 362 |
Float fastFromString:'1.7976931348623157e+308' |
363 |
Float fastFromString:'Nan' |
|
364 |
Float fastFromString:'+Inf' |
|
365 |
Float fastFromString:'-Inf' |
|
366 |
" |
|
367 |
||
368 |
" |
|
4295 | 369 |
Time millisecondsToRun:[ |
25002 | 370 |
1000000 timesRepeat:[ |
371 |
Float readFrom:'123.45' |
|
372 |
] |
|
4295 | 373 |
] |
374 |
" |
|
375 |
||
376 |
" |
|
377 |
Time millisecondsToRun:[ |
|
25002 | 378 |
1000000 timesRepeat:[ |
379 |
Float fastFromString:'123.45' at:1 |
|
380 |
] |
|
4295 | 381 |
] |
382 |
" |
|
21870 | 383 |
|
384 |
"Modified (comment): / 21-06-2017 / 09:24:32 / cg" |
|
23943 | 385 |
"Modified (comment): / 20-03-2019 / 13:45:12 / Claus Gittinger" |
4295 | 386 |
! |
387 |
||
15016 | 388 |
fromIEEE32Bit: anInteger |
389 |
"creates a double, given the four native float bytes as an integer" |
|
390 |
||
391 |
%{ /* NOCONTEXT */ |
|
21870 | 392 |
#ifndef __SCHTEAM__ |
15016 | 393 |
REGISTER union { |
24969 | 394 |
unsigned int i; |
395 |
float f; |
|
15016 | 396 |
} r; |
397 |
||
398 |
r.i = __unsignedLongIntVal( anInteger ); |
|
15017 | 399 |
RETURN( __MKFLOAT((double)(r.f)) ); |
18296 | 400 |
#endif |
21870 | 401 |
%}. |
402 |
self primitiveFailed. |
|
17218 | 403 |
|
404 |
" |
|
21870 | 405 |
ShortFloat fromIEEE32Bit:((ShortFloat pi digitBytesMSB:true) asIntegerMSB:true) |
17218 | 406 |
" |
21870 | 407 |
|
408 |
"Modified (comment): / 21-06-2017 / 09:26:41 / cg" |
|
17218 | 409 |
! |
410 |
||
411 |
fromIEEE64Bit: anInteger |
|
21875 | 412 |
"creates a double, given the eight native double bytes as an integer" |
17218 | 413 |
|
414 |
%{ /* NOCONTEXT */ |
|
21870 | 415 |
#ifndef __SCHTEAM__ |
21876 | 416 |
extern int __unsignedLong64IntVal(OBJ o, __uint64__ *pI); |
17218 | 417 |
|
418 |
REGISTER union { |
|
24969 | 419 |
__uint64__ u64; |
420 |
double d; |
|
17218 | 421 |
} r; |
422 |
||
423 |
if (__unsignedLong64IntVal(anInteger, &r.u64)) { |
|
24969 | 424 |
RETURN( __MKFLOAT(r.d) ); |
17218 | 425 |
} |
18296 | 426 |
#endif |
17218 | 427 |
%}. |
428 |
self primitiveFailed. |
|
429 |
||
430 |
" |
|
24969 | 431 |
Float fromIEEE64Bit:((Float pi digitBytesMSB:true) asIntegerMSB:true) |
17218 | 432 |
" |
21870 | 433 |
|
21876 | 434 |
"Modified: / 21-06-2017 / 09:37:47 / cg" |
15016 | 435 |
! |
436 |
||
19358 | 437 |
fromInteger:anInteger |
438 |
"return a new float, given an integer value" |
|
439 |
||
440 |
%{ /* NOCONTEXT */ |
|
441 |
#ifdef __SCHTEAM__ |
|
442 |
if (anInteger.isSmallInteger()) { |
|
24969 | 443 |
return __c__._RETURN( STDouble._new( (double)(anInteger.longValue()) )); |
19358 | 444 |
} |
445 |
#else |
|
446 |
OBJ newFloat; |
|
447 |
||
448 |
if (__isSmallInteger(anInteger)) { |
|
24969 | 449 |
double f = (double)__smallIntegerVal(anInteger); |
450 |
__qMKFLOAT(newFloat, f); /* OBJECT ALLOCATION */ |
|
451 |
RETURN (newFloat); |
|
19358 | 452 |
} |
453 |
#endif /* not SCHTEAM */ |
|
454 |
%}. |
|
455 |
^ super fromInteger:anInteger |
|
456 |
||
457 |
" |
|
458 |
Float fromInteger:123 |
|
459 |
Float fromInteger:(100 factorial) |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19358
diff
changeset
|
460 |
(100 factorial) asFloat |
19358 | 461 |
" |
462 |
! |
|
463 |
||
17653 | 464 |
fromNumber:aNumber |
21870 | 465 |
"return aNumber coerced to Float" |
22578 | 466 |
|
21870 | 467 |
^ aNumber asFloat |
468 |
||
469 |
"Modified (comment): / 21-06-2017 / 08:55:15 / cg" |
|
17653 | 470 |
! |
471 |
||
3279 | 472 |
fromVAXFloatBytes:b1 b2:b2 b3:b3 b4:b4 |
21870 | 473 |
"creates a double, given the four vax F-format float bytes to an ieee double. |
3427
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
474 |
For NaNs and Infinity, nil is returned. |
21870 | 475 |
To get an idea of how big the floating-point zoo really is, |
476 |
see: http://www.quadibloc.com/comp/cp0201.htm |
|
3279 | 477 |
" |
478 |
||
479 |
%{ /* NOCONTEXT */ |
|
21870 | 480 |
#ifndef __SCHTEAM__ |
3279 | 481 |
REGISTER union { |
24969 | 482 |
unsigned char b[4]; |
483 |
unsigned int l; |
|
484 |
float f; |
|
3279 | 485 |
} r; |
486 |
||
18296 | 487 |
# ifdef __LSBFIRST__ |
3279 | 488 |
r.b[3] = __intVal( b2 ); |
489 |
r.b[2] = __intVal( b1 ); |
|
490 |
r.b[1] = __intVal( b4 ); |
|
491 |
r.b[0] = __intVal( b3 ); |
|
18296 | 492 |
# else |
3279 | 493 |
r.b[0] = __intVal( b2 ); |
494 |
r.b[1] = __intVal( b1 ); |
|
495 |
r.b[2] = __intVal( b4 ); |
|
496 |
r.b[3] = __intVal( b3 ); |
|
18296 | 497 |
# endif |
21870 | 498 |
if( (r.l & 0xff800000) != 0x80000000 ) { |
24969 | 499 |
if( (r.l & 0x7f800000) > 0x01000000 ) |
500 |
r.l -= 0x01000000; |
|
501 |
else |
|
502 |
r.l = 0; |
|
503 |
||
504 |
RETURN( __MKFLOAT(r.f) ); |
|
21870 | 505 |
} else { |
24969 | 506 |
// add code to handle NaN and Inf |
507 |
RETURN( nil ); |
|
3279 | 508 |
} |
18296 | 509 |
#endif |
3279 | 510 |
%}. |
21870 | 511 |
^ self primitiveFailed |
512 |
||
513 |
"Modified: / 21-06-2017 / 09:21:55 / cg" |
|
1 | 514 |
! ! |
515 |
||
12916
a32796fca6ed
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12723
diff
changeset
|
516 |
!Float class methodsFor:'accessing'! |
a32796fca6ed
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12723
diff
changeset
|
517 |
|
a32796fca6ed
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12723
diff
changeset
|
518 |
defaultPrintFormat |
24954 | 519 |
"/ by default, I will print 15 digits |
25002 | 520 |
"/ ShortFloat pi -> 3.141593 |
521 |
"/ Float pi -> 3.14159265358979 |
|
522 |
"/ LongFloat pi -> 3.141592653589793239 |
|
24954 | 523 |
|
12916
a32796fca6ed
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12723
diff
changeset
|
524 |
^ DefaultPrintFormat |
a32796fca6ed
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12723
diff
changeset
|
525 |
! |
a32796fca6ed
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12723
diff
changeset
|
526 |
|
a32796fca6ed
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12723
diff
changeset
|
527 |
defaultPrintFormat:something |
a32796fca6ed
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12723
diff
changeset
|
528 |
DefaultPrintFormat := something. |
16658 | 529 |
! |
530 |
||
21868 | 531 |
defaultPrintfFormat |
532 |
^ DefaultPrintfFormat |
|
533 |
! |
|
534 |
||
535 |
defaultPrintfFormat:something |
|
536 |
DefaultPrintfFormat := something. |
|
12916
a32796fca6ed
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12723
diff
changeset
|
537 |
! ! |
a32796fca6ed
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12723
diff
changeset
|
538 |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
539 |
!Float class methodsFor:'binary storage'! |
701 | 540 |
|
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
541 |
readBinaryIEEEDoubleFrom:aStream |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
542 |
"read a float from the binary stream, aStream, |
14925 | 543 |
interpreting the next bytes as an IEEE formatted 8-byte float. |
544 |
The bytes are read in the native byte order (i.e.lsb on intel)" |
|
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
545 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
546 |
|f| |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
547 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
548 |
f := self basicNew. |
15013 | 549 |
self readBinaryIEEEDoubleFrom:aStream into:f MSB:(UninterpretedBytes isBigEndian). |
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
550 |
^ f |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
551 |
|
9637 | 552 |
"not part of libboss, as this is also used by others (TIFFReader)" |
553 |
||
554 |
"Created: / 16-04-1996 / 20:59:59 / cg" |
|
555 |
"Modified: / 23-08-2006 / 16:00:42 / cg" |
|
701 | 556 |
! |
557 |
||
15013 | 558 |
readBinaryIEEEDoubleFrom:aStream MSB:msbFirst |
559 |
"read a float from the binary stream, aStream, |
|
560 |
interpreting the next bytes as an IEEE formatted 8-byte float. |
|
561 |
The bytes are read in the specified byte order" |
|
562 |
||
563 |
|f| |
|
564 |
||
565 |
f := self basicNew. |
|
566 |
self readBinaryIEEEDoubleFrom:aStream into:f MSB:msbFirst. |
|
567 |
^ f |
|
568 |
||
569 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
570 |
! |
|
571 |
||
19616 | 572 |
readBinaryIEEEDoubleFrom:aStream into:aBasicNewFloat |
14923 | 573 |
"read the receiver's value from the binary stream, aStream, |
574 |
interpreting the next bytes as an IEEE formatted 8-byte float. |
|
14925 | 575 |
The bytes are read in the native byte order (i.e.lsb on intel)" |
14923 | 576 |
|
19616 | 577 |
^ self readBinaryIEEEDoubleFrom:aStream into:aBasicNewFloat MSB:(UninterpretedBytes isBigEndian) |
14923 | 578 |
! |
579 |
||
580 |
readBinaryIEEEDoubleFrom:aStream into:aFloat MSB:msb |
|
581 |
"read the receiver's value from the binary stream, aStream, |
|
582 |
interpreting the next bytes as an IEEE formatted 8-byte float. |
|
583 |
If msb is true, the stream bytes are most-significant-first." |
|
701 | 584 |
|
585 |
" |
|
586 |
this implementation is wrong: does not work on non-IEEE machines |
|
587 |
(to date all machines where ST/X is running on use |
|
14923 | 588 |
IEEE float format. Will need more here, when porting ST/X to 370's) |
701 | 589 |
" |
3427
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
590 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
591 |
|
14923 | 592 |
(UninterpretedBytes isBigEndian == msb) ifFalse:[ |
24969 | 593 |
"swap the bytes" |
594 |
8 to:1 by:-1 do:[:i | |
|
595 |
aFloat basicAt:i put:(aStream next) |
|
596 |
]. |
|
597 |
^ self |
|
701 | 598 |
]. |
599 |
1 to:8 do:[:i | |
|
24969 | 600 |
aFloat basicAt:i put:aStream next |
701 | 601 |
] |
9637 | 602 |
|
603 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
604 |
||
605 |
"Modified: / 23-08-2006 / 16:00:37 / cg" |
|
701 | 606 |
! |
607 |
||
608 |
storeBinaryIEEEDouble:aFloat on:aStream |
|
609 |
"store aFloat as an IEEE formatted 8-byte float |
|
14925 | 610 |
onto the binary stream, aStream. |
611 |
The bytes are written in the native byte order (i.e.lsb on intel)" |
|
612 |
||
613 |
self storeBinaryIEEEDouble:aFloat on:aStream MSB:(UninterpretedBytes isBigEndian) |
|
614 |
! |
|
615 |
||
616 |
storeBinaryIEEEDouble:aFloat on:aStream MSB:msb |
|
617 |
"store aFloat as an IEEE formatted 8-byte float |
|
618 |
onto the binary stream, aStream. |
|
619 |
If msb is true, the stream bytes are written most-significant-first." |
|
701 | 620 |
|
621 |
" |
|
622 |
this implementation is wrong: does not work on non-IEEE machines |
|
623 |
(to date all machines where ST/X is running on use |
|
624 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
625 |
" |
|
3427
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
626 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
627 |
|
14925 | 628 |
(UninterpretedBytes isBigEndian == msb) ifFalse:[ |
24969 | 629 |
"swap the bytes" |
630 |
8 to:1 by:-1 do:[:i | |
|
631 |
aStream nextPut:(aFloat basicAt:i). |
|
632 |
]. |
|
633 |
^ self |
|
701 | 634 |
]. |
635 |
1 to:8 do:[:i | |
|
24969 | 636 |
aStream nextPut:(aFloat basicAt:i). |
701 | 637 |
]. |
9637 | 638 |
|
639 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
640 |
||
641 |
"Modified: / 23-08-2006 / 16:00:47 / cg" |
|
701 | 642 |
! ! |
643 |
||
7441 | 644 |
!Float class methodsFor:'class initialization'! |
645 |
||
646 |
initialize |
|
14105 | 647 |
Pi isNil ifTrue:[ |
24969 | 648 |
DefaultPrintFormat := '.15'. "/ print 15 valid digits |
649 |
DefaultPrintfFormat := '%15f'. |
|
650 |
||
651 |
Pi := 3.14159265358979323846264338327950288419716939937510582097494459. |
|
652 |
Halfpi := Pi / 2.0. |
|
653 |
HalfpiNegative := Halfpi negated. |
|
654 |
Twopi := Pi * 2.0. |
|
655 |
E := 2.7182818284590452353602874713526625. |
|
656 |
Sqrt2 := 1.41421356237309504880168872420969808. |
|
657 |
RadiansPerDegree := Pi / 180.0. |
|
658 |
Ln2 := 0.69314718055994530941723212145817657. |
|
659 |
Ln10 := 10.0 ln. |
|
660 |
MaxSmallInteger := super maxSmallInteger. |
|
14105 | 661 |
]. |
10604 | 662 |
|
663 |
" |
|
14105 | 664 |
Pi := nil. |
10604 | 665 |
self initialize |
666 |
" |
|
7441 | 667 |
|
668 |
" |
|
11945 | 669 |
DefaultPrintFormat := '.9'. |
670 |
Float pi printString. |
|
671 |
||
672 |
DefaultPrintFormat := '.6'. |
|
673 |
Float pi printString. |
|
7441 | 674 |
" |
10604 | 675 |
|
676 |
"Modified: / 07-06-2007 / 21:17:53 / cg" |
|
7441 | 677 |
! ! |
678 |
||
21870 | 679 |
!Float class methodsFor:'coercing & converting'! |
680 |
||
681 |
coerce:aNumber |
|
682 |
"convert the argument aNumber into an instance of the receiver (class) and return it." |
|
683 |
||
684 |
^ aNumber asFloat. |
|
685 |
! ! |
|
686 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
687 |
!Float class methodsFor:'constants'! |
1 | 688 |
|
3945
1745e97364cb
added preliminary version of NaN
Claus Gittinger <cg@exept.de>
parents:
3903
diff
changeset
|
689 |
NaN |
21859 | 690 |
"return the constant NaN (not a Number)" |
691 |
||
692 |
NaN isNil ifTrue:[ |
|
693 |
%{ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8635
diff
changeset
|
694 |
#if defined(LINUX) && defined(__i386__) |
3955 | 695 |
# ifdef NAN |
24969 | 696 |
@global(Float:NaN) = __MKFLOAT(NAN); |
3955 | 697 |
# else |
24969 | 698 |
@global(Float:NaN) = __MKFLOAT(_SNAN); |
3955 | 699 |
# endif |
3945
1745e97364cb
added preliminary version of NaN
Claus Gittinger <cg@exept.de>
parents:
3903
diff
changeset
|
700 |
#endif |
1745e97364cb
added preliminary version of NaN
Claus Gittinger <cg@exept.de>
parents:
3903
diff
changeset
|
701 |
%}. |
24969 | 702 |
NaN isNil ifTrue:[ |
703 |
NaN := super NaN. |
|
704 |
]. |
|
21868 | 705 |
]. |
21859 | 706 |
^ NaN |
3945
1745e97364cb
added preliminary version of NaN
Claus Gittinger <cg@exept.de>
parents:
3903
diff
changeset
|
707 |
|
1745e97364cb
added preliminary version of NaN
Claus Gittinger <cg@exept.de>
parents:
3903
diff
changeset
|
708 |
" |
21868 | 709 |
Float NaN |
11945 | 710 |
Float NaN + 0.0 |
3945
1745e97364cb
added preliminary version of NaN
Claus Gittinger <cg@exept.de>
parents:
3903
diff
changeset
|
711 |
Float NaN + Float NaN |
1745e97364cb
added preliminary version of NaN
Claus Gittinger <cg@exept.de>
parents:
3903
diff
changeset
|
712 |
0.0 + Float NaN |
21859 | 713 |
|
714 |
it's a singleton: |
|
21868 | 715 |
|
21859 | 716 |
Float NaN == Float NaN |
3945
1745e97364cb
added preliminary version of NaN
Claus Gittinger <cg@exept.de>
parents:
3903
diff
changeset
|
717 |
" |
21859 | 718 |
|
719 |
"Modified (format): / 20-06-2017 / 13:52:31 / cg" |
|
3945
1745e97364cb
added preliminary version of NaN
Claus Gittinger <cg@exept.de>
parents:
3903
diff
changeset
|
720 |
! |
1745e97364cb
added preliminary version of NaN
Claus Gittinger <cg@exept.de>
parents:
3903
diff
changeset
|
721 |
|
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
722 |
e |
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
723 |
"return the constant e as Float" |
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
724 |
|
20799 | 725 |
"/ don't expect this many valid digits on all machines; |
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
726 |
"/ The actual precision is very CPU specific. |
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
727 |
|
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
728 |
^ 2.7182818284590452353602874713526625 |
6341 | 729 |
! |
730 |
||
22725 | 731 |
halfpi |
732 |
"return the constant pi/2 as Float" |
|
733 |
||
734 |
"/ don't expect this many valid digits on all machines; |
|
735 |
"/ The actual precision is very CPU specific. |
|
736 |
||
737 |
^ 1.570796326794896619231321691639751442098584699687552910 |
|
738 |
! |
|
739 |
||
740 |
halfpiNegative |
|
741 |
"return the constant -pi/2 as Float" |
|
742 |
||
743 |
"/ don't expect this many valid digits on all machines; |
|
744 |
"/ The actual precision is very CPU specific. |
|
745 |
||
746 |
^ -1.570796326794896619231321691639751442098584699687552910 |
|
747 |
! |
|
748 |
||
21859 | 749 |
infinity |
24302 | 750 |
"return a float which represents positive infinity (for my instances). |
751 |
Warning: do not compare equal against infinities; |
|
752 |
instead, check using isFinite or isInfinite" |
|
21859 | 753 |
|
754 |
PositiveInfinity isNil ifTrue:[ |
|
25002 | 755 |
PositiveInfinity := super infinity |
21859 | 756 |
]. |
757 |
^ PositiveInfinity |
|
758 |
||
759 |
" |
|
21868 | 760 |
Float infinity == Float infinity |
21859 | 761 |
" |
762 |
||
763 |
"Created: / 20-06-2017 / 13:56:07 / cg" |
|
24302 | 764 |
"Modified (comment): / 09-06-2019 / 12:54:54 / Claus Gittinger" |
21859 | 765 |
! |
766 |
||
18492 | 767 |
ln10 |
768 |
"return the natural logarithm of 10; |
|
769 |
will return something like 2.30258509299405" |
|
770 |
||
771 |
^ Ln10 |
|
772 |
! |
|
773 |
||
10604 | 774 |
ln2 |
18492 | 775 |
"return the natural logarithm of 2" |
776 |
||
777 |
"/ don't expect this many valid digits on all machines; |
|
10604 | 778 |
"/ The actual precision is very CPU specific. |
779 |
||
780 |
^ 0.69314718055994530941723212145817657. |
|
781 |
||
782 |
"Created: / 07-06-2007 / 21:11:55 / cg" |
|
783 |
! |
|
784 |
||
19358 | 785 |
maxSmallInteger |
21868 | 786 |
"return the max. smallInteger value as a float" |
787 |
||
19358 | 788 |
^ MaxSmallInteger |
789 |
! |
|
790 |
||
21859 | 791 |
negativeInfinity |
24302 | 792 |
"return a float which represents positive infinity (for my instances). |
793 |
Warning: do not compare equal against infinities; |
|
794 |
instead, check using isFinite or isInfinite" |
|
21859 | 795 |
|
796 |
NegativeInfinity isNil ifTrue:[ |
|
25002 | 797 |
NegativeInfinity := super negativeInfinity |
21859 | 798 |
]. |
799 |
^ NegativeInfinity |
|
800 |
||
801 |
" |
|
21868 | 802 |
Float negativeInfinity == Float negativeInfinity |
21859 | 803 |
" |
804 |
||
805 |
"Created: / 20-06-2017 / 13:56:35 / cg" |
|
24302 | 806 |
"Modified (comment): / 09-06-2019 / 12:55:10 / Claus Gittinger" |
21859 | 807 |
! |
808 |
||
24930 | 809 |
phi |
810 |
"return the constant phi as Float" |
|
811 |
||
812 |
"/ don't expect this many valid digits on all machines; |
|
813 |
"/ The actual precision is very CPU specific. |
|
814 |
||
815 |
^ 1.618033988749894848204586834365638117720309179805762862135 |
|
816 |
||
817 |
||
818 |
! |
|
819 |
||
701 | 820 |
pi |
1260 | 821 |
"return the constant pi as Float" |
1 | 822 |
|
20799 | 823 |
"/ don't expect this many valid digits on all machines; |
3244 | 824 |
"/ The actual precision is very CPU specific. |
825 |
||
6063 | 826 |
^ 3.14159265358979323846264338327950288419716939937510582097494459 |
1260 | 827 |
|
828 |
"Modified: 23.4.1996 / 09:27:02 / cg" |
|
1 | 829 |
! |
830 |
||
10604 | 831 |
sqrt2 |
20799 | 832 |
"/ don't expect this many valid digits on all machines; |
10604 | 833 |
"/ The actual precision is very CPU specific. |
834 |
||
835 |
^ 1.41421356237309504880168872420969808 |
|
836 |
||
837 |
"Created: / 07-06-2007 / 21:12:33 / cg" |
|
838 |
! |
|
839 |
||
24930 | 840 |
sqrt5 |
841 |
"/ don't expect this many valid digits on all machines; |
|
842 |
"/ The actual precision is very CPU specific. |
|
843 |
||
844 |
^ 2.236067977499789696409173668731276235440618359611525724270 |
|
845 |
||
846 |
"Created: / 07-06-2007 / 21:12:33 / cg" |
|
847 |
! |
|
848 |
||
1 | 849 |
unity |
1260 | 850 |
"return the neutral element for multiplication (1.0) as Float" |
1 | 851 |
|
852 |
^ 1.0 |
|
1260 | 853 |
|
854 |
"Modified: 23.4.1996 / 09:27:09 / cg" |
|
1 | 855 |
! |
856 |
||
701 | 857 |
zero |
1260 | 858 |
"return the neutral element for addition (0.0) as Float" |
1 | 859 |
|
701 | 860 |
^ 0.0 |
1260 | 861 |
|
862 |
"Modified: 23.4.1996 / 09:27:15 / cg" |
|
1 | 863 |
! ! |
864 |
||
14105 | 865 |
!Float class methodsFor:'misc'! |
866 |
||
867 |
getFPUControl |
|
24169 | 868 |
"BORLAND only: get the fpu control word." |
14105 | 869 |
|
870 |
%{ |
|
871 |
#ifdef __BORLANDC__ |
|
14106 | 872 |
unsigned int _control87(); |
873 |
||
14105 | 874 |
int result = _control87(0, 0); |
875 |
RETURN(__MKSMALLINT(result)); |
|
876 |
#endif |
|
24169 | 877 |
%}. |
878 |
^ 0 "/ otherwise an arbitrary value |
|
14105 | 879 |
|
880 |
" |
|
21849 | 881 |
self getFPUControl |
14105 | 882 |
" |
21849 | 883 |
|
884 |
"Modified (comment): / 20-06-2017 / 11:51:58 / cg" |
|
24169 | 885 |
"Modified (comment): / 27-05-2019 / 08:58:54 / Claus Gittinger" |
14105 | 886 |
! |
887 |
||
888 |
setFPUControl |
|
21849 | 889 |
"BORLAND only: set the fpu control word for 64 bit precision for long doubles here" |
14105 | 890 |
|
891 |
%{ |
|
892 |
#ifdef __BORLANDC__ |
|
893 |
#include <float.h> |
|
894 |
||
895 |
// Set precision to long double / 64bit |
|
896 |
int result = _control87(PC_64, MCW_PC); |
|
897 |
RETURN(__MKSMALLINT(result)); |
|
898 |
#endif |
|
24169 | 899 |
%}. |
900 |
"/ otherwise ignored |
|
14105 | 901 |
|
902 |
" |
|
21849 | 903 |
self setFPUControl |
14105 | 904 |
" |
21849 | 905 |
|
906 |
"Modified (comment): / 20-06-2017 / 11:51:54 / cg" |
|
24169 | 907 |
"Modified (comment): / 27-05-2019 / 08:58:35 / Claus Gittinger" |
14105 | 908 |
! ! |
909 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
910 |
!Float class methodsFor:'queries'! |
3 | 911 |
|
24969 | 912 |
eBias |
25002 | 913 |
"Answer the exponent's bias; |
24969 | 914 |
that is the offset of the zero exponent when stored" |
915 |
||
916 |
^ 1023 |
|
917 |
! |
|
918 |
||
919 |
emax |
|
25008 | 920 |
"The largest exponent value allowed by instances of this class." |
921 |
||
922 |
%{ /* NOCONTEXT */ |
|
923 |
#include <float.h> |
|
924 |
#if defined(DBL_MAX_EXP) |
|
925 |
RETURN(__MKSMALLINT(DBL_MAX_EXP)); |
|
926 |
#endif |
|
927 |
%}. |
|
928 |
^ super emax |
|
929 |
||
930 |
" |
|
931 |
1.0 fmax -> 1.189731495357231765E+4932 |
|
932 |
1.0 fmin -> 3.362103143112093506E-4932 |
|
933 |
1.0 emin -> -16381 |
|
934 |
1.0 emax -> 1024 |
|
935 |
" |
|
24969 | 936 |
! |
937 |
||
938 |
emin |
|
25008 | 939 |
"The smallest exponent value allowed by instances of this class." |
940 |
||
941 |
%{ /* NOCONTEXT */ |
|
942 |
#include <float.h> |
|
943 |
#if defined(DBL_MIN_EXP) |
|
944 |
RETURN(__MKSMALLINT(DBL_MIN_EXP)); |
|
945 |
#endif |
|
946 |
%}. |
|
947 |
^ super emin |
|
948 |
||
949 |
" |
|
950 |
1.0 fmax -> 1.189731495357231765E+4932 |
|
951 |
1.0 fmin -> 3.362103143112093506E-4932 |
|
952 |
1.0 emin -> -1021 |
|
953 |
1.0 emax -> 1024 |
|
954 |
" |
|
24969 | 955 |
! |
956 |
||
24320 | 957 |
epsilon |
958 |
"return the maximum relative spacing of instances of mySelf |
|
24957 | 959 |
(i.e. the value-delta of the least significant bit). |
960 |
see https://en.wikipedia.org/wiki/Machine_epsilon" |
|
24320 | 961 |
|
962 |
Epsilon isNil ifTrue:[ |
|
25002 | 963 |
Epsilon := self computeEpsilon. |
24320 | 964 |
]. |
965 |
^ Epsilon |
|
966 |
||
24915 | 967 |
" |
968 |
Float epsilon -> 1.11022302462516E-16 |
|
969 |
ShortFloat epsilon -> 5.960464e-08 |
|
970 |
LongFloat epsilon -> 5.42101086242752217E-20 |
|
25002 | 971 |
QuadFloat epsilon -> |
24915 | 972 |
" |
973 |
||
24320 | 974 |
"Modified (comment): / 22-06-2017 / 13:44:12 / cg" |
975 |
! |
|
976 |
||
6898 | 977 |
exponentCharacter |
24326 | 978 |
"return the character used to print between mantissa an exponent. |
979 |
Also used by the scanner when reading numbers." |
|
980 |
||
6898 | 981 |
^ $d |
24326 | 982 |
|
983 |
"Modified (comment): / 10-06-2019 / 21:27:46 / Claus Gittinger" |
|
6898 | 984 |
! |
985 |
||
25008 | 986 |
fmax |
987 |
%{ |
|
988 |
RETURN(__MKFLOAT(DBL_MAX)); |
|
989 |
%}. |
|
990 |
||
991 |
" |
|
992 |
Float fmax |
|
993 |
" |
|
994 |
! |
|
995 |
||
25002 | 996 |
fmin |
997 |
%{ |
|
998 |
RETURN(__MKFLOAT(DBL_MIN)); |
|
999 |
%}. |
|
1000 |
||
1001 |
" |
|
1002 |
Float fmin |
|
1003 |
" |
|
1004 |
! |
|
1005 |
||
3 | 1006 |
isBuiltInClass |
1264 | 1007 |
"return true if this class is known by the run-time-system. |
1008 |
Here, true is returned for myself, false for subclasses." |
|
3 | 1009 |
|
1010 |
^ self == Float |
|
1264 | 1011 |
|
1012 |
"Modified: 23.4.1996 / 15:59:04 / cg" |
|
3427
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
1013 |
! |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
1014 |
|
7355 | 1015 |
numBitsInExponent |
1016 |
"answer the number of bits in the exponent |
|
1017 |
This is an IEEE float, where 11 bits are available: |
|
24969 | 1018 |
seeeeeee eeeemmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm |
7355 | 1019 |
" |
1020 |
||
1021 |
^ 11 |
|
24206 | 1022 |
|
1023 |
" |
|
1024 |
1.0 numBitsInExponent |
|
1025 |
" |
|
1026 |
||
1027 |
"Modified (format): / 28-05-2019 / 08:55:51 / Claus Gittinger" |
|
7355 | 1028 |
! |
1029 |
||
1030 |
numBitsInMantissa |
|
1031 |
"answer the number of bits in the mantissa. |
|
1032 |
This is an IEEE double, where 52 bits (the hidden one is not counted here) are available: |
|
25008 | 1033 |
seeeeeee eeeemmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm |
7355 | 1034 |
" |
1035 |
||
1036 |
^ 52 |
|
25008 | 1037 |
|
1038 |
" |
|
1039 |
1.0 numBitsInExponent 11 |
|
1040 |
1.0 numBitsInMantissa 52 |
|
1041 |
1.0 precision 53 |
|
1042 |
1.0 decimalPrecision 16 |
|
1043 |
1.0 eBias 1023 |
|
1044 |
1.0 emin -1022 |
|
1045 |
1.0 emax 1023 |
|
1046 |
1.0 fmin 2.2250738585072E-308 |
|
1047 |
1.0 fmax 1.79769313486232E+308 |
|
1048 |
" |
|
1049 |
||
7355 | 1050 |
! |
1051 |
||
1052 |
precision |
|
24266 | 1053 |
"answer the precision (the number of bits in the mantissa) of a Float (in bits) |
11945 | 1054 |
This is an IEEE double, where only the fraction from the normalized mantissa is stored |
1055 |
and so there is a hidden bit and the mantissa is actually represented |
|
9150 | 1056 |
by 53 binary digits (although only 52 are needed in the binary representation)" |
7355 | 1057 |
|
1058 |
^ 53 |
|
19326 | 1059 |
|
1060 |
" |
|
1061 |
self numBitsInMantissa + 1 |
|
1062 |
self precision |
|
1063 |
" |
|
24266 | 1064 |
|
1065 |
"Modified (comment): / 06-06-2019 / 13:23:58 / Claus Gittinger" |
|
7355 | 1066 |
! |
1067 |
||
1068 |
radix |
|
24440 | 1069 |
"answer the radix of a Float's exponent |
7355 | 1070 |
This is an IEEE float, which is represented as binary" |
1071 |
||
8635 | 1072 |
^ 2 "must be careful here, whenever ST/X is used on VAX or a 370" |
24440 | 1073 |
|
1074 |
"Modified (comment): / 19-07-2019 / 17:28:11 / Claus Gittinger" |
|
3 | 1075 |
! ! |
1076 |
||
1 | 1077 |
!Float methodsFor:'arithmetic'! |
1078 |
||
701 | 1079 |
* aNumber |
11733 | 1080 |
"return the product of the receiver and the argument." |
701 | 1081 |
|
1082 |
%{ /* NOCONTEXT */ |
|
18296 | 1083 |
#ifdef __SCHTEAM__ |
1084 |
if (aNumber.isNumber()) { |
|
24969 | 1085 |
return context._RETURN( self.times(aNumber) ); |
18296 | 1086 |
} |
1087 |
#else |
|
701 | 1088 |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1089 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1090 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1091 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1092 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1093 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1094 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1095 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1096 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1097 |
*/ |
701 | 1098 |
OBJ newFloat; |
1099 |
double result; |
|
1100 |
||
1101 |
if (__isSmallInteger(aNumber)) { |
|
24969 | 1102 |
result = __floatVal(self) * (double)(__intVal(aNumber)); |
701 | 1103 |
retResult: |
24969 | 1104 |
__qMKFLOAT(newFloat, result); |
1105 |
RETURN ( newFloat ); |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1106 |
} |
14689
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1107 |
/* knowing that aNumber is not a SmallInt, we only need to check for nil; |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1108 |
* then can use qIsXXX macros which saves us some checks |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1109 |
*/ |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1110 |
if (aNumber != nil) { |
24969 | 1111 |
if (__qIsFloatLike(aNumber)) { |
1112 |
result = __floatVal(self) * __floatVal(aNumber); |
|
1113 |
goto retResult; |
|
1114 |
} |
|
1115 |
if (__qIsShortFloat(aNumber)) { |
|
1116 |
result = __floatVal(self) * (double)(__shortFloatVal(aNumber)); |
|
1117 |
goto retResult; |
|
1118 |
} |
|
701 | 1119 |
} |
18296 | 1120 |
#endif |
3530
a1af5dafc3ee
float op shortFloat added inline.
Claus Gittinger <cg@exept.de>
parents:
3472
diff
changeset
|
1121 |
%}. |
701 | 1122 |
^ aNumber productFromFloat:self |
1123 |
! |
|
1124 |
||
1 | 1125 |
+ aNumber |
1126 |
"return the sum of the receiver and the argument, aNumber" |
|
1127 |
||
1128 |
%{ /* NOCONTEXT */ |
|
18296 | 1129 |
#ifdef __SCHTEAM__ |
1130 |
if (aNumber.isNumber()) { |
|
24969 | 1131 |
return context._RETURN( self.plus(aNumber) ); |
18296 | 1132 |
} |
1133 |
#else |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1134 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1135 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1136 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1137 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1138 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1139 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1140 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1141 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1142 |
*/ |
1 | 1143 |
OBJ newFloat; |
1144 |
double result; |
|
1145 |
||
250 | 1146 |
if (__isSmallInteger(aNumber)) { |
24969 | 1147 |
result = __floatVal(self) + (double)(__intVal(aNumber)); |
1 | 1148 |
retResult: |
24969 | 1149 |
__qMKFLOAT(newFloat, result); |
1150 |
RETURN ( newFloat ); |
|
11945 | 1151 |
} |
14689
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1152 |
/* knowing that aNumber is not a SmallInt, we only need to check for nil; |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1153 |
* then can use qIsXXX macros which saves us some checks |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1154 |
*/ |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1155 |
if (aNumber != nil) { |
24969 | 1156 |
if (__qIsFloatLike(aNumber)) { |
1157 |
result = __floatVal(self) + __floatVal(aNumber); |
|
1158 |
goto retResult; |
|
1159 |
} |
|
1160 |
if (__qIsShortFloat(aNumber)) { |
|
1161 |
result = __floatVal(self) + (double)(__shortFloatVal(aNumber)); |
|
1162 |
goto retResult; |
|
1163 |
} |
|
1 | 1164 |
} |
18296 | 1165 |
#endif |
3530
a1af5dafc3ee
float op shortFloat added inline.
Claus Gittinger <cg@exept.de>
parents:
3472
diff
changeset
|
1166 |
%}. |
1 | 1167 |
^ aNumber sumFromFloat:self |
1168 |
! |
|
1169 |
||
1170 |
- aNumber |
|
1171 |
"return the difference of the receiver and the argument, aNumber" |
|
1172 |
||
1173 |
%{ /* NOCONTEXT */ |
|
18296 | 1174 |
#ifdef __SCHTEAM__ |
1175 |
if (aNumber.isNumber()) { |
|
24969 | 1176 |
return context._RETURN( self.minus(aNumber) ); |
18296 | 1177 |
} |
1178 |
#else |
|
13 | 1179 |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1180 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1181 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1182 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1183 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1184 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1185 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1186 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1187 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1188 |
*/ |
1 | 1189 |
OBJ newFloat; |
1190 |
double result; |
|
1191 |
||
250 | 1192 |
if (__isSmallInteger(aNumber)) { |
24969 | 1193 |
result = __floatVal(self) - (double)(__intVal(aNumber)); |
1 | 1194 |
retResult: |
24969 | 1195 |
__qMKFLOAT(newFloat, result); |
1196 |
RETURN ( newFloat ); |
|
11945 | 1197 |
} |
14689
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1198 |
/* knowing that aNumber is not a SmallInt, we only need to check for nil; |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1199 |
* then can use qIsXXX macros which saves us some checks |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1200 |
*/ |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1201 |
if (aNumber != nil) { |
24969 | 1202 |
if (__qIsFloatLike(aNumber)) { |
1203 |
result = __floatVal(self) - __floatVal(aNumber); |
|
1204 |
goto retResult; |
|
1205 |
} |
|
1206 |
if (__qIsShortFloat(aNumber)) { |
|
1207 |
result = __floatVal(self) - (double)(__shortFloatVal(aNumber)); |
|
1208 |
goto retResult; |
|
1209 |
} |
|
1 | 1210 |
} |
18296 | 1211 |
#endif |
3530
a1af5dafc3ee
float op shortFloat added inline.
Claus Gittinger <cg@exept.de>
parents:
3472
diff
changeset
|
1212 |
%}. |
1 | 1213 |
^ aNumber differenceFromFloat:self |
1214 |
! |
|
1215 |
||
1216 |
/ aNumber |
|
1217 |
"return the quotient of the receiver and the argument, aNumber" |
|
1218 |
||
1219 |
%{ /* NOCONTEXT */ |
|
18296 | 1220 |
#ifdef __SCHTEAM__ |
1221 |
if (aNumber.isNumber()) { |
|
24969 | 1222 |
return context._RETURN( self.quotient(aNumber) ); |
18296 | 1223 |
} |
1224 |
#else |
|
1 | 1225 |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1226 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1227 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1228 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1229 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1230 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1231 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1232 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1233 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
6063
diff
changeset
|
1234 |
*/ |
1 | 1235 |
OBJ newFloat; |
1236 |
double result, val; |
|
1237 |
||
250 | 1238 |
if (__isSmallInteger(aNumber)) { |
24969 | 1239 |
if (aNumber != __mkSmallInteger(0)) { |
1240 |
result = __floatVal(self) / ( (double)__intVal(aNumber)) ; |
|
1 | 1241 |
retResult: |
24969 | 1242 |
__qMKFLOAT(newFloat, result); |
1243 |
RETURN ( newFloat ); |
|
1244 |
} |
|
3530
a1af5dafc3ee
float op shortFloat added inline.
Claus Gittinger <cg@exept.de>
parents:
3472
diff
changeset
|
1245 |
} else if (__isFloatLike(aNumber)) { |
24969 | 1246 |
val = __floatVal(aNumber); |
1247 |
if (val != 0.0) { |
|
1248 |
result = __floatVal(self) / val; |
|
1249 |
goto retResult; |
|
1250 |
} |
|
3530
a1af5dafc3ee
float op shortFloat added inline.
Claus Gittinger <cg@exept.de>
parents:
3472
diff
changeset
|
1251 |
} else if (__isShortFloat(aNumber)) { |
24969 | 1252 |
val = (double)(__shortFloatVal(aNumber)); |
1253 |
if (val != 0.0) { |
|
1254 |
result = __floatVal(self) / val; |
|
1255 |
goto retResult; |
|
1256 |
} |
|
1 | 1257 |
} |
18296 | 1258 |
#endif |
3530
a1af5dafc3ee
float op shortFloat added inline.
Claus Gittinger <cg@exept.de>
parents:
3472
diff
changeset
|
1259 |
%}. |
1 | 1260 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
24969 | 1261 |
" |
1262 |
No, you shalt not divide by zero |
|
1263 |
" |
|
1264 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
1 | 1265 |
]. |
1266 |
^ aNumber quotientFromFloat:self |
|
1267 |
! |
|
1268 |
||
14923 | 1269 |
abs |
1270 |
"return the absolute value of the receiver |
|
1271 |
reimplemented here for speed" |
|
1272 |
||
1273 |
%{ /* NOCONTEXT */ |
|
18296 | 1274 |
#ifdef __SCHTEAM__ |
1275 |
return context._RETURN( self.abs() ); |
|
1276 |
#else |
|
14923 | 1277 |
|
1278 |
OBJ newFloat; |
|
1279 |
double val =__floatVal(self); |
|
1280 |
||
1281 |
if (val < 0.0) { |
|
24969 | 1282 |
__qMKFLOAT(newFloat, -val); |
1283 |
RETURN ( newFloat ); |
|
14923 | 1284 |
} |
1285 |
RETURN (self); |
|
18296 | 1286 |
#endif |
14923 | 1287 |
%}. |
1288 |
||
1289 |
" |
|
1290 |
3.0 abs |
|
1291 |
-3.0 abs |
|
1292 |
" |
|
1293 |
! |
|
1294 |
||
1295 |
negated |
|
17226 | 1296 |
"return the receiver negated" |
14923 | 1297 |
|
1298 |
%{ /* NOCONTEXT */ |
|
18296 | 1299 |
#ifdef __SCHTEAM__ |
1300 |
return context._RETURN( self.negated() ); |
|
1301 |
#else |
|
14923 | 1302 |
OBJ newFloat; |
1303 |
double rslt = - __floatVal(self); |
|
1304 |
||
1305 |
__qMKFLOAT(newFloat, rslt); |
|
1306 |
RETURN ( newFloat ); |
|
18296 | 1307 |
#endif |
14923 | 1308 |
%}. |
1309 |
! |
|
1310 |
||
14689
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1311 |
rem: aNumber |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1312 |
"return the floating point remainder of the receiver and the argument, aNumber" |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1313 |
|
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1314 |
%{ /* NOCONTEXT */ |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1315 |
|
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1316 |
/* |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1317 |
* notice: |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1318 |
* the following inline code handles some common cases, |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1319 |
* and exists as an optimization, to speed up those cases. |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1320 |
* |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1321 |
* Conceptionally, (and for most other argument types), |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1322 |
* mixed arithmetic is implemented by double dispatching |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1323 |
* (see the message send at the bottom) |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1324 |
*/ |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1325 |
OBJ newFloat; |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1326 |
double result, val; |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1327 |
|
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1328 |
if (__isSmallInteger(aNumber)) { |
24969 | 1329 |
if (aNumber != __mkSmallInteger(0)) { |
1330 |
val = (double)__intVal(aNumber); |
|
14689
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1331 |
computeResult: |
24969 | 1332 |
result = fmod(__floatVal(self), val) ; |
1333 |
__qMKFLOAT(newFloat, result); |
|
1334 |
RETURN ( newFloat ); |
|
1335 |
} |
|
14689
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1336 |
} else if (__isFloatLike(aNumber)) { |
24969 | 1337 |
val = __floatVal(aNumber); |
1338 |
if (val != 0.0) { |
|
1339 |
goto computeResult; |
|
1340 |
} |
|
14689
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1341 |
} else if (__isShortFloat(aNumber)) { |
24969 | 1342 |
val = (double)(__shortFloatVal(aNumber)); |
1343 |
if (val != 0.0) { |
|
1344 |
goto computeResult; |
|
1345 |
} |
|
14720 | 1346 |
#ifdef LONGFLOAT_KNOWN_HERE |
1347 |
} else if (__isLongFloat(aNumber)) { |
|
24969 | 1348 |
long double lval; |
1349 |
||
1350 |
lval = (long double)(__longFloatVal(aNumber)); |
|
1351 |
if (val != 0.0) { |
|
1352 |
long double lResult; |
|
1353 |
||
1354 |
lResult = fmodl((long double)(__floatVal(self)), lval); |
|
1355 |
__qMKLFLOAT(newFloat, lResult); |
|
1356 |
RETURN (newFloat); |
|
1357 |
} |
|
14720 | 1358 |
#endif |
14689
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1359 |
} |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1360 |
%}. |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1361 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
24969 | 1362 |
" |
1363 |
No, you shalt not divide by zero |
|
1364 |
" |
|
1365 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
14689
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1366 |
]. |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1367 |
^ aNumber remainderFromFloat:self |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1368 |
! |
f709732040c1
added rem: and a tiny performance speedup in other mixed-type arith messages
Claus Gittinger <cg@exept.de>
parents:
14672
diff
changeset
|
1369 |
|
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1370 |
uncheckedDivide:aNumber |
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1371 |
"return the quotient of the receiver and the argument, aNumber. |
7471 | 1372 |
Do not check for divide by zero (return NaN or Infinity). |
7355 | 1373 |
This operation is provided for emulators of other languages/semantics, |
1374 |
where no exception is raised for these results (i.e. Java). |
|
18838 | 1375 |
It is only defined if the argument's type is the same as the receiver's." |
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1376 |
|
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1377 |
%{ /* NOCONTEXT */ |
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1378 |
|
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1379 |
OBJ newFloat; |
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1380 |
double result, val; |
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1381 |
|
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1382 |
if (__isSmallInteger(aNumber)) { |
24969 | 1383 |
result = __floatVal(self) / ( (double)__intVal(aNumber)) ; |
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1384 |
retResult: |
24969 | 1385 |
__qMKFLOAT(newFloat, result); |
1386 |
RETURN ( newFloat ); |
|
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1387 |
} |
7355 | 1388 |
if (__isFloatLike(aNumber)) { |
24969 | 1389 |
val = __floatVal(aNumber); |
1390 |
result = __floatVal(self) / val; |
|
1391 |
goto retResult; |
|
7355 | 1392 |
} |
1393 |
if (__isShortFloat(aNumber)) { |
|
24969 | 1394 |
val = (double)(__shortFloatVal(aNumber)); |
1395 |
result = __floatVal(self) / val; |
|
1396 |
goto retResult; |
|
7355 | 1397 |
} |
1398 |
%}. |
|
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1399 |
^ aNumber quotientFromFloat:self |
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1400 |
|
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1401 |
" |
11945 | 1402 |
0.0 uncheckedDivide:0.0 |
1403 |
1.0 uncheckedDivide:0.0 |
|
1404 |
-1.0 uncheckedDivide:0.0 |
|
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
1405 |
" |
1 | 1406 |
! ! |
1407 |
||
701 | 1408 |
|
5238 | 1409 |
!Float methodsFor:'coercing & converting'! |
1 | 1410 |
|
3014 | 1411 |
asDouble |
17226 | 1412 |
"ST80 compatibility: return a double with the receiver's value. |
1413 |
In ST/X, floats are the equivalent to ST80 doubles" |
|
3014 | 1414 |
|
1415 |
^ self |
|
1416 |
! |
|
1417 |
||
701 | 1418 |
asFloat |
24280 | 1419 |
"return a Float with same value - that's me" |
701 | 1420 |
|
1421 |
^ self |
|
24280 | 1422 |
|
1423 |
"Modified (comment): / 07-06-2019 / 02:34:18 / Claus Gittinger" |
|
701 | 1424 |
! |
1425 |
||
1426 |
asInteger |
|
1427 |
"return an integer with same value - might truncate" |
|
1428 |
||
1 | 1429 |
%{ /* NOCONTEXT */ |
1670 | 1430 |
double dVal; |
13 | 1431 |
|
1670 | 1432 |
dVal = __floatVal(self); |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
1433 |
#ifdef __win32__ |
11945 | 1434 |
if (! isnan(dVal)) |
7420 | 1435 |
#endif |
1436 |
{ |
|
24969 | 1437 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
1438 |
RETURN ( __mkSmallInteger( (INT)dVal) ); |
|
1439 |
} |
|
701 | 1440 |
} |
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1441 |
%}. |
701 | 1442 |
^ super asInteger |
1443 |
||
1444 |
"12345.0 asInteger" |
|
1445 |
"1e15 asInteger" |
|
1 | 1446 |
! |
1447 |
||
5986 | 1448 |
asLongFloat |
24280 | 1449 |
"return a LongFloat with same value as receiver" |
5986 | 1450 |
|
7355 | 1451 |
^ LongFloat fromFloat:self |
5986 | 1452 |
|
1453 |
" |
|
11945 | 1454 |
123 asFloat asLongFloat |
5986 | 1455 |
" |
1456 |
||
24280 | 1457 |
"Created: / 07-09-2001 / 13:43:04 / cg" |
1458 |
"Modified (comment): / 07-06-2019 / 02:34:07 / Claus Gittinger" |
|
1459 |
! |
|
1460 |
||
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1461 |
asShortFloat |
24280 | 1462 |
"return a ShortFloat with same value as receiver. |
24249
17326e1df17f
raise exception on out-of-range in asShortFloat / asFloat
Claus Gittinger <cg@exept.de>
parents:
24230
diff
changeset
|
1463 |
Raises an error if the receiver exceeds the float range." |
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1464 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1465 |
%{ /* NOCONTEXT */ |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1466 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1467 |
OBJ dummy = @global(ShortFloat); |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1468 |
OBJ newFloat; |
24255 | 1469 |
double dVal = __floatVal(self); |
1470 |
float fVal = (float)dVal; |
|
1471 |
||
1472 |
if (isfinite(fVal) || !isfinite(dVal)) { |
|
25002 | 1473 |
__qMKSFLOAT(newFloat, fVal); |
1474 |
RETURN ( newFloat ); |
|
24249
17326e1df17f
raise exception on out-of-range in asShortFloat / asFloat
Claus Gittinger <cg@exept.de>
parents:
24230
diff
changeset
|
1475 |
} |
18296 | 1476 |
%}. |
24249
17326e1df17f
raise exception on out-of-range in asShortFloat / asFloat
Claus Gittinger <cg@exept.de>
parents:
24230
diff
changeset
|
1477 |
" |
17326e1df17f
raise exception on out-of-range in asShortFloat / asFloat
Claus Gittinger <cg@exept.de>
parents:
24230
diff
changeset
|
1478 |
value out of range |
17326e1df17f
raise exception on out-of-range in asShortFloat / asFloat
Claus Gittinger <cg@exept.de>
parents:
24230
diff
changeset
|
1479 |
if you need -INF for a zero receiver, try Number trapInfinity:[...] |
17326e1df17f
raise exception on out-of-range in asShortFloat / asFloat
Claus Gittinger <cg@exept.de>
parents:
24230
diff
changeset
|
1480 |
" |
17326e1df17f
raise exception on out-of-range in asShortFloat / asFloat
Claus Gittinger <cg@exept.de>
parents:
24230
diff
changeset
|
1481 |
^ self class |
25002 | 1482 |
raise:#infiniteResultSignal |
1483 |
receiver:self |
|
1484 |
selector:#asShortFloat |
|
1485 |
arguments:#() |
|
1486 |
errorString:'receiver is out of the single-precision float range' |
|
24280 | 1487 |
|
1488 |
"Modified: / 07-06-2019 / 02:34:14 / Claus Gittinger" |
|
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1489 |
! |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1490 |
|
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1491 |
asTrueFraction |
17226 | 1492 |
"Answer a fraction or integer that EXACTLY represents the receiver, |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1493 |
a double precision IEEE floating point number. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1494 |
Floats are stored in the same form on all platforms. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1495 |
(Does not handle gradual underflow or NANs.) |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1496 |
By David N. Smith with significant performance |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1497 |
improvements by Luciano Esteban Notarfrancesco. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1498 |
(Version of 11April97)" |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1499 |
|
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1500 |
|shifty sign expPart exp fraction fractionPart result zeroBitsCount| |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1501 |
|
6344 | 1502 |
self isFinite ifFalse:[ |
25002 | 1503 |
^ self asMetaNumber |
7441 | 1504 |
"/ ^ self class |
1505 |
"/ raise:#domainErrorSignal |
|
1506 |
"/ receiver:self |
|
1507 |
"/ selector:#asTrueFraction |
|
1508 |
"/ arguments:#() |
|
1509 |
"/ errorString:'Cannot represent non-finite float as a fraction'. |
|
6341 | 1510 |
]. |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1511 |
|
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1512 |
"Extract the bits of an IEEE double float " |
6341 | 1513 |
shifty := LargeInteger basicNew numberOfDigits:8. |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1514 |
UninterpretedBytes isBigEndian ifTrue:[ |
25002 | 1515 |
"/ shifty := ((self longWordAt: 1) bitShift: 32) + (self longWordAt: 2). |
1516 |
1 to:8 do:[:i | shifty digitAt:(9-i) put:(self basicAt:i)]. |
|
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1517 |
] ifFalse:[ |
25002 | 1518 |
1 to:8 do:[:i | shifty digitAt:i put:(self basicAt:i)]. |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1519 |
]. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1520 |
|
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1521 |
" Extract the sign and the biased exponent " |
24745 | 1522 |
sign := (shifty bitShift: -63) == 0 ifTrue: [1] ifFalse: [-1]. |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1523 |
expPart := (shifty bitShift: -52) bitAnd: 16r7FF. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1524 |
|
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1525 |
" Extract fractional part; answer 0 if this is a true 0.0 value " |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1526 |
fractionPart := shifty bitAnd: 16r000FFFFFFFFFFFFF. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1527 |
( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0 ]. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1528 |
|
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1529 |
" Replace omitted leading 1 in fraction " |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1530 |
fraction := fractionPart bitOr: 16r0010000000000000. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1531 |
|
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1532 |
"Unbias exponent: 16r3FF is bias; 52 is fraction width" |
7480 | 1533 |
exp := 16r3FF - expPart + 52. |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1534 |
|
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1535 |
" Form the result. When exp>52, the exponent is adjusted by |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1536 |
the number of trailing zero bits in the fraction to minimize |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1537 |
the (huge) time otherwise spent in #gcd:. " |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1538 |
exp negative ifTrue: [ |
25002 | 1539 |
result := sign * (fraction bitShift: exp negated) |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1540 |
] ifFalse:[ |
25002 | 1541 |
zeroBitsCount := fraction lowBit - 1. |
1542 |
exp := exp - zeroBitsCount. |
|
1543 |
exp <= 0 ifTrue: [ |
|
1544 |
zeroBitsCount := zeroBitsCount + exp. |
|
1545 |
"exp := 0." " Not needed; exp not refernced again " |
|
1546 |
result := sign * (fraction bitShift:zeroBitsCount negated) |
|
1547 |
] ifFalse: [ |
|
1548 |
result := Fraction |
|
1549 |
numerator: (sign * (fraction bitShift: zeroBitsCount negated)) |
|
1550 |
denominator: (1 bitShift:exp) |
|
1551 |
] |
|
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1552 |
]. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1553 |
|
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1554 |
"Low cost validation omitted after extensive testing" |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1555 |
"(result asFloat = self) ifFalse: [self error: 'asTrueFraction validation failed']." |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1556 |
|
11945 | 1557 |
^ result |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1558 |
|
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1559 |
" |
17226 | 1560 |
0.3 asTrueFraction - as you can see, Float is not able to represent this exactly |
1561 |
1.25 asTrueFraction - but this one (it is a sum of powers of two) |
|
11945 | 1562 |
0.25 asTrueFraction |
1563 |
-0.25 asTrueFraction |
|
1564 |
3e37 asTrueFraction |
|
17226 | 1565 |
2e37 asTrueFraction |
1566 |
1e37 asTrueFraction |
|
18296 | 1567 |
1e30 asTrueFraction |
6341 | 1568 |
Float NaN asTrueFraction |
6344 | 1569 |
Float infinity asTrueFraction |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1570 |
" |
24230 | 1571 |
|
1572 |
"Modified (format): / 29-05-2019 / 03:12:55 / Claus Gittinger" |
|
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1573 |
! |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
1574 |
|
701 | 1575 |
coerce:aNumber |
18838 | 1576 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
1 | 1577 |
|
701 | 1578 |
^ aNumber asFloat |
1579 |
! |
|
13 | 1580 |
|
701 | 1581 |
generality |
1582 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
1583 |
||
1584 |
^ 80 |
|
1 | 1585 |
! ! |
1586 |
||
1587 |
!Float methodsFor:'comparing'! |
|
1588 |
||
1589 |
< aNumber |
|
1590 |
"return true, if the argument is greater" |
|
1591 |
||
1592 |
%{ /* NOCONTEXT */ |
|
18296 | 1593 |
#ifdef __SCHTEAM__ |
1594 |
if (aNumber.isNumber()) { |
|
24969 | 1595 |
return context._RETURN( self.ltP(aNumber) ); |
18296 | 1596 |
} |
1597 |
#else |
|
11945 | 1598 |
/* |
1599 |
* notice: |
|
1600 |
* the following inline code handles some common cases, |
|
1601 |
* and exists as an optimization, to speed up those cases. |
|
1602 |
* |
|
1603 |
* Conceptionally, (and for most other argument types), |
|
1604 |
* mixed arithmetic is implemented by double dispatching |
|
1605 |
* (see the message send at the bottom) |
|
1606 |
*/ |
|
7355 | 1607 |
if (__isSmallInteger(aNumber)) { |
24969 | 1608 |
RETURN ( (__floatVal(self) < (double)(__intVal(aNumber))) ? true : false ); |
7355 | 1609 |
} |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
1610 |
if (aNumber != nil) { |
24969 | 1611 |
if (__qIsFloatLike(aNumber)) { |
1612 |
RETURN ( (__floatVal(self) < __floatVal(aNumber)) ? true : false ); |
|
1613 |
} |
|
1614 |
if (__qIsShortFloat(aNumber)) { |
|
1615 |
RETURN ( (__floatVal(self) < (double)(__shortFloatVal(aNumber))) ? true : false ); |
|
1616 |
} |
|
1 | 1617 |
} |
18296 | 1618 |
#endif |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
1619 |
%}. |
1 | 1620 |
^ aNumber lessFromFloat:self |
1621 |
! |
|
1622 |
||
1623 |
<= aNumber |
|
1624 |
"return true, if the argument is greater or equal" |
|
1625 |
||
1626 |
%{ /* NOCONTEXT */ |
|
18296 | 1627 |
#ifdef __SCHTEAM__ |
1628 |
if (aNumber.isNumber()) { |
|
24969 | 1629 |
return context._RETURN( self.leP(aNumber) ); |
18296 | 1630 |
} |
1631 |
#else |
|
11945 | 1632 |
/* |
1633 |
* notice: |
|
1634 |
* the following inline code handles some common cases, |
|
1635 |
* and exists as an optimization, to speed up those cases. |
|
1636 |
* |
|
1637 |
* Conceptionally, (and for most other argument types), |
|
1638 |
* mixed arithmetic is implemented by double dispatching |
|
1639 |
* (see the message send at the bottom) |
|
1640 |
*/ |
|
7355 | 1641 |
if (__isSmallInteger(aNumber)) { |
24969 | 1642 |
RETURN ( (__floatVal(self) <= (double)(__intVal(aNumber))) ? true : false ); |
7355 | 1643 |
} |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
1644 |
if (aNumber != nil) { |
24969 | 1645 |
if (__qIsFloatLike(aNumber)) { |
1646 |
RETURN ( (__floatVal(self) <= __floatVal(aNumber)) ? true : false ); |
|
1647 |
} |
|
1648 |
if (__qIsShortFloat(aNumber)) { |
|
1649 |
RETURN ( (__floatVal(self) <= (double)(__shortFloatVal(aNumber))) ? true : false ); |
|
1650 |
} |
|
1 | 1651 |
} |
18296 | 1652 |
#endif |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
1653 |
%}. |
1 | 1654 |
^ self retry:#<= coercing:aNumber |
1655 |
! |
|
1656 |
||
1657 |
= aNumber |
|
7355 | 1658 |
"return true, if the argument represents the same numeric value |
1659 |
as the receiver, false otherwise" |
|
1 | 1660 |
|
1661 |
%{ /* NOCONTEXT */ |
|
18296 | 1662 |
#ifdef __SCHTEAM__ |
1663 |
if (aNumber.isNumber()) { |
|
24969 | 1664 |
return context._RETURN( self.eqNrP(aNumber) ); |
18296 | 1665 |
} |
1666 |
#else |
|
13 | 1667 |
|
11945 | 1668 |
/* |
1669 |
* notice: |
|
1670 |
* the following inline code handles some common cases, |
|
1671 |
* and exists as an optimization, to speed up those cases. |
|
1672 |
* |
|
1673 |
* Conceptionally, (and for most other argument types), |
|
1674 |
* mixed arithmetic is implemented by double dispatching |
|
1675 |
* (see the message send at the bottom) |
|
1676 |
*/ |
|
7355 | 1677 |
if (__isSmallInteger(aNumber)) { |
24969 | 1678 |
RETURN ( (__floatVal(self) == (double)(__intVal(aNumber))) ? true : false ); |
7355 | 1679 |
} |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
1680 |
if (aNumber != nil) { |
24969 | 1681 |
if (__qIsFloatLike(aNumber)) { |
1682 |
RETURN ( (__floatVal(self) == __floatVal(aNumber)) ? true : false ); |
|
1683 |
} |
|
1684 |
if (__qIsShortFloat(aNumber)) { |
|
1685 |
RETURN ( (__floatVal(self) == (double)(__shortFloatVal(aNumber))) ? true : false ); |
|
1686 |
} |
|
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
1687 |
} else { |
24969 | 1688 |
RETURN (false); |
1 | 1689 |
} |
18296 | 1690 |
#endif |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
1691 |
%}. |
7355 | 1692 |
^ aNumber equalFromFloat:self |
1 | 1693 |
! |
1694 |
||
701 | 1695 |
> aNumber |
1696 |
"return true, if the argument is less" |
|
1 | 1697 |
|
1698 |
%{ /* NOCONTEXT */ |
|
18296 | 1699 |
#ifdef __SCHTEAM__ |
1700 |
if (aNumber.isNumber()) { |
|
24969 | 1701 |
return context._RETURN( self.gtP(aNumber) ); |
18296 | 1702 |
} |
1703 |
#else |
|
11945 | 1704 |
/* |
1705 |
* notice: |
|
1706 |
* the following inline code handles some common cases, |
|
1707 |
* and exists as an optimization, to speed up those cases. |
|
1708 |
* |
|
1709 |
* Conceptionally, (and for most other argument types), |
|
1710 |
* mixed arithmetic is implemented by double dispatching |
|
1711 |
* (see the message send at the bottom) |
|
1712 |
*/ |
|
7355 | 1713 |
if (__isSmallInteger(aNumber)) { |
24969 | 1714 |
RETURN ( (__floatVal(self) > (double)(__intVal(aNumber))) ? true : false ); |
7355 | 1715 |
} |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
1716 |
if (aNumber != nil) { |
24969 | 1717 |
if (__qIsFloatLike(aNumber)) { |
1718 |
RETURN ( (__floatVal(self) > __floatVal(aNumber)) ? true : false ); |
|
1719 |
} |
|
1720 |
if (__qIsShortFloat(aNumber)) { |
|
1721 |
RETURN ( (__floatVal(self) > (double)(__shortFloatVal(aNumber))) ? true : false ); |
|
1722 |
} |
|
1 | 1723 |
} |
18296 | 1724 |
#endif |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
1725 |
%}. |
701 | 1726 |
^ self retry:#> coercing:aNumber |
1727 |
! |
|
1728 |
||
1729 |
>= aNumber |
|
1730 |
"return true, if the argument is less or equal" |
|
1731 |
||
1732 |
%{ /* NOCONTEXT */ |
|
18296 | 1733 |
#ifdef __SCHTEAM__ |
1734 |
if (aNumber.isNumber()) { |
|
24969 | 1735 |
return context._RETURN( self.geP(aNumber) ); |
18296 | 1736 |
} |
1737 |
#else |
|
11945 | 1738 |
/* |
1739 |
* notice: |
|
1740 |
* the following inline code handles some common cases, |
|
1741 |
* and exists as an optimization, to speed up those cases. |
|
1742 |
* |
|
1743 |
* Conceptionally, (and for most other argument types), |
|
1744 |
* mixed arithmetic is implemented by double dispatching |
|
1745 |
* (see the message send at the bottom) |
|
1746 |
*/ |
|
7355 | 1747 |
if (__isSmallInteger(aNumber)) { |
24969 | 1748 |
RETURN ( (__floatVal(self) >= (double)(__intVal(aNumber))) ? true : false ); |
7355 | 1749 |
} |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
1750 |
if (aNumber != nil) { |
24969 | 1751 |
if (__qIsFloatLike(aNumber)) { |
1752 |
RETURN ( (__floatVal(self) >= __floatVal(aNumber)) ? true : false ); |
|
1753 |
} |
|
1754 |
if (__qIsShortFloat(aNumber)) { |
|
1755 |
RETURN ( (__floatVal(self) >= (double)(__shortFloatVal(aNumber))) ? true : false ); |
|
1756 |
} |
|
701 | 1757 |
} |
18296 | 1758 |
#endif |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
1759 |
%}. |
701 | 1760 |
^ self retry:#>= coercing:aNumber |
92 | 1761 |
! |
1762 |
||
1763 |
hash |
|
1764 |
"return a number for hashing; redefined, since floats compare |
|
1765 |
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
|
1766 |
as 3 hash." |
92 | 1767 |
|
305 | 1768 |
|i| |
92 | 1769 |
|
1770 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
24969 | 1771 |
i := self asInteger. |
1772 |
self = i ifTrue:[ |
|
1773 |
^ i hash |
|
1774 |
]. |
|
92 | 1775 |
]. |
1776 |
||
1777 |
" |
|
305 | 1778 |
mhmh take some of my value-bits to hash on |
92 | 1779 |
" |
4590 | 1780 |
^ (((self basicAt:8) bitAnd:16r1F) bitShift:24) + |
305 | 1781 |
((self basicAt:7) bitShift:16) + |
1782 |
((self basicAt:6) bitShift:8) + |
|
4592 | 1783 |
(((self basicAt:5) + (self basicAt:1) + (self basicAt:2)) bitAnd:16rFF) |
92 | 1784 |
|
1785 |
" |
|
11945 | 1786 |
3 hash |
92 | 1787 |
3.0 hash |
11945 | 1788 |
3.1 hash |
1789 |
3.14159 hash |
|
1790 |
31.4159 hash |
|
1791 |
3.141591 hash |
|
1792 |
1.234567890123456 hash |
|
1793 |
1.234567890123457 hash |
|
92 | 1794 |
Set withAll:#(3 3.0 99 99.0 3.1415) |
1795 |
" |
|
1 | 1796 |
! |
1797 |
||
17199 | 1798 |
isAlmostEqualTo:aNumber nEpsilon:nE |
16658 | 1799 |
"return true, if the argument, aNumber represents almost the same numeric value |
1800 |
as the receiver, false otherwise. |
|
1801 |
||
1802 |
nE is the number of minimal float distances, that the numbers may differ and |
|
22578 | 1803 |
still be considered equal. |
22559 | 1804 |
That is an integer, which counts the delta-steps in the mantissa. |
1805 |
Notice, that the absolute value of the epsilon depends on the receiver |
|
1806 |
(i.e. with a receiver of 10^17, a mantissa-step is much larger than with 10^-17 as receiver) |
|
16658 | 1807 |
|
17199 | 1808 |
For background information why floats need this |
16658 | 1809 |
read: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ |
1810 |
" |
|
1811 |
||
21148 | 1812 |
Epsilon isNil ifTrue:[ |
24969 | 1813 |
Epsilon := self class computeEpsilon. |
21148 | 1814 |
]. |
1815 |
||
16658 | 1816 |
%{ /* NOCONTEXT */ |
1817 |
||
1818 |
/* |
|
1819 |
* notice: |
|
1820 |
* the following inline code handles some common cases, |
|
1821 |
* and exists as an optimization, to speed up those cases. |
|
1822 |
* |
|
1823 |
* Conceptionally, (and for most other argument types), |
|
1824 |
* mixed arithmetic is implemented by double dispatching |
|
1825 |
* (see the message send at the bottom) |
|
1826 |
*/ |
|
1827 |
||
1828 |
INT64 ulpDiff; |
|
1829 |
union { |
|
24969 | 1830 |
double d; |
1831 |
INT64 i; |
|
16658 | 1832 |
} myself, otherFloat; |
1833 |
int nEpsilon; |
|
1834 |
double scaledEpsilon; |
|
1835 |
||
1836 |
if (!__isSmallInteger(nE)) { |
|
24969 | 1837 |
goto tryHarder; |
16658 | 1838 |
} |
1839 |
||
1840 |
nEpsilon = __intVal(nE); |
|
1841 |
scaledEpsilon = nEpsilon *__floatVal(@global(Epsilon)); |
|
1842 |
||
1843 |
if (__isSmallInteger(aNumber)) { |
|
24969 | 1844 |
otherFloat.d = (double)(__intVal(aNumber)); |
16658 | 1845 |
} else if (aNumber == nil) { |
24969 | 1846 |
RETURN(false) |
16658 | 1847 |
} else if (__qIsFloatLike(aNumber)) { |
24969 | 1848 |
otherFloat.d = (double)(__floatVal(aNumber)); |
16658 | 1849 |
} else if (__qIsShortFloat(aNumber)) { |
24969 | 1850 |
otherFloat.d = (double)(__shortFloatVal(aNumber)); |
16658 | 1851 |
} else { |
24969 | 1852 |
goto tryHarder; |
16658 | 1853 |
} |
1854 |
||
1855 |
myself.d = __floatVal(self); |
|
1856 |
||
1857 |
// Check if the numbers are really close -- needed |
|
1858 |
// when comparing numbers near zero (ULP method below fails for numbers near 0!). |
|
1859 |
if (fabs(myself.d - otherFloat.d) <= scaledEpsilon) { |
|
24969 | 1860 |
RETURN(true); |
16658 | 1861 |
} |
1862 |
||
1863 |
// if the signs differ, the numbers are different |
|
16667 | 1864 |
if ((myself.d >= 0) != (otherFloat.d >= 0)) { |
24969 | 1865 |
RETURN(false); |
16658 | 1866 |
} |
1867 |
||
1868 |
// compute the difference of the 'units in the last place" ULP |
|
1869 |
// (if ulpDiff == 1, two floats are adjecant) |
|
1870 |
ulpDiff = myself.i - otherFloat.i; |
|
1871 |
if (ulpDiff < 0) ulpDiff = -ulpDiff; |
|
1872 |
if (ulpDiff <= nEpsilon) { |
|
24969 | 1873 |
RETURN(true); |
16658 | 1874 |
} else { |
24969 | 1875 |
RETURN(false) |
16658 | 1876 |
} |
1877 |
||
1878 |
tryHarder:; |
|
1879 |
%}. |
|
22559 | 1880 |
nE isInteger ifFalse:[ |
24969 | 1881 |
self error:'nEpsilon argument must be an integer'. |
22559 | 1882 |
]. |
16658 | 1883 |
^ aNumber isAlmostEqualToFromFloat:self nEpsilon:nE |
1884 |
||
1885 |
" |
|
24969 | 1886 |
67329.234 isAlmostEqualTo:67329.23400000001 nEpsilon:1 |
1887 |
1.0 isAlmostEqualTo:1.0001 nEpsilon:1 |
|
1888 |
1.0 isAlmostEqualTo:-1.0 nEpsilon:1 |
|
1889 |
1 isAlmostEqualTo:1.000000000000001 nEpsilon:1 |
|
1890 |
1 isAlmostEqualTo:1.000000000000001 nEpsilon:10 |
|
1891 |
1.0 isAlmostEqualTo:1 nEpsilon:1 |
|
1892 |
1.0 isAlmostEqualTo:1 asFraction nEpsilon:1 |
|
1893 |
0.0 isAlmostEqualTo:0 nEpsilon:1 |
|
1894 |
0.0 isAlmostEqualTo:self epsilon nEpsilon:1 |
|
16658 | 1895 |
" |
22854 | 1896 |
|
1897 |
"Modified: / 10-05-2018 / 00:48:57 / stefan" |
|
16658 | 1898 |
! |
1899 |
||
701 | 1900 |
~= aNumber |
1901 |
"return true, if the arguments value are not equal" |
|
1 | 1902 |
|
1903 |
%{ /* NOCONTEXT */ |
|
1904 |
||
11945 | 1905 |
/* |
1906 |
* notice: |
|
1907 |
* the following inline code handles some common cases, |
|
1908 |
* and exists as an optimization, to speed up those cases. |
|
1909 |
* |
|
1910 |
* Conceptionally, (and for most other argument types), |
|
1911 |
* mixed arithmetic is implemented by double dispatching |
|
1912 |
* (see the message send at the bottom) |
|
1913 |
*/ |
|
7355 | 1914 |
if (__isSmallInteger(aNumber)) { |
24969 | 1915 |
RETURN ( (__floatVal(self) != (double)(__intVal(aNumber))) ? true : false ); |
7355 | 1916 |
} |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
1917 |
if (aNumber != nil) { |
24969 | 1918 |
if (__qIsFloatLike(aNumber)) { |
1919 |
RETURN ( (__floatVal(self) != __floatVal(aNumber)) ? true : false ); |
|
1920 |
} |
|
1921 |
if (__qIsShortFloat(aNumber)) { |
|
1922 |
RETURN ( (__floatVal(self) != (double)(__shortFloatVal(aNumber))) ? true : false ); |
|
1923 |
} |
|
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
1924 |
} else { |
24969 | 1925 |
RETURN ( true ); |
1 | 1926 |
} |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
1927 |
%}. |
7355 | 1928 |
^ super ~= aNumber |
1 | 1929 |
! ! |
1930 |
||
24191 | 1931 |
!Float methodsFor:'copying'! |
1932 |
||
1933 |
deepCopy |
|
1934 |
"return a deep copy of myself |
|
1935 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
1936 |
||
1937 |
^ self |
|
1938 |
! |
|
1939 |
||
1940 |
deepCopyUsing:aDictionary postCopySelector:postCopySelector |
|
1941 |
"return a deep copy of myself |
|
1942 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
1943 |
||
1944 |
^ self |
|
1945 |
! |
|
1946 |
||
1947 |
shallowCopy |
|
1948 |
"return a shallow copy of the receiver" |
|
1949 |
||
1950 |
^ self |
|
1951 |
! |
|
1952 |
||
1953 |
simpleDeepCopy |
|
1954 |
"return a deep copy of the receiver |
|
1955 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
1956 |
||
1957 |
^ self |
|
1958 |
! ! |
|
1959 |
||
1 | 1960 |
!Float methodsFor:'mathematical functions'! |
1961 |
||
17218 | 1962 |
cbrt |
1963 |
"return the cubic root of myself." |
|
1964 |
||
1965 |
%{ /* NOCONTEXT */ |
|
1966 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
1967 |
#ifndef __win32__ /* seems to be not avail. in WIN32 math lib */ |
17218 | 1968 |
double val, rslt; |
1969 |
OBJ newFloat; |
|
1970 |
||
1971 |
val = __floatVal(self); |
|
1972 |
||
1973 |
{ |
|
25002 | 1974 |
__threadErrno = 0; |
1975 |
rslt = cbrt(val); |
|
1976 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
|
1977 |
{ |
|
1978 |
if (__threadErrno == 0) { |
|
1979 |
__qMKFLOAT(newFloat, rslt); |
|
1980 |
RETURN ( newFloat ); |
|
1981 |
} |
|
1982 |
} |
|
17218 | 1983 |
} |
1984 |
# endif /* WIN32 */ |
|
1985 |
%}. |
|
1986 |
^ super cbrt |
|
1987 |
||
1988 |
" |
|
1989 |
8 cbrt |
|
1990 |
-8 cbrt |
|
25002 | 1991 |
8.0 cbrt |
1992 |
-8.0 cbrt |
|
1993 |
8.0 raisedTo:(1/3) |
|
1994 |
-8.0 raisedTo:(1/3) |
|
17218 | 1995 |
" |
1996 |
! |
|
1997 |
||
701 | 1998 |
exp |
1999 |
"return e raised to the power of the receiver" |
|
2000 |
||
2001 |
%{ /* NOCONTEXT */ |
|
2002 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2003 |
double rslt; |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2004 |
OBJ newFloat; |
701 | 2005 |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
2006 |
__threadErrno = 0; |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2007 |
rslt = exp(__floatVal(self)); |
7393 | 2008 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
2009 |
{ |
|
24969 | 2010 |
if (__threadErrno == 0) { |
2011 |
__qMKFLOAT(newFloat, rslt); |
|
2012 |
RETURN ( newFloat ); |
|
2013 |
} |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2014 |
} |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2015 |
%}. |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3279
diff
changeset
|
2016 |
^ self class |
24969 | 2017 |
raise:#domainErrorSignal |
2018 |
receiver:self |
|
2019 |
selector:#exp |
|
2020 |
arguments:#() |
|
2021 |
errorString:'bad receiver in exp' |
|
6195 | 2022 |
|
2023 |
"Modified: / 16.11.2001 / 14:14:29 / cg" |
|
701 | 2024 |
! |
2025 |
||
21832 | 2026 |
ldexp:exp |
2027 |
"multiply the receiver by an integral power of 2. |
|
24974 | 2028 |
I.e. return self * (2 ^ exp). |
2029 |
This is also the operation to reconstruct the original float from its |
|
2030 |
mantissa and exponent: (f mantissa ldexp:f exponent) = f" |
|
21832 | 2031 |
|
2032 |
%{ /* NOCONTEXT */ |
|
2033 |
#ifndef __SCHTEAM__ |
|
2034 |
double val, rslt; |
|
2035 |
OBJ newFloat; |
|
2036 |
||
2037 |
if (__isSmallInteger(exp)) { |
|
25002 | 2038 |
int __exp = __intVal(exp); |
2039 |
||
2040 |
val = __floatVal(self); |
|
2041 |
||
2042 |
{ |
|
2043 |
__threadErrno = 0; |
|
2044 |
rslt = ldexp(val, __exp); |
|
2045 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
|
2046 |
{ |
|
2047 |
if (__threadErrno == 0) { |
|
2048 |
__qMKFLOAT(newFloat, rslt); |
|
2049 |
RETURN ( newFloat ); |
|
2050 |
} |
|
2051 |
} |
|
2052 |
} |
|
21832 | 2053 |
} |
2054 |
#endif |
|
2055 |
%}. |
|
21835 | 2056 |
^ super ldexp:exp |
21832 | 2057 |
|
2058 |
" |
|
25002 | 2059 |
1.0 mantissa ldexp:1.0 exponent |
2060 |
-1.0 mantissa ldexp:-1.0 exponent |
|
24974 | 2061 |
|
24958 | 2062 |
1.0 ldexp:16 -> 65536.0 |
2063 |
1.0 ldexp:100 -> 1.26765060022823E+30 |
|
2064 |
1 * (2 raisedToInteger:100) -> 1267650600228229401496703205376 |
|
2065 |
1.0 ldexp:200 -> 1.60693804425899E+60 |
|
21832 | 2066 |
" |
2067 |
||
2068 |
"Created: / 19-06-2017 / 01:42:22 / cg" |
|
21835 | 2069 |
"Modified: / 19-06-2017 / 09:16:32 / cg" |
21832 | 2070 |
! |
2071 |
||
1 | 2072 |
ln |
6193 | 2073 |
"return the natural logarithm of myself. |
2074 |
Raises an exception, if the receiver is less or equal to zero." |
|
1 | 2075 |
|
2076 |
%{ /* NOCONTEXT */ |
|
18296 | 2077 |
#ifdef __SCHTEAM__ |
18299 | 2078 |
return context._RETURN( self.log() ); |
18296 | 2079 |
#else |
1 | 2080 |
|
6189
f4bfc2dacdb7
win32: care for some math errors
Claus Gittinger <cg@exept.de>
parents:
6145
diff
changeset
|
2081 |
double val, rslt; |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2082 |
OBJ newFloat; |
1 | 2083 |
|
6189
f4bfc2dacdb7
win32: care for some math errors
Claus Gittinger <cg@exept.de>
parents:
6145
diff
changeset
|
2084 |
val = __floatVal(self); |
f4bfc2dacdb7
win32: care for some math errors
Claus Gittinger <cg@exept.de>
parents:
6145
diff
changeset
|
2085 |
|
22578 | 2086 |
/* |
2087 |
* to suppress the warnBox opened by win32 |
|
21952 | 2088 |
* to avoid returning -INF from some unix math libs (__osx__) |
2089 |
*/ |
|
2090 |
if (val > 0.0) { |
|
24969 | 2091 |
__threadErrno = 0; |
2092 |
rslt = log(val); |
|
2093 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
|
2094 |
{ |
|
2095 |
if (__threadErrno == 0) { |
|
2096 |
__qMKFLOAT(newFloat, rslt); |
|
2097 |
RETURN ( newFloat ); |
|
2098 |
} |
|
2099 |
} |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2100 |
} |
18296 | 2101 |
#endif |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2102 |
%}. |
142 | 2103 |
" |
2104 |
an invalid value for logarithm |
|
21970 | 2105 |
if you need -INF for a zero receiver, try Number trapInfinity:[...] |
142 | 2106 |
" |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3279
diff
changeset
|
2107 |
^ self class |
24969 | 2108 |
raise:(self = 0 ifTrue:[#infiniteResultSignal] ifFalse:[#domainErrorSignal]) |
2109 |
receiver:self |
|
2110 |
selector:#ln |
|
2111 |
arguments:#() |
|
2112 |
errorString:'bad receiver in ln (not strictly positive)' |
|
21952 | 2113 |
|
21970 | 2114 |
"Modified (comment): / 03-07-2017 / 15:58:17 / cg" |
1 | 2115 |
! |
2116 |
||
7373 | 2117 |
log10 |
21824 | 2118 |
"return the base-10 logarithm of the receiver. |
7373 | 2119 |
Raises an exception, if the receiver is less or equal to zero." |
2120 |
||
2121 |
%{ /* NOCONTEXT */ |
|
18296 | 2122 |
#ifdef __SCHTEAM__ |
18299 | 2123 |
return context._RETURN( self.log10() ); |
18296 | 2124 |
#else |
7373 | 2125 |
|
2126 |
double val, rslt; |
|
2127 |
OBJ newFloat; |
|
2128 |
||
2129 |
val = __floatVal(self); |
|
2130 |
||
22578 | 2131 |
/* |
2132 |
* to suppress the warnBox opened by win32 |
|
21953 | 2133 |
* to avoid returning -INF from some unix math libs (__osx__) |
2134 |
*/ |
|
2135 |
if (val > 0.0) { |
|
24969 | 2136 |
__threadErrno = 0; |
2137 |
rslt = log10(val); |
|
2138 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
|
2139 |
{ |
|
2140 |
if (__threadErrno == 0) { |
|
2141 |
__qMKFLOAT(newFloat, rslt); |
|
2142 |
RETURN ( newFloat ); |
|
2143 |
} |
|
2144 |
} |
|
7373 | 2145 |
} |
18296 | 2146 |
#endif |
7373 | 2147 |
%}. |
2148 |
" |
|
2149 |
an invalid value for logarithm |
|
21970 | 2150 |
if you need -INF for a zero receiver, try Number trapInfinity:[...] |
7373 | 2151 |
" |
2152 |
^ self class |
|
24969 | 2153 |
raise:(self = 0 ifTrue:[#infiniteResultSignal] ifFalse:[#domainErrorSignal]) |
2154 |
receiver:self |
|
2155 |
selector:#log10 |
|
2156 |
arguments:#() |
|
2157 |
errorString:'bad receiver in log10 (not strictly positive)' |
|
21953 | 2158 |
|
21970 | 2159 |
"Modified (comment): / 03-07-2017 / 15:58:21 / cg" |
7373 | 2160 |
! |
2161 |
||
1 | 2162 |
raisedTo:aNumber |
2163 |
"return self raised to the power of aNumber" |
|
3127
590c7d2bb4b9
fixes isNAN (on linux); added #isInfinite
Claus Gittinger <cg@exept.de>
parents:
3118
diff
changeset
|
2164 |
|
1 | 2165 |
%{ |
21936 | 2166 |
double _n, rslt; |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2167 |
OBJ newFloat; |
1 | 2168 |
|
21936 | 2169 |
if (__isFloatLike(aNumber)) { |
24969 | 2170 |
_n = __floatVal(aNumber); |
21936 | 2171 |
} else if (__isSmallInteger(aNumber)) { |
24969 | 2172 |
_n = (double)__intVal(aNumber); |
21936 | 2173 |
} else { |
24969 | 2174 |
goto notEasy; |
22578 | 2175 |
} |
21936 | 2176 |
__threadErrno = 0; |
2177 |
rslt = pow(__floatVal(self), _n); |
|
2178 |
if (! isnan(rslt)) { /* Currently all our systems support isnan() */ |
|
24969 | 2179 |
if (__threadErrno == 0) { |
2180 |
__qMKFLOAT(newFloat, rslt); |
|
2181 |
RETURN ( newFloat ); |
|
2182 |
} |
|
1 | 2183 |
} |
22578 | 2184 |
notEasy: ; |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2185 |
%}. |
21913 | 2186 |
"/ the c-library pow function, has a bug: |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19358
diff
changeset
|
2187 |
"/ it does not deal correctly with negative numbers. |
19091 | 2188 |
"/ I.e. it raises an error on -8^(1/3) instead of returning a negative -2 |
2189 |
"/ work around with a kludge: |
|
2190 |
self < 0 ifTrue:[ |
|
24969 | 2191 |
^ (self negated raisedTo:aNumber) negated |
19091 | 2192 |
]. |
2193 |
||
21936 | 2194 |
^ aNumber raisedFromFloat:self |
2195 |
||
2196 |
"Modified: / 01-07-2017 / 21:58:26 / cg" |
|
1 | 2197 |
! |
2198 |
||
24969 | 2199 |
reciprocalLogBase2 |
2200 |
"optimized for self = 10, for use in conversion for printing" |
|
2201 |
||
25002 | 2202 |
^ self = 10.0s |
2203 |
ifTrue: [Ln2 / Ln10] |
|
2204 |
ifFalse: [Ln2 / self ln] |
|
24969 | 2205 |
! |
2206 |
||
1 | 2207 |
sqrt |
6193 | 2208 |
"return the square root of myself. |
2209 |
Raises an exception, if the receiver is less than zero." |
|
1 | 2210 |
|
2211 |
%{ /* NOCONTEXT */ |
|
2212 |
||
6189
f4bfc2dacdb7
win32: care for some math errors
Claus Gittinger <cg@exept.de>
parents:
6145
diff
changeset
|
2213 |
double val, rslt; |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2214 |
OBJ newFloat; |
1 | 2215 |
|
6189
f4bfc2dacdb7
win32: care for some math errors
Claus Gittinger <cg@exept.de>
parents:
6145
diff
changeset
|
2216 |
val = __floatVal(self); |
f4bfc2dacdb7
win32: care for some math errors
Claus Gittinger <cg@exept.de>
parents:
6145
diff
changeset
|
2217 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
2218 |
#ifdef __win32__ /* to suppress the warnBox opened by win32 */ |
6189
f4bfc2dacdb7
win32: care for some math errors
Claus Gittinger <cg@exept.de>
parents:
6145
diff
changeset
|
2219 |
if (val >= 0.0) |
3889
3466554b639f
manually check receiver for beeing >= 0 under WINDOWS
Claus Gittinger <cg@exept.de>
parents:
3632
diff
changeset
|
2220 |
#endif |
3466554b639f
manually check receiver for beeing >= 0 under WINDOWS
Claus Gittinger <cg@exept.de>
parents:
3632
diff
changeset
|
2221 |
{ |
24969 | 2222 |
__threadErrno = 0; |
2223 |
rslt = sqrt(val); |
|
2224 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
|
2225 |
{ |
|
2226 |
if (__threadErrno == 0) { |
|
2227 |
__qMKFLOAT(newFloat, rslt); |
|
2228 |
RETURN ( newFloat ); |
|
2229 |
} |
|
2230 |
} |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2231 |
} |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2232 |
%}. |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3279
diff
changeset
|
2233 |
^ self class |
24969 | 2234 |
raise:#imaginaryResultSignal |
2235 |
receiver:self |
|
2236 |
selector:#sqrt |
|
2237 |
arguments:#() |
|
2238 |
errorString:'bad (negative) receiver in sqrt' |
|
3399
4f45ebb7bc60
oops - linux does not set errNo on some domainErrors;
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
2239 |
|
4f45ebb7bc60
oops - linux does not set errNo on some domainErrors;
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
2240 |
" |
4f45ebb7bc60
oops - linux does not set errNo on some domainErrors;
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
2241 |
10 sqrt |
4f45ebb7bc60
oops - linux does not set errNo on some domainErrors;
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
2242 |
-10 sqrt |
4f45ebb7bc60
oops - linux does not set errNo on some domainErrors;
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
2243 |
" |
6193 | 2244 |
|
6195 | 2245 |
"Modified: / 16.11.2001 / 14:14:43 / cg" |
13 | 2246 |
! ! |
2247 |
||
92 | 2248 |
!Float methodsFor:'printing & storing'! |
1 | 2249 |
|
22301 | 2250 |
printOn:aStream |
2251 |
"append a printed representation of the receiver to |
|
2252 |
the argument, aStream. |
|
2253 |
||
2254 |
I use #printString instead of #printOn: as basic print mechanism." |
|
2255 |
||
2256 |
aStream nextPutAll:self printString |
|
2257 |
||
2258 |
"Created: / 10-10-2017 / 14:05:13 / cg" |
|
2259 |
! |
|
2260 |
||
1 | 2261 |
printString |
2358 | 2262 |
"return a printed representation of the receiver; |
2263 |
if not specified otherwise (by setting DefaultPrintFormat), |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3163
diff
changeset
|
2264 |
6 valid digits are printed. |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3163
diff
changeset
|
2265 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3163
diff
changeset
|
2266 |
#printOn: as basic print mechanism." |
1 | 2267 |
|
21868 | 2268 |
^ self printStringWithFormat:self class defaultPrintFormat |
18138 | 2269 |
|
2270 |
" |
|
24969 | 2271 |
Float pi printString. |
2272 |
1.0 printString |
|
2273 |
1.234 printString |
|
2274 |
1e10 printString |
|
2275 |
1e-60 printString |
|
2276 |
1.2e3 printString |
|
2277 |
1.2e30 printString |
|
2278 |
(1.0 uncheckedDivide:0) printString |
|
2279 |
(0.0 uncheckedDivide:0) printString |
|
2280 |
self pi printString. |
|
2281 |
||
2282 |
DecimalPointCharacter := $,. |
|
2283 |
1.234 printString. |
|
2284 |
1.0 printString. |
|
2285 |
1e10 printString. |
|
2286 |
1.2e3 printString. |
|
2287 |
1.2e30 printString. |
|
2288 |
(1.0 uncheckedDivide:0) printString. |
|
2289 |
(0.0 uncheckedDivide:0) printString. |
|
2290 |
DecimalPointCharacter := $. |
|
18138 | 2291 |
" |
21877 | 2292 |
|
2293 |
"Modified (comment): / 21-06-2017 / 09:46:01 / cg" |
|
18138 | 2294 |
! |
2295 |
||
2296 |
printStringWithFormat:format |
|
2297 |
"return a printed representation of the receiver; |
|
2298 |
fmt must be of the form: .nn, where nn is the number of digits. |
|
2299 |
To print 6 valid digits, use printStringWithFormat:'.6' |
|
2300 |
For Floats, the default used in printString, is 15 (because its a double); |
|
2301 |
for ShortFloats, it is 6 (because it is a float)" |
|
2302 |
||
1 | 2303 |
%{ /* NOCONTEXT */ |
2304 |
||
13 | 2305 |
char buffer[64]; |
1 | 2306 |
REGISTER char *cp; |
314 | 2307 |
OBJ s; |
2358 | 2308 |
char *fmt; |
2309 |
char fmtBuffer[20]; |
|
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8302
diff
changeset
|
2310 |
int len; |
2358 | 2311 |
|
18138 | 2312 |
if (__isStringLike(format)) { |
24969 | 2313 |
fmt = (char *) __stringVal(format); |
2358 | 2314 |
} else { |
24969 | 2315 |
/* |
2316 |
* in case we get called with garbage ... |
|
2317 |
*/ |
|
2318 |
fmt = ".15"; |
|
2358 | 2319 |
} |
2320 |
||
2321 |
/* |
|
2322 |
* build a printf format string |
|
2323 |
*/ |
|
2324 |
fmtBuffer[0] = '%'; |
|
2325 |
strncpy(fmtBuffer+1, fmt, 10); |
|
2326 |
#ifdef SYSV |
|
2327 |
strcat(fmtBuffer, "lg"); |
|
2328 |
#else |
|
2329 |
strcat(fmtBuffer, "G"); |
|
2330 |
#endif |
|
314 | 2331 |
|
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8302
diff
changeset
|
2332 |
__BEGIN_PROTECT_REGISTERS__ |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8302
diff
changeset
|
2333 |
len = snprintf(buffer, sizeof(buffer), fmtBuffer, __floatVal(self)); |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8302
diff
changeset
|
2334 |
__END_PROTECT_REGISTERS__ |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8302
diff
changeset
|
2335 |
|
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8302
diff
changeset
|
2336 |
if (len >= 0 && len < sizeof(buffer)-3) { |
24969 | 2337 |
/* |
2338 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
|
2339 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
|
2340 |
*/ |
|
2341 |
for (cp = buffer; *cp; cp++) { |
|
2342 |
if ((*cp == '.') || (*cp == ',') || (*cp == 'E') || (*cp == 'e')) break; |
|
2343 |
} |
|
2344 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
|
2345 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
|
2346 |
*cp++ = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
|
2347 |
} else { |
|
2348 |
*cp++ = '.'; |
|
2349 |
} |
|
2350 |
*cp++ = '0'; |
|
2351 |
*cp = '\0'; |
|
2352 |
} else { |
|
2353 |
if (cp && ((*cp == '.') || (*cp == ','))) { |
|
2354 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
|
2355 |
*cp = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
|
2356 |
} |
|
2357 |
} |
|
2358 |
} |
|
2359 |
||
2360 |
s = __MKSTRING(buffer); |
|
2361 |
if (s != nil) { |
|
2362 |
RETURN (s); |
|
2363 |
} |
|
314 | 2364 |
} |
2365 |
%}. |
|
2366 |
" |
|
2367 |
memory allocation (for the new string) failed. |
|
2368 |
When we arrive here, there was no memory, even after a garbage collect. |
|
2369 |
This means, that the VM wanted to get some more memory from the |
|
2370 |
OS, which was not kind enough to give it. |
|
2371 |
Bad luck - you should increase the swap space on your machine. |
|
2372 |
" |
|
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
20415
diff
changeset
|
2373 |
^ AllocationFailure raise. |
314 | 2374 |
|
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2375 |
" |
24969 | 2376 |
1.0 printString |
2377 |
1.234 printString |
|
2378 |
1e10 printString |
|
2379 |
1.2e3 printString |
|
2380 |
1.2e30 printString |
|
2381 |
(1.0 uncheckedDivide:0) printString |
|
2382 |
(0.0 uncheckedDivide:0) printString |
|
2383 |
self pi printString. |
|
2384 |
||
2385 |
DecimalPointCharacter := $,. |
|
2386 |
1.234 printString. |
|
2387 |
1.0 printString. |
|
2388 |
1e10 printString. |
|
2389 |
1.2e3 printString. |
|
2390 |
1.2e30 printString. |
|
2391 |
(1.0 uncheckedDivide:0) printString. |
|
2392 |
(0.0 uncheckedDivide:0) printString. |
|
2393 |
DecimalPointCharacter := $. |
|
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2394 |
" |
1 | 2395 |
! |
2396 |
||
2397 |
printfPrintString:formatString |
|
24690 | 2398 |
<unsave> |
2399 |
||
7746 | 2400 |
"non-standard: return a printed representation of the receiver |
1 | 2401 |
as specified by formatString, which is defined by printf. |
22578 | 2402 |
|
31 | 2403 |
If you use this, be aware, that specifying doubles differs on |
11945 | 2404 |
systems; on SYSV machines you have to give something like %lf, |
13 | 2405 |
while on BSD systems the format string has to be %F. |
31 | 2406 |
Also, the resulting string may not be longer than 255 bytes - |
20415 | 2407 |
since that's the (static) size of the buffer. |
21909 | 2408 |
|
11926
295e8bb99fde
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11733
diff
changeset
|
2409 |
This method is NONSTANDARD and may be removed without notice. |
24690 | 2410 |
|
2411 |
WARNING: this goes directly to the C-printf function and may therefore me inherently unsafe. |
|
11926
295e8bb99fde
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11733
diff
changeset
|
2412 |
Please use the printf: method, which is safe as it is completely implemented in Smalltalk." |
1 | 2413 |
|
25008 | 2414 |
%{ /* STACK: 16000 */ |
24690 | 2415 |
#ifndef __SCHTEAM__ |
25008 | 2416 |
char buffer[1024]; |
314 | 2417 |
OBJ s; |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8302
diff
changeset
|
2418 |
int len; |
1 | 2419 |
|
12478
e8051030cda6
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12423
diff
changeset
|
2420 |
if (__isStringLike(formatString)) { |
25008 | 2421 |
/* |
2422 |
* actually only needed on sparc: since thisContext is |
|
2423 |
* in a global register, which gets destroyed by printf, |
|
2424 |
* manually save it here - very stupid ... |
|
2425 |
*/ |
|
2426 |
__BEGIN_PROTECT_REGISTERS__ |
|
2427 |
||
2428 |
len = snprintf(buffer, sizeof(buffer), __stringVal(formatString), __floatVal(self)); |
|
2429 |
||
2430 |
__END_PROTECT_REGISTERS__ |
|
2431 |
||
2432 |
if (len < 0) goto fail; |
|
2433 |
if (len >= sizeof(buffer)) goto fail; |
|
2434 |
||
2435 |
s = __MKSTRING_L(buffer, len); |
|
2436 |
if (s != nil) { |
|
2437 |
RETURN (s); |
|
2438 |
} |
|
1 | 2439 |
} |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8302
diff
changeset
|
2440 |
fail: ; |
24690 | 2441 |
#endif /* not __SCHTEAM__ */ |
314 | 2442 |
%}. |
21909 | 2443 |
^ super printfPrintString:formatString |
1 | 2444 |
|
7355 | 2445 |
" |
2446 |
Float pi printfPrintString:'%%lg -> %lg' |
|
2447 |
Float pi printfPrintString:'%%lf -> %lf' |
|
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8302
diff
changeset
|
2448 |
Float pi printfPrintString:'%%7.15lg -> %7.15lg' |
7355 | 2449 |
Float pi printfPrintString:'%%7.5lf -> %7.5lf' |
2450 |
Float pi printfPrintString:'%%G -> %G' |
|
2451 |
Float pi printfPrintString:'%%F -> %F' |
|
2452 |
Float pi printfPrintString:'%%7.5G -> %7.5G' |
|
2453 |
Float pi printfPrintString:'%%7.5F -> %7.5F' |
|
22578 | 2454 |
|
21952 | 2455 |
Float pi printfPrintString:'%%7.5f -> %7.5f' |
2456 |
Float pi printfPrintString:'%%100.98f -> %100.98f' |
|
2457 |
Float pi printfPrintString:'%%100.6f -> %100.6f' |
|
2458 |
Float pi printfPrintString:'%%300.6f -> %300.6f' |
|
22578 | 2459 |
|
21952 | 2460 |
Float pi printfPrintString:'%%-100.98f -> %-100.98f' |
2461 |
Float pi printfPrintString:'%%300.298f -> %300.298f' |
|
2462 |
Float pi printfPrintString:'%%-300.298f -> %-300.298f' |
|
2463 |
||
2464 |
Float pi printfPrintString:'%%100.6f -> <%100.6f>' |
|
2465 |
Float pi printfPrintString:'%%300.6f -> <%300.6f>' |
|
2466 |
||
2467 |
Float pi printfPrintString:'%%100.6f -> <%100.6>' |
|
2468 |
Float pi printfPrintString:'%%300.6f -> <%300.6>' |
|
7355 | 2469 |
" |
21909 | 2470 |
|
21952 | 2471 |
"Modified (comment): / 03-07-2017 / 15:11:13 / cg" |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2472 |
! |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2473 |
|
22301 | 2474 |
storeOn:aStream |
2475 |
"append a printed representation of the receiver to |
|
2476 |
the argument, aStream. |
|
2477 |
||
2478 |
I use #storeString instead of #storeOn: as basic store mechanism." |
|
2479 |
||
2480 |
aStream nextPutAll:self storeString |
|
2481 |
||
2482 |
"Created: / 10-10-2017 / 14:06:39 / cg" |
|
2483 |
! |
|
2484 |
||
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2485 |
storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2486 |
"return a printed representation of the receiver; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2487 |
all valid digits are printed. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2488 |
LimitedPrecisonReal and its subclasses use #storeString instead of |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2489 |
#storeOn: as basic print mechanism." |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2490 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2491 |
%{ /* NOCONTEXT */ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2492 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2493 |
char buffer[64]; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2494 |
REGISTER char *cp; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2495 |
OBJ s; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2496 |
int len; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2497 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2498 |
__BEGIN_PROTECT_REGISTERS__ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2499 |
#ifdef SYSV |
16658 | 2500 |
len = snprintf(buffer, sizeof(buffer), "%.17lg", __floatVal(self)); |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2501 |
#else |
16658 | 2502 |
len = snprintf(buffer, sizeof(buffer), "%.17G", __floatVal(self)); |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2503 |
#endif |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2504 |
__END_PROTECT_REGISTERS__ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2505 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2506 |
if (len >= 0 && len < sizeof(buffer)-3) { |
24969 | 2507 |
/* |
2508 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
|
2509 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
|
2510 |
*/ |
|
2511 |
for (cp = buffer; *cp; cp++) { |
|
2512 |
if ((*cp == '.') || (*cp == ',') || (*cp == 'E') || (*cp == 'e')) break; |
|
2513 |
} |
|
2514 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
|
2515 |
*cp++ = '.'; |
|
2516 |
*cp++ = '0'; |
|
2517 |
*cp = '\0'; |
|
2518 |
} |
|
2519 |
||
2520 |
s = __MKSTRING(buffer); |
|
2521 |
if (s != nil) { |
|
2522 |
RETURN (s); |
|
2523 |
} |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2524 |
} |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2525 |
%}. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2526 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2527 |
memory allocation (for the new string) failed. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2528 |
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:
8417
diff
changeset
|
2529 |
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:
8417
diff
changeset
|
2530 |
OS, which was not kind enough to give it. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2531 |
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:
8417
diff
changeset
|
2532 |
" |
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
20415
diff
changeset
|
2533 |
^ AllocationFailure raise. |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2534 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2535 |
" |
24969 | 2536 |
1.0 storeString |
2537 |
0.1 storeString |
|
2538 |
((Array new:10 withAll:0.1) inject:0 into:[:v :sumSoFar| sumSoFar + v]) storeString |
|
2539 |
1.234 storeString |
|
2540 |
1e10 storeString |
|
2541 |
1.2e3 storeString |
|
2542 |
1.2e30 storeString |
|
2543 |
Float pi storeString |
|
2544 |
(1.0 uncheckedDivide:0) storeString |
|
2545 |
(0.0 uncheckedDivide:0) storeString |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2546 |
|
9124 | 2547 |
notice that the storeString is NOT affected by DecimalPointCharacterForPrinting: |
2548 |
||
24969 | 2549 |
DecimalPointCharacterForPrinting := $,. |
2550 |
1.234 storeString. |
|
2551 |
1.0 storeString. |
|
2552 |
1e10 storeString. |
|
2553 |
1.2e3 storeString. |
|
2554 |
1.2e30 storeString. |
|
2555 |
(1.0 uncheckedDivide:0) storeString. |
|
2556 |
(0.0 uncheckedDivide:0) storeString. |
|
2557 |
DecimalPointCharacterForPrinting := $. |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
2558 |
" |
1 | 2559 |
! ! |
701 | 2560 |
|
11926
295e8bb99fde
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11733
diff
changeset
|
2561 |
|
7258 | 2562 |
!Float methodsFor:'private-accessing'! |
701 | 2563 |
|
2564 |
basicAt:index |
|
2565 |
"return an internal byte of the float. |
|
2566 |
The value returned here depends on byte order, float representation etc. |
|
2567 |
Therefore, this method should be used strictly private. |
|
2568 |
||
11945 | 2569 |
Notice: |
24969 | 2570 |
the need to redefine this method here is due to the |
2571 |
inability of many machines to store floats in non-double aligned memory. |
|
2572 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
|
2573 |
and the actual float is stored at index 5 .. 12. |
|
2574 |
To hide this at one place, this method knows about that, and returns |
|
2575 |
values as if this filler wasnt present." |
|
701 | 2576 |
|
2577 |
%{ /* NOCONTEXT */ |
|
2578 |
||
2579 |
register int indx; |
|
2580 |
unsigned char *cp; |
|
2581 |
||
2582 |
/* |
|
2583 |
* notice the missing test for self being a nonNilObject - |
|
2584 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
2585 |
* and SmallInteger |
|
2586 |
*/ |
|
2587 |
if (__isSmallInteger(index)) { |
|
24969 | 2588 |
indx = __intVal(index) - 1; |
2589 |
if (((unsigned)(indx)) < sizeof(double)) { |
|
2590 |
cp = (unsigned char *)(& (__FloatInstPtr(self)->f_floatvalue)); |
|
2591 |
RETURN ( __mkSmallInteger(cp[indx] & 0xFF) ); |
|
2592 |
} |
|
701 | 2593 |
} |
2594 |
%}. |
|
7217 | 2595 |
^ self indexNotIntegerOrOutOfBounds:index |
701 | 2596 |
! |
2597 |
||
2598 |
basicAt:index put:value |
|
2599 |
"set an internal byte of the float. |
|
2600 |
The value to be stored here depends on byte order, float representation etc. |
|
2601 |
Therefore, this method should be used strictly private. |
|
2602 |
||
11945 | 2603 |
Notice: |
24969 | 2604 |
the need to redefine this method here is due to the |
2605 |
inability of many machines to store floats in non-double aligned memory. |
|
2606 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
|
2607 |
and the actual float is stored at index 5 .. 12. |
|
2608 |
To hide this at one place, this method knows about that, and returns |
|
2609 |
values as if this filler wasnt present." |
|
701 | 2610 |
|
2611 |
%{ /* NOCONTEXT */ |
|
2612 |
register int indx, val; |
|
2613 |
unsigned char *cp; |
|
2614 |
||
2615 |
/* |
|
2616 |
* notice the missing test for self being a nonNilObject - |
|
2617 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
2618 |
* and SmallInteger |
|
2619 |
*/ |
|
2620 |
if (__bothSmallInteger(index, value)) { |
|
24969 | 2621 |
val = __intVal(value); |
2622 |
if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) { |
|
2623 |
indx = __intVal(index) - 1; |
|
2624 |
if (((unsigned)(indx)) < sizeof(double)) { |
|
2625 |
cp = (unsigned char *)(& (__FloatInstPtr(self)->f_floatvalue)); |
|
2626 |
cp[indx] = val; |
|
2627 |
RETURN ( value ); |
|
2628 |
} |
|
2629 |
} |
|
701 | 2630 |
} |
2631 |
%}. |
|
2632 |
value isInteger ifFalse:[ |
|
24969 | 2633 |
" |
2634 |
the object to store should be an integer number |
|
2635 |
" |
|
2636 |
^ self elementNotInteger |
|
701 | 2637 |
]. |
2638 |
(value between:0 and:255) ifFalse:[ |
|
24969 | 2639 |
" |
2640 |
the object to store must be a bytes value |
|
2641 |
" |
|
2642 |
^ self elementBoundsError:value |
|
701 | 2643 |
]. |
7217 | 2644 |
^ self indexNotIntegerOrOutOfBounds:index |
16658 | 2645 |
! |
4592 | 2646 |
|
2647 |
basicSize |
|
2648 |
"return the size in bytes of the float. |
|
2649 |
||
11945 | 2650 |
Notice: |
24969 | 2651 |
the need to redefine this method here is due to the |
2652 |
inability of many machines to store floats in non-double aligned memory. |
|
2653 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
|
2654 |
and the actual float is stored at index 5 .. 12. |
|
2655 |
To hide this at one place, this method knows about that, and returns |
|
2656 |
values as if this filler wasn't present." |
|
4592 | 2657 |
|
2658 |
%{ /* NOCONTEXT */ |
|
2659 |
||
8913 | 2660 |
RETURN (__mkSmallInteger(sizeof(double))); |
4592 | 2661 |
%}. |
22260 | 2662 |
! |
2663 |
||
2664 |
byteAt:index |
|
2665 |
^ self basicAt:index |
|
2666 |
! |
|
2667 |
||
2668 |
byteAt:index put:newByte |
|
2669 |
self shouldNotImplement |
|
701 | 2670 |
! ! |
2671 |
||
16658 | 2672 |
!Float methodsFor:'queries'! |
2673 |
||
2674 |
nextFloat:count |
|
2675 |
"answer the next float count places after (or before if count is negative) myself" |
|
2676 |
||
2677 |
%{ |
|
2678 |
union { |
|
24969 | 2679 |
double d; |
2680 |
INT64 i; |
|
16658 | 2681 |
} this; |
2682 |
||
2683 |
if (__isSmallInteger(count)) { |
|
24969 | 2684 |
this.d = __floatVal(self); |
2685 |
if (isfinite(this.d)) |
|
2686 |
this.i += __intVal(count); |
|
2687 |
||
2688 |
RETURN(__MKFLOAT(this.d)); |
|
16658 | 2689 |
} |
2690 |
%}. |
|
2691 |
self primitiveFailed:#badArgument |
|
2692 |
||
2693 |
" |
|
2694 |
(1.0 nextFloat:1) storeString |
|
2695 |
(67329.234 nextFloat:1) storeString |
|
2696 |
(67329.234 asShortFloat nextFloat:1) storeString |
|
2697 |
Float NaN nextFloat:100000 |
|
2698 |
Float infinity nextFloat:100000 |
|
2699 |
" |
|
2700 |
! ! |
|
2701 |
||
3896 | 2702 |
!Float methodsFor:'special access'! |
2703 |
||
2704 |
exponent |
|
24969 | 2705 |
"extract a normalized float's (unbiased) exponent. |
3896 | 2706 |
The returned value depends on the float-representation of |
2707 |
the underlying machine and is therefore highly unportable. |
|
2708 |
This is not for general use. |
|
2709 |
This assumes that the mantissa is normalized to |
|
12423 | 2710 |
0.5 .. 1.0 and the float's value is: mantissa * 2^exp" |
3896 | 2711 |
|
2712 |
%{ /* NOCONTEXT */ |
|
21849 | 2713 |
double myVal; |
3896 | 2714 |
double frac; |
8913 | 2715 |
int exp; |
3896 | 2716 |
|
21849 | 2717 |
myVal = __floatVal(self); |
2718 |
// ouch: math libs seem to not care for NaN here; |
|
2719 |
#if 1 |
|
2720 |
// should we? |
|
2721 |
if (! (isnan(myVal) || isinf(myVal))) |
|
2722 |
#endif |
|
2723 |
{ |
|
25002 | 2724 |
frac = frexp(myVal, &exp); |
2725 |
RETURN (__mkSmallInteger(exp)); |
|
21849 | 2726 |
} |
3896 | 2727 |
%}. |
21849 | 2728 |
^ super exponent |
3896 | 2729 |
|
2730 |
" |
|
25002 | 2731 |
1.0 exponent |
2732 |
2.0 exponent |
|
14672
8abd84f0951a
setlocale once only in stxmain
Claus Gittinger <cg@exept.de>
parents:
14630
diff
changeset
|
2733 |
3.0 exponent |
24969 | 2734 |
3.0 mantissa |
2735 |
3.0 mantissa * (2 raisedTo:3.0 exponent) |
|
25002 | 2736 |
4.0 exponent |
14672
8abd84f0951a
setlocale once only in stxmain
Claus Gittinger <cg@exept.de>
parents:
14630
diff
changeset
|
2737 |
0.5 exponent |
8abd84f0951a
setlocale once only in stxmain
Claus Gittinger <cg@exept.de>
parents:
14630
diff
changeset
|
2738 |
0.4 exponent |
8abd84f0951a
setlocale once only in stxmain
Claus Gittinger <cg@exept.de>
parents:
14630
diff
changeset
|
2739 |
0.25 exponent |
8abd84f0951a
setlocale once only in stxmain
Claus Gittinger <cg@exept.de>
parents:
14630
diff
changeset
|
2740 |
0.2 exponent |
11945 | 2741 |
0.00000011111 exponent |
21849 | 2742 |
0.0 exponent |
2743 |
1e1000 exponent |
|
3896 | 2744 |
" |
21849 | 2745 |
|
2746 |
"Modified: / 20-06-2017 / 11:34:43 / cg" |
|
3896 | 2747 |
! |
2748 |
||
2749 |
mantissa |
|
24152 | 2750 |
"extract a normalized float's mantissa. |
3896 | 2751 |
The returned value depends on the float-representation of |
2752 |
the underlying machine and is therefore highly unportable. |
|
2753 |
This is not for general use. |
|
2754 |
This assumes that the mantissa is normalized to |
|
24152 | 2755 |
0.5 .. 1.0 and the float's value is mantissa * 2^exp" |
3896 | 2756 |
|
2757 |
%{ /* NOCONTEXT */ |
|
21849 | 2758 |
double myVal; |
3896 | 2759 |
double frac; |
8913 | 2760 |
int exp; |
3896 | 2761 |
|
21849 | 2762 |
myVal = __floatVal(self); |
2763 |
// ouch: math libs seem to not care for NaN here; |
|
2764 |
#if 1 |
|
2765 |
// should we? |
|
2766 |
if (! (isnan(myVal) || isinf(myVal))) |
|
2767 |
#endif |
|
2768 |
{ |
|
25008 | 2769 |
frac = frexp(myVal, &exp); |
2770 |
RETURN (__MKFLOAT(frac)); |
|
21868 | 2771 |
} |
3896 | 2772 |
%}. |
20009 | 2773 |
^ super mantissa |
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
20415
diff
changeset
|
2774 |
|
3896 | 2775 |
" |
11945 | 2776 |
1.0 exponent |
2777 |
1.0 mantissa |
|
2778 |
||
2779 |
0.25 exponent |
|
2780 |
0.25 mantissa |
|
2781 |
||
2782 |
0.00000011111 exponent |
|
2783 |
0.00000011111 mantissa |
|
21849 | 2784 |
|
2785 |
1e1000 mantissa |
|
25008 | 2786 |
|
2787 |
self assert:(1.0 mantissa * (2 raisedTo:1.0 exponent)) = 1.0. |
|
2788 |
self assert:(100.0 mantissa * (2 raisedTo:100.0 exponent)) = 100.0. |
|
2789 |
self assert:(10e15 mantissa * (2 raisedTo:10e15 exponent)) = 10e15. |
|
2790 |
self assert:(10e-15 mantissa * (2 raisedTo:10e-15 exponent)) = 10e-15. |
|
3896 | 2791 |
" |
21849 | 2792 |
|
2793 |
"Modified: / 20-06-2017 / 11:37:13 / cg" |
|
24152 | 2794 |
"Modified (comment): / 26-05-2019 / 03:12:55 / Claus Gittinger" |
3896 | 2795 |
! ! |
2796 |
||
701 | 2797 |
!Float methodsFor:'testing'! |
2798 |
||
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2799 |
isFinite |
24710 | 2800 |
"return true, if the receiver is a finite float (not NaN and not +/-INF)" |
3127
590c7d2bb4b9
fixes isNAN (on linux); added #isInfinite
Claus Gittinger <cg@exept.de>
parents:
3118
diff
changeset
|
2801 |
|
590c7d2bb4b9
fixes isNAN (on linux); added #isInfinite
Claus Gittinger <cg@exept.de>
parents:
3118
diff
changeset
|
2802 |
%{ /* NOCONTEXT */ |
590c7d2bb4b9
fixes isNAN (on linux); added #isInfinite
Claus Gittinger <cg@exept.de>
parents:
3118
diff
changeset
|
2803 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
2804 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
2805 |
* notice: on machines which do not provide |
11945 | 2806 |
* a finite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4296
diff
changeset
|
2807 |
* 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
|
2808 |
*/ |
17384 | 2809 |
RETURN (isfinite(__floatVal(self)) ? true : false) |
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2810 |
%}. |
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2811 |
|
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2812 |
" |
25002 | 2813 |
1.0 isFinite |
2814 |
1 isFinite |
|
2815 |
||
2816 |
Float NaN isFinite |
|
2817 |
Float infinity isFinite |
|
24696 | 2818 |
Float negativeInfinity isFinite |
2819 |
(0.0 uncheckedDivide: 0.0) isFinite |
|
2820 |
(1.0 uncheckedDivide: 0.0) isFinite |
|
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2821 |
" |
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2822 |
! |
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2823 |
|
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2824 |
isInfinite |
24710 | 2825 |
"return true, if the receiver is an infinite float (+Inf or -Inf). |
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2826 |
These are not created by ST/X float operations (they raise an exception); |
24710 | 2827 |
however, inline C-code could produce them." |
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2828 |
|
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2829 |
%{ /* NOCONTEXT */ |
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2830 |
|
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2831 |
double dV = __floatVal(self); |
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2832 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
2833 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
2834 |
* notice: on machines which do not provide |
11945 | 2835 |
* finite() & isnan() macros or functions (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4296
diff
changeset
|
2836 |
* 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
|
2837 |
*/ |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2838 |
#if defined(isinf) |
3127
590c7d2bb4b9
fixes isNAN (on linux); added #isInfinite
Claus Gittinger <cg@exept.de>
parents:
3118
diff
changeset
|
2839 |
if (isinf(dV)) { RETURN (true); } |
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2840 |
#else |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2841 |
if (!isfinite(dV) && !isnan(dV)) { RETURN (true); } |
3127
590c7d2bb4b9
fixes isNAN (on linux); added #isInfinite
Claus Gittinger <cg@exept.de>
parents:
3118
diff
changeset
|
2842 |
#endif |
590c7d2bb4b9
fixes isNAN (on linux); added #isInfinite
Claus Gittinger <cg@exept.de>
parents:
3118
diff
changeset
|
2843 |
%}. |
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2844 |
^ false |
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2845 |
|
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2846 |
" |
25002 | 2847 |
1.0 isInfinite |
2848 |
(0.0 uncheckedDivide: 0.0) isInfinite |
|
2849 |
(1.0 uncheckedDivide: 0.0) isInfinite |
|
2850 |
(-1.0 uncheckedDivide: 0.0) isInfinite |
|
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2851 |
" |
3127
590c7d2bb4b9
fixes isNAN (on linux); added #isInfinite
Claus Gittinger <cg@exept.de>
parents:
3118
diff
changeset
|
2852 |
! |
590c7d2bb4b9
fixes isNAN (on linux); added #isInfinite
Claus Gittinger <cg@exept.de>
parents:
3118
diff
changeset
|
2853 |
|
4655
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
2854 |
isLiteral |
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
2855 |
"return true, if the receiver can be used as a literal constant in ST syntax |
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
2856 |
(i.e. can be used in constant arrays)" |
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
2857 |
|
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
2858 |
^ true |
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
2859 |
|
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
2860 |
|
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
2861 |
! |
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
2862 |
|
2387 | 2863 |
isNaN |
2864 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
2865 |
These are not created by ST/X float operations (they raise an exception); |
|
24710 | 2866 |
however, inline C-code could produce them." |
2387 | 2867 |
|
2868 |
%{ /* NOCONTEXT */ |
|
2869 |
||
17384 | 2870 |
RETURN (isnan(__floatVal(self)) ? true : false) |
2392 | 2871 |
%}. |
3139 | 2872 |
|
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2873 |
" |
25002 | 2874 |
self NaN isNaN |
2875 |
1.0 isNaN |
|
2876 |
(0.0 uncheckedDivide: 0.0) isNaN |
|
2877 |
(1.0 uncheckedDivide: 0.0) isNaN |
|
2878 |
(-1.0 uncheckedDivide: 0.0) isNaN |
|
3146
4e46505d1d59
New #finite, fix #isNaN and #isInfinite.
Stefan Vogel <sv@exept.de>
parents:
3139
diff
changeset
|
2879 |
" |
2387 | 2880 |
! |
2881 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6674
diff
changeset
|
2882 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6674
diff
changeset
|
2883 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6674
diff
changeset
|
2884 |
|
18296 | 2885 |
%{ /* NOCONTEXT */ |
17389 | 2886 |
|
17392 | 2887 |
#if defined(__BORLANDC__) |
2888 |
union { double d; int i[2]; } __u; |
|
2889 |
__u.d = __floatVal(self); |
|
2890 |
RETURN ( (__u.d == 0.0 && __u.i[1] < 0) ? true : false ); |
|
17389 | 2891 |
#else |
17384 | 2892 |
RETURN ( (__floatVal(self) == 0.0 && signbit(__floatVal(self)) != 0) ? true : false ); |
17389 | 2893 |
#endif |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6674
diff
changeset
|
2894 |
%}. |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6674
diff
changeset
|
2895 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6674
diff
changeset
|
2896 |
" |
11945 | 2897 |
0.0 isNegativeZero |
2898 |
-0.0 isNegativeZero |
|
2899 |
-1.0 isNegativeZero |
|
2900 |
1.0 isNegativeZero |
|
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6674
diff
changeset
|
2901 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6674
diff
changeset
|
2902 |
! |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6674
diff
changeset
|
2903 |
|
24328 | 2904 |
isZero |
2905 |
"return true, if the receiver is zero" |
|
2906 |
||
2907 |
^ self = 0.0 |
|
2908 |
||
2909 |
"Created: / 10-06-2019 / 21:58:24 / Claus Gittinger" |
|
2910 |
! |
|
2911 |
||
701 | 2912 |
negative |
17384 | 2913 |
"return true if the receiver is less than zero. |
2914 |
-0.0 is positive for now." |
|
701 | 2915 |
|
2916 |
%{ /* NOCONTEXT */ |
|
2917 |
||
17384 | 2918 |
RETURN ( __floatVal(self) < 0.0 ? true : false ); |
2919 |
// RETURN ( signbit(__floatVal(self)) != 0 ? true : false ); |
|
5411 | 2920 |
%}. |
17384 | 2921 |
|
2922 |
" |
|
24696 | 2923 |
0.0 negative |
2924 |
-0.0 negative |
|
2925 |
1.0 negative |
|
2926 |
-1.0 negative |
|
2927 |
(1.0 uncheckedDivide: 0.0) negative |
|
2928 |
(-1.0 uncheckedDivide: 0.0) negative |
|
2929 |
||
25002 | 2930 |
Float infinity negative |
2931 |
Float negativeInfinity negative |
|
2932 |
Float NaN negative |
|
2933 |
||
2934 |
Float infinity positive |
|
2935 |
Float negativeInfinity positive |
|
2936 |
Float NaN positive |
|
17384 | 2937 |
" |
701 | 2938 |
! |
2939 |
||
5357 | 2940 |
numberOfBits |
2941 |
"return the size (in bits) of the real; |
|
2942 |
typically, 64 is returned here, |
|
2943 |
but who knows ..." |
|
2944 |
||
2945 |
%{ /* NOCONTEXT */ |
|
2946 |
||
8913 | 2947 |
RETURN (__mkSmallInteger (sizeof(double) * 8)); |
5357 | 2948 |
%} |
2949 |
||
2950 |
" |
|
11945 | 2951 |
1.2 numberOfBits |
2952 |
1.2 asShortFloat numberOfBits |
|
5357 | 2953 |
" |
2954 |
! |
|
2955 |
||
701 | 2956 |
positive |
18857 | 2957 |
"return true if the receiver is greater or equal to zero (not negative). |
17384 | 2958 |
0.0 and -0.0 are positive for now." |
701 | 2959 |
|
2960 |
%{ /* NOCONTEXT */ |
|
2961 |
||
17384 | 2962 |
RETURN ( __floatVal(self) >= 0.0 ? true : false ); |
2963 |
// RETURN ( signbit(__floatVal(self)) == 0 ? true : false ); |
|
2964 |
%}. |
|
2965 |
||
2966 |
" |
|
24696 | 2967 |
0.0 positive |
2968 |
-0.0 positive |
|
2969 |
1.0 positive |
|
2970 |
-1.0 positive |
|
2971 |
(1.0 uncheckedDivide: 0.0) positive |
|
2972 |
(-1.0 uncheckedDivide: 0.0) positive |
|
2973 |
||
25002 | 2974 |
Float infinity positive |
2975 |
Float negativeInfinity positive |
|
2976 |
Float NaN positive |
|
2977 |
||
2978 |
Float infinity negative |
|
2979 |
Float negativeInfinity negative |
|
2980 |
Float NaN negative |
|
17384 | 2981 |
" |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
2982 |
! |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
2983 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
2984 |
strictlyPositive |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
2985 |
"return true if the receiver is greater than zero" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
2986 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
2987 |
%{ /* NOCONTEXT */ |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
2988 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
2989 |
RETURN ( (__floatVal(self) > 0.0) ? true : false ); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
2990 |
%} |
701 | 2991 |
! ! |
2992 |
||
4655
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
2993 |
!Float methodsFor:'tracing'! |
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
2994 |
|
4682 | 2995 |
traceInto:aRequestor level:level from:referrer |
4655
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
2996 |
"double dispatch into tracer, passing my type implicitely in the selector" |
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
2997 |
|
4682 | 2998 |
^ aRequestor traceFloat:self level:level from:referrer |
4655
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
2999 |
|
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
3000 |
|
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
3001 |
! ! |
b9405ca0bb4e
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
3002 |
|
7385 | 3003 |
!Float methodsFor:'trigonometric'! |
3004 |
||
3005 |
arcCos |
|
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3006 |
"return the arccosine of the receiver (as radians). |
7385 | 3007 |
Raises an exception, if the receiver is not in -1..1" |
3008 |
||
3009 |
%{ /* NOCONTEXT */ |
|
18296 | 3010 |
#ifdef __SCHTEAM__ |
18299 | 3011 |
return context._RETURN( self.acos() ); |
18296 | 3012 |
#else |
7385 | 3013 |
|
3014 |
double val, rslt; |
|
3015 |
OBJ newFloat; |
|
3016 |
||
3017 |
val = __floatVal(self); |
|
3018 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
3019 |
# ifdef __win32__ /* to suppress the warnBox opened by win32 */ |
7385 | 3020 |
if ((val >= -1.0) && (val <= 1.0)) |
18296 | 3021 |
# endif |
7385 | 3022 |
{ |
24969 | 3023 |
__threadErrno = 0; |
3024 |
rslt = acos(val); |
|
3025 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
|
3026 |
{ |
|
3027 |
if (__threadErrno == 0) { |
|
3028 |
__qMKFLOAT(newFloat, rslt); |
|
3029 |
RETURN ( newFloat ); |
|
3030 |
} |
|
3031 |
} |
|
7385 | 3032 |
} |
18296 | 3033 |
#endif |
7385 | 3034 |
%}. |
3035 |
^ self class |
|
24969 | 3036 |
raise:#domainErrorSignal |
3037 |
receiver:self |
|
3038 |
selector:#arcCos |
|
3039 |
arguments:#() |
|
3040 |
errorString:'bad receiver in arcCos' |
|
7385 | 3041 |
|
3042 |
" |
|
3043 |
-10 arcCos |
|
11945 | 3044 |
1 arcCos |
7385 | 3045 |
" |
3046 |
||
3047 |
"Modified: / 16.11.2001 / 14:14:13 / cg" |
|
3048 |
! |
|
3049 |
||
3050 |
arcCosh |
|
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3051 |
"return the hyperbolic arccosine of the receiver." |
7385 | 3052 |
|
7410 | 3053 |
|useFallBack| |
3054 |
||
3055 |
%{ |
|
3056 |
#ifdef NO_ACOSH |
|
3057 |
useFallBack = true; |
|
3058 |
#else |
|
7385 | 3059 |
double val, rslt; |
3060 |
OBJ newFloat; |
|
3061 |
||
3062 |
val = __floatVal(self); |
|
3063 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
3064 |
# ifdef __win32__ /* to suppress the warnBox opened by win32 */ |
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3065 |
if (val >= 1.0) |
7410 | 3066 |
# endif |
7385 | 3067 |
{ |
24969 | 3068 |
__threadErrno = 0; |
3069 |
rslt = acosh(val); |
|
3070 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
|
3071 |
{ |
|
3072 |
if (__threadErrno == 0) { |
|
3073 |
__qMKFLOAT(newFloat, rslt); |
|
3074 |
RETURN ( newFloat ); |
|
3075 |
} |
|
3076 |
} |
|
7385 | 3077 |
} |
7410 | 3078 |
#endif |
7385 | 3079 |
%}. |
7410 | 3080 |
useFallBack notNil ifTrue:[ |
24969 | 3081 |
^ super arcCosh |
7410 | 3082 |
]. |
3083 |
||
7385 | 3084 |
^ self class |
24969 | 3085 |
raise:#domainErrorSignal |
3086 |
receiver:self |
|
3087 |
selector:#arcCosh |
|
3088 |
arguments:#() |
|
3089 |
errorString:'bad receiver in arcCosh' |
|
7385 | 3090 |
|
3091 |
" |
|
3092 |
-10.0 arcCosh |
|
11945 | 3093 |
1.0 arcCosh |
7385 | 3094 |
" |
3095 |
||
3096 |
"Modified: / 16.11.2001 / 14:14:13 / cg" |
|
3097 |
! |
|
3098 |
||
3099 |
arcSin |
|
3100 |
"return the arcsine of myself (I am interpreted as radians). |
|
3101 |
Raises an exception, if the receiver is not in -1..1" |
|
3102 |
||
3103 |
%{ /* NOCONTEXT */ |
|
18296 | 3104 |
#ifdef __SCHTEAM__ |
18299 | 3105 |
return context._RETURN( self.asin() ); |
18296 | 3106 |
#else |
7385 | 3107 |
|
3108 |
double val, rslt; |
|
3109 |
OBJ newFloat; |
|
3110 |
||
3111 |
val = __floatVal(self); |
|
3112 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
3113 |
# ifdef __win32__ /* to suppress the warnBox opened by win32 */ |
7385 | 3114 |
if ((val >= -1.0) && (val <= 1.0)) |
18296 | 3115 |
# endif |
7385 | 3116 |
{ |
24969 | 3117 |
__threadErrno = 0; |
3118 |
rslt = asin(val); |
|
3119 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
|
3120 |
{ |
|
3121 |
if (__threadErrno == 0) { |
|
3122 |
__qMKFLOAT(newFloat, rslt); |
|
3123 |
RETURN ( newFloat ); |
|
3124 |
} |
|
3125 |
} |
|
7385 | 3126 |
} |
18296 | 3127 |
#endif |
7385 | 3128 |
%}. |
3129 |
^ self class |
|
24969 | 3130 |
raise:#domainErrorSignal |
3131 |
receiver:self |
|
3132 |
selector:#arcSin |
|
3133 |
arguments:#() |
|
3134 |
errorString:'bad receiver in arcSin' |
|
7385 | 3135 |
|
3136 |
" |
|
3137 |
-10 arcSin |
|
11945 | 3138 |
1 arcSin |
7385 | 3139 |
" |
3140 |
||
3141 |
"Modified: / 16.11.2001 / 14:14:18 / cg" |
|
3142 |
! |
|
3143 |
||
3144 |
arcSinh |
|
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3145 |
"return the hyperbolic arcsine of the receiver." |
7385 | 3146 |
|
7410 | 3147 |
|useFallBack| |
3148 |
%{ |
|
3149 |
#ifdef NO_ASINH |
|
3150 |
useFallBack = true; |
|
3151 |
#else |
|
7385 | 3152 |
double val, rslt; |
3153 |
OBJ newFloat; |
|
3154 |
||
3155 |
val = __floatVal(self); |
|
3156 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
3157 |
# ifdef __win32__ /* to suppress the warnBox opened by win32 */ |
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3158 |
if (val >= 1.0) |
7410 | 3159 |
# endif |
7385 | 3160 |
{ |
24969 | 3161 |
__threadErrno = 0; |
3162 |
rslt = asinh(val); |
|
3163 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
|
3164 |
{ |
|
3165 |
if (__threadErrno == 0) { |
|
3166 |
__qMKFLOAT(newFloat, rslt); |
|
3167 |
RETURN ( newFloat ); |
|
3168 |
} |
|
3169 |
} |
|
7385 | 3170 |
} |
7410 | 3171 |
#endif |
7385 | 3172 |
%}. |
7410 | 3173 |
useFallBack notNil ifTrue:[ |
24969 | 3174 |
^ super arcSinh |
7410 | 3175 |
]. |
7385 | 3176 |
^ self class |
24969 | 3177 |
raise:#domainErrorSignal |
3178 |
receiver:self |
|
3179 |
selector:#arcSinh |
|
3180 |
arguments:#() |
|
3181 |
errorString:'bad receiver in arcSinh' |
|
7385 | 3182 |
|
3183 |
" |
|
3184 |
-10.0 arcSinh |
|
11945 | 3185 |
1.0 arcSinh |
7385 | 3186 |
" |
3187 |
! |
|
3188 |
||
3189 |
arcTan |
|
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3190 |
"return the arctangent of the receiver (as radians)" |
7385 | 3191 |
|
3192 |
%{ /* NOCONTEXT */ |
|
18296 | 3193 |
#ifdef __SCHTEAM__ |
18299 | 3194 |
return context._RETURN( self.atan() ); |
18296 | 3195 |
#else |
7385 | 3196 |
|
3197 |
double rslt; |
|
3198 |
OBJ newFloat; |
|
3199 |
||
3200 |
__threadErrno = 0; |
|
3201 |
rslt = atan(__floatVal(self)); |
|
7393 | 3202 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
3203 |
{ |
|
24969 | 3204 |
if (__threadErrno == 0) { |
3205 |
__qMKFLOAT(newFloat, rslt); |
|
3206 |
RETURN ( newFloat ); |
|
3207 |
} |
|
7385 | 3208 |
} |
18296 | 3209 |
#endif |
7385 | 3210 |
%}. |
3211 |
^ self class |
|
24969 | 3212 |
raise:#domainErrorSignal |
3213 |
receiver:self |
|
3214 |
selector:#arcTan |
|
3215 |
arguments:#() |
|
3216 |
errorString:'bad receiver in arcTan' |
|
7385 | 3217 |
|
3218 |
"Modified: / 16.11.2001 / 14:14:22 / cg" |
|
3219 |
! |
|
3220 |
||
11487 | 3221 |
arcTan2:x |
3222 |
"return the atan2(self,x)" |
|
3223 |
||
3224 |
%{ /* NOCONTEXT */ |
|
3225 |
||
3226 |
double rslt; |
|
3227 |
OBJ newFloat; |
|
3228 |
||
3229 |
if (__isFloat(x)) { |
|
24969 | 3230 |
__threadErrno = 0; |
3231 |
rslt = atan2(__floatVal(self),__floatVal(x)); |
|
3232 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
|
3233 |
{ |
|
3234 |
if (__threadErrno == 0) { |
|
3235 |
__qMKFLOAT(newFloat, rslt); |
|
3236 |
RETURN ( newFloat ); |
|
3237 |
} |
|
3238 |
} |
|
11487 | 3239 |
} |
3240 |
%}. |
|
3241 |
x isFloat ifFalse:[ |
|
24969 | 3242 |
^ self arcTan2:(x asFloat). |
11487 | 3243 |
]. |
3244 |
||
3245 |
^ self class |
|
24969 | 3246 |
raise:#domainErrorSignal |
3247 |
receiver:self |
|
3248 |
selector:#arcTan2: |
|
3249 |
arguments:(Array with:x) |
|
3250 |
errorString:'bad receiver in arcTan2:' |
|
11487 | 3251 |
! |
3252 |
||
10604 | 3253 |
arcTan: denominator |
22728 | 3254 |
"Evaluate the four quadrant arc tangent of the argument denominator (x) and the receiver (y)." |
3255 |
||
11945 | 3256 |
(self = 0.0) ifTrue: [ |
24969 | 3257 |
(denominator > 0.0) |
3258 |
ifTrue: [ ^ 0 ] |
|
3259 |
ifFalse: [ ^ Pi ] |
|
10604 | 3260 |
]. |
11945 | 3261 |
(denominator = 0.0) ifTrue: [ |
24969 | 3262 |
(self > 0.0) |
3263 |
ifTrue: [ ^ Halfpi ] |
|
3264 |
ifFalse: [ ^ HalfpiNegative ] |
|
10604 | 3265 |
]. |
11945 | 3266 |
(denominator > 0) |
24969 | 3267 |
ifTrue: [ ^ (self / denominator) arcTan ] |
3268 |
ifFalse: [ ^ ((self / denominator) arcTan) + Pi ] |
|
10604 | 3269 |
|
3270 |
"Created: / 07-06-2007 / 21:10:32 / cg" |
|
3271 |
"Modified: / 11-06-2007 / 12:58:34 / cg" |
|
3272 |
! |
|
3273 |
||
7385 | 3274 |
arcTanh |
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3275 |
"return the hyperbolic arctangent of the receiver." |
7385 | 3276 |
|
7410 | 3277 |
|useFallBack| |
3278 |
%{ |
|
3279 |
#ifdef NO_ATANH |
|
3280 |
useFallBack = true; |
|
3281 |
#else |
|
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3282 |
double val, rslt; |
7385 | 3283 |
OBJ newFloat; |
3284 |
||
3285 |
__threadErrno = 0; |
|
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3286 |
val = __floatVal(self); |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
3287 |
# ifdef __win32__ /* to suppress the warnBox opened by win32 */ |
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3288 |
if ((val >= -1.0) && (val <= 1.0)) |
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3289 |
# endif |
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3290 |
{ |
24969 | 3291 |
rslt = atanh(val); |
3292 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
3293 |
# ifdef __osx__ |
24969 | 3294 |
if (! isinf(rslt)) |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19616
diff
changeset
|
3295 |
# endif |
24969 | 3296 |
{ |
3297 |
if (__threadErrno == 0) { |
|
3298 |
__qMKFLOAT(newFloat, rslt); |
|
3299 |
RETURN ( newFloat ); |
|
3300 |
} |
|
3301 |
} |
|
7385 | 3302 |
} |
7410 | 3303 |
#endif |
7385 | 3304 |
%}. |
7410 | 3305 |
useFallBack notNil ifTrue:[ |
24969 | 3306 |
^ super arcTanh |
7410 | 3307 |
]. |
7385 | 3308 |
^ self class |
24969 | 3309 |
raise:#domainErrorSignal |
3310 |
receiver:self |
|
3311 |
selector:#arcTanh |
|
3312 |
arguments:#() |
|
3313 |
errorString:'bad receiver in arcTanh' |
|
7385 | 3314 |
! |
3315 |
||
3316 |
cos |
|
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3317 |
"return the cosine of the receiver (interpreted as radians)" |
7385 | 3318 |
|
3319 |
%{ /* NOCONTEXT */ |
|
18296 | 3320 |
#ifdef __SCHTEAM__ |
18299 | 3321 |
return context._RETURN( self.cos() ); |
18296 | 3322 |
#else |
7385 | 3323 |
|
3324 |
double rslt; |
|
3325 |
OBJ newFloat; |
|
3326 |
||
3327 |
__threadErrno = 0; |
|
3328 |
rslt = cos(__floatVal(self)); |
|
7393 | 3329 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
3330 |
{ |
|
24969 | 3331 |
if (__threadErrno == 0) { |
3332 |
__qMKFLOAT(newFloat, rslt); |
|
3333 |
RETURN ( newFloat ); |
|
3334 |
} |
|
7385 | 3335 |
} |
18296 | 3336 |
#endif |
7385 | 3337 |
%}. |
3338 |
^ self class |
|
24969 | 3339 |
raise:#domainErrorSignal |
3340 |
receiver:self |
|
3341 |
selector:#cos |
|
3342 |
arguments:#() |
|
3343 |
errorString:'bad receiver in cos' |
|
7385 | 3344 |
|
3345 |
"Modified: / 16.11.2001 / 14:14:26 / cg" |
|
3346 |
! |
|
3347 |
||
3348 |
cosh |
|
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3349 |
"return the hyperbolic cosine of the receiver" |
7385 | 3350 |
|
3351 |
%{ /* NOCONTEXT */ |
|
18296 | 3352 |
#ifdef __SCHTEAM__ |
18299 | 3353 |
return context._RETURN( self.cosh() ); |
18296 | 3354 |
#else |
7385 | 3355 |
|
3356 |
double rslt; |
|
3357 |
OBJ newFloat; |
|
3358 |
||
3359 |
__threadErrno = 0; |
|
3360 |
rslt = cosh(__floatVal(self)); |
|
7393 | 3361 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
3362 |
{ |
|
24969 | 3363 |
if (__threadErrno == 0) { |
3364 |
__qMKFLOAT(newFloat, rslt); |
|
3365 |
RETURN ( newFloat ); |
|
3366 |
} |
|
7385 | 3367 |
} |
18296 | 3368 |
#endif |
7385 | 3369 |
%}. |
3370 |
^ self class |
|
24969 | 3371 |
raise:#domainErrorSignal |
3372 |
receiver:self |
|
3373 |
selector:#cosh |
|
3374 |
arguments:#() |
|
3375 |
errorString:'bad receiver in cosh' |
|
7385 | 3376 |
! |
3377 |
||
3378 |
sin |
|
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3379 |
"return the sine of the receiver (interpreted as radians)" |
7385 | 3380 |
|
3381 |
%{ /* NOCONTEXT */ |
|
18296 | 3382 |
#ifdef __SCHTEAM__ |
18299 | 3383 |
return context._RETURN( self.sin() ); |
18296 | 3384 |
#else |
7385 | 3385 |
|
3386 |
double rslt; |
|
3387 |
OBJ newFloat; |
|
3388 |
||
3389 |
__threadErrno = 0; |
|
3390 |
rslt = sin(__floatVal(self)); |
|
7393 | 3391 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
3392 |
{ |
|
24969 | 3393 |
if (__threadErrno == 0) { |
3394 |
__qMKFLOAT(newFloat, rslt); |
|
3395 |
RETURN ( newFloat ); |
|
3396 |
} |
|
7385 | 3397 |
} |
18296 | 3398 |
#endif |
7385 | 3399 |
%}. |
3400 |
^ self class |
|
24969 | 3401 |
raise:#domainErrorSignal |
3402 |
receiver:self |
|
3403 |
selector:#sin |
|
3404 |
arguments:#() |
|
3405 |
errorString:'bad receiver in sin' |
|
7385 | 3406 |
|
3407 |
"Modified: / 16.11.2001 / 14:14:37 / cg" |
|
3408 |
! |
|
3409 |
||
3410 |
sinh |
|
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3411 |
"return the hyperbolic sine of the receiver" |
7385 | 3412 |
|
3413 |
%{ /* NOCONTEXT */ |
|
18296 | 3414 |
#ifdef __SCHTEAM__ |
18299 | 3415 |
return context._RETURN( self.sinh() ); |
18296 | 3416 |
#else |
7385 | 3417 |
|
3418 |
double rslt; |
|
3419 |
OBJ newFloat; |
|
3420 |
||
3421 |
__threadErrno = 0; |
|
3422 |
rslt = sinh(__floatVal(self)); |
|
7393 | 3423 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
3424 |
{ |
|
24969 | 3425 |
if (__threadErrno == 0) { |
3426 |
__qMKFLOAT(newFloat, rslt); |
|
3427 |
RETURN ( newFloat ); |
|
3428 |
} |
|
7385 | 3429 |
} |
18296 | 3430 |
#endif |
7385 | 3431 |
%}. |
3432 |
^ self class |
|
24969 | 3433 |
raise:#domainErrorSignal |
3434 |
receiver:self |
|
3435 |
selector:#sinh |
|
3436 |
arguments:#() |
|
3437 |
errorString:'bad receiver in sinh' |
|
7385 | 3438 |
! |
3439 |
||
3440 |
tan |
|
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3441 |
"return the tangens of the receiver (interpreted as radians)" |
7385 | 3442 |
|
3443 |
%{ /* NOCONTEXT */ |
|
18296 | 3444 |
#ifdef __SCHTEAM__ |
18299 | 3445 |
return context._RETURN( self.tan() ); |
18296 | 3446 |
#else |
7385 | 3447 |
|
3448 |
double rslt; |
|
3449 |
OBJ newFloat; |
|
3450 |
||
3451 |
__threadErrno = 0; |
|
3452 |
rslt = tan(__floatVal(self)); |
|
7393 | 3453 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
3454 |
{ |
|
24969 | 3455 |
if (__threadErrno == 0) { |
3456 |
__qMKFLOAT(newFloat, rslt); |
|
3457 |
RETURN ( newFloat ); |
|
3458 |
} |
|
7385 | 3459 |
} |
18296 | 3460 |
#endif |
7385 | 3461 |
%}. |
3462 |
^ self class |
|
24969 | 3463 |
raise:#domainErrorSignal |
3464 |
receiver:self |
|
3465 |
selector:#tan |
|
3466 |
arguments:#() |
|
3467 |
errorString:'bad receiver in tan' |
|
7385 | 3468 |
|
3469 |
"Modified: / 16.11.2001 / 14:14:49 / cg" |
|
3470 |
! |
|
3471 |
||
3472 |
tanh |
|
7391
55ca5a24162a
added more math functions (hyperbolic)
Claus Gittinger <cg@exept.de>
parents:
7385
diff
changeset
|
3473 |
"return the hyperbolic tangens of the receiver" |
7385 | 3474 |
|
3475 |
%{ /* NOCONTEXT */ |
|
3476 |
||
3477 |
double rslt; |
|
3478 |
OBJ newFloat; |
|
3479 |
||
3480 |
__threadErrno = 0; |
|
3481 |
rslt = tanh(__floatVal(self)); |
|
7393 | 3482 |
if (! isnan(rslt)) /* Currently all our systems support isnan() */ |
3483 |
{ |
|
24969 | 3484 |
if (__threadErrno == 0) { |
3485 |
__qMKFLOAT(newFloat, rslt); |
|
3486 |
RETURN ( newFloat ); |
|
3487 |
} |
|
7385 | 3488 |
} |
3489 |
%}. |
|
3490 |
^ self class |
|
24969 | 3491 |
raise:#domainErrorSignal |
3492 |
receiver:self |
|
3493 |
selector:#tanh |
|
3494 |
arguments:#() |
|
3495 |
errorString:'bad receiver in tanh' |
|
7385 | 3496 |
! ! |
3497 |
||
5552 | 3498 |
!Float methodsFor:'truncation & rounding'! |
701 | 3499 |
|
3500 |
ceiling |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3501 |
"return the smallest integer which is greater or equal to the receiver." |
701 | 3502 |
|
3503 |
|val| |
|
3504 |
||
3505 |
%{ |
|
18296 | 3506 |
#ifdef __SCHTEAM__ |
3507 |
ERROR("unimplemented"); |
|
3508 |
#else |
|
701 | 3509 |
double dVal; |
3510 |
||
7355 | 3511 |
dVal = ceil(__floatVal(self)); |
701 | 3512 |
/* |
3513 |
* ST-80 (and X3J20) returns integer. |
|
3514 |
*/ |
|
3515 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
|
24969 | 3516 |
RETURN ( __mkSmallInteger( (INT) dVal ) ); |
701 | 3517 |
} |
3163
212a539ee379
added #readFrom: to avoid returning an int.
Claus Gittinger <cg@exept.de>
parents:
3146
diff
changeset
|
3518 |
__qMKFLOAT(val, dVal); |
18296 | 3519 |
#endif |
701 | 3520 |
%}. |
3521 |
^ val asInteger |
|
3522 |
! |
|
3523 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3524 |
ceilingAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3525 |
"return the smallest integer-valued float greater or equal to the receiver. |
11945 | 3526 |
This is much like #ceiling, but avoids a (possibly expensive) conversion |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3527 |
of the result to an integer. |
7355 | 3528 |
It may be useful, if the result is to be further used in another float-operation." |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3529 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3530 |
%{ /* NOCONTEXT */ |
18296 | 3531 |
#ifdef __SCHTEAM__ |
18299 | 3532 |
return context._RETURN( self.ceiling() ); |
18296 | 3533 |
#else |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3534 |
double dVal; |
7355 | 3535 |
OBJ v; |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3536 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3537 |
dVal = ceil(__floatVal(self)); |
7355 | 3538 |
__qMKFLOAT(v, dVal); |
3539 |
RETURN (v); |
|
18296 | 3540 |
#endif |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3541 |
%} |
7355 | 3542 |
" |
11945 | 3543 |
0.5 ceilingAsFloat |
3544 |
-0.5 ceilingAsFloat |
|
3545 |
-1.5 ceilingAsFloat |
|
7355 | 3546 |
" |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3547 |
! |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3548 |
|
701 | 3549 |
floor |
3550 |
"return the integer nearest the receiver towards negative infinity." |
|
3551 |
||
3552 |
|val| |
|
3553 |
||
3554 |
%{ |
|
18296 | 3555 |
#ifdef __SCHTEAM__ |
3556 |
ERROR("unimplemented"); |
|
3557 |
#else |
|
701 | 3558 |
double dVal; |
3559 |
||
7355 | 3560 |
dVal = floor(__floatVal(self)); |
701 | 3561 |
/* |
3562 |
* ST-80 (and X3J20) returns integer. |
|
3563 |
*/ |
|
3564 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
|
24969 | 3565 |
RETURN ( __mkSmallInteger( (INT) dVal ) ); |
701 | 3566 |
} |
3163
212a539ee379
added #readFrom: to avoid returning an int.
Claus Gittinger <cg@exept.de>
parents:
3146
diff
changeset
|
3567 |
__qMKFLOAT(val, dVal); |
18296 | 3568 |
#endif |
701 | 3569 |
%}. |
3570 |
^ val asInteger |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3571 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3572 |
" |
11945 | 3573 |
0.5 floor |
3574 |
0.5 floorAsFloat |
|
3575 |
-0.5 floor |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3576 |
-0.5 floorAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3577 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3578 |
! |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3579 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3580 |
floorAsFloat |
7355 | 3581 |
"return the integer nearest the receiver towards negative infinity as a float. |
11945 | 3582 |
This is much like #floor, but avoids a (possibly expensive) conversion |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3583 |
of the result to an integer. |
7355 | 3584 |
It may be useful, if the result is to be further used in another float-operation." |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3585 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3586 |
%{ /* NOCONTEXT */ |
18296 | 3587 |
#ifdef __SCHTEAM__ |
18299 | 3588 |
return context._RETURN( self.floor() ); |
18296 | 3589 |
#else |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3590 |
double dVal; |
3163
212a539ee379
added #readFrom: to avoid returning an int.
Claus Gittinger <cg@exept.de>
parents:
3146
diff
changeset
|
3591 |
OBJ v; |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3592 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3593 |
dVal = floor(__floatVal(self)); |
3163
212a539ee379
added #readFrom: to avoid returning an int.
Claus Gittinger <cg@exept.de>
parents:
3146
diff
changeset
|
3594 |
__qMKFLOAT(v, dVal); |
212a539ee379
added #readFrom: to avoid returning an int.
Claus Gittinger <cg@exept.de>
parents:
3146
diff
changeset
|
3595 |
RETURN (v); |
18296 | 3596 |
#endif |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3597 |
%} |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3598 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3599 |
" |
11945 | 3600 |
0.5 floor |
3601 |
0.5 floorAsFloat |
|
3602 |
-0.5 floor |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3603 |
-0.5 floorAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3604 |
" |
701 | 3605 |
! |
3606 |
||
3896 | 3607 |
fractionPart |
3608 |
"extract the after-decimal fraction part. |
|
7355 | 3609 |
such that (self truncated + self fractionPart) = self" |
3896 | 3610 |
|
3611 |
%{ /* NOCONTEXT */ |
|
3612 |
||
14728 | 3613 |
double modf(double, double*); |
3896 | 3614 |
double frac, trunc; |
3615 |
||
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
3616 |
__threadErrno = 0; |
3896 | 3617 |
frac = modf(__floatVal(self), &trunc); |
11945 | 3618 |
if (! isnan(frac)) { |
24969 | 3619 |
if (__threadErrno == 0) { |
3620 |
RETURN (__MKFLOAT(frac)); |
|
3621 |
} |
|
3896 | 3622 |
} |
3623 |
%}. |
|
7403 | 3624 |
^ self class |
24969 | 3625 |
raise:#domainErrorSignal |
3626 |
receiver:self |
|
3627 |
selector:#fractionPart |
|
3628 |
arguments:#() |
|
3629 |
errorString:'bad receiver in fractionPart' |
|
3896 | 3630 |
|
3631 |
" |
|
11945 | 3632 |
1.6 fractionPart + 1.6 truncated |
3633 |
-1.6 fractionPart + -1.6 truncated |
|
3634 |
||
3635 |
1.0 fractionPart |
|
3636 |
2.0 fractionPart |
|
3637 |
3.0 fractionPart |
|
3638 |
4.0 fractionPart |
|
3639 |
0.5 fractionPart |
|
3640 |
0.25 fractionPart |
|
3641 |
3.14159 fractionPart |
|
3642 |
12345673.14159 fractionPart |
|
3643 |
123456731231231231.14159 fractionPart |
|
3644 |
||
3645 |
3.14159 fractionPart + 3.14159 truncated |
|
3646 |
||
3647 |
12345673.14159 fractionPart + 12345673.14159 truncated |
|
3648 |
||
3649 |
123456731231231231.14159 fractionPart + 123456731231231231.14159 truncated |
|
3896 | 3650 |
" |
3651 |
! |
|
3652 |
||
701 | 3653 |
rounded |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3654 |
"return the receiver rounded to the nearest integer" |
701 | 3655 |
|
3656 |
|val| |
|
3657 |
||
11945 | 3658 |
%{ |
18296 | 3659 |
#ifdef __SCHTEAM__ |
3660 |
ERROR("unimplemented"); |
|
3661 |
#else |
|
701 | 3662 |
double dVal; |
3663 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3664 |
dVal = __floatVal(self); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3665 |
if (dVal < 0.0) { |
24969 | 3666 |
dVal = ceil(dVal - 0.5); |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3667 |
} else { |
24969 | 3668 |
dVal = floor(dVal + 0.5); |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3669 |
} |
701 | 3670 |
/* |
3671 |
* ST-80 (and X3J20) returns integer. |
|
3672 |
*/ |
|
3673 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
|
24969 | 3674 |
RETURN ( __mkSmallInteger( (INT) dVal ) ); |
701 | 3675 |
} |
3163
212a539ee379
added #readFrom: to avoid returning an int.
Claus Gittinger <cg@exept.de>
parents:
3146
diff
changeset
|
3676 |
__qMKFLOAT(val, dVal); |
18296 | 3677 |
#endif |
701 | 3678 |
%}. |
7355 | 3679 |
^ val asInteger |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3680 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3681 |
" |
11945 | 3682 |
0.4 rounded |
3683 |
0.5 rounded |
|
3684 |
0.6 rounded |
|
3685 |
-0.4 rounded |
|
3686 |
-0.5 rounded |
|
3687 |
-0.6 rounded |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3688 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3689 |
! |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3690 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3691 |
roundedAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3692 |
"return the receiver rounded to the nearest integer as a float. |
11945 | 3693 |
This is much like #rounded, but avoids a (possibly expensive) conversion |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3694 |
of the result to an integer. |
11945 | 3695 |
It may be useful, if the result is to be further used in another |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3696 |
float-operation." |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3697 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3698 |
|val| |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3699 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3700 |
%{ /* NOCONTEXT */ |
18296 | 3701 |
#ifdef __SCHTEAM__ |
18299 | 3702 |
return context._RETURN( self.round() ); |
18296 | 3703 |
#else |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3704 |
double dVal; |
3163
212a539ee379
added #readFrom: to avoid returning an int.
Claus Gittinger <cg@exept.de>
parents:
3146
diff
changeset
|
3705 |
OBJ v; |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3706 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3707 |
dVal = __floatVal(self); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3708 |
if (dVal < 0.0) { |
24969 | 3709 |
dVal = ceil(dVal - 0.5); |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3710 |
} else { |
24969 | 3711 |
dVal = floor(dVal + 0.5); |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3712 |
} |
3163
212a539ee379
added #readFrom: to avoid returning an int.
Claus Gittinger <cg@exept.de>
parents:
3146
diff
changeset
|
3713 |
__qMKFLOAT(v, dVal); |
212a539ee379
added #readFrom: to avoid returning an int.
Claus Gittinger <cg@exept.de>
parents:
3146
diff
changeset
|
3714 |
RETURN (v); |
18296 | 3715 |
#endif |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3716 |
%} |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3717 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3718 |
" |
11945 | 3719 |
0.5 rounded |
3720 |
-0.5 rounded |
|
3721 |
0.5 roundedAsFloat |
|
3722 |
-0.5 roundedAsFloat |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3723 |
" |
701 | 3724 |
! |
3725 |
||
3726 |
truncated |
|
3727 |
"return the receiver truncated towards zero as an integer" |
|
3728 |
||
3729 |
|val| |
|
3730 |
||
3731 |
%{ |
|
18296 | 3732 |
#ifdef __SCHTEAM__ |
3733 |
ERROR("unimplemented"); |
|
3734 |
#else |
|
701 | 3735 |
double dVal; |
3736 |
||
1133 | 3737 |
dVal = __floatVal(self); |
701 | 3738 |
if (dVal < 0.0) { |
24969 | 3739 |
dVal = ceil(dVal); |
701 | 3740 |
} else { |
24969 | 3741 |
dVal = floor(dVal); |
701 | 3742 |
} |
3743 |
||
3744 |
/* |
|
3745 |
* ST-80 (and X3J20) returns integer. |
|
3746 |
*/ |
|
3747 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
|
24969 | 3748 |
RETURN ( __mkSmallInteger( (INT) dVal ) ); |
701 | 3749 |
} |
3163
212a539ee379
added #readFrom: to avoid returning an int.
Claus Gittinger <cg@exept.de>
parents:
3146
diff
changeset
|
3750 |
__qMKFLOAT(val, dVal); |
18296 | 3751 |
#endif |
701 | 3752 |
%}. |
3753 |
^ val asInteger |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3754 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3755 |
" |
11945 | 3756 |
0.5 truncated |
3757 |
-0.5 truncated |
|
3758 |
0.5 truncatedAsFloat |
|
3759 |
-0.5 truncatedAsFloat |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3760 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3761 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3762 |
! |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3763 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3764 |
truncatedAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3765 |
"return the receiver truncated towards zero as a float. |
11945 | 3766 |
This is much like #truncated, but avoids a (possibly expensive) conversion |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3767 |
of the result to an integer. |
11945 | 3768 |
It may be useful, if the result is to be further used in another |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3769 |
float-operation." |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3770 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3771 |
%{ /* NOCONTEXT */ |
18296 | 3772 |
#ifdef __SCHTEAM__ |
18300 | 3773 |
return context._RETURN( self.truncated() ); |
18296 | 3774 |
#else |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3775 |
double dVal; |
3163
212a539ee379
added #readFrom: to avoid returning an int.
Claus Gittinger <cg@exept.de>
parents:
3146
diff
changeset
|
3776 |
OBJ v; |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3777 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3778 |
dVal = __floatVal(self); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3779 |
if (dVal < 0.0) { |
24969 | 3780 |
dVal = ceil(dVal); |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3781 |
} else { |
24969 | 3782 |
dVal = floor(dVal); |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3783 |
} |
3163
212a539ee379
added #readFrom: to avoid returning an int.
Claus Gittinger <cg@exept.de>
parents:
3146
diff
changeset
|
3784 |
__qMKFLOAT(v, dVal); |
212a539ee379
added #readFrom: to avoid returning an int.
Claus Gittinger <cg@exept.de>
parents:
3146
diff
changeset
|
3785 |
RETURN (v); |
18296 | 3786 |
#endif |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3787 |
%} |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3788 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3789 |
" |
11945 | 3790 |
0.5 truncated |
3791 |
-0.5 truncated |
|
3792 |
0.5 truncatedAsFloat |
|
3793 |
-0.5 truncatedAsFloat |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3794 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3795 |
|
701 | 3796 |
! ! |
3797 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3798 |
!Float class methodsFor:'documentation'! |
701 | 3799 |
|
3800 |
version |
|
18492 | 3801 |
^ '$Header$' |
12423 | 3802 |
! |
3803 |
||
3804 |
version_CVS |
|
18492 | 3805 |
^ '$Header$' |
701 | 3806 |
! ! |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6674
diff
changeset
|
3807 |
|
14599 | 3808 |
|
2387 | 3809 |
Float initialize! |