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