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