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