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