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