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