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