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