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