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