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