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