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