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