author | Claus Gittinger <cg@exept.de> |
Thu, 27 Jun 2019 16:34:40 +0200 | |
changeset 24379 | 852be153b0ec |
parent 24330 | a06e1fba915a |
child 24439 | 2701503c110a |
permissions | -rw-r--r-- |
22785 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1197 | 3 |
" |
4 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3158 | 5 |
All Rights Reserved |
1197 | 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 |
||
17382 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1197 | 18 |
LimitedPrecisionReal variableByteSubclass:#ShortFloat |
19 |
instanceVariableNames:'' |
|
21872 | 20 |
classVariableNames:'DefaultPrintFormat DefaultPrintfFormat Pi E Epsilon Ln10 NaN |
21 |
PositiveInfinity NegativeInfinity' |
|
1197 | 22 |
poolDictionaries:'' |
23 |
category:'Magnitude-Numbers' |
|
24 |
! |
|
25 |
||
2394 | 26 |
!ShortFloat primitiveDefinitions! |
27 |
%{ |
|
28 |
||
14744 | 29 |
#include <stdio.h> |
3471 | 30 |
#include <errno.h> |
31 |
||
32 |
#ifndef __OPTIMIZE__ |
|
33 |
# define __OPTIMIZE__ |
|
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 |
3471 | 38 |
#include <math.h> |
39 |
||
16670 | 40 |
#ifndef INT32 |
41 |
# define INT32 int |
|
42 |
#endif |
|
43 |
||
3471 | 44 |
/* |
45 |
* on some systems errno is a macro ... check for it here |
|
46 |
*/ |
|
47 |
#ifndef errno |
|
48 |
extern errno; |
|
49 |
#endif |
|
50 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
51 |
#if !defined (__win32__) |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
52 |
# include <locale.h> |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
53 |
#endif |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
54 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
55 |
#if defined (__aix__) |
2394 | 56 |
# include <float.h> |
57 |
#endif |
|
4825 | 58 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
59 |
#if defined(__irix__) |
2395 | 60 |
# include <nan.h> |
61 |
#endif |
|
4825 | 62 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
63 |
#if defined(__linux__) |
4825 | 64 |
# ifndef NAN |
5822
4e56f8aadda5
Get nan.h from bits/nan.h for Linux
Stefan Vogel <sv@exept.de>
parents:
5718
diff
changeset
|
65 |
# include <bits/nan.h> |
4825 | 66 |
# endif |
2397 | 67 |
#endif |
4825 | 68 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
69 |
#if defined(__solaris__) || defined(__sunos__) |
2398 | 70 |
# include <nan.h> |
71 |
#endif |
|
2394 | 72 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
73 |
#ifdef __win32__ |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
74 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
75 |
* no finite(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
76 |
* no isnan(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
77 |
*/ |
7419 | 78 |
# ifndef isnan |
79 |
# define isnan(x) \ |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
80 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
81 |
(((unsigned int *)(&x))[1] == 0xFFF80000)) |
7419 | 82 |
# endif |
83 |
||
84 |
# ifndef isPositiveInfinity |
|
85 |
# define isPositiveInfinity(x) \ |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
86 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
87 |
(((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
|
88 |
# endif |
7419 | 89 |
|
90 |
# ifndef isNegativeInfinity |
|
14931 | 91 |
# define isNegativeInfinity(x) \ |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
92 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
93 |
(((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
|
94 |
# endif |
7419 | 95 |
|
96 |
# ifndef isinf |
|
97 |
# define isinf(x) \ |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
98 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
99 |
((((unsigned int *)(&x))[1] & 0x7FF00000) == 0x7FF00000)) |
7419 | 100 |
# endif |
101 |
||
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
102 |
# ifndef isfinite |
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
103 |
# define isfinite(x) (!isinf(x) && !isnan(x)) |
7419 | 104 |
# endif |
105 |
||
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
106 |
# ifndef isnanf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
107 |
# define isnanf(x) \ |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
108 |
(((unsigned int *)(&x))[0] == 0xFFC00000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
109 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
110 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
111 |
# ifndef isPositiveInfinityf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
112 |
# define isPositiveInfinityf(x) \ |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
113 |
(((unsigned int *)(&x))[0] == 0x7F800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
114 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
115 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
116 |
# ifndef isNegativeInfinityf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
117 |
# define isNegativeInfinityf(x) \ |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
118 |
(((unsigned int *)(&x))[0] == 0xFF800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
119 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
120 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
121 |
# ifndef isinff |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
122 |
# define isinff(x) \ |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
123 |
((((unsigned int *)(&x))[0] & 0x7FFFFFFF) == 0x7F800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
124 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
125 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
126 |
# ifndef isfinitef |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
127 |
# define isfinitef(x) (!isinff(x) && !isnanf(x)) |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
128 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
129 |
|
7419 | 130 |
# define NO_ASINH |
131 |
# define NO_ACOSH |
|
132 |
# define NO_ATANH |
|
22919 | 133 |
|
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
134 |
# ifdef __BORLANDC__ |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
135 |
# define NO_FMODF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
136 |
# define NO_MODFF |
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
137 |
# define NO_LOG10F |
21976 | 138 |
# define NO_LOGF |
22919 | 139 |
# define NO_ABSF |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
140 |
# endif |
14931 | 141 |
|
142 |
# ifdef __MINGW__ |
|
22491 | 143 |
# ifndef _STRING_H_INCLUDED_ |
144 |
# include <string.h> |
|
145 |
# define _STRING_H_INCLUDED_ |
|
146 |
# endif |
|
14931 | 147 |
# endif |
148 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
149 |
#endif /* __win32__ */ |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
150 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
151 |
#ifdef __solaris__ |
8984 | 152 |
# ifndef isfinite |
153 |
# define isfinite(f) finite((double)(f)) |
|
154 |
# endif |
|
155 |
#endif |
|
156 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
157 |
#ifdef __realIX__ |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
158 |
# ifndef isfinite |
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
159 |
# define isfinite(x) 1 |
3414 | 160 |
# endif |
8706 | 161 |
#endif /* realIX */ |
162 |
||
9896 | 163 |
#ifndef isfinitef |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
164 |
# define isfinitef(x) isfinite(x) |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
165 |
#endif |
9896 | 166 |
#ifndef isnanf |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
167 |
# define isnanf(x) isnan(x) |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
168 |
#endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
169 |
|
2394 | 170 |
%} |
171 |
! ! |
|
172 |
||
1880 | 173 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 174 |
|
175 |
copyright |
|
176 |
" |
|
177 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3158 | 178 |
All Rights Reserved |
1197 | 179 |
|
180 |
This software is furnished under a license and may be used |
|
181 |
only in accordance with the terms of that license and with the |
|
182 |
inclusion of the above copyright notice. This software may not |
|
183 |
be provided or otherwise made available to, or used by, any |
|
184 |
other person. No title to or ownership of the software is |
|
185 |
hereby transferred. |
|
186 |
" |
|
187 |
||
188 |
||
189 |
! |
|
190 |
||
191 |
documentation |
|
192 |
" |
|
11945 | 193 |
ShortFloats represent rational numbers with limited precision. |
22785 | 194 |
They use the C-compiler's 'float' format, which is usually the IEEE single float format. |
7380 | 195 |
|
196 |
In contrast to Floats (which use the C-compilers 64bit 'double' format), |
|
197 |
ShortFloats give you 32 bit floats. |
|
1197 | 198 |
|
199 |
Notice, that ST/X Floats are what Doubles are in ST-80 and ShortFloats are |
|
14719 | 200 |
ST-80's Floats respectively. The reason was to make ST/X's floats compatible |
22785 | 201 |
to bothe visualWorks and other smalltalks, which use C-doubles for the Float class |
202 |
(i.e. VisualST and V'Age). |
|
14719 | 203 |
Thus, STX's Float precision is not worse than that of other ST's. |
204 |
||
1197 | 205 |
WARNING: |
22785 | 206 |
The layout of shortFloat instances is known by the runtime system and the compiler; |
207 |
you may not add instance variables here. |
|
208 |
Also, subclassing is complicated by the fact, that the VM creates floats/shortFloats, |
|
209 |
and does some of its float-checks by an identity compare with the ShortFloat-class. |
|
210 |
(i.e. your subclasses instances may not be recognized as float-like objects, |
|
211 |
thus mixed mode arithmetic will always coerce them, effectively slowing things down). |
|
212 |
This may be changed, to use a flag bit in the class. |
|
1197 | 213 |
|
7380 | 214 |
Mixed mode arithmetic: |
22785 | 215 |
shortFloat op shortFloat -> shortFloat |
216 |
shortFloat op fix -> shortFloat |
|
217 |
shortFloat op fraction -> shortFloat |
|
218 |
shortFloat op integer -> shortFloat |
|
219 |
shortFloat op longFloat -> longFloat |
|
220 |
shortFloat op float -> float |
|
221 |
shortFloat op complex -> complex |
|
1295 | 222 |
|
7405 | 223 |
Representation: |
22785 | 224 |
32bit single precision IEEE floats |
225 |
23 bit mantissa, |
|
226 |
8 bit exponent, |
|
227 |
6 decimal digits (approx) |
|
7405 | 228 |
|
229 |
Range and Precision of Storage Formats: see LimitedPrecisionReal >> documentation |
|
230 |
||
1295 | 231 |
[author:] |
22785 | 232 |
Claus Gittinger |
1556 | 233 |
|
234 |
[see also:] |
|
22785 | 235 |
Number |
236 |
Float LongFloat Fraction FixedPoint Integer Complex |
|
237 |
FloatArray DoubleArray |
|
1197 | 238 |
" |
239 |
! ! |
|
240 |
||
1880 | 241 |
!ShortFloat class methodsFor:'instance creation'! |
1207 | 242 |
|
243 |
basicNew |
|
244 |
"return a new shortFloat - here we return 0.0 |
|
245 |
- shortFloats are usually NOT created this way ... |
|
246 |
Its implemented here to allow things like binary store & load |
|
247 |
of shortFloats. (but even this support will go away eventually, its not |
|
248 |
a good idea to store the bits of a float - the reader might have a |
|
11945 | 249 |
totally different representation - so floats will eventually be |
1207 | 250 |
binary stored in a device independent format." |
251 |
||
252 |
%{ /* NOCONTEXT */ |
|
253 |
OBJ newFloat; |
|
254 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
255 |
__qMKSFLOAT(newFloat, 0.0); |
1207 | 256 |
RETURN (newFloat); |
257 |
%} |
|
3158 | 258 |
! |
259 |
||
4297 | 260 |
fastFromString:aString at:startIndex |
11945 | 261 |
"return the next ShortFloat from the string starting at startIndex. |
4297 | 262 |
No spaces are skipped. |
23478 | 263 |
Raises an exception, if the startIndex is not valid. |
264 |
Returns garbage if the argument string is not a valid float number. |
|
265 |
||
266 |
This is a specially tuned entry (using a low-level C-call to atof). |
|
267 |
It has been added to allow high speed string decomposition |
|
268 |
into numbers, especially for mass-data (reading millions of floats)." |
|
11945 | 269 |
|
270 |
%{ /* NOCONTEXT */ |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
271 |
if (__isStringLike(aString) && __isSmallInteger(startIndex)) { |
23475 | 272 |
char *cp = (char *)(__stringVal(aString)); |
273 |
int idx = __intVal(startIndex) - 1; |
|
274 |
double atof(const char *); |
|
275 |
||
276 |
if ((unsigned)idx < __stringSize(aString)) { |
|
277 |
double val = atof(cp + idx); |
|
278 |
RETURN (__MKSFLOAT(val)); |
|
279 |
} |
|
4297 | 280 |
} |
281 |
%}. |
|
282 |
self primitiveFailed. |
|
283 |
||
284 |
" |
|
11945 | 285 |
ShortFloat fastFromString:'123.45' at:1 |
286 |
ShortFloat fastFromString:'123.45' at:2 |
|
287 |
ShortFloat fastFromString:'123.45E4' at:1 |
|
288 |
ShortFloat fastFromString:'hello123.45E4' at:6 |
|
289 |
ShortFloat fastFromString:'12345' at:1 |
|
290 |
ShortFloat fastFromString:'12345' at:2 |
|
291 |
ShortFloat fastFromString:'12345' at:3 |
|
292 |
ShortFloat fastFromString:'12345' at:4 |
|
293 |
ShortFloat fastFromString:'12345' at:5 |
|
294 |
ShortFloat fastFromString:'12345' at:6 |
|
295 |
ShortFloat fastFromString:'12345' at:0 |
|
296 |
ShortFloat fastFromString:'hello123.45E4' at:1 |
|
23478 | 297 |
" |
298 |
||
299 |
" |
|
4297 | 300 |
Time millisecondsToRun:[ |
23478 | 301 |
1000000 timesRepeat:[ |
23475 | 302 |
ShortFloat readFrom:'123.45' |
303 |
] |
|
4297 | 304 |
] |
305 |
" |
|
306 |
||
307 |
" |
|
308 |
Time millisecondsToRun:[ |
|
23478 | 309 |
1000000 timesRepeat:[ |
23475 | 310 |
ShortFloat fastFromString:'123.45' at:1 |
311 |
] |
|
4297 | 312 |
] |
313 |
" |
|
23475 | 314 |
|
23478 | 315 |
"Modified (comment): / 27-10-2018 / 08:58:13 / Claus Gittinger" |
4297 | 316 |
! |
317 |
||
15015 | 318 |
fromIEEE32Bit: anInteger |
319 |
"creates a float, given the four native float bytes as an integer" |
|
320 |
||
321 |
%{ /* NOCONTEXT */ |
|
322 |
||
323 |
REGISTER union { |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
324 |
unsigned int i; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
325 |
float f; |
15015 | 326 |
} r; |
327 |
||
328 |
r.i = __unsignedLongIntVal( anInteger ); |
|
15019 | 329 |
RETURN( __MKSFLOAT(r.f) ); |
15015 | 330 |
%} |
17220 | 331 |
|
332 |
" |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
333 |
ShortFloat fromIEEE32Bit:(#[64 73 15 219] asInteger) |
17220 | 334 |
" |
1207 | 335 |
! ! |
336 |
||
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
337 |
!ShortFloat class methodsFor:'accessing'! |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
338 |
|
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
339 |
defaultPrintFormat |
16670 | 340 |
^ DefaultPrintFormat |
341 |
! |
|
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
342 |
|
16670 | 343 |
defaultPrintFormat:aString |
344 |
DefaultPrintFormat := aString. |
|
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
345 |
! ! |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
346 |
|
24323 | 347 |
|
7356 | 348 |
!ShortFloat class methodsFor:'binary storage'! |
349 |
||
350 |
readBinaryIEEESingleFrom:aStream |
|
351 |
"read a float value from the binary stream, aStream, |
|
14924 | 352 |
interpreting the next bytes as an IEEE formatted 4-byte float. |
353 |
The bytes are read in the native byte order (i.e.lsb on intel)" |
|
1197 | 354 |
|
7356 | 355 |
|f| |
1197 | 356 |
|
21878 | 357 |
f := self basicNew. |
15012 | 358 |
self readBinaryIEEESingleFrom:aStream into:f MSB:(UninterpretedBytes isBigEndian). |
359 |
^ f |
|
360 |
||
361 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
362 |
||
363 |
"Created: / 16-04-1996 / 21:00:35 / cg" |
|
21878 | 364 |
"Modified: / 21-06-2017 / 10:38:31 / cg" |
15012 | 365 |
! |
366 |
||
367 |
readBinaryIEEESingleFrom:aStream MSB:msbFirst |
|
368 |
"read a float value from the binary stream, aStream, |
|
369 |
interpreting the next bytes as an IEEE formatted 4-byte float. |
|
15014 | 370 |
The bytes are read in the soecified byte order" |
15012 | 371 |
|
372 |
|f| |
|
373 |
||
21878 | 374 |
f := self basicNew. |
15012 | 375 |
self readBinaryIEEESingleFrom:aStream into:f MSB:msbFirst. |
7356 | 376 |
^ f |
1197 | 377 |
|
9636 | 378 |
"not part of libboss, as this is also used by others (TIFFReader)" |
379 |
||
380 |
"Created: / 16-04-1996 / 21:00:35 / cg" |
|
21878 | 381 |
"Modified: / 21-06-2017 / 10:38:35 / cg" |
1197 | 382 |
! |
383 |
||
19617 | 384 |
readBinaryIEEESingleFrom:aStream into:aBasicNewShortFloat |
7356 | 385 |
"read a float value from the binary stream, aStream, |
14922 | 386 |
interpreting the next bytes as an IEEE formatted 4-byte float. |
14924 | 387 |
The bytes are read in the native byte order (i.e.lsb on intel)" |
14922 | 388 |
|
19617 | 389 |
^ self readBinaryIEEESingleFrom:aStream into:aBasicNewShortFloat MSB:(UninterpretedBytes isBigEndian) |
14922 | 390 |
|
391 |
"Modified: / 23-08-2006 / 16:01:52 / cg" |
|
392 |
! |
|
393 |
||
19617 | 394 |
readBinaryIEEESingleFrom:aStream into:aBasicNewShortFloat MSB:msb |
14922 | 395 |
"read a float value from the binary stream, aStream, |
396 |
interpreting the next bytes as an IEEE formatted 4-byte float. |
|
397 |
If msb is true, the stream bytes are most-significant-first." |
|
7356 | 398 |
|
21878 | 399 |
aBasicNewShortFloat class == self ifFalse:[self error:'not a ShortFloat']. |
6574 | 400 |
|
7356 | 401 |
" |
402 |
this implementation is wrong: does not work on non-IEEE machines |
|
403 |
(to date all machines where ST/X is running on use |
|
404 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
405 |
" |
|
406 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
407 |
||
14922 | 408 |
(UninterpretedBytes isBigEndian == msb) ifFalse:[ |
21976 | 409 |
"swap the bytes" |
410 |
4 to:1 by:-1 do:[:i | |
|
411 |
aBasicNewShortFloat basicAt:i put:(aStream next) |
|
412 |
]. |
|
413 |
^ self |
|
7356 | 414 |
]. |
415 |
1 to:4 do:[:i | |
|
21976 | 416 |
aBasicNewShortFloat basicAt:i put:aStream next |
7356 | 417 |
] |
9636 | 418 |
|
419 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
420 |
||
21878 | 421 |
"Modified: / 21-06-2017 / 10:36:28 / cg" |
6574 | 422 |
! |
423 |
||
7356 | 424 |
storeBinaryIEEESingle:aFloat on:aStream |
425 |
"store aFloat as an IEEE formatted 4-byte float |
|
14924 | 426 |
onto the binary stream, aStream. |
427 |
The bytes are written in the native byte order (i.e.lsb on intel)" |
|
428 |
||
429 |
self storeBinaryIEEESingle:aFloat on:aStream MSB:(UninterpretedBytes isBigEndian). |
|
430 |
! |
|
431 |
||
432 |
storeBinaryIEEESingle:aFloat on:aStream MSB:msb |
|
433 |
"store aFloat as an IEEE formatted 4-byte float |
|
434 |
onto the binary stream, aStream. |
|
435 |
If msb is true, the stream bytes are written most-significant-first." |
|
7356 | 436 |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
437 |
|float| |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
438 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
439 |
float := aFloat asShortFloat. |
7356 | 440 |
|
441 |
" |
|
442 |
this implementation is wrong: does not work on non-IEEE machines |
|
443 |
(to date all machines where ST/X is running on use |
|
444 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
445 |
" |
|
446 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
6574 | 447 |
|
14924 | 448 |
(UninterpretedBytes isBigEndian == msb) ifFalse:[ |
14931 | 449 |
"swap the bytes" |
450 |
4 to:1 by:-1 do:[:i | |
|
451 |
aStream nextPut:(float basicAt:i). |
|
452 |
]. |
|
453 |
^ self |
|
7356 | 454 |
]. |
455 |
1 to:4 do:[:i | |
|
14931 | 456 |
aStream nextPut:(float basicAt:i). |
7356 | 457 |
] |
9636 | 458 |
|
459 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
460 |
||
461 |
"Modified: / 23-08-2006 / 16:01:55 / cg" |
|
7356 | 462 |
! ! |
463 |
||
16670 | 464 |
!ShortFloat class methodsFor:'class initialization'! |
465 |
||
466 |
initialize |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
467 |
DefaultPrintFormat := '.7'. "/ print 7 valid digits |
21868 | 468 |
DefaultPrintfFormat := '%7f'. |
16670 | 469 |
|
470 |
" |
|
471 |
self initialize |
|
472 |
" |
|
473 |
! ! |
|
474 |
||
21872 | 475 |
!ShortFloat class methodsFor:'coercing & converting'! |
476 |
||
477 |
coerce:aNumber |
|
478 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
|
479 |
||
480 |
^ aNumber asShortFloat. |
|
481 |
! ! |
|
482 |
||
7356 | 483 |
!ShortFloat class methodsFor:'constants'! |
6574 | 484 |
|
21858 | 485 |
NaN |
486 |
"return a shortFloat which represents not-a-Number (i.e. an invalid number)" |
|
487 |
||
488 |
NaN isNil ifTrue:[ |
|
24261 | 489 |
NaN := super NaN |
21858 | 490 |
]. |
491 |
^ NaN |
|
492 |
||
24261 | 493 |
" |
494 |
self NaN |
|
495 |
" |
|
496 |
||
21858 | 497 |
"Created: / 20-06-2017 / 13:44:12 / cg" |
24261 | 498 |
"Modified (comment): / 06-06-2019 / 16:53:34 / Claus Gittinger" |
21858 | 499 |
! |
500 |
||
7392 | 501 |
e |
502 |
"return the constant e as ShortFloat" |
|
503 |
||
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
504 |
E isNil ifTrue:[ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
505 |
E := Float e asShortFloat |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
506 |
]. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
507 |
^ E |
7392 | 508 |
! |
509 |
||
18128 | 510 |
emax |
511 |
"Answer the maximum exponent for this representation." |
|
512 |
||
513 |
^127 |
|
514 |
! |
|
515 |
||
18130 | 516 |
emin |
517 |
"Answer the minimum exponent for this representation." |
|
518 |
||
519 |
^-126 |
|
520 |
! |
|
521 |
||
21858 | 522 |
infinity |
24293 | 523 |
"return a shortFloat which represents positive infinity (for my instances)" |
21858 | 524 |
|
525 |
PositiveInfinity isNil ifTrue:[ |
|
24293 | 526 |
PositiveInfinity := Float infinity asShortFloat |
21858 | 527 |
]. |
528 |
^ PositiveInfinity |
|
529 |
||
530 |
"Created: / 20-06-2017 / 13:44:45 / cg" |
|
24293 | 531 |
"Modified (comment): / 08-06-2019 / 14:32:27 / Claus Gittinger" |
21858 | 532 |
! |
533 |
||
21829 | 534 |
ln10 |
535 |
"return the natural logarithm of 10 as a shortFloat" |
|
536 |
||
537 |
Ln10 isNil ifTrue:[ |
|
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
538 |
Ln10 := Float ln10 asShortFloat |
21829 | 539 |
]. |
540 |
^ Ln10 |
|
541 |
||
542 |
" |
|
543 |
self ln10 |
|
544 |
" |
|
545 |
||
546 |
"Created: / 16-06-2017 / 11:09:37 / cg" |
|
547 |
! |
|
548 |
||
21858 | 549 |
negativeInfinity |
24303 | 550 |
"return a shortFloat which represents negative infinity (for my instances). |
551 |
Warning: do not compare equal against infinities; |
|
552 |
instead, check using isFinite or isInfinite" |
|
21858 | 553 |
|
554 |
NegativeInfinity isNil ifTrue:[ |
|
24293 | 555 |
NegativeInfinity := Float negativeInfinity asShortFloat |
21858 | 556 |
]. |
557 |
^ NegativeInfinity |
|
558 |
||
559 |
"Created: / 20-06-2017 / 13:45:08 / cg" |
|
24303 | 560 |
"Modified (comment): / 09-06-2019 / 12:57:11 / Claus Gittinger" |
21858 | 561 |
! |
562 |
||
7392 | 563 |
pi |
564 |
"return the constant pi as ShortFloat" |
|
565 |
||
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
566 |
Pi isNil ifTrue:[ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
567 |
Pi := Float pi asShortFloat |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
568 |
]. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
569 |
^ Pi |
7392 | 570 |
! |
571 |
||
1197 | 572 |
unity |
1260 | 573 |
"return the neutral element for multiplication (1.0) as ShortFloat" |
1197 | 574 |
|
575 |
^ 1.0 asShortFloat |
|
576 |
||
1260 | 577 |
"Modified: 23.4.1996 / 09:26:51 / cg" |
1197 | 578 |
! |
579 |
||
580 |
zero |
|
1260 | 581 |
"return the neutral element for addition (0.0) as ShortFloat" |
1197 | 582 |
|
583 |
^ 0.0 asShortFloat |
|
584 |
||
1260 | 585 |
"Modified: 23.4.1996 / 09:26:45 / cg" |
1197 | 586 |
! ! |
587 |
||
24323 | 588 |
|
21829 | 589 |
!ShortFloat class methodsFor:'queries'! |
21149 | 590 |
|
21829 | 591 |
defaultPrintPrecision |
592 |
"return the number of decimal digits printed by default" |
|
593 |
||
594 |
^ 5 |
|
595 |
||
596 |
" |
|
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
597 |
ShortFloat defaultPrintPrecision |
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
598 |
Float defaultPrintPrecision |
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
599 |
LongFloat defaultPrintPrecision |
21829 | 600 |
" |
601 |
||
602 |
"Created: / 17-06-2017 / 02:59:31 / cg" |
|
603 |
! |
|
1197 | 604 |
|
24323 | 605 |
epsilon |
606 |
"return the maximum relative spacing of instances of mySelf |
|
607 |
(i.e. the value-delta of the least significant bit)" |
|
608 |
||
609 |
Epsilon isNil ifTrue:[ |
|
610 |
Epsilon := self computeEpsilon. |
|
611 |
]. |
|
612 |
^ Epsilon |
|
613 |
||
614 |
" |
|
615 |
self epsilon |
|
616 |
" |
|
617 |
||
618 |
"Modified (comment): / 21-06-2017 / 13:56:47 / cg" |
|
619 |
"Modified (comment): / 10-05-2018 / 01:09:13 / stefan" |
|
620 |
"Modified (comment): / 10-06-2019 / 21:22:03 / Claus Gittinger" |
|
621 |
! |
|
622 |
||
6898 | 623 |
exponentCharacter |
24324 | 624 |
"return the character used to print between mantissa an exponent. |
625 |
Also used by the scanner when reading numbers." |
|
626 |
||
6898 | 627 |
^ $e |
24324 | 628 |
|
629 |
"Modified (comment): / 10-06-2019 / 21:27:52 / Claus Gittinger" |
|
6898 | 630 |
! |
631 |
||
1197 | 632 |
isBuiltInClass |
1264 | 633 |
"return true if this class is known by the run-time-system. |
634 |
Here, true is returned for myself, false for subclasses." |
|
1197 | 635 |
|
636 |
^ self == ShortFloat |
|
637 |
||
1264 | 638 |
"Modified: 23.4.1996 / 16:00:23 / cg" |
3427
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
639 |
! |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
640 |
|
7356 | 641 |
numBitsInExponent |
642 |
"answer the number of bits in the exponent |
|
643 |
This is an IEEE float, where 8 bits are available: |
|
24204 | 644 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
7356 | 645 |
" |
646 |
||
647 |
^ 8 |
|
24204 | 648 |
|
649 |
" |
|
650 |
1.0 asShortFloat numBitsInExponent |
|
651 |
" |
|
652 |
||
653 |
"Modified (comment): / 28-05-2019 / 08:55:35 / Claus Gittinger" |
|
7356 | 654 |
! |
655 |
||
656 |
numBitsInMantissa |
|
657 |
"answer the number of bits in the mantissa. |
|
658 |
This is an IEEE float, where 23 bits (the hidden one is not counted here) are available: |
|
11945 | 659 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
7356 | 660 |
" |
661 |
||
662 |
^ 23 |
|
663 |
! |
|
664 |
||
665 |
precision |
|
24261 | 666 |
"answer the precision (the number of bits in the mantissa) of a ShortFloat (in bits) |
11945 | 667 |
This is an IEEE float, where only the fraction from the normalized mantissa is stored |
9149 | 668 |
and so there is a hidden bit and the mantissa is actually represented by 24 binary digits |
669 |
(although only 23 are needed in the binary representation)" |
|
7356 | 670 |
|
671 |
^ 24 |
|
19325 | 672 |
|
673 |
" |
|
674 |
self numBitsInMantissa + 1 |
|
675 |
self precision |
|
676 |
" |
|
24261 | 677 |
|
678 |
"Modified (comment): / 06-06-2019 / 13:23:53 / Claus Gittinger" |
|
7356 | 679 |
! |
680 |
||
681 |
radix |
|
682 |
"answer the radix of a ShortFloats exponent |
|
683 |
This is an IEEE float, which is represented as binary" |
|
684 |
||
9149 | 685 |
^ 2 "must be careful here, whenever ST/X is used on a VAX or a 370" |
1197 | 686 |
! ! |
687 |
||
688 |
!ShortFloat methodsFor:'arithmetic'! |
|
689 |
||
690 |
* aNumber |
|
11730 | 691 |
"return the product of the receiver and the argument." |
1197 | 692 |
|
693 |
%{ /* NOCONTEXT */ |
|
694 |
||
695 |
OBJ newFloat; |
|
696 |
float result; |
|
697 |
double dResult; |
|
698 |
||
699 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 700 |
result = __shortFloatVal(self) * (float)(__intVal(aNumber)); |
1197 | 701 |
retResult: |
11945 | 702 |
__qMKSFLOAT(newFloat, result); |
703 |
RETURN ( newFloat ); |
|
704 |
} |
|
7356 | 705 |
if (__isShortFloat(aNumber)) { |
11945 | 706 |
result = __shortFloatVal(self) * __shortFloatVal(aNumber); |
707 |
goto retResult; |
|
708 |
} |
|
7356 | 709 |
if (__isFloatLike(aNumber)) { |
11945 | 710 |
dResult = (double) __shortFloatVal(self)* __floatVal(aNumber); |
711 |
__qMKFLOAT(newFloat, dResult); |
|
712 |
RETURN ( newFloat ); |
|
1197 | 713 |
} |
714 |
%}. |
|
715 |
^ aNumber productFromShortFloat:self |
|
716 |
! |
|
717 |
||
718 |
+ aNumber |
|
719 |
"return the sum of the receiver and the argument, aNumber" |
|
720 |
||
721 |
%{ /* NOCONTEXT */ |
|
722 |
||
723 |
OBJ newFloat; |
|
724 |
float result; |
|
725 |
double dResult; |
|
726 |
||
727 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 728 |
result = __shortFloatVal(self) + (float)(__intVal(aNumber)); |
1197 | 729 |
retResult: |
11945 | 730 |
__qMKSFLOAT(newFloat, result); |
731 |
RETURN ( newFloat ); |
|
732 |
} |
|
7356 | 733 |
if (__isShortFloat(aNumber)) { |
11945 | 734 |
result = __shortFloatVal(self) + __shortFloatVal(aNumber); |
735 |
goto retResult; |
|
736 |
} |
|
7356 | 737 |
if (__isFloatLike(aNumber)) { |
11945 | 738 |
dResult = (double) __shortFloatVal(self) + __floatVal(aNumber); |
739 |
__qMKFLOAT(newFloat, dResult); |
|
740 |
RETURN ( newFloat ); |
|
1197 | 741 |
} |
742 |
%}. |
|
743 |
^ aNumber sumFromShortFloat:self |
|
744 |
! |
|
745 |
||
746 |
- aNumber |
|
747 |
"return the difference of the receiver and the argument, aNumber" |
|
748 |
||
749 |
%{ /* NOCONTEXT */ |
|
750 |
||
751 |
OBJ newFloat; |
|
752 |
float result; |
|
753 |
double dResult; |
|
754 |
||
755 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 756 |
result = __shortFloatVal(self) - (float)(__intVal(aNumber)); |
1197 | 757 |
retResult: |
11945 | 758 |
__qMKSFLOAT(newFloat, result); |
759 |
RETURN ( newFloat ); |
|
760 |
} |
|
7356 | 761 |
if (__isShortFloat(aNumber)) { |
11945 | 762 |
result = __shortFloatVal(self) - __shortFloatVal(aNumber); |
763 |
goto retResult; |
|
764 |
} |
|
7356 | 765 |
if (__isFloatLike(aNumber)) { |
11945 | 766 |
dResult = (double) __shortFloatVal(self) - __floatVal(aNumber); |
767 |
__qMKFLOAT(newFloat, dResult); |
|
768 |
RETURN ( newFloat ); |
|
1197 | 769 |
} |
770 |
%}. |
|
771 |
^ aNumber differenceFromShortFloat:self |
|
772 |
! |
|
773 |
||
774 |
/ aNumber |
|
775 |
"return the quotient of the receiver and the argument, aNumber" |
|
776 |
||
777 |
%{ /* NOCONTEXT */ |
|
778 |
||
779 |
OBJ newFloat; |
|
780 |
float result, val; |
|
781 |
double dResult, dVal; |
|
782 |
||
783 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 784 |
if (aNumber != __mkSmallInteger(0)) { |
785 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
1197 | 786 |
retResult: |
11945 | 787 |
__qMKSFLOAT(newFloat, result); |
788 |
RETURN ( newFloat ); |
|
789 |
} |
|
790 |
} |
|
7356 | 791 |
if (__isShortFloat(aNumber)) { |
11945 | 792 |
val = __shortFloatVal(aNumber); |
793 |
if (val != 0.0) { |
|
794 |
result = __shortFloatVal(self) / val; |
|
795 |
goto retResult; |
|
796 |
} |
|
797 |
} |
|
7356 | 798 |
if (__isFloatLike(aNumber)) { |
11945 | 799 |
dVal = __floatVal(aNumber); |
800 |
if (dVal != 0.0) { |
|
801 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
802 |
__qMKFLOAT(newFloat, dResult); |
|
803 |
RETURN ( newFloat ); |
|
804 |
} |
|
1197 | 805 |
} |
806 |
%}. |
|
807 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
11945 | 808 |
" |
809 |
No, you shalt not divide by zero |
|
810 |
" |
|
811 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
1197 | 812 |
]. |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
813 |
^ aNumber quotientFromShortFloat:self |
1197 | 814 |
! |
815 |
||
14710 | 816 |
abs |
817 |
"return the absolute value of the receiver |
|
818 |
reimplemented here for speed" |
|
819 |
||
820 |
%{ /* NOCONTEXT */ |
|
821 |
||
822 |
OBJ newFloat; |
|
823 |
float val = __shortFloatVal(self); |
|
824 |
||
825 |
if (val < 0.0) { |
|
826 |
__qMKSFLOAT(newFloat, -val); |
|
827 |
RETURN ( newFloat ); |
|
828 |
} |
|
829 |
RETURN (self); |
|
830 |
%}. |
|
831 |
||
832 |
" |
|
833 |
3.0 asShortFloat abs |
|
834 |
-3.0 asShortFloat abs |
|
835 |
" |
|
836 |
! |
|
837 |
||
838 |
negated |
|
839 |
"return myself negated" |
|
840 |
||
841 |
%{ /* NOCONTEXT */ |
|
842 |
OBJ newFloat; |
|
843 |
float rslt = - __shortFloatVal(self); |
|
844 |
||
845 |
__qMKSFLOAT(newFloat, rslt); |
|
846 |
RETURN ( newFloat ); |
|
847 |
%}. |
|
848 |
^ 0.0 - self |
|
849 |
||
850 |
! |
|
851 |
||
14690 | 852 |
rem: aNumber |
853 |
"return the floating point remainder of the receiver and the argument, aNumber" |
|
854 |
||
855 |
%{ /* NOCONTEXT */ |
|
856 |
||
857 |
/* |
|
858 |
* notice: |
|
859 |
* the following inline code handles some common cases, |
|
860 |
* and exists as an optimization, to speed up those cases. |
|
861 |
* |
|
862 |
* Conceptionally, (and for most other argument types), |
|
863 |
* mixed arithmetic is implemented by double dispatching |
|
864 |
* (see the message send at the bottom) |
|
865 |
*/ |
|
866 |
OBJ newFloat; |
|
867 |
float result, val; |
|
868 |
double dResult, dVal; |
|
869 |
||
870 |
if (__isSmallInteger(aNumber)) { |
|
871 |
if (aNumber != __mkSmallInteger(0)) { |
|
872 |
val = (float)__intVal(aNumber); |
|
873 |
computeResult: |
|
14709
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
874 |
#ifdef NO_FMODF |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
875 |
dResult = fmod((double)__shortFloatVal(self), (double)val) ; |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
876 |
result = (float)dResult; |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
877 |
#else |
14690 | 878 |
result = fmodf(__shortFloatVal(self), val) ; |
14709
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
879 |
#endif |
14690 | 880 |
__qMKSFLOAT(newFloat, result); |
881 |
RETURN ( newFloat ); |
|
882 |
} |
|
883 |
} else if (__isFloatLike(aNumber)) { |
|
884 |
dVal = __floatVal(aNumber); |
|
885 |
if (dVal != 0.0) { |
|
886 |
dResult = fmod((double)(__shortFloatVal(self)), dVal) ; |
|
887 |
__qMKFLOAT(newFloat, dResult); |
|
888 |
RETURN ( newFloat ); |
|
889 |
} |
|
890 |
} else if (__isShortFloat(aNumber)) { |
|
891 |
val = __shortFloatVal(aNumber); |
|
892 |
if (val != 0.0) { |
|
893 |
goto computeResult; |
|
894 |
} |
|
895 |
} |
|
896 |
%}. |
|
897 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
898 |
" |
|
899 |
No, you shalt not divide by zero |
|
900 |
" |
|
901 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
902 |
]. |
|
903 |
^ aNumber remainderFromShortFloat:self |
|
904 |
! |
|
905 |
||
3147 | 906 |
uncheckedDivide:aNumber |
7356 | 907 |
"return the quotient of the receiver and the argument, aNumber. |
7471 | 908 |
Do not check for divide by zero (return NaN or Infinity). |
7356 | 909 |
This operation is provided for emulators of other languages/semantics, |
910 |
where no exception is raised for these results (i.e. Java). |
|
18841 | 911 |
It is only defined if the argument's type is the same as the receiver's." |
3147 | 912 |
|
913 |
%{ /* NOCONTEXT */ |
|
914 |
||
915 |
OBJ newFloat; |
|
916 |
float result, val; |
|
917 |
double dResult, dVal; |
|
918 |
||
919 |
if (__isSmallInteger(aNumber)) { |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
920 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
3147 | 921 |
retResult: |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
922 |
__qMKSFLOAT(newFloat, result); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
923 |
RETURN ( newFloat ); |
11945 | 924 |
} |
7356 | 925 |
if (__isShortFloat(aNumber)) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
926 |
val = __shortFloatVal(aNumber); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
927 |
result = __shortFloatVal(self) / val; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
928 |
goto retResult; |
11945 | 929 |
} |
7356 | 930 |
if (__isFloatLike(aNumber)) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
931 |
dVal = __floatVal(aNumber); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
932 |
dResult = (double) __shortFloatVal(self) / dVal; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
933 |
__qMKFLOAT(newFloat, dResult); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
934 |
RETURN ( newFloat ); |
3147 | 935 |
} |
936 |
%}. |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
937 |
^ aNumber quotientFromShortFloat:self |
3147 | 938 |
|
939 |
" |
|
940 |
0.0 asShortFloat uncheckedDivide:0 |
|
941 |
1.0 asShortFloat uncheckedDivide:0.0 |
|
942 |
" |
|
1197 | 943 |
! ! |
944 |
||
5257 | 945 |
!ShortFloat methodsFor:'coercing & converting'! |
1197 | 946 |
|
947 |
asFloat |
|
11238 | 948 |
"return a Float with same value as the receiver. |
949 |
Redefined for performance (machine can do it faster)" |
|
1197 | 950 |
|
951 |
%{ /* NOCONTEXT */ |
|
952 |
||
953 |
OBJ newFloat; |
|
954 |
double dVal = (double)__shortFloatVal(self); |
|
955 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
956 |
__qMKFLOAT(newFloat, dVal); |
1197 | 957 |
RETURN ( newFloat ); |
958 |
%} |
|
959 |
||
960 |
" |
|
11945 | 961 |
1.0 asShortFloat asFloat |
1197 | 962 |
" |
963 |
! |
|
964 |
||
965 |
asInteger |
|
966 |
"return an integer with same value - might truncate" |
|
967 |
||
968 |
%{ /* NOCONTEXT */ |
|
1670 | 969 |
float fVal; |
1197 | 970 |
|
1670 | 971 |
fVal = __shortFloatVal(self); |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
972 |
#ifdef __win32__ |
11945 | 973 |
if (! isnanf(fVal)) |
7419 | 974 |
#endif |
975 |
{ |
|
11945 | 976 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
977 |
RETURN ( __mkSmallInteger( (INT)fVal) ); |
|
978 |
} |
|
1197 | 979 |
} |
980 |
%}. |
|
981 |
^ super asInteger |
|
982 |
||
983 |
" |
|
984 |
12345.0 asShortFloat asInteger |
|
985 |
1e15 asShortFloat asInteger |
|
986 |
" |
|
987 |
! |
|
988 |
||
7437
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
989 |
asLongFloat |
21891 | 990 |
"return a LongFloat with same value as the receiver" |
991 |
||
7437
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
992 |
^ LongFloat fromShortFloat:self |
21891 | 993 |
|
994 |
"Modified (comment): / 21-06-2017 / 13:59:41 / cg" |
|
7437
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
995 |
! |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
996 |
|
24284 | 997 |
asQuadFloat |
998 |
"return a QuadFloat with same value as the receiver" |
|
999 |
||
1000 |
^ QuadFloat fromShortFloat:self |
|
1001 |
||
1002 |
"Created: / 07-06-2019 / 02:29:14 / Claus Gittinger" |
|
1003 |
! |
|
1004 |
||
1197 | 1005 |
asShortFloat |
15022 | 1006 |
"return a ShortFloat with same value as the receiver - that's me" |
1197 | 1007 |
|
1008 |
^ self |
|
1200 | 1009 |
! |
1010 |
||
7356 | 1011 |
coerce:aNumber |
18841 | 1012 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
7356 | 1013 |
|
1014 |
^ aNumber asShortFloat |
|
1015 |
! |
|
1016 |
||
1200 | 1017 |
generality |
1018 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
1019 |
||
1020 |
^ 70 |
|
1021 |
||
1022 |
||
1197 | 1023 |
! ! |
1024 |
||
1025 |
!ShortFloat methodsFor:'comparing'! |
|
1026 |
||
1027 |
< aNumber |
|
1028 |
"return true, if the argument is greater" |
|
1029 |
||
1030 |
%{ /* NOCONTEXT */ |
|
1031 |
||
1032 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 1033 |
RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false ); |
1197 | 1034 |
} |
7356 | 1035 |
if (aNumber != nil) { |
11945 | 1036 |
if (__qIsFloatLike(aNumber)) { |
1037 |
RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false ); |
|
1038 |
} |
|
1039 |
if (__qIsShortFloat(aNumber)) { |
|
1040 |
RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false ); |
|
1041 |
} |
|
1197 | 1042 |
} |
1043 |
%}. |
|
1044 |
^ aNumber lessFromShortFloat:self |
|
1045 |
||
1200 | 1046 |
" |
1047 |
1.0 asShortFloat > (1/3) |
|
1048 |
" |
|
1197 | 1049 |
! |
1050 |
||
1051 |
<= aNumber |
|
1052 |
"return true, if the argument is greater or equal" |
|
1053 |
||
1054 |
%{ /* NOCONTEXT */ |
|
1055 |
||
1056 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 1057 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 1058 |
} |
7356 | 1059 |
if (aNumber != nil) { |
11945 | 1060 |
if (__qIsFloatLike(aNumber)) { |
1061 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
|
1062 |
} |
|
1063 |
if (__qIsShortFloat(aNumber)) { |
|
1064 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
|
1065 |
} |
|
1066 |
} |
|
1197 | 1067 |
%}. |
1068 |
^ self retry:#<= coercing:aNumber |
|
1069 |
! |
|
1070 |
||
1071 |
= aNumber |
|
7356 | 1072 |
"return true, if the argument represents the same numeric value |
1073 |
as the receiver, false otherwise" |
|
1197 | 1074 |
|
1075 |
%{ /* NOCONTEXT */ |
|
1076 |
||
1077 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 1078 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
7356 | 1079 |
} |
1080 |
if (aNumber == nil) { |
|
11945 | 1081 |
RETURN (false); |
1197 | 1082 |
} |
7356 | 1083 |
if (__qIsFloatLike(aNumber)) { |
11945 | 1084 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
1197 | 1085 |
} |
7356 | 1086 |
if (__qIsShortFloat(aNumber)) { |
11945 | 1087 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
1197 | 1088 |
} |
1089 |
%}. |
|
7356 | 1090 |
^ aNumber equalFromShortFloat:self |
1197 | 1091 |
! |
1092 |
||
1093 |
> aNumber |
|
1094 |
"return true, if the argument is less" |
|
1095 |
||
1096 |
%{ /* NOCONTEXT */ |
|
1097 |
||
1098 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 1099 |
RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false ); |
1197 | 1100 |
} |
7356 | 1101 |
if (aNumber != nil) { |
11945 | 1102 |
if (__qIsFloatLike(aNumber)) { |
1103 |
RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false ); |
|
1104 |
} |
|
1105 |
if (__qIsShortFloat(aNumber)) { |
|
1106 |
RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false ); |
|
1107 |
} |
|
1197 | 1108 |
} |
1109 |
%}. |
|
1110 |
^ self retry:#> coercing:aNumber |
|
1111 |
! |
|
1112 |
||
1113 |
>= aNumber |
|
1114 |
"return true, if the argument is less or equal" |
|
1115 |
||
1116 |
%{ /* NOCONTEXT */ |
|
1117 |
||
1118 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 1119 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 1120 |
} |
7356 | 1121 |
if (aNumber != nil) { |
11945 | 1122 |
if (__qIsFloatLike(aNumber)) { |
1123 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
|
1124 |
} |
|
1125 |
if (__qIsShortFloat(aNumber)) { |
|
1126 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
|
1127 |
} |
|
1197 | 1128 |
} |
1129 |
%}. |
|
1130 |
^ self retry:#>= coercing:aNumber |
|
1131 |
! |
|
1132 |
||
1133 |
hash |
|
1134 |
"return a number for hashing; redefined, since floats compare |
|
1135 |
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
|
1136 |
as 3 hash." |
1197 | 1137 |
|
1138 |
|i| |
|
1139 |
||
1140 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1141 |
i := self asInteger. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1142 |
self = i ifTrue:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1143 |
^ i hash |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1144 |
]. |
1197 | 1145 |
]. |
1146 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
1147 |
^ self asFloat hash |
1197 | 1148 |
|
1149 |
" |
|
11945 | 1150 |
1.2345 hash |
1151 |
1.2345 asShortFloat hash |
|
1152 |
1.0 hash |
|
1153 |
1.0 asShortFloat hash |
|
1154 |
0.5 asShortFloat hash |
|
1155 |
0.25 asShortFloat hash |
|
1156 |
0.5 hash |
|
1157 |
0.25 hash |
|
1197 | 1158 |
" |
1159 |
! |
|
1160 |
||
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1161 |
isAlmostEqualTo:aNumber nEpsilon:nE |
16670 | 1162 |
"return true, if the argument, aNumber represents almost the same numeric value |
1163 |
as the receiver, false otherwise. |
|
1164 |
||
1165 |
nE is the number of minimal float distances, that the numbers may differ and |
|
1166 |
still be considered equal. |
|
1167 |
||
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1168 |
For background information why floats need this |
16670 | 1169 |
read: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ |
1170 |
" |
|
1171 |
||
21149 | 1172 |
Epsilon isNil ifTrue:[ |
22853 | 1173 |
Epsilon := self class computeEpsilon. |
21149 | 1174 |
]. |
1175 |
||
16670 | 1176 |
%{ /* NOCONTEXT */ |
1177 |
||
1178 |
/* |
|
1179 |
* notice: |
|
1180 |
* the following inline code handles some common cases, |
|
1181 |
* and exists as an optimization, to speed up those cases. |
|
1182 |
* |
|
1183 |
* Conceptionally, (and for most other argument types), |
|
1184 |
* mixed arithmetic is implemented by double dispatching |
|
1185 |
* (see the message send at the bottom) |
|
1186 |
*/ |
|
1187 |
||
1188 |
INT32 ulpDiff; |
|
1189 |
union { |
|
22853 | 1190 |
float f; |
1191 |
INT32 i; |
|
16670 | 1192 |
} myself, otherFloat; |
1193 |
int nEpsilon; |
|
1194 |
float scaledEpsilon; |
|
1195 |
||
1196 |
if (!__isSmallInteger(nE)) { |
|
22853 | 1197 |
goto tryHarder; |
16670 | 1198 |
} |
1199 |
||
1200 |
nEpsilon = __intVal(nE); |
|
1201 |
scaledEpsilon = nEpsilon *__shortFloatVal(@global(Epsilon)); |
|
1202 |
||
1203 |
if (__isSmallInteger(aNumber)) { |
|
22853 | 1204 |
otherFloat.f = (float)(__intVal(aNumber)); |
16670 | 1205 |
} else if (aNumber == nil) { |
22853 | 1206 |
RETURN(false) |
16670 | 1207 |
} else if (__qIsFloatLike(aNumber)) { |
22853 | 1208 |
otherFloat.f = (float)(__floatVal(aNumber)); |
16670 | 1209 |
} else if (__qIsShortFloat(aNumber)) { |
22853 | 1210 |
otherFloat.f = __shortFloatVal(aNumber); |
1211 |
// } else if (__qIsLongFloat(aNumber)) { |
|
1212 |
// otherFloat.f = (float)(__longFloatVal(aNumber)); |
|
16670 | 1213 |
} else { |
22853 | 1214 |
goto tryHarder; |
16670 | 1215 |
} |
1216 |
||
1217 |
myself.f = __shortFloatVal(self); |
|
1218 |
||
1219 |
// Check if the numbers are really close -- needed |
|
1220 |
// when comparing numbers near zero (ULP method below fails for numbers near 0!). |
|
22919 | 1221 |
# ifdef NO_ABSF |
22920 | 1222 |
if (fabs((double)(myself.f - otherFloat.f)) <= scaledEpsilon) { |
22919 | 1223 |
RETURN(true); |
1224 |
} |
|
1225 |
# else |
|
22853 | 1226 |
// fprintf(stderr, "%.10f %.10f\n", fabsf(myself.f - otherFloat.f), scaledEpsilon); |
1227 |
if (fabsf(myself.f - otherFloat.f) <= scaledEpsilon) { |
|
1228 |
RETURN(true); |
|
16670 | 1229 |
} |
22919 | 1230 |
#endif |
16670 | 1231 |
|
1232 |
// if the signs differ, the numbers are different |
|
1233 |
if ((myself.f >= 0) != (otherFloat.f >= 0)) { |
|
22853 | 1234 |
RETURN(false); |
16670 | 1235 |
} |
1236 |
||
1237 |
// compute the difference of the 'units in the last place" ULP |
|
1238 |
// (if ulpDiff == 1, two floats are adjecant) |
|
1239 |
ulpDiff = myself.i - otherFloat.i; |
|
1240 |
if (ulpDiff < 0) ulpDiff = -ulpDiff; |
|
1241 |
if (ulpDiff <= nEpsilon) { |
|
22853 | 1242 |
RETURN(true); |
16670 | 1243 |
} else { |
22853 | 1244 |
RETURN(false) |
16670 | 1245 |
} |
1246 |
||
1247 |
tryHarder:; |
|
1248 |
%}. |
|
1249 |
^ aNumber isAlmostEqualToFromShortFloat:self nEpsilon:nE |
|
1250 |
||
1251 |
" |
|
22853 | 1252 |
67329.234 asShortFloat isAlmostEqualTo:67329.23401 asShortFloat nEpsilon:1 |
1253 |
1.0 asShortFloat isAlmostEqualTo:1.0000001 asShortFloat nEpsilon:1 |
|
1254 |
1.0 asShortFloat isAlmostEqualTo:1.0000001 nEpsilon:1 |
|
1255 |
1.0 asShortFloat isAlmostEqualTo:-1.0 nEpsilon:1 |
|
1256 |
1.0 asShortFloat isAlmostEqualTo:1 nEpsilon:1 |
|
22922 | 1257 |
0.0 asShortFloat isAlmostEqualTo:0.0000001 asShortFloat nEpsilon:1 |
1258 |
0.0 asShortFloat isAlmostEqualTo:0.000001 asShortFloat nEpsilon:1 |
|
22853 | 1259 |
0.0 asShortFloat isAlmostEqualTo:self epsilon nEpsilon:1 |
1260 |
0.0 asShortFloat - 1.192093e-07 asShortFloat |
|
16670 | 1261 |
" |
22853 | 1262 |
|
1263 |
"Modified: / 10-05-2018 / 00:47:22 / stefan" |
|
16670 | 1264 |
! |
1265 |
||
1197 | 1266 |
~= aNumber |
1267 |
"return true, if the arguments value are not equal" |
|
1268 |
||
1269 |
%{ /* NOCONTEXT */ |
|
1270 |
||
7356 | 1271 |
if (aNumber != nil) { |
11945 | 1272 |
if (__isSmallInteger(aNumber)) { |
1273 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
|
1274 |
} |
|
1275 |
if (__qIsFloatLike(aNumber)) { |
|
1276 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
|
1277 |
} |
|
1278 |
if (__qIsShortFloat(aNumber)) { |
|
1279 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
|
1280 |
} |
|
7356 | 1281 |
} else { |
11945 | 1282 |
RETURN ( true ); |
1197 | 1283 |
} |
1284 |
%}. |
|
7356 | 1285 |
^ super ~= aNumber |
1197 | 1286 |
! ! |
1287 |
||
24189 | 1288 |
!ShortFloat methodsFor:'copying'! |
1289 |
||
1290 |
deepCopy |
|
1291 |
"return a deep copy of myself |
|
1292 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
1293 |
||
1294 |
^ self |
|
1295 |
! |
|
1296 |
||
1297 |
deepCopyUsing:aDictionary postCopySelector:postCopySelector |
|
1298 |
"return a deep copy of myself |
|
1299 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
1300 |
||
1301 |
^ self |
|
1302 |
! |
|
1303 |
||
1304 |
shallowCopy |
|
1305 |
"return a shallow copy of the receiver" |
|
1306 |
||
1307 |
^ self |
|
1308 |
! |
|
1309 |
||
1310 |
simpleDeepCopy |
|
1311 |
"return a deep copy of the receiver |
|
1312 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
1313 |
||
1314 |
^ self |
|
1315 |
! ! |
|
1316 |
||
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1317 |
!ShortFloat methodsFor:'mathematical functions'! |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1318 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1319 |
fastInverseSqrt |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1320 |
"return a rough but fast approximation of (1 / self sqrt). |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1321 |
The error is some 1%, which is ok for many 3D computations or physics simulations. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1322 |
Do not use this for now: it is non-portable and probably not speeding things up |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1323 |
much, unless inlined into the sender code. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1324 |
The code is here as a reminder and might be later used as a hint for the inliner |
13360
1aac241c9981
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13359
diff
changeset
|
1325 |
(to speed up 3D computations, for example). |
1aac241c9981
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13359
diff
changeset
|
1326 |
see: http://betterexplained.com/articles/understanding-quakes-fast-inverse-square-root/" |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1327 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1328 |
%{ /* NOCONTEXT */ |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1329 |
float x, rslt; |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1330 |
OBJ newFloat; |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1331 |
|
13361 | 1332 |
if (sizeof(float) == 4) { |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1333 |
x = __shortFloatVal(self); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1334 |
{ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1335 |
float xhalf = 0.5f * x; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1336 |
int i = *(int*)&x; // store floating-point bits in integer |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1337 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1338 |
i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1339 |
x = *(float*)&i; // convert new bits into float |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1340 |
x = x*(1.5f - xhalf*x*x); // One round of Newton's method |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1341 |
__qMKSFLOAT(newFloat, x); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1342 |
RETURN ( newFloat ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1343 |
} |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1344 |
} |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1345 |
%}. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1346 |
^ 1 / self sqrt |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1347 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1348 |
" |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1349 |
10.0 asShortFloat fastInverseSqrt |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1350 |
(1 / 10.0 asShortFloat sqrt) |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1351 |
" |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1352 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1353 |
" |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1354 |
|a b t0 t1 t2| |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1355 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1356 |
a := 345 asShortFloat. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1357 |
t0 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1358 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1359 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1360 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1361 |
t1 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1362 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1363 |
a fastInverseSqrt |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1364 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1365 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1366 |
t2 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1367 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1368 |
(1 / a sqrt) |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1369 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1370 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1371 |
Transcript show:'empty: '; showCR:t0. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1372 |
Transcript show:'fast: '; showCR:t1. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1373 |
Transcript show:'regular: '; showCR:t2. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1374 |
" |
21829 | 1375 |
! |
1376 |
||
21956 | 1377 |
ln |
1378 |
"return the natural logarithm of the receiver. |
|
1379 |
Raises an exception, if the receiver is less or equal to zero." |
|
1380 |
||
1381 |
%{ /* NOCONTEXT */ |
|
1382 |
#ifndef __SCHTEAM__ |
|
1383 |
float val, rslt; |
|
1384 |
OBJ newFloat; |
|
1385 |
||
1386 |
val = __shortFloatVal(self); |
|
1387 |
||
21976 | 1388 |
/* |
1389 |
* to suppress the warnBox opened by win32 |
|
21956 | 1390 |
* to avoid returning -INF from some unix math libs (__osx__) |
1391 |
*/ |
|
1392 |
if (val > 0.0) { |
|
21976 | 1393 |
__threadErrno = 0; |
21956 | 1394 |
# ifdef NO_LOGF |
21976 | 1395 |
{ |
1396 |
double dRslt = log((double)val); |
|
1397 |
rslt = (float)dRslt; |
|
1398 |
} |
|
21956 | 1399 |
# else |
21976 | 1400 |
rslt = logf(val); |
21956 | 1401 |
# endif |
21976 | 1402 |
if (! isnanf(rslt)) /* Currently all our systems support isnan() */ |
1403 |
{ |
|
1404 |
if (__threadErrno == 0) { |
|
1405 |
__qMKSFLOAT(newFloat, rslt); |
|
1406 |
RETURN ( newFloat ); |
|
1407 |
} |
|
1408 |
} |
|
21956 | 1409 |
} |
1410 |
#endif |
|
1411 |
%}. |
|
1412 |
" |
|
1413 |
an invalid value for logarithm |
|
21972 | 1414 |
if you need -INF for a zero receiver, try Number trapInfinity:[...] |
21956 | 1415 |
" |
1416 |
^ self class |
|
21976 | 1417 |
raise:(self = 0 ifTrue:[#infiniteResultSignal] ifFalse:[#domainErrorSignal]) |
1418 |
receiver:self |
|
1419 |
selector:#ln |
|
1420 |
arguments:#() |
|
1421 |
errorString:'bad receiver in log10 (not strictly positive)' |
|
21956 | 1422 |
|
1423 |
" |
|
21976 | 1424 |
0 asFloat ln |
21965 | 1425 |
Number trapInfinity:[ 0 asFloat ln ] |
1426 |
||
21956 | 1427 |
10 asFloat ln 2.30258509299405 |
1428 |
10 asShortFloat ln 2.302585 |
|
1429 |
10 asLongFloat ln 2.302585092994045684 |
|
1430 |
10 asQDouble ln 2.30258509299404568402 |
|
1431 |
||
1432 |
50 asFloat ln 3.91202300542815 |
|
1433 |
50 asShortFloat ln 3.912023 |
|
1434 |
50 asLongFloat ln 3.912023005428146059 |
|
1435 |
50 asQDouble ln 3.91202300542814605862 |
|
1436 |
" |
|
1437 |
||
1438 |
"Created: / 03-07-2017 / 15:20:07 / cg" |
|
1439 |
! |
|
1440 |
||
21829 | 1441 |
log10 |
1442 |
"return the base-10 logarithm of the receiver. |
|
1443 |
Raises an exception, if the receiver is less or equal to zero." |
|
1444 |
||
1445 |
%{ /* NOCONTEXT */ |
|
1446 |
#ifndef __SCHTEAM__ |
|
1447 |
float val, rslt; |
|
1448 |
OBJ newFloat; |
|
1449 |
||
1450 |
val = __shortFloatVal(self); |
|
1451 |
||
21976 | 1452 |
/* |
1453 |
* to suppress the warnBox opened by win32 |
|
21955 | 1454 |
* to avoid returning -INF from some unix math libs (__osx__) |
1455 |
*/ |
|
1456 |
if (val > 0.0) { |
|
21976 | 1457 |
__threadErrno = 0; |
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
1458 |
# ifdef NO_LOG10F |
21976 | 1459 |
{ |
1460 |
double dRslt = log10((double)val); |
|
1461 |
rslt = (float)dRslt; |
|
1462 |
} |
|
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
1463 |
# else |
21976 | 1464 |
rslt = log10f(val); |
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
1465 |
# endif |
21976 | 1466 |
if (! isnanf(rslt)) /* Currently all our systems support isnan() */ |
1467 |
{ |
|
1468 |
if (__threadErrno == 0) { |
|
1469 |
__qMKSFLOAT(newFloat, rslt); |
|
1470 |
RETURN ( newFloat ); |
|
1471 |
} |
|
1472 |
} |
|
21829 | 1473 |
} |
1474 |
#endif |
|
1475 |
%}. |
|
1476 |
" |
|
1477 |
an invalid value for logarithm |
|
21972 | 1478 |
if you need -INF for a zero receiver, try Number trapInfinity:[...] |
21829 | 1479 |
" |
1480 |
^ self class |
|
21976 | 1481 |
raise:(self = 0 ifTrue:[#infiniteResultSignal] ifFalse:[#domainErrorSignal]) |
1482 |
receiver:self |
|
1483 |
selector:#log10 |
|
1484 |
arguments:#() |
|
1485 |
errorString:'bad receiver in log10 (not strictly positive)' |
|
21829 | 1486 |
|
1487 |
" |
|
21976 | 1488 |
0 asShortFloat log10 |
21965 | 1489 |
Number trapInfinity:[ 0 asShortFloat log10 ] |
1490 |
||
21829 | 1491 |
10 asFloat log10 1.0 |
1492 |
10 asShortFloat log10 1.0 |
|
1493 |
10 asLongFloat log10 1.0 |
|
1494 |
10 asQDouble log10 1.00000000000000000000000000000000000000000 |
|
1495 |
||
1496 |
50 asFloat log10 1.69897000433602 |
|
1497 |
50 asShortFloat log10 1.69897 |
|
1498 |
50 asLongFloat log10 1.698970004336018805 |
|
1499 |
50 asQDouble log10 1.69897000433601880478626110527550697323181 |
|
1500 |
" |
|
1501 |
||
1502 |
"Created: / 16-06-2017 / 10:47:53 / cg" |
|
21972 | 1503 |
"Modified (comment): / 03-07-2017 / 15:59:05 / cg" |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1504 |
! ! |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1505 |
|
1197 | 1506 |
!ShortFloat methodsFor:'printing & storing'! |
1507 |
||
22303 | 1508 |
printOn:aStream |
1509 |
"append a printed representation of the receiver to |
|
1510 |
the argument, aStream. |
|
1511 |
||
1512 |
I use #printString instead of #printOn: as basic print mechanism." |
|
1513 |
||
1514 |
aStream nextPutAll:self printString |
|
1515 |
||
1516 |
"Created: / 10-10-2017 / 14:05:28 / cg" |
|
1517 |
! |
|
1518 |
||
1197 | 1519 |
printString |
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1520 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1521 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1522 |
#printOn: as basic print mechanism." |
1197 | 1523 |
|
18139 | 1524 |
^ self printStringWithFormat:DefaultPrintFormat |
1525 |
||
1526 |
" |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1527 |
1.234 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1528 |
1.0 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1529 |
1e10 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1530 |
1.2e3 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1531 |
1.2e30 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1532 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1533 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1534 |
self pi printString. |
18139 | 1535 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1536 |
self pi printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1537 |
DefaultPrintFormat := '.3'. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1538 |
self pi printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1539 |
DefaultPrintFormat := '.7'. |
18139 | 1540 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1541 |
DecimalPointCharacterForPrinting := $,. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1542 |
1.234 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1543 |
1.0 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1544 |
1e10 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1545 |
1.2e3 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1546 |
1.2e30 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1547 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1548 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1549 |
DecimalPointCharacterForPrinting := $. |
18139 | 1550 |
" |
1551 |
! |
|
1552 |
||
1553 |
printStringWithFormat:format |
|
1554 |
"return a printed representation of the receiver; |
|
1555 |
fmt must be of the form: .nn, where nn is the number of digits. |
|
1556 |
To print 6 valid digits, use printStringWithFormat:'.6' |
|
1557 |
For Floats, the default used in printString, is 15 (because its a double); |
|
1558 |
for ShortFloats, it is 6 (because it is a float)" |
|
1559 |
||
1197 | 1560 |
%{ /* NOCONTEXT */ |
1561 |
||
1562 |
char buffer[64]; |
|
1563 |
REGISTER char *cp; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1564 |
int len; |
1197 | 1565 |
OBJ s; |
16670 | 1566 |
char *fmt; |
1567 |
char fmtBuffer[20]; |
|
1568 |
||
18139 | 1569 |
if (__isStringLike(format)) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1570 |
fmt = (char *) __stringVal(format); |
16670 | 1571 |
} else { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1572 |
/* |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1573 |
* in case we get called with garbage... |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1574 |
*/ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1575 |
fmt = ".7"; |
16670 | 1576 |
} |
1577 |
||
1578 |
/* |
|
1579 |
* build a printf format string |
|
1580 |
*/ |
|
1581 |
fmtBuffer[0] = '%'; |
|
1582 |
strncpy(fmtBuffer+1, fmt, 10); |
|
1583 |
strcat(fmtBuffer, "g"); |
|
1197 | 1584 |
|
1585 |
/* |
|
1586 |
* actually only needed on sparc: since thisContext is |
|
1587 |
* in a global register, which gets destroyed by printf, |
|
1588 |
* manually save it here - very stupid ... |
|
1589 |
*/ |
|
1590 |
__BEGIN_PROTECT_REGISTERS__ |
|
16670 | 1591 |
len = snprintf(buffer, sizeof(buffer), fmtBuffer, (float)__shortFloatVal(self)); |
1197 | 1592 |
__END_PROTECT_REGISTERS__ |
1593 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1594 |
if (len >= 0 && len <= sizeof(buffer)-3) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1595 |
/* |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1596 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1597 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1598 |
*/ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1599 |
for (cp = buffer; *cp; cp++) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1600 |
if ((*cp == '.') || (*cp == ',') || (*cp == 'E') || (*cp == 'e')) break; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1601 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1602 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1603 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1604 |
*cp++ = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1605 |
} else { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1606 |
*cp++ = '.'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1607 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1608 |
*cp++ = '0'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1609 |
*cp = '\0'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1610 |
} else { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1611 |
if (cp && (*cp == '.')) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1612 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1613 |
*cp = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1614 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1615 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1616 |
} |
1197 | 1617 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1618 |
s = __MKSTRING(buffer); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1619 |
if (s != nil) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1620 |
RETURN (s); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1621 |
} |
1197 | 1622 |
} |
1623 |
%}. |
|
5432 | 1624 |
^ self asFloat printString |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1625 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1626 |
" |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1627 |
1.234 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1628 |
ShortFloat pi printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1629 |
1.0 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1630 |
1e10 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1631 |
1.2e3 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1632 |
1.2e30 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1633 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1634 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1635 |
self pi printString. |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1636 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1637 |
self pi printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1638 |
DefaultPrintFormat := '.3'. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1639 |
self pi printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1640 |
DefaultPrintFormat := '.7'. |
16670 | 1641 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1642 |
DecimalPointCharacterForPrinting := $,. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1643 |
1.234 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1644 |
1.0 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1645 |
1e10 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1646 |
1.2e3 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1647 |
1.2e30 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1648 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1649 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1650 |
DecimalPointCharacterForPrinting := $. |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1651 |
" |
5718 | 1652 |
! |
1653 |
||
1654 |
printfPrintString:formatString |
|
7746 | 1655 |
"non-standard: return a printed representation of the receiver |
5718 | 1656 |
as specified by formatString, which is defined by printf. |
21950 | 1657 |
|
1658 |
If you use this, be aware, that the format string must be correct and something like %f. |
|
5718 | 1659 |
Also, the resulting string may not be longer than 255 bytes - |
20433 | 1660 |
since that's the (static) size of the buffer. |
21906 | 1661 |
|
11924
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1662 |
This method is NONSTANDARD and may be removed without notice. |
21976 | 1663 |
WARNNG: this goes directly to the C-printf function and may therefore me inherently unsafe. |
11924
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1664 |
Please use the printf: method, which is safe as it is completely implemented in Smalltalk." |
5718 | 1665 |
|
1666 |
%{ /* STACK: 400 */ |
|
1667 |
char buffer[256]; |
|
1668 |
OBJ s; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1669 |
int len; |
5718 | 1670 |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1671 |
if (__isStringLike(formatString)) { |
21976 | 1672 |
/* |
1673 |
* actually only needed on sparc: since thisContext is |
|
1674 |
* in a global register, which gets destroyed by printf, |
|
1675 |
* manually save it here - very stupid ... |
|
1676 |
*/ |
|
1677 |
__BEGIN_PROTECT_REGISTERS__ |
|
5718 | 1678 |
|
21976 | 1679 |
len = snprintf(buffer, sizeof(buffer), __stringVal(formatString), __shortFloatVal(self)); |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1680 |
|
21976 | 1681 |
__END_PROTECT_REGISTERS__ |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1682 |
|
21976 | 1683 |
if (len < 0) goto fail; |
1684 |
if (len >= sizeof(buffer)) goto fail; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1685 |
|
21976 | 1686 |
s = __MKSTRING_L(buffer, len); |
1687 |
if (s != nil) { |
|
1688 |
RETURN (s); |
|
1689 |
} |
|
5718 | 1690 |
} |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1691 |
fail: ; |
5718 | 1692 |
%}. |
21906 | 1693 |
^ super printfPrintString:formatString |
5718 | 1694 |
|
1695 |
"ShortFloat pi printfPrintString:'%%lg -> %lg'" |
|
1696 |
"ShortFloat pi printfPrintString:'%%lf -> %lf'" |
|
1697 |
"ShortFloat pi printfPrintString:'%%7.5lg -> %7.5lg'" |
|
1698 |
"ShortFloat pi printfPrintString:'%%G -> %G'" |
|
1699 |
"ShortFloat pi printfPrintString:'%%F -> %F'" |
|
1700 |
"ShortFloat pi printfPrintString:'%%7.5G -> %7.5G'" |
|
1701 |
"ShortFloat pi printfPrintString:'%%7.5F -> %7.5F'" |
|
21906 | 1702 |
|
21950 | 1703 |
"Modified (comment): / 03-07-2017 / 15:12:20 / cg" |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1704 |
! |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1705 |
|
22303 | 1706 |
storeOn:aStream |
1707 |
"append a printed representation of the receiver to |
|
1708 |
the argument, aStream. |
|
1709 |
||
1710 |
I use #storeString instead of #storeOn: as basic store mechanism." |
|
1711 |
||
1712 |
aStream nextPutAll:self storeString |
|
1713 |
||
1714 |
"Created: / 10-10-2017 / 14:06:52 / cg" |
|
1715 |
! |
|
1716 |
||
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1717 |
storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1718 |
"return a printed representation of the receiver; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1719 |
all valid digits are printed. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1720 |
LimitedPrecisonReal and its subclasses use #storeString instead of |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1721 |
#storeOn: as basic print mechanism." |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1722 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1723 |
%{ /* NOCONTEXT */ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1724 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1725 |
char buffer[64]; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1726 |
REGISTER char *cp; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1727 |
OBJ s; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1728 |
int len; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1729 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1730 |
/* |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1731 |
* build a printf format string |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1732 |
*/ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1733 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1734 |
__BEGIN_PROTECT_REGISTERS__ |
16670 | 1735 |
len = snprintf(buffer, sizeof(buffer), "%.8g", (float)__shortFloatVal(self)); |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1736 |
__END_PROTECT_REGISTERS__ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1737 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1738 |
if (len >= 0 && len < sizeof(buffer)-3) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1739 |
/* |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1740 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1741 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1742 |
*/ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1743 |
for (cp = buffer; *cp; cp++) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1744 |
if ((*cp == '.') || (*cp == ',') || (*cp == 'E') || (*cp == 'e')) break; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1745 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1746 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1747 |
*cp++ = '.'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1748 |
*cp++ = '0'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1749 |
*cp = '\0'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1750 |
} |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1751 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1752 |
s = __MKSTRING(buffer); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1753 |
if (s != nil) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1754 |
RETURN (s); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1755 |
} |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1756 |
} |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1757 |
%}. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1758 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1759 |
memory allocation (for the new string) failed. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1760 |
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:
8418
diff
changeset
|
1761 |
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:
8418
diff
changeset
|
1762 |
OS, which was not kind enough to give it. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1763 |
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:
8418
diff
changeset
|
1764 |
" |
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
20433
diff
changeset
|
1765 |
^ AllocationFailure raise. |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1766 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1767 |
" |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1768 |
0.1 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1769 |
((Array new:10 withAll:0.1 asShortFloat) inject:0 into:[:v :sumSoFar| sumSoFar + v]) storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1770 |
1.0 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1771 |
1.234 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1772 |
1e10 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1773 |
1.2e3 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1774 |
1.2e30 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1775 |
Float pi asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1776 |
(1.0 uncheckedDivide:0) asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1777 |
(0.0 uncheckedDivide:0) asShortFloat storeString |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1778 |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1779 |
notice that the storeString is NOT affected by DecimalPointCharacterForPrinting: |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1780 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1781 |
DecimalPointCharacterForPrinting := $,. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1782 |
1.234 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1783 |
1.0 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1784 |
1e10 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1785 |
1.2e3 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1786 |
1.2e30 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1787 |
(1.0 uncheckedDivide:0) asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1788 |
(0.0 uncheckedDivide:0) asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1789 |
DecimalPointCharacterForPrinting := $. |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1790 |
" |
1197 | 1791 |
! ! |
1792 |
||
12628 | 1793 |
!ShortFloat methodsFor:'private accessing'! |
1794 |
||
1795 |
basicAt:index |
|
1796 |
"return an internal byte of the float. |
|
1797 |
The value returned here depends on byte order, float representation etc. |
|
1798 |
Therefore, this method should be used strictly private. |
|
1799 |
||
1800 |
Notice: |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1801 |
the need to redefine this method here is due to the |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1802 |
inability of many machines to store floats in non-double aligned memory. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1803 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1804 |
and the actual float is stored at index 5 .. 12. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1805 |
To hide this at one place, this method knows about that, and returns |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1806 |
values as if this filler wasnt present." |
12628 | 1807 |
|
1808 |
%{ /* NOCONTEXT */ |
|
1809 |
||
1810 |
register int indx; |
|
1811 |
unsigned char *cp; |
|
1812 |
||
1813 |
/* |
|
1814 |
* notice the missing test for self being a nonNilObject - |
|
1815 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
1816 |
* and SmallInteger |
|
1817 |
*/ |
|
1818 |
if (__isSmallInteger(index)) { |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1819 |
indx = __intVal(index) - 1; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1820 |
if (((unsigned)(indx)) < sizeof(float)) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1821 |
cp = (unsigned char *)(& (__ShortFloatInstPtr(self)->f_floatvalue)); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1822 |
RETURN ( __mkSmallInteger(cp[indx] & 0xFF) ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1823 |
} |
12628 | 1824 |
} |
1825 |
%}. |
|
1826 |
^ self indexNotIntegerOrOutOfBounds:index |
|
1827 |
! |
|
1828 |
||
1829 |
basicAt:index put:value |
|
1830 |
"set an internal byte of the float. |
|
1831 |
The value to be stored here depends on byte order, float representation etc. |
|
1832 |
Therefore, this method should be used strictly private. |
|
1833 |
||
1834 |
Notice: |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1835 |
the need to redefine this method here is due to the |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1836 |
inability of many machines to store floats in non-double aligned memory. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1837 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1838 |
and the actual float is stored at index 5 .. 12. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1839 |
To hide this at one place, this method knows about that, and returns |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1840 |
values as if this filler wasnt present." |
12628 | 1841 |
|
1842 |
%{ /* NOCONTEXT */ |
|
1843 |
register int indx, val; |
|
1844 |
unsigned char *cp; |
|
1845 |
||
1846 |
/* |
|
1847 |
* notice the missing test for self being a nonNilObject - |
|
1848 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
1849 |
* and SmallInteger |
|
1850 |
*/ |
|
1851 |
if (__bothSmallInteger(index, value)) { |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1852 |
val = __intVal(value); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1853 |
if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1854 |
indx = __intVal(index) - 1; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1855 |
if (((unsigned)(indx)) < sizeof(float)) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1856 |
cp = (unsigned char *)(& (__ShortFloatInstPtr(self)->f_floatvalue)); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1857 |
cp[indx] = val; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1858 |
RETURN ( value ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1859 |
} |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1860 |
} |
12628 | 1861 |
} |
1862 |
%}. |
|
1863 |
value isInteger ifFalse:[ |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1864 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1865 |
the object to store should be an integer number |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1866 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1867 |
^ self elementNotInteger |
12628 | 1868 |
]. |
1869 |
(value between:0 and:255) ifFalse:[ |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1870 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1871 |
the object to store must be a bytes value |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1872 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1873 |
^ self elementBoundsError:value |
12628 | 1874 |
]. |
1875 |
^ self indexNotIntegerOrOutOfBounds:index |
|
1876 |
! ! |
|
1877 |
||
22259 | 1878 |
!ShortFloat methodsFor:'private-accessing'! |
1879 |
||
1880 |
byteAt:index |
|
1881 |
^ self basicAt:index |
|
1882 |
! |
|
1883 |
||
1884 |
byteAt:index put:newByte |
|
1885 |
self shouldNotImplement |
|
1886 |
! ! |
|
1887 |
||
16670 | 1888 |
!ShortFloat methodsFor:'queries'! |
1889 |
||
1890 |
nextFloat:count |
|
1891 |
"answer the next float count places after (or before if count is negative) myself" |
|
1892 |
||
1893 |
%{ |
|
1894 |
union u { |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1895 |
float d; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1896 |
INT32 i; |
16670 | 1897 |
} this; |
1898 |
||
1899 |
if (__isSmallInteger(count)) { |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1900 |
this.d = __shortFloatVal(self); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1901 |
if (isfinite(this.d)) |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1902 |
this.i += __intVal(count); |
16670 | 1903 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1904 |
RETURN(__MKSFLOAT(this.d)); |
16670 | 1905 |
} |
1906 |
%}. |
|
1907 |
self primitiveFailed:#badArgument |
|
1908 |
||
1909 |
" |
|
1910 |
(1.0 asShortFloat nextFloat:2) storeString |
|
1911 |
(67329.234 asShortFloat nextFloat:1) storeString |
|
1912 |
ShortFloat NaN nextFloat:100000 |
|
1913 |
ShortFloat infinity nextFloat:100000 |
|
1914 |
" |
|
1915 |
! ! |
|
1916 |
||
3897 | 1917 |
!ShortFloat methodsFor:'special access'! |
1918 |
||
1919 |
exponent |
|
21850 | 1920 |
"extract a normalized float's exponent. |
3897 | 1921 |
The returned value depends on the float-representation of |
1922 |
the underlying machine and is therefore highly unportable. |
|
1923 |
This is not for general use. |
|
1924 |
This assumes that the mantissa is normalized to |
|
21850 | 1925 |
0.5 .. 1.0 and the float's value is mantissa * 2^exp" |
3897 | 1926 |
|
1927 |
%{ /* NOCONTEXT */ |
|
8913 | 1928 |
int exp; |
3897 | 1929 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1930 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1931 |
frexpf( __shortFloatVal(self), &exp); |
1932 |
#else |
|
1933 |
frexp( (double)(__shortFloatVal(self)), &exp); |
|
1934 |
#endif |
|
14744 | 1935 |
RETURN (__mkSmallInteger(exp)); |
3897 | 1936 |
%}. |
21850 | 1937 |
^ super exponent |
3897 | 1938 |
|
1939 |
" |
|
11945 | 1940 |
4.0 asShortFloat exponent |
1941 |
2.0 asShortFloat exponent |
|
1942 |
1.0 asShortFloat exponent |
|
1943 |
0.5 asShortFloat exponent |
|
1944 |
0.25 asShortFloat exponent |
|
1945 |
0.00000011111 asShortFloat exponent |
|
3897 | 1946 |
" |
21850 | 1947 |
|
1948 |
"Modified: / 20-06-2017 / 11:33:41 / cg" |
|
3897 | 1949 |
! |
1950 |
||
1951 |
mantissa |
|
24150 | 1952 |
"extract a normalized float's mantissa. |
3897 | 1953 |
The returned value depends on the float-representation of |
1954 |
the underlying machine and is therefore highly unportable. |
|
1955 |
This is not for general use. |
|
1956 |
This assumes that the mantissa is normalized to |
|
24150 | 1957 |
0.5 .. 1.0 and the float's value is mantissa * 2^exp" |
3897 | 1958 |
|
1959 |
%{ /* NOCONTEXT */ |
|
21850 | 1960 |
float myVal; |
8913 | 1961 |
int exp; |
3897 | 1962 |
|
21850 | 1963 |
myVal = __shortFloatVal(self); |
1964 |
// ouch: math libs seem to not care for NaN here; |
|
1965 |
#if 1 |
|
1966 |
// should we? |
|
21868 | 1967 |
if (!isnanf(myVal) && isfinitef(myVal)) |
21850 | 1968 |
#endif |
1969 |
{ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1970 |
#if defined(__i386__) && defined(__GNUC__) |
24150 | 1971 |
float frac = frexpf(myVal, &exp); |
1972 |
RETURN (__MKSFLOAT(frac)); |
|
7356 | 1973 |
#else |
24150 | 1974 |
double frac = frexp( (double)(myVal), &exp); |
1975 |
RETURN (__MKFLOAT(frac)); |
|
7356 | 1976 |
#endif |
21850 | 1977 |
} |
3897 | 1978 |
%}. |
20008 | 1979 |
^ super mantissa |
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
20433
diff
changeset
|
1980 |
|
3897 | 1981 |
" |
11945 | 1982 |
1.0 asShortFloat exponent |
3897 | 1983 |
1.0 asShortFloat mantissa |
21850 | 1984 |
1e1000 asShortFloat mantissa |
3897 | 1985 |
|
11945 | 1986 |
0.5 asShortFloat exponent |
1987 |
0.5 asShortFloat mantissa |
|
3897 | 1988 |
|
11945 | 1989 |
0.25 asShortFloat exponent |
1990 |
0.25 asShortFloat mantissa |
|
3897 | 1991 |
|
11945 | 1992 |
0.00000011111 asShortFloat exponent |
1993 |
0.00000011111 asShortFloat mantissa |
|
3897 | 1994 |
" |
21850 | 1995 |
|
1996 |
"Modified: / 20-06-2017 / 11:41:11 / cg" |
|
24150 | 1997 |
"Modified: / 26-05-2019 / 03:13:14 / Claus Gittinger" |
3897 | 1998 |
! ! |
1999 |
||
1197 | 2000 |
!ShortFloat methodsFor:'testing'! |
2001 |
||
3147 | 2002 |
isFinite |
11945 | 2003 |
"return true, if the receiver is a finite float |
3147 | 2004 |
i.e. not NaN and not infinite." |
2005 |
||
2006 |
%{ /* NOCONTEXT */ |
|
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2007 |
float fV = __shortFloatVal(self); |
3147 | 2008 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
2009 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
2010 |
* notice: on machines which do not provide |
11945 | 2011 |
* a isfinite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
2012 |
* 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
|
2013 |
*/ |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
2014 |
if (isfinitef(fV)) { RETURN (true); } |
3147 | 2015 |
%}. |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2016 |
^ false |
3147 | 2017 |
|
2018 |
" |
|
11945 | 2019 |
1.0 asShortFloat isFinite |
2020 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
2021 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
3147 | 2022 |
" |
2023 |
! |
|
2024 |
||
2387 | 2025 |
isNaN |
2026 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
2027 |
These are not created by ST/X float operations (they raise an exception); |
|
2028 |
however, inline C-code could produce them ..." |
|
2029 |
||
2030 |
%{ /* NOCONTEXT */ |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
2031 |
float sV = __shortFloatVal(self); |
3139 | 2032 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
2033 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
2034 |
* notice: on machines which do not provide |
11945 | 2035 |
* a isnan() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
2036 |
* 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
|
2037 |
*/ |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
2038 |
if (isnanf(sV)) { RETURN (true); } |
2392 | 2039 |
%}. |
2040 |
^ false |
|
2387 | 2041 |
|
3147 | 2042 |
" |
7356 | 2043 |
1.0 asShortFloat isNaN |
11945 | 2044 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
2045 |
(0.0 asShortFloat uncheckedDivide: 0.0 asShortFloat) isNaN |
|
3147 | 2046 |
" |
2387 | 2047 |
! |
2048 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2049 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2050 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2051 |
|
17396 | 2052 |
%{ /* NOCONTEXT */ |
17394 | 2053 |
#if defined(__BORLANDC__) |
2054 |
union { float f; int i; } __u; |
|
2055 |
__u.f = __shortFloatVal(self); |
|
2056 |
RETURN ( (__u.f == 0.0 && __u.i < 0) ? true : false ); |
|
17391 | 2057 |
#else |
17382 | 2058 |
RETURN ( (__shortFloatVal(self) == 0.0 && signbit(__shortFloatVal(self)) != 0) ? true : false ); |
17391 | 2059 |
#endif |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2060 |
%}. |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2061 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2062 |
" |
11945 | 2063 |
0.0 asShortFloat isNegativeZero |
2064 |
-0.0 asShortFloat isNegativeZero |
|
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2065 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2066 |
! |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2067 |
|
24330 | 2068 |
isZero |
2069 |
"return true, if the receiver is zero" |
|
2070 |
||
2071 |
^ self = 0.0 |
|
2072 |
||
2073 |
"Created: / 10-06-2019 / 21:59:09 / Claus Gittinger" |
|
2074 |
! |
|
2075 |
||
1197 | 2076 |
negative |
17382 | 2077 |
"return true if the receiver is less than zero. |
2078 |
-0.0 is positive for now." |
|
1197 | 2079 |
|
2080 |
%{ /* NOCONTEXT */ |
|
2081 |
||
17382 | 2082 |
RETURN ( __shortFloatVal(self) < 0.0 ? true : false ); |
2083 |
// RETURN ( signbit(__shortFloatVal(self)) != 0 ? true : false ); |
|
5411 | 2084 |
%}. |
17382 | 2085 |
|
2086 |
" |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2087 |
0.0 asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2088 |
-0.0 asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2089 |
1.0 asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2090 |
-1.0 asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2091 |
(1.0 uncheckedDivide: 0.0) asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2092 |
(-1.0 uncheckedDivide: 0.0) asShortFloat negative |
17382 | 2093 |
" |
1197 | 2094 |
! |
2095 |
||
5357 | 2096 |
numberOfBits |
2097 |
"return the size (in bits) of the real; |
|
2098 |
typically, 32 is returned here, |
|
2099 |
but who knows ..." |
|
2100 |
||
2101 |
%{ /* NOCONTEXT */ |
|
2102 |
||
8913 | 2103 |
RETURN (__mkSmallInteger (sizeof(float) * 8)); |
5357 | 2104 |
%} |
2105 |
||
2106 |
" |
|
11945 | 2107 |
1.2 numberOfBits |
2108 |
1.2 asShortFloat numberOfBits |
|
5357 | 2109 |
" |
2110 |
||
2111 |
! |
|
2112 |
||
1197 | 2113 |
positive |
18852 | 2114 |
"return true if the receiver is greater or equal to zero (not negative) |
17382 | 2115 |
0.0 and -0.0 are positive for now." |
1197 | 2116 |
|
2117 |
%{ /* NOCONTEXT */ |
|
2118 |
||
17382 | 2119 |
RETURN ( __shortFloatVal(self) >= 0.0 ? true : false ); |
2120 |
// RETURN ( (signbit(__shortFloatVal(self)) == 0 ? true : false ) ); |
|
2121 |
%}. |
|
2122 |
||
2123 |
" |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2124 |
0.0 asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2125 |
-0.0 asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2126 |
1.0 asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2127 |
-1.0 asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2128 |
(1.0 uncheckedDivide: 0.0) asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2129 |
(-1.0 uncheckedDivide: 0.0) asShortFloat positive |
17382 | 2130 |
" |
1880 | 2131 |
! |
2132 |
||
2133 |
strictlyPositive |
|
2134 |
"return true if the receiver is greater than zero" |
|
2135 |
||
2136 |
%{ /* NOCONTEXT */ |
|
2137 |
||
2138 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
2139 |
%} |
|
1197 | 2140 |
! ! |
2141 |
||
5552 | 2142 |
!ShortFloat methodsFor:'truncation & rounding'! |
3897 | 2143 |
|
7356 | 2144 |
ceiling |
2145 |
"return the smallest integer which is greater or equal to the receiver." |
|
2146 |
||
2147 |
|val| |
|
2148 |
||
2149 |
%{ |
|
2150 |
float fVal; |
|
2151 |
||
22788 | 2152 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2153 |
fVal = ceilf(__shortFloatVal(self)); |
2154 |
#else |
|
2155 |
fVal = (float)(ceil((double)__shortFloatVal(self))); |
|
2156 |
#endif |
|
2157 |
/* |
|
2158 |
* ST-80 (and X3J20) returns integer. |
|
2159 |
*/ |
|
2160 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
22788 | 2161 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 2162 |
} |
2163 |
__qMKSFLOAT(val, fVal); |
|
2164 |
%}. |
|
2165 |
^ val asInteger |
|
2166 |
||
2167 |
" |
|
11945 | 2168 |
0.5 asShortFloat ceiling |
2169 |
-0.5 asShortFloat ceiling |
|
7356 | 2170 |
" |
2171 |
! |
|
2172 |
||
2173 |
ceilingAsFloat |
|
2174 |
"return the smallest integer-valued float greater or equal to the receiver. |
|
11945 | 2175 |
This is much like #ceiling, but avoids a (possibly expensive) conversion |
7356 | 2176 |
of the result to an integer. |
2177 |
It may be useful, if the result is to be further used in another float-operation." |
|
2178 |
||
2179 |
%{ /* NOCONTEXT */ |
|
2180 |
float fVal; |
|
2181 |
OBJ v; |
|
2182 |
||
22788 | 2183 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2184 |
fVal = ceilf(__shortFloatVal(self)); |
2185 |
#else |
|
2186 |
fVal = (float) ceil((double)__shortFloatVal(self)); |
|
2187 |
#endif |
|
2188 |
__qMKSFLOAT(v, fVal); |
|
2189 |
RETURN (v); |
|
2190 |
%} |
|
2191 |
" |
|
11945 | 2192 |
0.5 asShortFloat ceilingAsFloat |
2193 |
-0.5 asShortFloat ceilingAsFloat |
|
2194 |
-1.5 asShortFloat ceilingAsFloat |
|
7356 | 2195 |
" |
2196 |
! |
|
2197 |
||
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2198 |
floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2199 |
"return the integer nearest the receiver towards negative infinity." |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2200 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2201 |
|val| |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2202 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2203 |
%{ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2204 |
float fVal; |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2205 |
|
22788 | 2206 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2207 |
fVal = floorf(__shortFloatVal(self)); |
2208 |
#else |
|
2209 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
2210 |
#endif |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2211 |
/* |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2212 |
* ST-80 (and X3J20) returns integer. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2213 |
*/ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2214 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
22788 | 2215 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2216 |
} |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2217 |
__qMKSFLOAT(val, fVal); |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2218 |
%}. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2219 |
^ val asInteger |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2220 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2221 |
" |
11945 | 2222 |
0.5 asShortFloat floor |
2223 |
-0.5 asShortFloat floor |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2224 |
" |
7356 | 2225 |
! |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2226 |
|
7356 | 2227 |
floorAsFloat |
2228 |
"return the float which represents the next lower |
|
2229 |
integer nearest the receiver towards negative infinity. |
|
2230 |
Much like floor, but returns a float result - useful if the result |
|
2231 |
will be used in another float operation, to avoid costy int-conversion." |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2232 |
|
7356 | 2233 |
%{ /* NOCONTEXT */ |
2234 |
float fVal; |
|
2235 |
OBJ v; |
|
2236 |
||
22788 | 2237 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2238 |
fVal = floorf(__shortFloatVal(self)); |
2239 |
#else |
|
2240 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
2241 |
#endif |
|
2242 |
__qMKSFLOAT(v, fVal); |
|
2243 |
RETURN ( v ); |
|
2244 |
%} |
|
2245 |
||
2246 |
" |
|
11945 | 2247 |
0.5 asShortFloat floorAsFloat |
2248 |
-0.5 asShortFloat floorAsFloat |
|
7356 | 2249 |
" |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2250 |
! |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2251 |
|
3897 | 2252 |
fractionPart |
2253 |
"extract the after-decimal fraction part. |
|
7356 | 2254 |
such that (self truncated + self fractionPart) = self" |
3897 | 2255 |
|
2256 |
%{ /* NOCONTEXT */ |
|
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2257 |
#ifdef NO_MODFF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2258 |
double dFrac, dTrunc; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2259 |
#else |
14741 | 2260 |
# ifndef __VISUALC__ |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2261 |
float modff(float, float *); |
14741 | 2262 |
# endif |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2263 |
#endif |
14718 | 2264 |
float frac, trunc; |
3897 | 2265 |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
2266 |
__threadErrno = 0; |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2267 |
#ifdef NO_MODFF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2268 |
dFrac = modf((double)__shortFloatVal(self), &dTrunc) ; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2269 |
frac = (float)dFrac; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2270 |
#else |
14718 | 2271 |
frac = modff(__shortFloatVal(self), &trunc); |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2272 |
#endif |
11945 | 2273 |
if (! isnan(frac)) { |
14719 | 2274 |
if (__threadErrno == 0) { |
2275 |
RETURN (__MKSFLOAT(frac)); |
|
2276 |
} |
|
3897 | 2277 |
} |
2278 |
%}. |
|
7403 | 2279 |
^ self class |
14719 | 2280 |
raise:#domainErrorSignal |
2281 |
receiver:self |
|
2282 |
selector:#fractionPart |
|
2283 |
arguments:#() |
|
2284 |
errorString:'bad receiver in fractionPart' |
|
3897 | 2285 |
|
2286 |
" |
|
11945 | 2287 |
1.6 asShortFloat fractionPart + 1.6 asShortFloat truncated |
2288 |
-1.6 asShortFloat fractionPart + -1.6 asShortFloat truncated |
|
7356 | 2289 |
|
14718 | 2290 |
1.0 asShortFloat fractionPart |
2291 |
0.5 asShortFloat fractionPart |
|
2292 |
0.25 asShortFloat fractionPart |
|
2293 |
3.14159 asShortFloat fractionPart |
|
2294 |
12345673.14159 asShortFloat fractionPart |
|
2295 |
123456731231231231.14159 asShortFloat fractionPart |
|
3897 | 2296 |
" |
7356 | 2297 |
! |
3897 | 2298 |
|
7356 | 2299 |
rounded |
2300 |
"return the receiver rounded to the nearest integer" |
|
2301 |
||
2302 |
|val| |
|
2303 |
||
11945 | 2304 |
%{ |
7356 | 2305 |
float fVal; |
2306 |
||
2307 |
fVal = __shortFloatVal(self); |
|
22788 | 2308 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2309 |
if (fVal < 0.0) { |
22788 | 2310 |
fVal = ceilf(fVal - (float)0.5); |
7356 | 2311 |
} else { |
22788 | 2312 |
fVal = floorf(fVal + (float)0.5); |
7356 | 2313 |
} |
2314 |
#else |
|
2315 |
if (fVal < 0.0) { |
|
22788 | 2316 |
fVal = (float)ceil((double)fVal - 0.5); |
7356 | 2317 |
} else { |
22788 | 2318 |
fVal = (float)floor((double)fVal + 0.5); |
7356 | 2319 |
} |
2320 |
#endif |
|
2321 |
/* |
|
22788 | 2322 |
* ST-80 (and X3J20) return integer. |
7356 | 2323 |
*/ |
2324 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
22788 | 2325 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 2326 |
} |
2327 |
__qMKSFLOAT(val, fVal); |
|
2328 |
%}. |
|
2329 |
^ val asInteger |
|
2330 |
||
2331 |
" |
|
11945 | 2332 |
0.4 asShortFloat rounded |
2333 |
0.5 asShortFloat rounded |
|
2334 |
0.6 asShortFloat rounded |
|
2335 |
-0.4 asShortFloat rounded |
|
2336 |
-0.5 asShortFloat rounded |
|
2337 |
-0.6 asShortFloat rounded |
|
22788 | 2338 |
1e32 asShortFloat rounded |
7356 | 2339 |
" |
2340 |
! |
|
2341 |
||
2342 |
roundedAsFloat |
|
2343 |
"return the receiver rounded to the nearest integer as a float. |
|
11945 | 2344 |
This is much like #rounded, but avoids a (possibly expensive) conversion |
7356 | 2345 |
of the result to an integer. |
2346 |
It may be useful, if the result is to be further used in another float-operation." |
|
2347 |
||
2348 |
%{ /* NOCONTEXT */ |
|
2349 |
float fVal; |
|
2350 |
OBJ v; |
|
2351 |
||
2352 |
fVal = __shortFloatVal(self); |
|
22788 | 2353 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2354 |
if (fVal < 0.0) { |
22788 | 2355 |
fVal = ceilf(fVal - (float)0.5); |
7356 | 2356 |
} else { |
22788 | 2357 |
fVal = floorf(fVal + (float)0.5); |
7356 | 2358 |
} |
2359 |
#else |
|
2360 |
if (fVal < 0.0) { |
|
22788 | 2361 |
fVal = (float)ceil((double)fVal - 0.5); |
7356 | 2362 |
} else { |
22788 | 2363 |
fVal = (float)floor((double)fVal + 0.5); |
7356 | 2364 |
} |
2365 |
#endif |
|
2366 |
__qMKSFLOAT(v, fVal); |
|
2367 |
RETURN (v); |
|
2368 |
%} |
|
2369 |
||
2370 |
" |
|
11945 | 2371 |
0.4 asShortFloat rounded |
2372 |
0.5 asShortFloat rounded |
|
2373 |
0.6 asShortFloat rounded |
|
2374 |
-0.4 asShortFloat rounded |
|
2375 |
-0.5 asShortFloat rounded |
|
2376 |
-0.6 asShortFloat rounded |
|
7356 | 2377 |
|
11945 | 2378 |
0.4 asShortFloat roundedAsFloat |
2379 |
0.5 asShortFloat roundedAsFloat |
|
2380 |
0.6 asShortFloat roundedAsFloat |
|
2381 |
-0.4 asShortFloat roundedAsFloat |
|
2382 |
-0.5 asShortFloat roundedAsFloat |
|
2383 |
-0.6 asShortFloat roundedAsFloat |
|
7356 | 2384 |
" |
2385 |
! |
|
2386 |
||
2387 |
truncated |
|
2388 |
"return the receiver truncated towards zero as an integer" |
|
2389 |
||
2390 |
|val| |
|
2391 |
||
11945 | 2392 |
%{ |
7356 | 2393 |
float fVal; |
2394 |
||
2395 |
fVal = __shortFloatVal(self); |
|
22788 | 2396 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2397 |
if (fVal < 0.0) { |
22788 | 2398 |
fVal = ceilf(fVal); |
7356 | 2399 |
} else { |
22788 | 2400 |
fVal = floorf(fVal); |
7356 | 2401 |
} |
2402 |
#else |
|
2403 |
if (fVal < 0.0) { |
|
22788 | 2404 |
fVal = (float)ceil((double)fVal); |
7356 | 2405 |
} else { |
22788 | 2406 |
fVal = (float)floor((double)fVal); |
7356 | 2407 |
} |
2408 |
#endif |
|
2409 |
/* |
|
2410 |
* ST-80 (and X3J20) returns integer. |
|
2411 |
*/ |
|
2412 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
22788 | 2413 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 2414 |
} |
2415 |
__qMKSFLOAT(val, fVal); |
|
2416 |
%}. |
|
2417 |
^ val asInteger |
|
2418 |
||
2419 |
" |
|
11945 | 2420 |
0.5 asShortFloat truncated |
2421 |
-0.5 asShortFloat truncated |
|
2422 |
0.5 asShortFloat truncatedAsFloat |
|
2423 |
-0.5 asShortFloat truncatedAsFloat |
|
7356 | 2424 |
" |
2425 |
! |
|
2426 |
||
2427 |
truncatedAsFloat |
|
2428 |
"return the receiver truncated towards zero as a float. |
|
11945 | 2429 |
This is much like #truncated, but avoids a (possibly expensive) conversion |
7356 | 2430 |
of the result to an integer. |
11945 | 2431 |
It may be useful, if the result is to be further used in another |
7356 | 2432 |
float-operation." |
2433 |
||
2434 |
%{ /* NOCONTEXT */ |
|
2435 |
float fVal; |
|
2436 |
OBJ v; |
|
2437 |
||
2438 |
fVal = __shortFloatVal(self); |
|
22788 | 2439 |
#if (defined(__i386__) || defined(xx__x86_64__)) && defined(__GNUC__) |
7356 | 2440 |
if (fVal < 0.0) { |
22788 | 2441 |
fVal = ceilf(fVal); |
7356 | 2442 |
} else { |
22788 | 2443 |
fVal = floorf(fVal); |
7356 | 2444 |
} |
2445 |
#else |
|
2446 |
if (fVal < 0.0) { |
|
22788 | 2447 |
fVal = (float)ceil((double)fVal); |
7356 | 2448 |
} else { |
22788 | 2449 |
fVal = (float)floor((double)fVal); |
7356 | 2450 |
} |
2451 |
#endif |
|
2452 |
__qMKSFLOAT(v, fVal); |
|
2453 |
RETURN (v); |
|
2454 |
%} |
|
2455 |
||
2456 |
" |
|
11945 | 2457 |
0.5 asShortFloat truncated |
2458 |
-0.5 asShortFloat truncated |
|
2459 |
0.5 asShortFloat truncatedAsFloat |
|
2460 |
-0.5 asShortFloat truncatedAsFloat |
|
7356 | 2461 |
" |
3897 | 2462 |
! ! |
2463 |
||
1880 | 2464 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 2465 |
|
2466 |
version |
|
18841 | 2467 |
^ '$Header$' |
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
2468 |
! |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
2469 |
|
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
2470 |
version_CVS |
18841 | 2471 |
^ '$Header$' |
1197 | 2472 |
! ! |
15012 | 2473 |
|
16670 | 2474 |
|
2475 |
ShortFloat initialize! |