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