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