author | Claus Gittinger <cg@exept.de> |
Fri, 19 Jul 2019 17:36:16 +0200 | |
changeset 24442 | 28cbdec4221a |
parent 24439 | 2701503c110a |
child 24458 | 1fa49076d39b |
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 |
24442 | 225 |
23 bit mantissa + 1 hidden bit, providing a precision of 24bits, |
22785 | 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 |
|
24439 | 682 |
"answer the radix of a ShortFloat's exponent |
7356 | 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" |
24439 | 686 |
|
687 |
"Modified (comment): / 19-07-2019 / 17:28:40 / Claus Gittinger" |
|
1197 | 688 |
! ! |
689 |
||
690 |
!ShortFloat methodsFor:'arithmetic'! |
|
691 |
||
692 |
* aNumber |
|
11730 | 693 |
"return the product of the receiver and the argument." |
1197 | 694 |
|
695 |
%{ /* NOCONTEXT */ |
|
696 |
||
697 |
OBJ newFloat; |
|
698 |
float result; |
|
699 |
double dResult; |
|
700 |
||
701 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 702 |
result = __shortFloatVal(self) * (float)(__intVal(aNumber)); |
1197 | 703 |
retResult: |
11945 | 704 |
__qMKSFLOAT(newFloat, result); |
705 |
RETURN ( newFloat ); |
|
706 |
} |
|
7356 | 707 |
if (__isShortFloat(aNumber)) { |
11945 | 708 |
result = __shortFloatVal(self) * __shortFloatVal(aNumber); |
709 |
goto retResult; |
|
710 |
} |
|
7356 | 711 |
if (__isFloatLike(aNumber)) { |
11945 | 712 |
dResult = (double) __shortFloatVal(self)* __floatVal(aNumber); |
713 |
__qMKFLOAT(newFloat, dResult); |
|
714 |
RETURN ( newFloat ); |
|
1197 | 715 |
} |
716 |
%}. |
|
717 |
^ aNumber productFromShortFloat:self |
|
718 |
! |
|
719 |
||
720 |
+ aNumber |
|
721 |
"return the sum of the receiver and the argument, aNumber" |
|
722 |
||
723 |
%{ /* NOCONTEXT */ |
|
724 |
||
725 |
OBJ newFloat; |
|
726 |
float result; |
|
727 |
double dResult; |
|
728 |
||
729 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 730 |
result = __shortFloatVal(self) + (float)(__intVal(aNumber)); |
1197 | 731 |
retResult: |
11945 | 732 |
__qMKSFLOAT(newFloat, result); |
733 |
RETURN ( newFloat ); |
|
734 |
} |
|
7356 | 735 |
if (__isShortFloat(aNumber)) { |
11945 | 736 |
result = __shortFloatVal(self) + __shortFloatVal(aNumber); |
737 |
goto retResult; |
|
738 |
} |
|
7356 | 739 |
if (__isFloatLike(aNumber)) { |
11945 | 740 |
dResult = (double) __shortFloatVal(self) + __floatVal(aNumber); |
741 |
__qMKFLOAT(newFloat, dResult); |
|
742 |
RETURN ( newFloat ); |
|
1197 | 743 |
} |
744 |
%}. |
|
745 |
^ aNumber sumFromShortFloat:self |
|
746 |
! |
|
747 |
||
748 |
- aNumber |
|
749 |
"return the difference of the receiver and the argument, aNumber" |
|
750 |
||
751 |
%{ /* NOCONTEXT */ |
|
752 |
||
753 |
OBJ newFloat; |
|
754 |
float result; |
|
755 |
double dResult; |
|
756 |
||
757 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 758 |
result = __shortFloatVal(self) - (float)(__intVal(aNumber)); |
1197 | 759 |
retResult: |
11945 | 760 |
__qMKSFLOAT(newFloat, result); |
761 |
RETURN ( newFloat ); |
|
762 |
} |
|
7356 | 763 |
if (__isShortFloat(aNumber)) { |
11945 | 764 |
result = __shortFloatVal(self) - __shortFloatVal(aNumber); |
765 |
goto retResult; |
|
766 |
} |
|
7356 | 767 |
if (__isFloatLike(aNumber)) { |
11945 | 768 |
dResult = (double) __shortFloatVal(self) - __floatVal(aNumber); |
769 |
__qMKFLOAT(newFloat, dResult); |
|
770 |
RETURN ( newFloat ); |
|
1197 | 771 |
} |
772 |
%}. |
|
773 |
^ aNumber differenceFromShortFloat:self |
|
774 |
! |
|
775 |
||
776 |
/ aNumber |
|
777 |
"return the quotient of the receiver and the argument, aNumber" |
|
778 |
||
779 |
%{ /* NOCONTEXT */ |
|
780 |
||
781 |
OBJ newFloat; |
|
782 |
float result, val; |
|
783 |
double dResult, dVal; |
|
784 |
||
785 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 786 |
if (aNumber != __mkSmallInteger(0)) { |
787 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
1197 | 788 |
retResult: |
11945 | 789 |
__qMKSFLOAT(newFloat, result); |
790 |
RETURN ( newFloat ); |
|
791 |
} |
|
792 |
} |
|
7356 | 793 |
if (__isShortFloat(aNumber)) { |
11945 | 794 |
val = __shortFloatVal(aNumber); |
795 |
if (val != 0.0) { |
|
796 |
result = __shortFloatVal(self) / val; |
|
797 |
goto retResult; |
|
798 |
} |
|
799 |
} |
|
7356 | 800 |
if (__isFloatLike(aNumber)) { |
11945 | 801 |
dVal = __floatVal(aNumber); |
802 |
if (dVal != 0.0) { |
|
803 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
804 |
__qMKFLOAT(newFloat, dResult); |
|
805 |
RETURN ( newFloat ); |
|
806 |
} |
|
1197 | 807 |
} |
808 |
%}. |
|
809 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
11945 | 810 |
" |
811 |
No, you shalt not divide by zero |
|
812 |
" |
|
813 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
1197 | 814 |
]. |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
815 |
^ aNumber quotientFromShortFloat:self |
1197 | 816 |
! |
817 |
||
14710 | 818 |
abs |
819 |
"return the absolute value of the receiver |
|
820 |
reimplemented here for speed" |
|
821 |
||
822 |
%{ /* NOCONTEXT */ |
|
823 |
||
824 |
OBJ newFloat; |
|
825 |
float val = __shortFloatVal(self); |
|
826 |
||
827 |
if (val < 0.0) { |
|
828 |
__qMKSFLOAT(newFloat, -val); |
|
829 |
RETURN ( newFloat ); |
|
830 |
} |
|
831 |
RETURN (self); |
|
832 |
%}. |
|
833 |
||
834 |
" |
|
835 |
3.0 asShortFloat abs |
|
836 |
-3.0 asShortFloat abs |
|
837 |
" |
|
838 |
! |
|
839 |
||
840 |
negated |
|
841 |
"return myself negated" |
|
842 |
||
843 |
%{ /* NOCONTEXT */ |
|
844 |
OBJ newFloat; |
|
845 |
float rslt = - __shortFloatVal(self); |
|
846 |
||
847 |
__qMKSFLOAT(newFloat, rslt); |
|
848 |
RETURN ( newFloat ); |
|
849 |
%}. |
|
850 |
^ 0.0 - self |
|
851 |
||
852 |
! |
|
853 |
||
14690 | 854 |
rem: aNumber |
855 |
"return the floating point remainder of the receiver and the argument, aNumber" |
|
856 |
||
857 |
%{ /* NOCONTEXT */ |
|
858 |
||
859 |
/* |
|
860 |
* notice: |
|
861 |
* the following inline code handles some common cases, |
|
862 |
* and exists as an optimization, to speed up those cases. |
|
863 |
* |
|
864 |
* Conceptionally, (and for most other argument types), |
|
865 |
* mixed arithmetic is implemented by double dispatching |
|
866 |
* (see the message send at the bottom) |
|
867 |
*/ |
|
868 |
OBJ newFloat; |
|
869 |
float result, val; |
|
870 |
double dResult, dVal; |
|
871 |
||
872 |
if (__isSmallInteger(aNumber)) { |
|
873 |
if (aNumber != __mkSmallInteger(0)) { |
|
874 |
val = (float)__intVal(aNumber); |
|
875 |
computeResult: |
|
14709
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
876 |
#ifdef NO_FMODF |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
877 |
dResult = fmod((double)__shortFloatVal(self), (double)val) ; |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
878 |
result = (float)dResult; |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
879 |
#else |
14690 | 880 |
result = fmodf(__shortFloatVal(self), val) ; |
14709
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
881 |
#endif |
14690 | 882 |
__qMKSFLOAT(newFloat, result); |
883 |
RETURN ( newFloat ); |
|
884 |
} |
|
885 |
} else if (__isFloatLike(aNumber)) { |
|
886 |
dVal = __floatVal(aNumber); |
|
887 |
if (dVal != 0.0) { |
|
888 |
dResult = fmod((double)(__shortFloatVal(self)), dVal) ; |
|
889 |
__qMKFLOAT(newFloat, dResult); |
|
890 |
RETURN ( newFloat ); |
|
891 |
} |
|
892 |
} else if (__isShortFloat(aNumber)) { |
|
893 |
val = __shortFloatVal(aNumber); |
|
894 |
if (val != 0.0) { |
|
895 |
goto computeResult; |
|
896 |
} |
|
897 |
} |
|
898 |
%}. |
|
899 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
900 |
" |
|
901 |
No, you shalt not divide by zero |
|
902 |
" |
|
903 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
904 |
]. |
|
905 |
^ aNumber remainderFromShortFloat:self |
|
906 |
! |
|
907 |
||
3147 | 908 |
uncheckedDivide:aNumber |
7356 | 909 |
"return the quotient of the receiver and the argument, aNumber. |
7471 | 910 |
Do not check for divide by zero (return NaN or Infinity). |
7356 | 911 |
This operation is provided for emulators of other languages/semantics, |
912 |
where no exception is raised for these results (i.e. Java). |
|
18841 | 913 |
It is only defined if the argument's type is the same as the receiver's." |
3147 | 914 |
|
915 |
%{ /* NOCONTEXT */ |
|
916 |
||
917 |
OBJ newFloat; |
|
918 |
float result, val; |
|
919 |
double dResult, dVal; |
|
920 |
||
921 |
if (__isSmallInteger(aNumber)) { |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
922 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
3147 | 923 |
retResult: |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
924 |
__qMKSFLOAT(newFloat, result); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
925 |
RETURN ( newFloat ); |
11945 | 926 |
} |
7356 | 927 |
if (__isShortFloat(aNumber)) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
928 |
val = __shortFloatVal(aNumber); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
929 |
result = __shortFloatVal(self) / val; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
930 |
goto retResult; |
11945 | 931 |
} |
7356 | 932 |
if (__isFloatLike(aNumber)) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
933 |
dVal = __floatVal(aNumber); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
934 |
dResult = (double) __shortFloatVal(self) / dVal; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
935 |
__qMKFLOAT(newFloat, dResult); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
936 |
RETURN ( newFloat ); |
3147 | 937 |
} |
938 |
%}. |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
939 |
^ aNumber quotientFromShortFloat:self |
3147 | 940 |
|
941 |
" |
|
942 |
0.0 asShortFloat uncheckedDivide:0 |
|
943 |
1.0 asShortFloat uncheckedDivide:0.0 |
|
944 |
" |
|
1197 | 945 |
! ! |
946 |
||
5257 | 947 |
!ShortFloat methodsFor:'coercing & converting'! |
1197 | 948 |
|
949 |
asFloat |
|
11238 | 950 |
"return a Float with same value as the receiver. |
951 |
Redefined for performance (machine can do it faster)" |
|
1197 | 952 |
|
953 |
%{ /* NOCONTEXT */ |
|
954 |
||
955 |
OBJ newFloat; |
|
956 |
double dVal = (double)__shortFloatVal(self); |
|
957 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
958 |
__qMKFLOAT(newFloat, dVal); |
1197 | 959 |
RETURN ( newFloat ); |
960 |
%} |
|
961 |
||
962 |
" |
|
11945 | 963 |
1.0 asShortFloat asFloat |
1197 | 964 |
" |
965 |
! |
|
966 |
||
967 |
asInteger |
|
968 |
"return an integer with same value - might truncate" |
|
969 |
||
970 |
%{ /* NOCONTEXT */ |
|
1670 | 971 |
float fVal; |
1197 | 972 |
|
1670 | 973 |
fVal = __shortFloatVal(self); |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
974 |
#ifdef __win32__ |
11945 | 975 |
if (! isnanf(fVal)) |
7419 | 976 |
#endif |
977 |
{ |
|
11945 | 978 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
979 |
RETURN ( __mkSmallInteger( (INT)fVal) ); |
|
980 |
} |
|
1197 | 981 |
} |
982 |
%}. |
|
983 |
^ super asInteger |
|
984 |
||
985 |
" |
|
986 |
12345.0 asShortFloat asInteger |
|
987 |
1e15 asShortFloat asInteger |
|
988 |
" |
|
989 |
! |
|
990 |
||
7437
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
991 |
asLongFloat |
21891 | 992 |
"return a LongFloat with same value as the receiver" |
993 |
||
7437
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
994 |
^ LongFloat fromShortFloat:self |
21891 | 995 |
|
996 |
"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
|
997 |
! |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
998 |
|
24284 | 999 |
asQuadFloat |
1000 |
"return a QuadFloat with same value as the receiver" |
|
1001 |
||
1002 |
^ QuadFloat fromShortFloat:self |
|
1003 |
||
1004 |
"Created: / 07-06-2019 / 02:29:14 / Claus Gittinger" |
|
1005 |
! |
|
1006 |
||
1197 | 1007 |
asShortFloat |
15022 | 1008 |
"return a ShortFloat with same value as the receiver - that's me" |
1197 | 1009 |
|
1010 |
^ self |
|
1200 | 1011 |
! |
1012 |
||
7356 | 1013 |
coerce:aNumber |
18841 | 1014 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
7356 | 1015 |
|
1016 |
^ aNumber asShortFloat |
|
1017 |
! |
|
1018 |
||
1200 | 1019 |
generality |
1020 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
1021 |
||
1022 |
^ 70 |
|
1023 |
||
1024 |
||
1197 | 1025 |
! ! |
1026 |
||
1027 |
!ShortFloat methodsFor:'comparing'! |
|
1028 |
||
1029 |
< aNumber |
|
1030 |
"return true, if the argument is greater" |
|
1031 |
||
1032 |
%{ /* NOCONTEXT */ |
|
1033 |
||
1034 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 1035 |
RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false ); |
1197 | 1036 |
} |
7356 | 1037 |
if (aNumber != nil) { |
11945 | 1038 |
if (__qIsFloatLike(aNumber)) { |
1039 |
RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false ); |
|
1040 |
} |
|
1041 |
if (__qIsShortFloat(aNumber)) { |
|
1042 |
RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false ); |
|
1043 |
} |
|
1197 | 1044 |
} |
1045 |
%}. |
|
1046 |
^ aNumber lessFromShortFloat:self |
|
1047 |
||
1200 | 1048 |
" |
1049 |
1.0 asShortFloat > (1/3) |
|
1050 |
" |
|
1197 | 1051 |
! |
1052 |
||
1053 |
<= aNumber |
|
1054 |
"return true, if the argument is greater or equal" |
|
1055 |
||
1056 |
%{ /* NOCONTEXT */ |
|
1057 |
||
1058 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 1059 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 1060 |
} |
7356 | 1061 |
if (aNumber != nil) { |
11945 | 1062 |
if (__qIsFloatLike(aNumber)) { |
1063 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
|
1064 |
} |
|
1065 |
if (__qIsShortFloat(aNumber)) { |
|
1066 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
|
1067 |
} |
|
1068 |
} |
|
1197 | 1069 |
%}. |
1070 |
^ self retry:#<= coercing:aNumber |
|
1071 |
! |
|
1072 |
||
1073 |
= aNumber |
|
7356 | 1074 |
"return true, if the argument represents the same numeric value |
1075 |
as the receiver, false otherwise" |
|
1197 | 1076 |
|
1077 |
%{ /* NOCONTEXT */ |
|
1078 |
||
1079 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 1080 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
7356 | 1081 |
} |
1082 |
if (aNumber == nil) { |
|
11945 | 1083 |
RETURN (false); |
1197 | 1084 |
} |
7356 | 1085 |
if (__qIsFloatLike(aNumber)) { |
11945 | 1086 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
1197 | 1087 |
} |
7356 | 1088 |
if (__qIsShortFloat(aNumber)) { |
11945 | 1089 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
1197 | 1090 |
} |
1091 |
%}. |
|
7356 | 1092 |
^ aNumber equalFromShortFloat:self |
1197 | 1093 |
! |
1094 |
||
1095 |
> aNumber |
|
1096 |
"return true, if the argument is less" |
|
1097 |
||
1098 |
%{ /* NOCONTEXT */ |
|
1099 |
||
1100 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 1101 |
RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false ); |
1197 | 1102 |
} |
7356 | 1103 |
if (aNumber != nil) { |
11945 | 1104 |
if (__qIsFloatLike(aNumber)) { |
1105 |
RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false ); |
|
1106 |
} |
|
1107 |
if (__qIsShortFloat(aNumber)) { |
|
1108 |
RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false ); |
|
1109 |
} |
|
1197 | 1110 |
} |
1111 |
%}. |
|
1112 |
^ self retry:#> coercing:aNumber |
|
1113 |
! |
|
1114 |
||
1115 |
>= aNumber |
|
1116 |
"return true, if the argument is less or equal" |
|
1117 |
||
1118 |
%{ /* NOCONTEXT */ |
|
1119 |
||
1120 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 1121 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 1122 |
} |
7356 | 1123 |
if (aNumber != nil) { |
11945 | 1124 |
if (__qIsFloatLike(aNumber)) { |
1125 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
|
1126 |
} |
|
1127 |
if (__qIsShortFloat(aNumber)) { |
|
1128 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
|
1129 |
} |
|
1197 | 1130 |
} |
1131 |
%}. |
|
1132 |
^ self retry:#>= coercing:aNumber |
|
1133 |
! |
|
1134 |
||
1135 |
hash |
|
1136 |
"return a number for hashing; redefined, since floats compare |
|
1137 |
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
|
1138 |
as 3 hash." |
1197 | 1139 |
|
1140 |
|i| |
|
1141 |
||
1142 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1143 |
i := self asInteger. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1144 |
self = i ifTrue:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1145 |
^ i hash |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1146 |
]. |
1197 | 1147 |
]. |
1148 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
1149 |
^ self asFloat hash |
1197 | 1150 |
|
1151 |
" |
|
11945 | 1152 |
1.2345 hash |
1153 |
1.2345 asShortFloat hash |
|
1154 |
1.0 hash |
|
1155 |
1.0 asShortFloat hash |
|
1156 |
0.5 asShortFloat hash |
|
1157 |
0.25 asShortFloat hash |
|
1158 |
0.5 hash |
|
1159 |
0.25 hash |
|
1197 | 1160 |
" |
1161 |
! |
|
1162 |
||
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1163 |
isAlmostEqualTo:aNumber nEpsilon:nE |
16670 | 1164 |
"return true, if the argument, aNumber represents almost the same numeric value |
1165 |
as the receiver, false otherwise. |
|
1166 |
||
1167 |
nE is the number of minimal float distances, that the numbers may differ and |
|
1168 |
still be considered equal. |
|
1169 |
||
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1170 |
For background information why floats need this |
16670 | 1171 |
read: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ |
1172 |
" |
|
1173 |
||
21149 | 1174 |
Epsilon isNil ifTrue:[ |
22853 | 1175 |
Epsilon := self class computeEpsilon. |
21149 | 1176 |
]. |
1177 |
||
16670 | 1178 |
%{ /* NOCONTEXT */ |
1179 |
||
1180 |
/* |
|
1181 |
* notice: |
|
1182 |
* the following inline code handles some common cases, |
|
1183 |
* and exists as an optimization, to speed up those cases. |
|
1184 |
* |
|
1185 |
* Conceptionally, (and for most other argument types), |
|
1186 |
* mixed arithmetic is implemented by double dispatching |
|
1187 |
* (see the message send at the bottom) |
|
1188 |
*/ |
|
1189 |
||
1190 |
INT32 ulpDiff; |
|
1191 |
union { |
|
22853 | 1192 |
float f; |
1193 |
INT32 i; |
|
16670 | 1194 |
} myself, otherFloat; |
1195 |
int nEpsilon; |
|
1196 |
float scaledEpsilon; |
|
1197 |
||
1198 |
if (!__isSmallInteger(nE)) { |
|
22853 | 1199 |
goto tryHarder; |
16670 | 1200 |
} |
1201 |
||
1202 |
nEpsilon = __intVal(nE); |
|
1203 |
scaledEpsilon = nEpsilon *__shortFloatVal(@global(Epsilon)); |
|
1204 |
||
1205 |
if (__isSmallInteger(aNumber)) { |
|
22853 | 1206 |
otherFloat.f = (float)(__intVal(aNumber)); |
16670 | 1207 |
} else if (aNumber == nil) { |
22853 | 1208 |
RETURN(false) |
16670 | 1209 |
} else if (__qIsFloatLike(aNumber)) { |
22853 | 1210 |
otherFloat.f = (float)(__floatVal(aNumber)); |
16670 | 1211 |
} else if (__qIsShortFloat(aNumber)) { |
22853 | 1212 |
otherFloat.f = __shortFloatVal(aNumber); |
1213 |
// } else if (__qIsLongFloat(aNumber)) { |
|
1214 |
// otherFloat.f = (float)(__longFloatVal(aNumber)); |
|
16670 | 1215 |
} else { |
22853 | 1216 |
goto tryHarder; |
16670 | 1217 |
} |
1218 |
||
1219 |
myself.f = __shortFloatVal(self); |
|
1220 |
||
1221 |
// Check if the numbers are really close -- needed |
|
1222 |
// when comparing numbers near zero (ULP method below fails for numbers near 0!). |
|
22919 | 1223 |
# ifdef NO_ABSF |
22920 | 1224 |
if (fabs((double)(myself.f - otherFloat.f)) <= scaledEpsilon) { |
22919 | 1225 |
RETURN(true); |
1226 |
} |
|
1227 |
# else |
|
22853 | 1228 |
// fprintf(stderr, "%.10f %.10f\n", fabsf(myself.f - otherFloat.f), scaledEpsilon); |
1229 |
if (fabsf(myself.f - otherFloat.f) <= scaledEpsilon) { |
|
1230 |
RETURN(true); |
|
16670 | 1231 |
} |
22919 | 1232 |
#endif |
16670 | 1233 |
|
1234 |
// if the signs differ, the numbers are different |
|
1235 |
if ((myself.f >= 0) != (otherFloat.f >= 0)) { |
|
22853 | 1236 |
RETURN(false); |
16670 | 1237 |
} |
1238 |
||
1239 |
// compute the difference of the 'units in the last place" ULP |
|
1240 |
// (if ulpDiff == 1, two floats are adjecant) |
|
1241 |
ulpDiff = myself.i - otherFloat.i; |
|
1242 |
if (ulpDiff < 0) ulpDiff = -ulpDiff; |
|
1243 |
if (ulpDiff <= nEpsilon) { |
|
22853 | 1244 |
RETURN(true); |
16670 | 1245 |
} else { |
22853 | 1246 |
RETURN(false) |
16670 | 1247 |
} |
1248 |
||
1249 |
tryHarder:; |
|
1250 |
%}. |
|
1251 |
^ aNumber isAlmostEqualToFromShortFloat:self nEpsilon:nE |
|
1252 |
||
1253 |
" |
|
22853 | 1254 |
67329.234 asShortFloat isAlmostEqualTo:67329.23401 asShortFloat nEpsilon:1 |
1255 |
1.0 asShortFloat isAlmostEqualTo:1.0000001 asShortFloat nEpsilon:1 |
|
1256 |
1.0 asShortFloat isAlmostEqualTo:1.0000001 nEpsilon:1 |
|
1257 |
1.0 asShortFloat isAlmostEqualTo:-1.0 nEpsilon:1 |
|
1258 |
1.0 asShortFloat isAlmostEqualTo:1 nEpsilon:1 |
|
22922 | 1259 |
0.0 asShortFloat isAlmostEqualTo:0.0000001 asShortFloat nEpsilon:1 |
1260 |
0.0 asShortFloat isAlmostEqualTo:0.000001 asShortFloat nEpsilon:1 |
|
22853 | 1261 |
0.0 asShortFloat isAlmostEqualTo:self epsilon nEpsilon:1 |
1262 |
0.0 asShortFloat - 1.192093e-07 asShortFloat |
|
16670 | 1263 |
" |
22853 | 1264 |
|
1265 |
"Modified: / 10-05-2018 / 00:47:22 / stefan" |
|
16670 | 1266 |
! |
1267 |
||
1197 | 1268 |
~= aNumber |
1269 |
"return true, if the arguments value are not equal" |
|
1270 |
||
1271 |
%{ /* NOCONTEXT */ |
|
1272 |
||
7356 | 1273 |
if (aNumber != nil) { |
11945 | 1274 |
if (__isSmallInteger(aNumber)) { |
1275 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
|
1276 |
} |
|
1277 |
if (__qIsFloatLike(aNumber)) { |
|
1278 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
|
1279 |
} |
|
1280 |
if (__qIsShortFloat(aNumber)) { |
|
1281 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
|
1282 |
} |
|
7356 | 1283 |
} else { |
11945 | 1284 |
RETURN ( true ); |
1197 | 1285 |
} |
1286 |
%}. |
|
7356 | 1287 |
^ super ~= aNumber |
1197 | 1288 |
! ! |
1289 |
||
24189 | 1290 |
!ShortFloat methodsFor:'copying'! |
1291 |
||
1292 |
deepCopy |
|
1293 |
"return a deep copy of myself |
|
1294 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
1295 |
||
1296 |
^ self |
|
1297 |
! |
|
1298 |
||
1299 |
deepCopyUsing:aDictionary postCopySelector:postCopySelector |
|
1300 |
"return a deep copy of myself |
|
1301 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
1302 |
||
1303 |
^ self |
|
1304 |
! |
|
1305 |
||
1306 |
shallowCopy |
|
1307 |
"return a shallow copy of the receiver" |
|
1308 |
||
1309 |
^ self |
|
1310 |
! |
|
1311 |
||
1312 |
simpleDeepCopy |
|
1313 |
"return a deep copy of the receiver |
|
1314 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
1315 |
||
1316 |
^ self |
|
1317 |
! ! |
|
1318 |
||
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1319 |
!ShortFloat methodsFor:'mathematical functions'! |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1320 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1321 |
fastInverseSqrt |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1322 |
"return a rough but fast approximation of (1 / self sqrt). |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1323 |
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
|
1324 |
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
|
1325 |
much, unless inlined into the sender code. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1326 |
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
|
1327 |
(to speed up 3D computations, for example). |
1aac241c9981
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13359
diff
changeset
|
1328 |
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
|
1329 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1330 |
%{ /* NOCONTEXT */ |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1331 |
float x, rslt; |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1332 |
OBJ newFloat; |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1333 |
|
13361 | 1334 |
if (sizeof(float) == 4) { |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1335 |
x = __shortFloatVal(self); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1336 |
{ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1337 |
float xhalf = 0.5f * x; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1338 |
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
|
1339 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1340 |
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
|
1341 |
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
|
1342 |
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
|
1343 |
__qMKSFLOAT(newFloat, x); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1344 |
RETURN ( newFloat ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1345 |
} |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1346 |
} |
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 |
^ 1 / self sqrt |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1349 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1350 |
" |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1351 |
10.0 asShortFloat fastInverseSqrt |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1352 |
(1 / 10.0 asShortFloat sqrt) |
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 |
|
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 b t0 t1 t2| |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1357 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1358 |
a := 345 asShortFloat. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1359 |
t0 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1360 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1361 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1362 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1363 |
t1 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1364 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1365 |
a fastInverseSqrt |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1366 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1367 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1368 |
t2 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1369 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1370 |
(1 / a sqrt) |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1371 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1372 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1373 |
Transcript show:'empty: '; showCR:t0. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1374 |
Transcript show:'fast: '; showCR:t1. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1375 |
Transcript show:'regular: '; showCR:t2. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1376 |
" |
21829 | 1377 |
! |
1378 |
||
21956 | 1379 |
ln |
1380 |
"return the natural logarithm of the receiver. |
|
1381 |
Raises an exception, if the receiver is less or equal to zero." |
|
1382 |
||
1383 |
%{ /* NOCONTEXT */ |
|
1384 |
#ifndef __SCHTEAM__ |
|
1385 |
float val, rslt; |
|
1386 |
OBJ newFloat; |
|
1387 |
||
1388 |
val = __shortFloatVal(self); |
|
1389 |
||
21976 | 1390 |
/* |
1391 |
* to suppress the warnBox opened by win32 |
|
21956 | 1392 |
* to avoid returning -INF from some unix math libs (__osx__) |
1393 |
*/ |
|
1394 |
if (val > 0.0) { |
|
21976 | 1395 |
__threadErrno = 0; |
21956 | 1396 |
# ifdef NO_LOGF |
21976 | 1397 |
{ |
1398 |
double dRslt = log((double)val); |
|
1399 |
rslt = (float)dRslt; |
|
1400 |
} |
|
21956 | 1401 |
# else |
21976 | 1402 |
rslt = logf(val); |
21956 | 1403 |
# endif |
21976 | 1404 |
if (! isnanf(rslt)) /* Currently all our systems support isnan() */ |
1405 |
{ |
|
1406 |
if (__threadErrno == 0) { |
|
1407 |
__qMKSFLOAT(newFloat, rslt); |
|
1408 |
RETURN ( newFloat ); |
|
1409 |
} |
|
1410 |
} |
|
21956 | 1411 |
} |
1412 |
#endif |
|
1413 |
%}. |
|
1414 |
" |
|
1415 |
an invalid value for logarithm |
|
21972 | 1416 |
if you need -INF for a zero receiver, try Number trapInfinity:[...] |
21956 | 1417 |
" |
1418 |
^ self class |
|
21976 | 1419 |
raise:(self = 0 ifTrue:[#infiniteResultSignal] ifFalse:[#domainErrorSignal]) |
1420 |
receiver:self |
|
1421 |
selector:#ln |
|
1422 |
arguments:#() |
|
1423 |
errorString:'bad receiver in log10 (not strictly positive)' |
|
21956 | 1424 |
|
1425 |
" |
|
21976 | 1426 |
0 asFloat ln |
21965 | 1427 |
Number trapInfinity:[ 0 asFloat ln ] |
1428 |
||
21956 | 1429 |
10 asFloat ln 2.30258509299405 |
1430 |
10 asShortFloat ln 2.302585 |
|
1431 |
10 asLongFloat ln 2.302585092994045684 |
|
1432 |
10 asQDouble ln 2.30258509299404568402 |
|
1433 |
||
1434 |
50 asFloat ln 3.91202300542815 |
|
1435 |
50 asShortFloat ln 3.912023 |
|
1436 |
50 asLongFloat ln 3.912023005428146059 |
|
1437 |
50 asQDouble ln 3.91202300542814605862 |
|
1438 |
" |
|
1439 |
||
1440 |
"Created: / 03-07-2017 / 15:20:07 / cg" |
|
1441 |
! |
|
1442 |
||
21829 | 1443 |
log10 |
1444 |
"return the base-10 logarithm of the receiver. |
|
1445 |
Raises an exception, if the receiver is less or equal to zero." |
|
1446 |
||
1447 |
%{ /* NOCONTEXT */ |
|
1448 |
#ifndef __SCHTEAM__ |
|
1449 |
float val, rslt; |
|
1450 |
OBJ newFloat; |
|
1451 |
||
1452 |
val = __shortFloatVal(self); |
|
1453 |
||
21976 | 1454 |
/* |
1455 |
* to suppress the warnBox opened by win32 |
|
21955 | 1456 |
* to avoid returning -INF from some unix math libs (__osx__) |
1457 |
*/ |
|
1458 |
if (val > 0.0) { |
|
21976 | 1459 |
__threadErrno = 0; |
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
1460 |
# ifdef NO_LOG10F |
21976 | 1461 |
{ |
1462 |
double dRslt = log10((double)val); |
|
1463 |
rslt = (float)dRslt; |
|
1464 |
} |
|
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
1465 |
# else |
21976 | 1466 |
rslt = log10f(val); |
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
1467 |
# endif |
21976 | 1468 |
if (! isnanf(rslt)) /* Currently all our systems support isnan() */ |
1469 |
{ |
|
1470 |
if (__threadErrno == 0) { |
|
1471 |
__qMKSFLOAT(newFloat, rslt); |
|
1472 |
RETURN ( newFloat ); |
|
1473 |
} |
|
1474 |
} |
|
21829 | 1475 |
} |
1476 |
#endif |
|
1477 |
%}. |
|
1478 |
" |
|
1479 |
an invalid value for logarithm |
|
21972 | 1480 |
if you need -INF for a zero receiver, try Number trapInfinity:[...] |
21829 | 1481 |
" |
1482 |
^ self class |
|
21976 | 1483 |
raise:(self = 0 ifTrue:[#infiniteResultSignal] ifFalse:[#domainErrorSignal]) |
1484 |
receiver:self |
|
1485 |
selector:#log10 |
|
1486 |
arguments:#() |
|
1487 |
errorString:'bad receiver in log10 (not strictly positive)' |
|
21829 | 1488 |
|
1489 |
" |
|
21976 | 1490 |
0 asShortFloat log10 |
21965 | 1491 |
Number trapInfinity:[ 0 asShortFloat log10 ] |
1492 |
||
21829 | 1493 |
10 asFloat log10 1.0 |
1494 |
10 asShortFloat log10 1.0 |
|
1495 |
10 asLongFloat log10 1.0 |
|
1496 |
10 asQDouble log10 1.00000000000000000000000000000000000000000 |
|
1497 |
||
1498 |
50 asFloat log10 1.69897000433602 |
|
1499 |
50 asShortFloat log10 1.69897 |
|
1500 |
50 asLongFloat log10 1.698970004336018805 |
|
1501 |
50 asQDouble log10 1.69897000433601880478626110527550697323181 |
|
1502 |
" |
|
1503 |
||
1504 |
"Created: / 16-06-2017 / 10:47:53 / cg" |
|
21972 | 1505 |
"Modified (comment): / 03-07-2017 / 15:59:05 / cg" |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1506 |
! ! |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1507 |
|
1197 | 1508 |
!ShortFloat methodsFor:'printing & storing'! |
1509 |
||
22303 | 1510 |
printOn:aStream |
1511 |
"append a printed representation of the receiver to |
|
1512 |
the argument, aStream. |
|
1513 |
||
1514 |
I use #printString instead of #printOn: as basic print mechanism." |
|
1515 |
||
1516 |
aStream nextPutAll:self printString |
|
1517 |
||
1518 |
"Created: / 10-10-2017 / 14:05:28 / cg" |
|
1519 |
! |
|
1520 |
||
1197 | 1521 |
printString |
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1522 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1523 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1524 |
#printOn: as basic print mechanism." |
1197 | 1525 |
|
18139 | 1526 |
^ self printStringWithFormat:DefaultPrintFormat |
1527 |
||
1528 |
" |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1529 |
1.234 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1530 |
1.0 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1531 |
1e10 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1532 |
1.2e3 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1533 |
1.2e30 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1534 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1535 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1536 |
self pi printString. |
18139 | 1537 |
|
19570
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 := '.3'. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1540 |
self pi printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1541 |
DefaultPrintFormat := '.7'. |
18139 | 1542 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1543 |
DecimalPointCharacterForPrinting := $,. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1544 |
1.234 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1545 |
1.0 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1546 |
1e10 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1547 |
1.2e3 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1548 |
1.2e30 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1549 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1550 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1551 |
DecimalPointCharacterForPrinting := $. |
18139 | 1552 |
" |
1553 |
! |
|
1554 |
||
1555 |
printStringWithFormat:format |
|
1556 |
"return a printed representation of the receiver; |
|
1557 |
fmt must be of the form: .nn, where nn is the number of digits. |
|
1558 |
To print 6 valid digits, use printStringWithFormat:'.6' |
|
1559 |
For Floats, the default used in printString, is 15 (because its a double); |
|
1560 |
for ShortFloats, it is 6 (because it is a float)" |
|
1561 |
||
1197 | 1562 |
%{ /* NOCONTEXT */ |
1563 |
||
1564 |
char buffer[64]; |
|
1565 |
REGISTER char *cp; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1566 |
int len; |
1197 | 1567 |
OBJ s; |
16670 | 1568 |
char *fmt; |
1569 |
char fmtBuffer[20]; |
|
1570 |
||
18139 | 1571 |
if (__isStringLike(format)) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1572 |
fmt = (char *) __stringVal(format); |
16670 | 1573 |
} else { |
19570
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 |
* in case we get called with garbage... |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1576 |
*/ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1577 |
fmt = ".7"; |
16670 | 1578 |
} |
1579 |
||
1580 |
/* |
|
1581 |
* build a printf format string |
|
1582 |
*/ |
|
1583 |
fmtBuffer[0] = '%'; |
|
1584 |
strncpy(fmtBuffer+1, fmt, 10); |
|
1585 |
strcat(fmtBuffer, "g"); |
|
1197 | 1586 |
|
1587 |
/* |
|
1588 |
* actually only needed on sparc: since thisContext is |
|
1589 |
* in a global register, which gets destroyed by printf, |
|
1590 |
* manually save it here - very stupid ... |
|
1591 |
*/ |
|
1592 |
__BEGIN_PROTECT_REGISTERS__ |
|
16670 | 1593 |
len = snprintf(buffer, sizeof(buffer), fmtBuffer, (float)__shortFloatVal(self)); |
1197 | 1594 |
__END_PROTECT_REGISTERS__ |
1595 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1596 |
if (len >= 0 && len <= sizeof(buffer)-3) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1597 |
/* |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1598 |
* 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
|
1599 |
* (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
|
1600 |
*/ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1601 |
for (cp = buffer; *cp; cp++) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1602 |
if ((*cp == '.') || (*cp == ',') || (*cp == 'E') || (*cp == 'e')) break; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1603 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1604 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1605 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1606 |
*cp++ = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1607 |
} else { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1608 |
*cp++ = '.'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1609 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1610 |
*cp++ = '0'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1611 |
*cp = '\0'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1612 |
} else { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1613 |
if (cp && (*cp == '.')) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1614 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1615 |
*cp = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1616 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1617 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1618 |
} |
1197 | 1619 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1620 |
s = __MKSTRING(buffer); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1621 |
if (s != nil) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1622 |
RETURN (s); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1623 |
} |
1197 | 1624 |
} |
1625 |
%}. |
|
5432 | 1626 |
^ self asFloat printString |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1627 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1628 |
" |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1629 |
1.234 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1630 |
ShortFloat pi printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1631 |
1.0 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1632 |
1e10 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1633 |
1.2e3 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1634 |
1.2e30 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1635 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1636 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1637 |
self pi printString. |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1638 |
|
19570
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 := '.3'. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1641 |
self pi printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1642 |
DefaultPrintFormat := '.7'. |
16670 | 1643 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1644 |
DecimalPointCharacterForPrinting := $,. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1645 |
1.234 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1646 |
1.0 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1647 |
1e10 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1648 |
1.2e3 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1649 |
1.2e30 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1650 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1651 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1652 |
DecimalPointCharacterForPrinting := $. |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1653 |
" |
5718 | 1654 |
! |
1655 |
||
1656 |
printfPrintString:formatString |
|
7746 | 1657 |
"non-standard: return a printed representation of the receiver |
5718 | 1658 |
as specified by formatString, which is defined by printf. |
21950 | 1659 |
|
1660 |
If you use this, be aware, that the format string must be correct and something like %f. |
|
5718 | 1661 |
Also, the resulting string may not be longer than 255 bytes - |
20433 | 1662 |
since that's the (static) size of the buffer. |
21906 | 1663 |
|
11924
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1664 |
This method is NONSTANDARD and may be removed without notice. |
21976 | 1665 |
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
|
1666 |
Please use the printf: method, which is safe as it is completely implemented in Smalltalk." |
5718 | 1667 |
|
1668 |
%{ /* STACK: 400 */ |
|
1669 |
char buffer[256]; |
|
1670 |
OBJ s; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1671 |
int len; |
5718 | 1672 |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1673 |
if (__isStringLike(formatString)) { |
21976 | 1674 |
/* |
1675 |
* actually only needed on sparc: since thisContext is |
|
1676 |
* in a global register, which gets destroyed by printf, |
|
1677 |
* manually save it here - very stupid ... |
|
1678 |
*/ |
|
1679 |
__BEGIN_PROTECT_REGISTERS__ |
|
5718 | 1680 |
|
21976 | 1681 |
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
|
1682 |
|
21976 | 1683 |
__END_PROTECT_REGISTERS__ |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1684 |
|
21976 | 1685 |
if (len < 0) goto fail; |
1686 |
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
|
1687 |
|
21976 | 1688 |
s = __MKSTRING_L(buffer, len); |
1689 |
if (s != nil) { |
|
1690 |
RETURN (s); |
|
1691 |
} |
|
5718 | 1692 |
} |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1693 |
fail: ; |
5718 | 1694 |
%}. |
21906 | 1695 |
^ super printfPrintString:formatString |
5718 | 1696 |
|
1697 |
"ShortFloat pi printfPrintString:'%%lg -> %lg'" |
|
1698 |
"ShortFloat pi printfPrintString:'%%lf -> %lf'" |
|
1699 |
"ShortFloat pi printfPrintString:'%%7.5lg -> %7.5lg'" |
|
1700 |
"ShortFloat pi printfPrintString:'%%G -> %G'" |
|
1701 |
"ShortFloat pi printfPrintString:'%%F -> %F'" |
|
1702 |
"ShortFloat pi printfPrintString:'%%7.5G -> %7.5G'" |
|
1703 |
"ShortFloat pi printfPrintString:'%%7.5F -> %7.5F'" |
|
21906 | 1704 |
|
21950 | 1705 |
"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
|
1706 |
! |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1707 |
|
22303 | 1708 |
storeOn:aStream |
1709 |
"append a printed representation of the receiver to |
|
1710 |
the argument, aStream. |
|
1711 |
||
1712 |
I use #storeString instead of #storeOn: as basic store mechanism." |
|
1713 |
||
1714 |
aStream nextPutAll:self storeString |
|
1715 |
||
1716 |
"Created: / 10-10-2017 / 14:06:52 / cg" |
|
1717 |
! |
|
1718 |
||
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1719 |
storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1720 |
"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
|
1721 |
all valid digits are printed. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1722 |
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
|
1723 |
#storeOn: as basic print mechanism." |
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 |
%{ /* NOCONTEXT */ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1726 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1727 |
char buffer[64]; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1728 |
REGISTER char *cp; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1729 |
OBJ s; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1730 |
int len; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1731 |
|
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 |
* build a printf format string |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1734 |
*/ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1735 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1736 |
__BEGIN_PROTECT_REGISTERS__ |
16670 | 1737 |
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
|
1738 |
__END_PROTECT_REGISTERS__ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1739 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1740 |
if (len >= 0 && len < sizeof(buffer)-3) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1741 |
/* |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1742 |
* 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
|
1743 |
* (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
|
1744 |
*/ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1745 |
for (cp = buffer; *cp; cp++) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1746 |
if ((*cp == '.') || (*cp == ',') || (*cp == 'E') || (*cp == 'e')) break; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1747 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1748 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1749 |
*cp++ = '.'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1750 |
*cp++ = '0'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1751 |
*cp = '\0'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1752 |
} |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1753 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1754 |
s = __MKSTRING(buffer); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1755 |
if (s != nil) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1756 |
RETURN (s); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1757 |
} |
8422
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 |
%}. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1760 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1761 |
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
|
1762 |
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
|
1763 |
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
|
1764 |
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
|
1765 |
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
|
1766 |
" |
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
20433
diff
changeset
|
1767 |
^ AllocationFailure raise. |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1768 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1769 |
" |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1770 |
0.1 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1771 |
((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
|
1772 |
1.0 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1773 |
1.234 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1774 |
1e10 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1775 |
1.2e3 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1776 |
1.2e30 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1777 |
Float pi asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1778 |
(1.0 uncheckedDivide:0) asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1779 |
(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
|
1780 |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1781 |
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
|
1782 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1783 |
DecimalPointCharacterForPrinting := $,. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1784 |
1.234 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1785 |
1.0 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1786 |
1e10 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1787 |
1.2e3 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1788 |
1.2e30 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1789 |
(1.0 uncheckedDivide:0) asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1790 |
(0.0 uncheckedDivide:0) asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1791 |
DecimalPointCharacterForPrinting := $. |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1792 |
" |
1197 | 1793 |
! ! |
1794 |
||
12628 | 1795 |
!ShortFloat methodsFor:'private accessing'! |
1796 |
||
1797 |
basicAt:index |
|
1798 |
"return an internal byte of the float. |
|
1799 |
The value returned here depends on byte order, float representation etc. |
|
1800 |
Therefore, this method should be used strictly private. |
|
1801 |
||
1802 |
Notice: |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1803 |
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
|
1804 |
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
|
1805 |
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
|
1806 |
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
|
1807 |
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
|
1808 |
values as if this filler wasnt present." |
12628 | 1809 |
|
1810 |
%{ /* NOCONTEXT */ |
|
1811 |
||
1812 |
register int indx; |
|
1813 |
unsigned char *cp; |
|
1814 |
||
1815 |
/* |
|
1816 |
* notice the missing test for self being a nonNilObject - |
|
1817 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
1818 |
* and SmallInteger |
|
1819 |
*/ |
|
1820 |
if (__isSmallInteger(index)) { |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1821 |
indx = __intVal(index) - 1; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1822 |
if (((unsigned)(indx)) < sizeof(float)) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1823 |
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
|
1824 |
RETURN ( __mkSmallInteger(cp[indx] & 0xFF) ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1825 |
} |
12628 | 1826 |
} |
1827 |
%}. |
|
1828 |
^ self indexNotIntegerOrOutOfBounds:index |
|
1829 |
! |
|
1830 |
||
1831 |
basicAt:index put:value |
|
1832 |
"set an internal byte of the float. |
|
1833 |
The value to be stored here depends on byte order, float representation etc. |
|
1834 |
Therefore, this method should be used strictly private. |
|
1835 |
||
1836 |
Notice: |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1837 |
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
|
1838 |
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
|
1839 |
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
|
1840 |
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
|
1841 |
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
|
1842 |
values as if this filler wasnt present." |
12628 | 1843 |
|
1844 |
%{ /* NOCONTEXT */ |
|
1845 |
register int indx, val; |
|
1846 |
unsigned char *cp; |
|
1847 |
||
1848 |
/* |
|
1849 |
* notice the missing test for self being a nonNilObject - |
|
1850 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
1851 |
* and SmallInteger |
|
1852 |
*/ |
|
1853 |
if (__bothSmallInteger(index, value)) { |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1854 |
val = __intVal(value); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1855 |
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
|
1856 |
indx = __intVal(index) - 1; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1857 |
if (((unsigned)(indx)) < sizeof(float)) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1858 |
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
|
1859 |
cp[indx] = val; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1860 |
RETURN ( value ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1861 |
} |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1862 |
} |
12628 | 1863 |
} |
1864 |
%}. |
|
1865 |
value isInteger ifFalse:[ |
|
14673
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 |
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
|
1868 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1869 |
^ self elementNotInteger |
12628 | 1870 |
]. |
1871 |
(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
|
1872 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1873 |
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
|
1874 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1875 |
^ self elementBoundsError:value |
12628 | 1876 |
]. |
1877 |
^ self indexNotIntegerOrOutOfBounds:index |
|
1878 |
! ! |
|
1879 |
||
22259 | 1880 |
!ShortFloat methodsFor:'private-accessing'! |
1881 |
||
1882 |
byteAt:index |
|
1883 |
^ self basicAt:index |
|
1884 |
! |
|
1885 |
||
1886 |
byteAt:index put:newByte |
|
1887 |
self shouldNotImplement |
|
1888 |
! ! |
|
1889 |
||
16670 | 1890 |
!ShortFloat methodsFor:'queries'! |
1891 |
||
1892 |
nextFloat:count |
|
1893 |
"answer the next float count places after (or before if count is negative) myself" |
|
1894 |
||
1895 |
%{ |
|
1896 |
union u { |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1897 |
float d; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1898 |
INT32 i; |
16670 | 1899 |
} this; |
1900 |
||
1901 |
if (__isSmallInteger(count)) { |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1902 |
this.d = __shortFloatVal(self); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1903 |
if (isfinite(this.d)) |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1904 |
this.i += __intVal(count); |
16670 | 1905 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1906 |
RETURN(__MKSFLOAT(this.d)); |
16670 | 1907 |
} |
1908 |
%}. |
|
1909 |
self primitiveFailed:#badArgument |
|
1910 |
||
1911 |
" |
|
1912 |
(1.0 asShortFloat nextFloat:2) storeString |
|
1913 |
(67329.234 asShortFloat nextFloat:1) storeString |
|
1914 |
ShortFloat NaN nextFloat:100000 |
|
1915 |
ShortFloat infinity nextFloat:100000 |
|
1916 |
" |
|
1917 |
! ! |
|
1918 |
||
3897 | 1919 |
!ShortFloat methodsFor:'special access'! |
1920 |
||
1921 |
exponent |
|
21850 | 1922 |
"extract a normalized float's exponent. |
3897 | 1923 |
The returned value depends on the float-representation of |
1924 |
the underlying machine and is therefore highly unportable. |
|
1925 |
This is not for general use. |
|
1926 |
This assumes that the mantissa is normalized to |
|
21850 | 1927 |
0.5 .. 1.0 and the float's value is mantissa * 2^exp" |
3897 | 1928 |
|
1929 |
%{ /* NOCONTEXT */ |
|
8913 | 1930 |
int exp; |
3897 | 1931 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1932 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1933 |
frexpf( __shortFloatVal(self), &exp); |
1934 |
#else |
|
1935 |
frexp( (double)(__shortFloatVal(self)), &exp); |
|
1936 |
#endif |
|
14744 | 1937 |
RETURN (__mkSmallInteger(exp)); |
3897 | 1938 |
%}. |
21850 | 1939 |
^ super exponent |
3897 | 1940 |
|
1941 |
" |
|
11945 | 1942 |
4.0 asShortFloat exponent |
1943 |
2.0 asShortFloat exponent |
|
1944 |
1.0 asShortFloat exponent |
|
1945 |
0.5 asShortFloat exponent |
|
1946 |
0.25 asShortFloat exponent |
|
1947 |
0.00000011111 asShortFloat exponent |
|
3897 | 1948 |
" |
21850 | 1949 |
|
1950 |
"Modified: / 20-06-2017 / 11:33:41 / cg" |
|
3897 | 1951 |
! |
1952 |
||
1953 |
mantissa |
|
24150 | 1954 |
"extract a normalized float's mantissa. |
3897 | 1955 |
The returned value depends on the float-representation of |
1956 |
the underlying machine and is therefore highly unportable. |
|
1957 |
This is not for general use. |
|
1958 |
This assumes that the mantissa is normalized to |
|
24150 | 1959 |
0.5 .. 1.0 and the float's value is mantissa * 2^exp" |
3897 | 1960 |
|
1961 |
%{ /* NOCONTEXT */ |
|
21850 | 1962 |
float myVal; |
8913 | 1963 |
int exp; |
3897 | 1964 |
|
21850 | 1965 |
myVal = __shortFloatVal(self); |
1966 |
// ouch: math libs seem to not care for NaN here; |
|
1967 |
#if 1 |
|
1968 |
// should we? |
|
21868 | 1969 |
if (!isnanf(myVal) && isfinitef(myVal)) |
21850 | 1970 |
#endif |
1971 |
{ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1972 |
#if defined(__i386__) && defined(__GNUC__) |
24150 | 1973 |
float frac = frexpf(myVal, &exp); |
1974 |
RETURN (__MKSFLOAT(frac)); |
|
7356 | 1975 |
#else |
24150 | 1976 |
double frac = frexp( (double)(myVal), &exp); |
1977 |
RETURN (__MKFLOAT(frac)); |
|
7356 | 1978 |
#endif |
21850 | 1979 |
} |
3897 | 1980 |
%}. |
20008 | 1981 |
^ super mantissa |
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
20433
diff
changeset
|
1982 |
|
3897 | 1983 |
" |
11945 | 1984 |
1.0 asShortFloat exponent |
3897 | 1985 |
1.0 asShortFloat mantissa |
21850 | 1986 |
1e1000 asShortFloat mantissa |
3897 | 1987 |
|
11945 | 1988 |
0.5 asShortFloat exponent |
1989 |
0.5 asShortFloat mantissa |
|
3897 | 1990 |
|
11945 | 1991 |
0.25 asShortFloat exponent |
1992 |
0.25 asShortFloat mantissa |
|
3897 | 1993 |
|
11945 | 1994 |
0.00000011111 asShortFloat exponent |
1995 |
0.00000011111 asShortFloat mantissa |
|
3897 | 1996 |
" |
21850 | 1997 |
|
1998 |
"Modified: / 20-06-2017 / 11:41:11 / cg" |
|
24150 | 1999 |
"Modified: / 26-05-2019 / 03:13:14 / Claus Gittinger" |
3897 | 2000 |
! ! |
2001 |
||
1197 | 2002 |
!ShortFloat methodsFor:'testing'! |
2003 |
||
3147 | 2004 |
isFinite |
11945 | 2005 |
"return true, if the receiver is a finite float |
3147 | 2006 |
i.e. not NaN and not infinite." |
2007 |
||
2008 |
%{ /* NOCONTEXT */ |
|
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2009 |
float fV = __shortFloatVal(self); |
3147 | 2010 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
2011 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
2012 |
* notice: on machines which do not provide |
11945 | 2013 |
* a isfinite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
2014 |
* 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
|
2015 |
*/ |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
2016 |
if (isfinitef(fV)) { RETURN (true); } |
3147 | 2017 |
%}. |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
2018 |
^ false |
3147 | 2019 |
|
2020 |
" |
|
11945 | 2021 |
1.0 asShortFloat isFinite |
2022 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
2023 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
3147 | 2024 |
" |
2025 |
! |
|
2026 |
||
2387 | 2027 |
isNaN |
2028 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
2029 |
These are not created by ST/X float operations (they raise an exception); |
|
2030 |
however, inline C-code could produce them ..." |
|
2031 |
||
2032 |
%{ /* NOCONTEXT */ |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
2033 |
float sV = __shortFloatVal(self); |
3139 | 2034 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
2035 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
2036 |
* notice: on machines which do not provide |
11945 | 2037 |
* a isnan() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
2038 |
* 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
|
2039 |
*/ |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
2040 |
if (isnanf(sV)) { RETURN (true); } |
2392 | 2041 |
%}. |
2042 |
^ false |
|
2387 | 2043 |
|
3147 | 2044 |
" |
7356 | 2045 |
1.0 asShortFloat isNaN |
11945 | 2046 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
2047 |
(0.0 asShortFloat uncheckedDivide: 0.0 asShortFloat) isNaN |
|
3147 | 2048 |
" |
2387 | 2049 |
! |
2050 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2051 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2052 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2053 |
|
17396 | 2054 |
%{ /* NOCONTEXT */ |
17394 | 2055 |
#if defined(__BORLANDC__) |
2056 |
union { float f; int i; } __u; |
|
2057 |
__u.f = __shortFloatVal(self); |
|
2058 |
RETURN ( (__u.f == 0.0 && __u.i < 0) ? true : false ); |
|
17391 | 2059 |
#else |
17382 | 2060 |
RETURN ( (__shortFloatVal(self) == 0.0 && signbit(__shortFloatVal(self)) != 0) ? true : false ); |
17391 | 2061 |
#endif |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2062 |
%}. |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2063 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2064 |
" |
11945 | 2065 |
0.0 asShortFloat isNegativeZero |
2066 |
-0.0 asShortFloat isNegativeZero |
|
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2067 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2068 |
! |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
2069 |
|
24330 | 2070 |
isZero |
2071 |
"return true, if the receiver is zero" |
|
2072 |
||
2073 |
^ self = 0.0 |
|
2074 |
||
2075 |
"Created: / 10-06-2019 / 21:59:09 / Claus Gittinger" |
|
2076 |
! |
|
2077 |
||
1197 | 2078 |
negative |
17382 | 2079 |
"return true if the receiver is less than zero. |
2080 |
-0.0 is positive for now." |
|
1197 | 2081 |
|
2082 |
%{ /* NOCONTEXT */ |
|
2083 |
||
17382 | 2084 |
RETURN ( __shortFloatVal(self) < 0.0 ? true : false ); |
2085 |
// RETURN ( signbit(__shortFloatVal(self)) != 0 ? true : false ); |
|
5411 | 2086 |
%}. |
17382 | 2087 |
|
2088 |
" |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2089 |
0.0 asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2090 |
-0.0 asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2091 |
1.0 asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2092 |
-1.0 asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2093 |
(1.0 uncheckedDivide: 0.0) asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2094 |
(-1.0 uncheckedDivide: 0.0) asShortFloat negative |
17382 | 2095 |
" |
1197 | 2096 |
! |
2097 |
||
5357 | 2098 |
numberOfBits |
2099 |
"return the size (in bits) of the real; |
|
2100 |
typically, 32 is returned here, |
|
2101 |
but who knows ..." |
|
2102 |
||
2103 |
%{ /* NOCONTEXT */ |
|
2104 |
||
8913 | 2105 |
RETURN (__mkSmallInteger (sizeof(float) * 8)); |
5357 | 2106 |
%} |
2107 |
||
2108 |
" |
|
11945 | 2109 |
1.2 numberOfBits |
2110 |
1.2 asShortFloat numberOfBits |
|
5357 | 2111 |
" |
2112 |
||
2113 |
! |
|
2114 |
||
1197 | 2115 |
positive |
18852 | 2116 |
"return true if the receiver is greater or equal to zero (not negative) |
17382 | 2117 |
0.0 and -0.0 are positive for now." |
1197 | 2118 |
|
2119 |
%{ /* NOCONTEXT */ |
|
2120 |
||
17382 | 2121 |
RETURN ( __shortFloatVal(self) >= 0.0 ? true : false ); |
2122 |
// RETURN ( (signbit(__shortFloatVal(self)) == 0 ? true : false ) ); |
|
2123 |
%}. |
|
2124 |
||
2125 |
" |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2126 |
0.0 asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2127 |
-0.0 asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2128 |
1.0 asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2129 |
-1.0 asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2130 |
(1.0 uncheckedDivide: 0.0) asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2131 |
(-1.0 uncheckedDivide: 0.0) asShortFloat positive |
17382 | 2132 |
" |
1880 | 2133 |
! |
2134 |
||
2135 |
strictlyPositive |
|
2136 |
"return true if the receiver is greater than zero" |
|
2137 |
||
2138 |
%{ /* NOCONTEXT */ |
|
2139 |
||
2140 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
2141 |
%} |
|
1197 | 2142 |
! ! |
2143 |
||
5552 | 2144 |
!ShortFloat methodsFor:'truncation & rounding'! |
3897 | 2145 |
|
7356 | 2146 |
ceiling |
2147 |
"return the smallest integer which is greater or equal to the receiver." |
|
2148 |
||
2149 |
|val| |
|
2150 |
||
2151 |
%{ |
|
2152 |
float fVal; |
|
2153 |
||
22788 | 2154 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2155 |
fVal = ceilf(__shortFloatVal(self)); |
2156 |
#else |
|
2157 |
fVal = (float)(ceil((double)__shortFloatVal(self))); |
|
2158 |
#endif |
|
2159 |
/* |
|
2160 |
* ST-80 (and X3J20) returns integer. |
|
2161 |
*/ |
|
2162 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
22788 | 2163 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 2164 |
} |
2165 |
__qMKSFLOAT(val, fVal); |
|
2166 |
%}. |
|
2167 |
^ val asInteger |
|
2168 |
||
2169 |
" |
|
11945 | 2170 |
0.5 asShortFloat ceiling |
2171 |
-0.5 asShortFloat ceiling |
|
7356 | 2172 |
" |
2173 |
! |
|
2174 |
||
2175 |
ceilingAsFloat |
|
2176 |
"return the smallest integer-valued float greater or equal to the receiver. |
|
11945 | 2177 |
This is much like #ceiling, but avoids a (possibly expensive) conversion |
7356 | 2178 |
of the result to an integer. |
2179 |
It may be useful, if the result is to be further used in another float-operation." |
|
2180 |
||
2181 |
%{ /* NOCONTEXT */ |
|
2182 |
float fVal; |
|
2183 |
OBJ v; |
|
2184 |
||
22788 | 2185 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2186 |
fVal = ceilf(__shortFloatVal(self)); |
2187 |
#else |
|
2188 |
fVal = (float) ceil((double)__shortFloatVal(self)); |
|
2189 |
#endif |
|
2190 |
__qMKSFLOAT(v, fVal); |
|
2191 |
RETURN (v); |
|
2192 |
%} |
|
2193 |
" |
|
11945 | 2194 |
0.5 asShortFloat ceilingAsFloat |
2195 |
-0.5 asShortFloat ceilingAsFloat |
|
2196 |
-1.5 asShortFloat ceilingAsFloat |
|
7356 | 2197 |
" |
2198 |
! |
|
2199 |
||
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2200 |
floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2201 |
"return the integer nearest the receiver towards negative infinity." |
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 |
|val| |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2204 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2205 |
%{ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2206 |
float fVal; |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2207 |
|
22788 | 2208 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2209 |
fVal = floorf(__shortFloatVal(self)); |
2210 |
#else |
|
2211 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
2212 |
#endif |
|
4100
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 |
* ST-80 (and X3J20) returns integer. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2215 |
*/ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2216 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
22788 | 2217 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
4100
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 |
__qMKSFLOAT(val, fVal); |
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 |
^ val asInteger |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2222 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2223 |
" |
11945 | 2224 |
0.5 asShortFloat floor |
2225 |
-0.5 asShortFloat floor |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2226 |
" |
7356 | 2227 |
! |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2228 |
|
7356 | 2229 |
floorAsFloat |
2230 |
"return the float which represents the next lower |
|
2231 |
integer nearest the receiver towards negative infinity. |
|
2232 |
Much like floor, but returns a float result - useful if the result |
|
2233 |
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
|
2234 |
|
7356 | 2235 |
%{ /* NOCONTEXT */ |
2236 |
float fVal; |
|
2237 |
OBJ v; |
|
2238 |
||
22788 | 2239 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2240 |
fVal = floorf(__shortFloatVal(self)); |
2241 |
#else |
|
2242 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
2243 |
#endif |
|
2244 |
__qMKSFLOAT(v, fVal); |
|
2245 |
RETURN ( v ); |
|
2246 |
%} |
|
2247 |
||
2248 |
" |
|
11945 | 2249 |
0.5 asShortFloat floorAsFloat |
2250 |
-0.5 asShortFloat floorAsFloat |
|
7356 | 2251 |
" |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2252 |
! |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2253 |
|
3897 | 2254 |
fractionPart |
2255 |
"extract the after-decimal fraction part. |
|
7356 | 2256 |
such that (self truncated + self fractionPart) = self" |
3897 | 2257 |
|
2258 |
%{ /* NOCONTEXT */ |
|
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2259 |
#ifdef NO_MODFF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2260 |
double dFrac, dTrunc; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2261 |
#else |
14741 | 2262 |
# ifndef __VISUALC__ |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2263 |
float modff(float, float *); |
14741 | 2264 |
# endif |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2265 |
#endif |
14718 | 2266 |
float frac, trunc; |
3897 | 2267 |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
2268 |
__threadErrno = 0; |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2269 |
#ifdef NO_MODFF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2270 |
dFrac = modf((double)__shortFloatVal(self), &dTrunc) ; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2271 |
frac = (float)dFrac; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2272 |
#else |
14718 | 2273 |
frac = modff(__shortFloatVal(self), &trunc); |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2274 |
#endif |
11945 | 2275 |
if (! isnan(frac)) { |
14719 | 2276 |
if (__threadErrno == 0) { |
2277 |
RETURN (__MKSFLOAT(frac)); |
|
2278 |
} |
|
3897 | 2279 |
} |
2280 |
%}. |
|
7403 | 2281 |
^ self class |
14719 | 2282 |
raise:#domainErrorSignal |
2283 |
receiver:self |
|
2284 |
selector:#fractionPart |
|
2285 |
arguments:#() |
|
2286 |
errorString:'bad receiver in fractionPart' |
|
3897 | 2287 |
|
2288 |
" |
|
11945 | 2289 |
1.6 asShortFloat fractionPart + 1.6 asShortFloat truncated |
2290 |
-1.6 asShortFloat fractionPart + -1.6 asShortFloat truncated |
|
7356 | 2291 |
|
14718 | 2292 |
1.0 asShortFloat fractionPart |
2293 |
0.5 asShortFloat fractionPart |
|
2294 |
0.25 asShortFloat fractionPart |
|
2295 |
3.14159 asShortFloat fractionPart |
|
2296 |
12345673.14159 asShortFloat fractionPart |
|
2297 |
123456731231231231.14159 asShortFloat fractionPart |
|
3897 | 2298 |
" |
7356 | 2299 |
! |
3897 | 2300 |
|
7356 | 2301 |
rounded |
2302 |
"return the receiver rounded to the nearest integer" |
|
2303 |
||
2304 |
|val| |
|
2305 |
||
11945 | 2306 |
%{ |
7356 | 2307 |
float fVal; |
2308 |
||
2309 |
fVal = __shortFloatVal(self); |
|
22788 | 2310 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2311 |
if (fVal < 0.0) { |
22788 | 2312 |
fVal = ceilf(fVal - (float)0.5); |
7356 | 2313 |
} else { |
22788 | 2314 |
fVal = floorf(fVal + (float)0.5); |
7356 | 2315 |
} |
2316 |
#else |
|
2317 |
if (fVal < 0.0) { |
|
22788 | 2318 |
fVal = (float)ceil((double)fVal - 0.5); |
7356 | 2319 |
} else { |
22788 | 2320 |
fVal = (float)floor((double)fVal + 0.5); |
7356 | 2321 |
} |
2322 |
#endif |
|
2323 |
/* |
|
22788 | 2324 |
* ST-80 (and X3J20) return integer. |
7356 | 2325 |
*/ |
2326 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
22788 | 2327 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 2328 |
} |
2329 |
__qMKSFLOAT(val, fVal); |
|
2330 |
%}. |
|
2331 |
^ val asInteger |
|
2332 |
||
2333 |
" |
|
11945 | 2334 |
0.4 asShortFloat rounded |
2335 |
0.5 asShortFloat rounded |
|
2336 |
0.6 asShortFloat rounded |
|
2337 |
-0.4 asShortFloat rounded |
|
2338 |
-0.5 asShortFloat rounded |
|
2339 |
-0.6 asShortFloat rounded |
|
22788 | 2340 |
1e32 asShortFloat rounded |
7356 | 2341 |
" |
2342 |
! |
|
2343 |
||
2344 |
roundedAsFloat |
|
2345 |
"return the receiver rounded to the nearest integer as a float. |
|
11945 | 2346 |
This is much like #rounded, but avoids a (possibly expensive) conversion |
7356 | 2347 |
of the result to an integer. |
2348 |
It may be useful, if the result is to be further used in another float-operation." |
|
2349 |
||
2350 |
%{ /* NOCONTEXT */ |
|
2351 |
float fVal; |
|
2352 |
OBJ v; |
|
2353 |
||
2354 |
fVal = __shortFloatVal(self); |
|
22788 | 2355 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2356 |
if (fVal < 0.0) { |
22788 | 2357 |
fVal = ceilf(fVal - (float)0.5); |
7356 | 2358 |
} else { |
22788 | 2359 |
fVal = floorf(fVal + (float)0.5); |
7356 | 2360 |
} |
2361 |
#else |
|
2362 |
if (fVal < 0.0) { |
|
22788 | 2363 |
fVal = (float)ceil((double)fVal - 0.5); |
7356 | 2364 |
} else { |
22788 | 2365 |
fVal = (float)floor((double)fVal + 0.5); |
7356 | 2366 |
} |
2367 |
#endif |
|
2368 |
__qMKSFLOAT(v, fVal); |
|
2369 |
RETURN (v); |
|
2370 |
%} |
|
2371 |
||
2372 |
" |
|
11945 | 2373 |
0.4 asShortFloat rounded |
2374 |
0.5 asShortFloat rounded |
|
2375 |
0.6 asShortFloat rounded |
|
2376 |
-0.4 asShortFloat rounded |
|
2377 |
-0.5 asShortFloat rounded |
|
2378 |
-0.6 asShortFloat rounded |
|
7356 | 2379 |
|
11945 | 2380 |
0.4 asShortFloat roundedAsFloat |
2381 |
0.5 asShortFloat roundedAsFloat |
|
2382 |
0.6 asShortFloat roundedAsFloat |
|
2383 |
-0.4 asShortFloat roundedAsFloat |
|
2384 |
-0.5 asShortFloat roundedAsFloat |
|
2385 |
-0.6 asShortFloat roundedAsFloat |
|
7356 | 2386 |
" |
2387 |
! |
|
2388 |
||
2389 |
truncated |
|
2390 |
"return the receiver truncated towards zero as an integer" |
|
2391 |
||
2392 |
|val| |
|
2393 |
||
11945 | 2394 |
%{ |
7356 | 2395 |
float fVal; |
2396 |
||
2397 |
fVal = __shortFloatVal(self); |
|
22788 | 2398 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2399 |
if (fVal < 0.0) { |
22788 | 2400 |
fVal = ceilf(fVal); |
7356 | 2401 |
} else { |
22788 | 2402 |
fVal = floorf(fVal); |
7356 | 2403 |
} |
2404 |
#else |
|
2405 |
if (fVal < 0.0) { |
|
22788 | 2406 |
fVal = (float)ceil((double)fVal); |
7356 | 2407 |
} else { |
22788 | 2408 |
fVal = (float)floor((double)fVal); |
7356 | 2409 |
} |
2410 |
#endif |
|
2411 |
/* |
|
2412 |
* ST-80 (and X3J20) returns integer. |
|
2413 |
*/ |
|
2414 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
22788 | 2415 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 2416 |
} |
2417 |
__qMKSFLOAT(val, fVal); |
|
2418 |
%}. |
|
2419 |
^ val asInteger |
|
2420 |
||
2421 |
" |
|
11945 | 2422 |
0.5 asShortFloat truncated |
2423 |
-0.5 asShortFloat truncated |
|
2424 |
0.5 asShortFloat truncatedAsFloat |
|
2425 |
-0.5 asShortFloat truncatedAsFloat |
|
7356 | 2426 |
" |
2427 |
! |
|
2428 |
||
2429 |
truncatedAsFloat |
|
2430 |
"return the receiver truncated towards zero as a float. |
|
11945 | 2431 |
This is much like #truncated, but avoids a (possibly expensive) conversion |
7356 | 2432 |
of the result to an integer. |
11945 | 2433 |
It may be useful, if the result is to be further used in another |
7356 | 2434 |
float-operation." |
2435 |
||
2436 |
%{ /* NOCONTEXT */ |
|
2437 |
float fVal; |
|
2438 |
OBJ v; |
|
2439 |
||
2440 |
fVal = __shortFloatVal(self); |
|
22788 | 2441 |
#if (defined(__i386__) || defined(xx__x86_64__)) && defined(__GNUC__) |
7356 | 2442 |
if (fVal < 0.0) { |
22788 | 2443 |
fVal = ceilf(fVal); |
7356 | 2444 |
} else { |
22788 | 2445 |
fVal = floorf(fVal); |
7356 | 2446 |
} |
2447 |
#else |
|
2448 |
if (fVal < 0.0) { |
|
22788 | 2449 |
fVal = (float)ceil((double)fVal); |
7356 | 2450 |
} else { |
22788 | 2451 |
fVal = (float)floor((double)fVal); |
7356 | 2452 |
} |
2453 |
#endif |
|
2454 |
__qMKSFLOAT(v, fVal); |
|
2455 |
RETURN (v); |
|
2456 |
%} |
|
2457 |
||
2458 |
" |
|
11945 | 2459 |
0.5 asShortFloat truncated |
2460 |
-0.5 asShortFloat truncated |
|
2461 |
0.5 asShortFloat truncatedAsFloat |
|
2462 |
-0.5 asShortFloat truncatedAsFloat |
|
7356 | 2463 |
" |
3897 | 2464 |
! ! |
2465 |
||
1880 | 2466 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 2467 |
|
2468 |
version |
|
18841 | 2469 |
^ '$Header$' |
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
2470 |
! |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
2471 |
|
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
2472 |
version_CVS |
18841 | 2473 |
^ '$Header$' |
1197 | 2474 |
! ! |
15012 | 2475 |
|
16670 | 2476 |
|
2477 |
ShortFloat initialize! |