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