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