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