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