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