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