author | Claus Gittinger <cg@exept.de> |
Wed, 20 Mar 2013 16:01:14 +0100 | |
changeset 14922 | 30546dbb18c6 |
parent 14920 | 5b452cc39879 |
child 14924 | 7115c6830ed8 |
permissions | -rw-r--r-- |
1197 | 1 |
" |
2 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3158 | 3 |
All Rights Reserved |
1197 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5411 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1197 | 14 |
LimitedPrecisionReal variableByteSubclass:#ShortFloat |
15 |
instanceVariableNames:'' |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
16 |
classVariableNames:'' |
1197 | 17 |
poolDictionaries:'' |
18 |
category:'Magnitude-Numbers' |
|
19 |
! |
|
20 |
||
2394 | 21 |
!ShortFloat primitiveDefinitions! |
22 |
%{ |
|
23 |
||
14744 | 24 |
#include <stdio.h> |
3471 | 25 |
#include <errno.h> |
26 |
||
27 |
#ifndef __OPTIMIZE__ |
|
28 |
# define __OPTIMIZE__ |
|
29 |
#endif |
|
4825 | 30 |
|
31 |
#define __USE_ISOC9X 1 |
|
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
32 |
#define __USE_ISOC99 1 |
3471 | 33 |
#include <math.h> |
34 |
||
35 |
/* |
|
36 |
* on some systems errno is a macro ... check for it here |
|
37 |
*/ |
|
38 |
#ifndef errno |
|
39 |
extern errno; |
|
40 |
#endif |
|
41 |
||
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
42 |
#if !defined (WIN32) |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
43 |
# include <locale.h> |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
44 |
#endif |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
45 |
|
2394 | 46 |
#if defined (_AIX) |
47 |
# include <float.h> |
|
48 |
#endif |
|
4825 | 49 |
|
2395 | 50 |
#if defined(IRIX) |
51 |
# include <nan.h> |
|
52 |
#endif |
|
4825 | 53 |
|
2397 | 54 |
#if defined(LINUX) |
4825 | 55 |
# ifndef NAN |
5822
4e56f8aadda5
Get nan.h from bits/nan.h for Linux
Stefan Vogel <sv@exept.de>
parents:
5718
diff
changeset
|
56 |
# include <bits/nan.h> |
4825 | 57 |
# endif |
2397 | 58 |
#endif |
4825 | 59 |
|
2400 | 60 |
#if defined(solaris) || defined(sunos) |
2398 | 61 |
# include <nan.h> |
62 |
#endif |
|
2394 | 63 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
64 |
#ifdef WIN32 |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
65 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
66 |
* no finite(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
67 |
* no isnan(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
68 |
*/ |
7419 | 69 |
# ifndef isnan |
70 |
# define isnan(x) \ |
|
11945 | 71 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
72 |
(((unsigned int *)(&x))[1] == 0xFFF80000)) |
|
7419 | 73 |
# endif |
74 |
||
75 |
# ifndef isPositiveInfinity |
|
76 |
# define isPositiveInfinity(x) \ |
|
11945 | 77 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
78 |
(((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
|
79 |
# endif |
7419 | 80 |
|
81 |
# ifndef isNegativeInfinity |
|
82 |
# define isPositiveInfinity(x) \ |
|
11945 | 83 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
84 |
(((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
|
85 |
# endif |
7419 | 86 |
|
87 |
# ifndef isinf |
|
88 |
# define isinf(x) \ |
|
11945 | 89 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
90 |
((((unsigned int *)(&x))[1] & 0x7FF00000) == 0x7FF00000)) |
|
7419 | 91 |
# endif |
92 |
||
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
93 |
# ifndef isfinite |
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
94 |
# define isfinite(x) (!isinf(x) && !isnan(x)) |
7419 | 95 |
# endif |
96 |
||
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
97 |
# ifndef isnanf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
98 |
# define isnanf(x) \ |
11945 | 99 |
(((unsigned int *)(&x))[0] == 0xFFC00000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
100 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
101 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
102 |
# ifndef isPositiveInfinityf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
103 |
# define isPositiveInfinityf(x) \ |
11945 | 104 |
(((unsigned int *)(&x))[0] == 0x7F800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
105 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
106 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
107 |
# ifndef isNegativeInfinityf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
108 |
# define isNegativeInfinityf(x) \ |
11945 | 109 |
(((unsigned int *)(&x))[0] == 0xFF800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
110 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
111 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
112 |
# ifndef isinff |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
113 |
# define isinff(x) \ |
11945 | 114 |
((((unsigned int *)(&x))[0] & 0x7FFFFFFF) == 0x7F800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
115 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
116 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
117 |
# ifndef isfinitef |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
118 |
# define isfinitef(x) (!isinff(x) && !isnanf(x)) |
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 |
|
7419 | 121 |
# define NO_ASINH |
122 |
# define NO_ACOSH |
|
123 |
# define NO_ATANH |
|
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
124 |
# ifdef __BORLANDC__ |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
125 |
# define NO_FMODF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
126 |
# define NO_MODFF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
127 |
# endif |
8706 | 128 |
#endif /* WIN32 */ |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
129 |
|
8984 | 130 |
#ifdef solaris |
131 |
# ifndef isfinite |
|
132 |
# define isfinite(f) finite((double)(f)) |
|
133 |
# endif |
|
134 |
#endif |
|
135 |
||
3414 | 136 |
#ifdef realIX |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
137 |
# ifndef isfinite |
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
138 |
# define isfinite(x) 1 |
3414 | 139 |
# endif |
8706 | 140 |
#endif /* realIX */ |
141 |
||
9896 | 142 |
#ifndef isfinitef |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
143 |
# define isfinitef(x) isfinite(x) |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
144 |
#endif |
9896 | 145 |
#ifndef isnanf |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
146 |
# define isnanf(x) isnan(x) |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
147 |
#endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
148 |
|
2394 | 149 |
%} |
150 |
! ! |
|
151 |
||
1880 | 152 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 153 |
|
154 |
copyright |
|
155 |
" |
|
156 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3158 | 157 |
All Rights Reserved |
1197 | 158 |
|
159 |
This software is furnished under a license and may be used |
|
160 |
only in accordance with the terms of that license and with the |
|
161 |
inclusion of the above copyright notice. This software may not |
|
162 |
be provided or otherwise made available to, or used by, any |
|
163 |
other person. No title to or ownership of the software is |
|
164 |
hereby transferred. |
|
165 |
" |
|
166 |
||
167 |
||
168 |
! |
|
169 |
||
170 |
documentation |
|
171 |
" |
|
11945 | 172 |
ShortFloats represent rational numbers with limited precision. |
7380 | 173 |
They use the C-compilers 'float' format, which is usually the IEE single float format. |
174 |
||
175 |
In contrast to Floats (which use the C-compilers 64bit 'double' format), |
|
176 |
ShortFloats give you 32 bit floats. |
|
1197 | 177 |
|
178 |
Notice, that ST/X Floats are what Doubles are in ST-80 and ShortFloats are |
|
14719 | 179 |
ST-80's Floats respectively. The reason was to make ST/X's floats compatible |
180 |
to bothe visualWorks and other smalltalks, which use C-doubles for the Float class. |
|
181 |
Thus, STX's Float precision is not worse than that of other ST's. |
|
182 |
||
11945 | 183 |
This may change in one of the next versions (at least on machines, which |
1197 | 184 |
provide different float and double types in their C-compiler. |
185 |
||
186 |
WARNING: |
|
11945 | 187 |
The layout of shortFloat instances is known by the runtime system and the compiler; |
188 |
you may not add instance variables here. |
|
189 |
Also, subclassing is complicated by the fact, that the VM creates floats/shortFloats, |
|
190 |
and does some of its float-checks by an identity compare with the ShortFloat-class. |
|
191 |
(i.e. your subclasses instances may not be recognized as float-like objects, |
|
192 |
thus mixed mode arithmetic will always coerce them, effectively slowing things down). |
|
193 |
This may be changed, to use a flag bit in the class. |
|
1197 | 194 |
|
7380 | 195 |
Mixed mode arithmetic: |
11945 | 196 |
shortFloat op shortFloat -> shortFloat |
197 |
shortFloat op fix -> shortFloat |
|
198 |
shortFloat op fraction -> shortFloat |
|
199 |
shortFloat op integer -> shortFloat |
|
200 |
shortFloat op longFloat -> longFloat |
|
201 |
shortFloat op float -> float |
|
202 |
shortFloat op complex -> complex |
|
1295 | 203 |
|
7405 | 204 |
Representation: |
11945 | 205 |
32bit single precision IEE floats |
206 |
23 bit mantissa, |
|
207 |
8 bit exponent, |
|
208 |
6 decimal digits (approx) |
|
7405 | 209 |
|
210 |
Range and Precision of Storage Formats: see LimitedPrecisionReal >> documentation |
|
211 |
||
1295 | 212 |
[author:] |
11945 | 213 |
Claus Gittinger |
1556 | 214 |
|
215 |
[see also:] |
|
11945 | 216 |
Number |
217 |
Float LongFloat Fraction FixedPoint Integer Complex |
|
218 |
FloatArray DoubleArray |
|
1197 | 219 |
" |
220 |
! ! |
|
221 |
||
1880 | 222 |
!ShortFloat class methodsFor:'instance creation'! |
1207 | 223 |
|
224 |
basicNew |
|
225 |
"return a new shortFloat - here we return 0.0 |
|
226 |
- shortFloats are usually NOT created this way ... |
|
227 |
Its implemented here to allow things like binary store & load |
|
228 |
of shortFloats. (but even this support will go away eventually, its not |
|
229 |
a good idea to store the bits of a float - the reader might have a |
|
11945 | 230 |
totally different representation - so floats will eventually be |
1207 | 231 |
binary stored in a device independent format." |
232 |
||
233 |
%{ /* NOCONTEXT */ |
|
234 |
OBJ newFloat; |
|
235 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
236 |
__qMKSFLOAT(newFloat, 0.0); |
1207 | 237 |
RETURN (newFloat); |
238 |
%} |
|
3158 | 239 |
! |
240 |
||
4297 | 241 |
fastFromString:aString at:startIndex |
11945 | 242 |
"return the next ShortFloat from the string starting at startIndex. |
4297 | 243 |
No spaces are skipped. |
244 |
||
245 |
This is a specially tuned entry (using a low-level C-call), which |
|
246 |
returns garbage if the argument string is not a valid float number. |
|
11945 | 247 |
It has been added to allow higher speed string decomposition into numbers, |
248 |
especially for mass-data." |
|
249 |
||
250 |
%{ /* NOCONTEXT */ |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
251 |
if (__isStringLike(aString) && __isSmallInteger(startIndex)) { |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
252 |
char *cp = (char *)(__stringVal(aString)); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
253 |
int idx = __intVal(startIndex) - 1; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
254 |
double atof(); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
255 |
double val; |
4297 | 256 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
257 |
if ((unsigned)idx < __stringSize(aString)) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
258 |
val = atof(cp + idx); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
259 |
RETURN (__MKSFLOAT(val)); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
260 |
} |
4297 | 261 |
} |
262 |
%}. |
|
263 |
self primitiveFailed. |
|
264 |
||
265 |
" |
|
11945 | 266 |
ShortFloat fastFromString:'123.45' at:1 |
267 |
ShortFloat fastFromString:'123.45' at:2 |
|
268 |
ShortFloat fastFromString:'123.45E4' at:1 |
|
269 |
ShortFloat fastFromString:'hello123.45E4' at:6 |
|
270 |
ShortFloat fastFromString:'12345' at:1 |
|
271 |
ShortFloat fastFromString:'12345' at:2 |
|
272 |
ShortFloat fastFromString:'12345' at:3 |
|
273 |
ShortFloat fastFromString:'12345' at:4 |
|
274 |
ShortFloat fastFromString:'12345' at:5 |
|
275 |
ShortFloat fastFromString:'12345' at:6 |
|
276 |
ShortFloat fastFromString:'12345' at:0 |
|
277 |
ShortFloat fastFromString:'hello123.45E4' at:1 |
|
4297 | 278 |
|
279 |
Time millisecondsToRun:[ |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
280 |
100000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
281 |
ShortFloat readFrom:'123.45' |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
282 |
] |
4297 | 283 |
] |
284 |
" |
|
285 |
||
286 |
" |
|
287 |
Time millisecondsToRun:[ |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
288 |
100000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
289 |
ShortFloat fastFromString:'123.45' at:1 |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
290 |
] |
4297 | 291 |
] |
292 |
" |
|
293 |
! |
|
294 |
||
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
295 |
readFrom:aStringOrStream |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
296 |
"read a shortFloat from a string" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
297 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
298 |
|num| |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
299 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
300 |
num := super readFrom:aStringOrStream onError:[self error:'conversion error for: ' , self name]. |
11945 | 301 |
num notNil ifTrue:[ |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
302 |
num := num asShortFloat |
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
303 |
]. |
11945 | 304 |
^ num |
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
305 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
306 |
" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
307 |
ShortFloat readFrom:'0.1' |
11945 | 308 |
ShortFloat readFrom:'0' |
309 |
ShortFloat readFrom:'.123' |
|
310 |
ShortFloat readFrom:'-.123' |
|
311 |
ShortFloat readFrom:'1e4' |
|
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
312 |
" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
313 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
314 |
"Modified: / 7.1.1998 / 16:17:59 / cg" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
315 |
! |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
316 |
|
3158 | 317 |
readFrom:aStringOrStream onError:exceptionBlock |
318 |
"read a shortFloat from a string" |
|
319 |
||
320 |
|num| |
|
321 |
||
322 |
num := super readFrom:aStringOrStream onError:nil. |
|
11945 | 323 |
num isNil ifTrue:[ |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
324 |
^ exceptionBlock value |
3158 | 325 |
]. |
326 |
^ num asShortFloat |
|
327 |
||
328 |
" |
|
329 |
ShortFloat readFrom:'0.1' |
|
330 |
ShortFloat readFrom:'0' |
|
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
331 |
ShortFloat readFrom:'.123' |
3158 | 332 |
" |
333 |
||
334 |
"Modified: / 7.1.1998 / 16:17:59 / cg" |
|
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 |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
340 |
"this is fixed in ShortFloat" |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
341 |
|
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
342 |
^ '.6' |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
343 |
! ! |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
344 |
|
7356 | 345 |
!ShortFloat class methodsFor:'binary storage'! |
346 |
||
347 |
readBinaryIEEESingleFrom:aStream |
|
348 |
"read a float value from the binary stream, aStream, |
|
349 |
interpreting the next bytes as an IEEE formatted 4-byte float" |
|
1197 | 350 |
|
7356 | 351 |
|f| |
1197 | 352 |
|
7356 | 353 |
f := ShortFloat basicNew. |
354 |
self readBinaryIEEESingleFrom:aStream into:f. |
|
355 |
^ f |
|
1197 | 356 |
|
9636 | 357 |
"not part of libboss, as this is also used by others (TIFFReader)" |
358 |
||
359 |
"Created: / 16-04-1996 / 21:00:35 / cg" |
|
360 |
"Modified: / 23-08-2006 / 16:01:47 / cg" |
|
1197 | 361 |
! |
362 |
||
7356 | 363 |
readBinaryIEEESingleFrom:aStream into:aFloat |
364 |
"read a float value from the binary stream, aStream, |
|
14922 | 365 |
interpreting the next bytes as an IEEE formatted 4-byte float. |
366 |
The bytes are read in the nativ byte order (i.e.lsb on intel)" |
|
367 |
||
368 |
^ self readBinaryIEEESingleFrom:aStream into:aFloat MSB:(UninterpretedBytes isBigEndian) |
|
369 |
||
370 |
"Modified: / 23-08-2006 / 16:01:52 / cg" |
|
371 |
! |
|
372 |
||
373 |
readBinaryIEEESingleFrom:aStream into:aFloat MSB:msb |
|
374 |
"read a float value from the binary stream, aStream, |
|
375 |
interpreting the next bytes as an IEEE formatted 4-byte float. |
|
376 |
If msb is true, the stream bytes are most-significant-first." |
|
7356 | 377 |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
378 |
aFloat class == self ifFalse:[self error:'no a ShortFloat']. |
6574 | 379 |
|
7356 | 380 |
" |
381 |
this implementation is wrong: does not work on non-IEEE machines |
|
382 |
(to date all machines where ST/X is running on use |
|
383 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
384 |
" |
|
385 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
386 |
||
14922 | 387 |
(UninterpretedBytes isBigEndian == msb) ifFalse:[ |
388 |
"swap the bytes" |
|
389 |
4 to:1 by:-1 do:[:i | |
|
390 |
aFloat basicAt:i put:(aStream next) |
|
391 |
]. |
|
392 |
^ self |
|
7356 | 393 |
]. |
394 |
1 to:4 do:[:i | |
|
14922 | 395 |
aFloat basicAt:i put:aStream next |
7356 | 396 |
] |
9636 | 397 |
|
398 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
399 |
||
400 |
"Modified: / 23-08-2006 / 16:01:52 / cg" |
|
6574 | 401 |
! |
402 |
||
7356 | 403 |
storeBinaryIEEESingle:aFloat on:aStream |
404 |
"store aFloat as an IEEE formatted 4-byte float |
|
405 |
onto the binary stream, aStream" |
|
406 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
407 |
|float| |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
408 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
409 |
float := aFloat asShortFloat. |
7356 | 410 |
|
411 |
" |
|
412 |
this implementation is wrong: does not work on non-IEEE machines |
|
413 |
(to date all machines where ST/X is running on use |
|
414 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
415 |
" |
|
416 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
6574 | 417 |
|
7356 | 418 |
UninterpretedBytes isBigEndian ifFalse:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
419 |
"swap the bytes" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
420 |
4 to:1 by:-1 do:[:i | |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
421 |
aStream nextPut:(float basicAt:i). |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
422 |
]. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
423 |
^ self |
7356 | 424 |
]. |
425 |
1 to:4 do:[:i | |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
426 |
aStream nextPut:(float basicAt:i). |
7356 | 427 |
] |
9636 | 428 |
|
429 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
430 |
||
431 |
"Modified: / 23-08-2006 / 16:01:55 / cg" |
|
7356 | 432 |
! ! |
433 |
||
434 |
!ShortFloat class methodsFor:'constants'! |
|
6574 | 435 |
|
7392 | 436 |
e |
437 |
"return the constant e as ShortFloat" |
|
438 |
||
439 |
^ Float e asShortFloat |
|
440 |
! |
|
441 |
||
442 |
pi |
|
443 |
"return the constant pi as ShortFloat" |
|
444 |
||
445 |
^ Float pi asShortFloat |
|
446 |
! |
|
447 |
||
1197 | 448 |
unity |
1260 | 449 |
"return the neutral element for multiplication (1.0) as ShortFloat" |
1197 | 450 |
|
451 |
^ 1.0 asShortFloat |
|
452 |
||
1260 | 453 |
"Modified: 23.4.1996 / 09:26:51 / cg" |
1197 | 454 |
! |
455 |
||
456 |
zero |
|
1260 | 457 |
"return the neutral element for addition (0.0) as ShortFloat" |
1197 | 458 |
|
459 |
^ 0.0 asShortFloat |
|
460 |
||
1260 | 461 |
"Modified: 23.4.1996 / 09:26:45 / cg" |
1197 | 462 |
! ! |
463 |
||
1880 | 464 |
!ShortFloat class methodsFor:'queries'! |
1197 | 465 |
|
6898 | 466 |
exponentCharacter |
467 |
^ $e |
|
468 |
! |
|
469 |
||
1197 | 470 |
isBuiltInClass |
1264 | 471 |
"return true if this class is known by the run-time-system. |
472 |
Here, true is returned for myself, false for subclasses." |
|
1197 | 473 |
|
474 |
^ self == ShortFloat |
|
475 |
||
1264 | 476 |
"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
|
477 |
! |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
478 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
479 |
isIEEEFormat |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
480 |
"return true, if this machine represents floats in IEEE format. |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
481 |
Currently, no support is provided for non-ieee machines |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
482 |
to convert their floats into this (which is only relevant, |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
483 |
if such a machine wants to send floats as binary to some other |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
484 |
machine). |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
485 |
Machines with non-IEEE format are VAXed and IBM370-type systems |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
486 |
(among others). Today, most systems use IEEE format floats." |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
487 |
|
14920 | 488 |
%{ |
489 |
#ifdef __s390__ |
|
490 |
RETURN(false); |
|
491 |
#endif |
|
492 |
%}. |
|
493 |
||
3427
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
494 |
^ true "/ this may be a lie |
7356 | 495 |
! |
496 |
||
497 |
numBitsInExponent |
|
498 |
"answer the number of bits in the exponent |
|
499 |
This is an IEEE float, where 8 bits are available: |
|
11945 | 500 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
7356 | 501 |
" |
502 |
||
503 |
^ 8 |
|
504 |
! |
|
505 |
||
506 |
numBitsInMantissa |
|
507 |
"answer the number of bits in the mantissa. |
|
508 |
This is an IEEE float, where 23 bits (the hidden one is not counted here) are available: |
|
11945 | 509 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
7356 | 510 |
" |
511 |
||
512 |
^ 23 |
|
513 |
! |
|
514 |
||
515 |
precision |
|
516 |
"answer the precision of a ShortFloat (in bits) |
|
11945 | 517 |
This is an IEEE float, where only the fraction from the normalized mantissa is stored |
9149 | 518 |
and so there is a hidden bit and the mantissa is actually represented by 24 binary digits |
519 |
(although only 23 are needed in the binary representation)" |
|
7356 | 520 |
|
521 |
^ 24 |
|
522 |
! |
|
523 |
||
524 |
radix |
|
525 |
"answer the radix of a ShortFloats exponent |
|
526 |
This is an IEEE float, which is represented as binary" |
|
527 |
||
9149 | 528 |
^ 2 "must be careful here, whenever ST/X is used on a VAX or a 370" |
1197 | 529 |
! ! |
530 |
||
531 |
!ShortFloat methodsFor:'arithmetic'! |
|
532 |
||
533 |
* aNumber |
|
11730 | 534 |
"return the product of the receiver and the argument." |
1197 | 535 |
|
536 |
%{ /* NOCONTEXT */ |
|
537 |
||
538 |
OBJ newFloat; |
|
539 |
float result; |
|
540 |
double dResult; |
|
541 |
||
542 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 543 |
result = __shortFloatVal(self) * (float)(__intVal(aNumber)); |
1197 | 544 |
retResult: |
11945 | 545 |
__qMKSFLOAT(newFloat, result); |
546 |
RETURN ( newFloat ); |
|
547 |
} |
|
7356 | 548 |
if (__isShortFloat(aNumber)) { |
11945 | 549 |
result = __shortFloatVal(self) * __shortFloatVal(aNumber); |
550 |
goto retResult; |
|
551 |
} |
|
7356 | 552 |
if (__isFloatLike(aNumber)) { |
11945 | 553 |
dResult = (double) __shortFloatVal(self)* __floatVal(aNumber); |
554 |
__qMKFLOAT(newFloat, dResult); |
|
555 |
RETURN ( newFloat ); |
|
1197 | 556 |
} |
557 |
%}. |
|
558 |
^ aNumber productFromShortFloat:self |
|
559 |
! |
|
560 |
||
561 |
+ aNumber |
|
562 |
"return the sum of the receiver and the argument, aNumber" |
|
563 |
||
564 |
%{ /* NOCONTEXT */ |
|
565 |
||
566 |
OBJ newFloat; |
|
567 |
float result; |
|
568 |
double dResult; |
|
569 |
||
570 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 571 |
result = __shortFloatVal(self) + (float)(__intVal(aNumber)); |
1197 | 572 |
retResult: |
11945 | 573 |
__qMKSFLOAT(newFloat, result); |
574 |
RETURN ( newFloat ); |
|
575 |
} |
|
7356 | 576 |
if (__isShortFloat(aNumber)) { |
11945 | 577 |
result = __shortFloatVal(self) + __shortFloatVal(aNumber); |
578 |
goto retResult; |
|
579 |
} |
|
7356 | 580 |
if (__isFloatLike(aNumber)) { |
11945 | 581 |
dResult = (double) __shortFloatVal(self) + __floatVal(aNumber); |
582 |
__qMKFLOAT(newFloat, dResult); |
|
583 |
RETURN ( newFloat ); |
|
1197 | 584 |
} |
585 |
%}. |
|
586 |
^ aNumber sumFromShortFloat:self |
|
587 |
! |
|
588 |
||
589 |
- aNumber |
|
590 |
"return the difference of the receiver and the argument, aNumber" |
|
591 |
||
592 |
%{ /* NOCONTEXT */ |
|
593 |
||
594 |
OBJ newFloat; |
|
595 |
float result; |
|
596 |
double dResult; |
|
597 |
||
598 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 599 |
result = __shortFloatVal(self) - (float)(__intVal(aNumber)); |
1197 | 600 |
retResult: |
11945 | 601 |
__qMKSFLOAT(newFloat, result); |
602 |
RETURN ( newFloat ); |
|
603 |
} |
|
7356 | 604 |
if (__isShortFloat(aNumber)) { |
11945 | 605 |
result = __shortFloatVal(self) - __shortFloatVal(aNumber); |
606 |
goto retResult; |
|
607 |
} |
|
7356 | 608 |
if (__isFloatLike(aNumber)) { |
11945 | 609 |
dResult = (double) __shortFloatVal(self) - __floatVal(aNumber); |
610 |
__qMKFLOAT(newFloat, dResult); |
|
611 |
RETURN ( newFloat ); |
|
1197 | 612 |
} |
613 |
%}. |
|
614 |
^ aNumber differenceFromShortFloat:self |
|
615 |
! |
|
616 |
||
617 |
/ aNumber |
|
618 |
"return the quotient of the receiver and the argument, aNumber" |
|
619 |
||
620 |
%{ /* NOCONTEXT */ |
|
621 |
||
622 |
OBJ newFloat; |
|
623 |
float result, val; |
|
624 |
double dResult, dVal; |
|
625 |
||
626 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 627 |
if (aNumber != __mkSmallInteger(0)) { |
628 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
1197 | 629 |
retResult: |
11945 | 630 |
__qMKSFLOAT(newFloat, result); |
631 |
RETURN ( newFloat ); |
|
632 |
} |
|
633 |
} |
|
7356 | 634 |
if (__isShortFloat(aNumber)) { |
11945 | 635 |
val = __shortFloatVal(aNumber); |
636 |
if (val != 0.0) { |
|
637 |
result = __shortFloatVal(self) / val; |
|
638 |
goto retResult; |
|
639 |
} |
|
640 |
} |
|
7356 | 641 |
if (__isFloatLike(aNumber)) { |
11945 | 642 |
dVal = __floatVal(aNumber); |
643 |
if (dVal != 0.0) { |
|
644 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
645 |
__qMKFLOAT(newFloat, dResult); |
|
646 |
RETURN ( newFloat ); |
|
647 |
} |
|
1197 | 648 |
} |
649 |
%}. |
|
650 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
11945 | 651 |
" |
652 |
No, you shalt not divide by zero |
|
653 |
" |
|
654 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
1197 | 655 |
]. |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
656 |
^ aNumber quotientFromShortFloat:self |
1197 | 657 |
! |
658 |
||
14710 | 659 |
abs |
660 |
"return the absolute value of the receiver |
|
661 |
reimplemented here for speed" |
|
662 |
||
663 |
%{ /* NOCONTEXT */ |
|
664 |
||
665 |
OBJ newFloat; |
|
666 |
float val = __shortFloatVal(self); |
|
667 |
||
668 |
if (val < 0.0) { |
|
669 |
__qMKSFLOAT(newFloat, -val); |
|
670 |
RETURN ( newFloat ); |
|
671 |
} |
|
672 |
RETURN (self); |
|
673 |
%}. |
|
674 |
||
675 |
" |
|
676 |
3.0 asShortFloat abs |
|
677 |
-3.0 asShortFloat abs |
|
678 |
" |
|
679 |
! |
|
680 |
||
681 |
negated |
|
682 |
"return myself negated" |
|
683 |
||
684 |
%{ /* NOCONTEXT */ |
|
685 |
OBJ newFloat; |
|
686 |
float rslt = - __shortFloatVal(self); |
|
687 |
||
688 |
__qMKSFLOAT(newFloat, rslt); |
|
689 |
RETURN ( newFloat ); |
|
690 |
%}. |
|
691 |
^ 0.0 - self |
|
692 |
||
693 |
! |
|
694 |
||
14690 | 695 |
rem: aNumber |
696 |
"return the floating point remainder of the receiver and the argument, aNumber" |
|
697 |
||
698 |
%{ /* NOCONTEXT */ |
|
699 |
||
700 |
/* |
|
701 |
* notice: |
|
702 |
* the following inline code handles some common cases, |
|
703 |
* and exists as an optimization, to speed up those cases. |
|
704 |
* |
|
705 |
* Conceptionally, (and for most other argument types), |
|
706 |
* mixed arithmetic is implemented by double dispatching |
|
707 |
* (see the message send at the bottom) |
|
708 |
*/ |
|
709 |
OBJ newFloat; |
|
710 |
float result, val; |
|
711 |
double dResult, dVal; |
|
712 |
||
713 |
if (__isSmallInteger(aNumber)) { |
|
714 |
if (aNumber != __mkSmallInteger(0)) { |
|
715 |
val = (float)__intVal(aNumber); |
|
716 |
computeResult: |
|
14709
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
717 |
#ifdef NO_FMODF |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
718 |
dResult = fmod((double)__shortFloatVal(self), (double)val) ; |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
719 |
result = (float)dResult; |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
720 |
#else |
14690 | 721 |
result = fmodf(__shortFloatVal(self), val) ; |
14709
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
722 |
#endif |
14690 | 723 |
__qMKSFLOAT(newFloat, result); |
724 |
RETURN ( newFloat ); |
|
725 |
} |
|
726 |
} else if (__isFloatLike(aNumber)) { |
|
727 |
dVal = __floatVal(aNumber); |
|
728 |
if (dVal != 0.0) { |
|
729 |
dResult = fmod((double)(__shortFloatVal(self)), dVal) ; |
|
730 |
__qMKFLOAT(newFloat, dResult); |
|
731 |
RETURN ( newFloat ); |
|
732 |
} |
|
733 |
} else if (__isShortFloat(aNumber)) { |
|
734 |
val = __shortFloatVal(aNumber); |
|
735 |
if (val != 0.0) { |
|
736 |
goto computeResult; |
|
737 |
} |
|
738 |
} |
|
739 |
%}. |
|
740 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
741 |
" |
|
742 |
No, you shalt not divide by zero |
|
743 |
" |
|
744 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
745 |
]. |
|
746 |
^ aNumber remainderFromShortFloat:self |
|
747 |
! |
|
748 |
||
3147 | 749 |
uncheckedDivide:aNumber |
7356 | 750 |
"return the quotient of the receiver and the argument, aNumber. |
7471 | 751 |
Do not check for divide by zero (return NaN or Infinity). |
7356 | 752 |
This operation is provided for emulators of other languages/semantics, |
753 |
where no exception is raised for these results (i.e. Java). |
|
754 |
Its only defined if the arguments type is the same as the receivers." |
|
3147 | 755 |
|
756 |
%{ /* NOCONTEXT */ |
|
757 |
||
758 |
OBJ newFloat; |
|
759 |
float result, val; |
|
760 |
double dResult, dVal; |
|
761 |
||
762 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 763 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
3147 | 764 |
retResult: |
11945 | 765 |
__qMKSFLOAT(newFloat, result); |
766 |
RETURN ( newFloat ); |
|
767 |
} |
|
7356 | 768 |
if (__isShortFloat(aNumber)) { |
11945 | 769 |
val = __shortFloatVal(aNumber); |
770 |
result = __shortFloatVal(self) / val; |
|
771 |
goto retResult; |
|
772 |
} |
|
7356 | 773 |
if (__isFloatLike(aNumber)) { |
11945 | 774 |
dVal = __floatVal(aNumber); |
775 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
776 |
__qMKFLOAT(newFloat, dResult); |
|
777 |
RETURN ( newFloat ); |
|
3147 | 778 |
} |
779 |
%}. |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
780 |
^ aNumber quotientFromShortFloat:self |
3147 | 781 |
|
782 |
" |
|
783 |
0.0 asShortFloat uncheckedDivide:0 |
|
784 |
1.0 asShortFloat uncheckedDivide:0.0 |
|
785 |
" |
|
1197 | 786 |
! ! |
787 |
||
5257 | 788 |
!ShortFloat methodsFor:'coercing & converting'! |
1197 | 789 |
|
790 |
asFloat |
|
11238 | 791 |
"return a Float with same value as the receiver. |
792 |
Redefined for performance (machine can do it faster)" |
|
1197 | 793 |
|
794 |
%{ /* NOCONTEXT */ |
|
795 |
||
796 |
OBJ newFloat; |
|
797 |
double dVal = (double)__shortFloatVal(self); |
|
798 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
799 |
__qMKFLOAT(newFloat, dVal); |
1197 | 800 |
RETURN ( newFloat ); |
801 |
%} |
|
802 |
||
803 |
" |
|
11945 | 804 |
1.0 asShortFloat asFloat |
1197 | 805 |
" |
806 |
! |
|
807 |
||
808 |
asInteger |
|
809 |
"return an integer with same value - might truncate" |
|
810 |
||
811 |
%{ /* NOCONTEXT */ |
|
1670 | 812 |
float fVal; |
1197 | 813 |
|
1670 | 814 |
fVal = __shortFloatVal(self); |
7419 | 815 |
#ifdef WIN32 |
11945 | 816 |
if (! isnanf(fVal)) |
7419 | 817 |
#endif |
818 |
{ |
|
11945 | 819 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
820 |
RETURN ( __mkSmallInteger( (INT)fVal) ); |
|
821 |
} |
|
1197 | 822 |
} |
823 |
%}. |
|
824 |
^ super asInteger |
|
825 |
||
826 |
" |
|
827 |
12345.0 asShortFloat asInteger |
|
828 |
1e15 asShortFloat asInteger |
|
829 |
" |
|
830 |
! |
|
831 |
||
7437
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
832 |
asLongFloat |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
833 |
^ LongFloat fromShortFloat:self |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
834 |
! |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
835 |
|
1197 | 836 |
asShortFloat |
837 |
"return a ShortFloat with same value as the receiver - thats me" |
|
838 |
||
839 |
^ self |
|
1200 | 840 |
! |
841 |
||
7356 | 842 |
coerce:aNumber |
11720 | 843 |
"convert the argument aNumber into an instance of the receivers class and return it." |
7356 | 844 |
|
845 |
^ aNumber asShortFloat |
|
846 |
! |
|
847 |
||
1200 | 848 |
generality |
849 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
850 |
||
851 |
^ 70 |
|
852 |
||
853 |
||
1197 | 854 |
! ! |
855 |
||
856 |
!ShortFloat methodsFor:'comparing'! |
|
857 |
||
858 |
< aNumber |
|
859 |
"return true, if the argument is greater" |
|
860 |
||
861 |
%{ /* NOCONTEXT */ |
|
862 |
||
863 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 864 |
RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false ); |
1197 | 865 |
} |
7356 | 866 |
if (aNumber != nil) { |
11945 | 867 |
if (__qIsFloatLike(aNumber)) { |
868 |
RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false ); |
|
869 |
} |
|
870 |
if (__qIsShortFloat(aNumber)) { |
|
871 |
RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false ); |
|
872 |
} |
|
1197 | 873 |
} |
874 |
%}. |
|
875 |
^ aNumber lessFromShortFloat:self |
|
876 |
||
1200 | 877 |
" |
878 |
1.0 asShortFloat > (1/3) |
|
879 |
" |
|
1197 | 880 |
! |
881 |
||
882 |
<= aNumber |
|
883 |
"return true, if the argument is greater or equal" |
|
884 |
||
885 |
%{ /* NOCONTEXT */ |
|
886 |
||
887 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 888 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 889 |
} |
7356 | 890 |
if (aNumber != nil) { |
11945 | 891 |
if (__qIsFloatLike(aNumber)) { |
892 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
|
893 |
} |
|
894 |
if (__qIsShortFloat(aNumber)) { |
|
895 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
|
896 |
} |
|
897 |
} |
|
1197 | 898 |
%}. |
899 |
^ self retry:#<= coercing:aNumber |
|
900 |
! |
|
901 |
||
902 |
= aNumber |
|
7356 | 903 |
"return true, if the argument represents the same numeric value |
904 |
as the receiver, false otherwise" |
|
1197 | 905 |
|
906 |
%{ /* NOCONTEXT */ |
|
907 |
||
908 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 909 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
7356 | 910 |
} |
911 |
if (aNumber == nil) { |
|
11945 | 912 |
RETURN (false); |
1197 | 913 |
} |
7356 | 914 |
if (__qIsFloatLike(aNumber)) { |
11945 | 915 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
1197 | 916 |
} |
7356 | 917 |
if (__qIsShortFloat(aNumber)) { |
11945 | 918 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
1197 | 919 |
} |
920 |
%}. |
|
7356 | 921 |
^ aNumber equalFromShortFloat:self |
1197 | 922 |
! |
923 |
||
924 |
> aNumber |
|
925 |
"return true, if the argument is less" |
|
926 |
||
927 |
%{ /* NOCONTEXT */ |
|
928 |
||
929 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 930 |
RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false ); |
1197 | 931 |
} |
7356 | 932 |
if (aNumber != nil) { |
11945 | 933 |
if (__qIsFloatLike(aNumber)) { |
934 |
RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false ); |
|
935 |
} |
|
936 |
if (__qIsShortFloat(aNumber)) { |
|
937 |
RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false ); |
|
938 |
} |
|
1197 | 939 |
} |
940 |
%}. |
|
941 |
^ self retry:#> coercing:aNumber |
|
942 |
! |
|
943 |
||
944 |
>= aNumber |
|
945 |
"return true, if the argument is less or equal" |
|
946 |
||
947 |
%{ /* NOCONTEXT */ |
|
948 |
||
949 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 950 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 951 |
} |
7356 | 952 |
if (aNumber != nil) { |
11945 | 953 |
if (__qIsFloatLike(aNumber)) { |
954 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
|
955 |
} |
|
956 |
if (__qIsShortFloat(aNumber)) { |
|
957 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
|
958 |
} |
|
1197 | 959 |
} |
960 |
%}. |
|
961 |
^ self retry:#>= coercing:aNumber |
|
962 |
! |
|
963 |
||
964 |
hash |
|
965 |
"return a number for hashing; redefined, since floats compare |
|
966 |
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
|
967 |
as 3 hash." |
1197 | 968 |
|
969 |
|i| |
|
970 |
||
971 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
972 |
i := self asInteger. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
973 |
self = i ifTrue:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
974 |
^ i hash |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
975 |
]. |
1197 | 976 |
]. |
977 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
978 |
^ self asFloat hash |
1197 | 979 |
|
980 |
" |
|
11945 | 981 |
1.2345 hash |
982 |
1.2345 asShortFloat hash |
|
983 |
1.0 hash |
|
984 |
1.0 asShortFloat hash |
|
985 |
0.5 asShortFloat hash |
|
986 |
0.25 asShortFloat hash |
|
987 |
0.5 hash |
|
988 |
0.25 hash |
|
1197 | 989 |
" |
990 |
! |
|
991 |
||
992 |
~= aNumber |
|
993 |
"return true, if the arguments value are not equal" |
|
994 |
||
995 |
%{ /* NOCONTEXT */ |
|
996 |
||
7356 | 997 |
if (aNumber != nil) { |
11945 | 998 |
if (__isSmallInteger(aNumber)) { |
999 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
|
1000 |
} |
|
1001 |
if (__qIsFloatLike(aNumber)) { |
|
1002 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
|
1003 |
} |
|
1004 |
if (__qIsShortFloat(aNumber)) { |
|
1005 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
|
1006 |
} |
|
7356 | 1007 |
} else { |
11945 | 1008 |
RETURN ( true ); |
1197 | 1009 |
} |
1010 |
%}. |
|
7356 | 1011 |
^ super ~= aNumber |
1197 | 1012 |
! ! |
1013 |
||
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1014 |
!ShortFloat methodsFor:'mathematical functions'! |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1015 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1016 |
fastInverseSqrt |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1017 |
"return a rough but fast approximation of (1 / self sqrt). |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1018 |
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
|
1019 |
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
|
1020 |
much, unless inlined into the sender code. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1021 |
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
|
1022 |
(to speed up 3D computations, for example). |
1aac241c9981
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13359
diff
changeset
|
1023 |
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
|
1024 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1025 |
%{ /* NOCONTEXT */ |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1026 |
float x, rslt; |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1027 |
OBJ newFloat; |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1028 |
|
13361 | 1029 |
if (sizeof(float) == 4) { |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1030 |
x = __shortFloatVal(self); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1031 |
{ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1032 |
float xhalf = 0.5f * x; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1033 |
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
|
1034 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1035 |
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
|
1036 |
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
|
1037 |
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
|
1038 |
__qMKSFLOAT(newFloat, x); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1039 |
RETURN ( newFloat ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1040 |
} |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1041 |
} |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1042 |
%}. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1043 |
^ 1 / self sqrt |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1044 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1045 |
" |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1046 |
10.0 asShortFloat fastInverseSqrt |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1047 |
(1 / 10.0 asShortFloat sqrt) |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1048 |
" |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1049 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1050 |
" |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1051 |
|a b t0 t1 t2| |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1052 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1053 |
a := 345 asShortFloat. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1054 |
t0 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1055 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1056 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1057 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1058 |
t1 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1059 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1060 |
a fastInverseSqrt |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1061 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1062 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1063 |
t2 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1064 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1065 |
(1 / a sqrt) |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1066 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1067 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1068 |
Transcript show:'empty: '; showCR:t0. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1069 |
Transcript show:'fast: '; showCR:t1. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1070 |
Transcript show:'regular: '; showCR:t2. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1071 |
" |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1072 |
! ! |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1073 |
|
1197 | 1074 |
!ShortFloat methodsFor:'printing & storing'! |
1075 |
||
1076 |
printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1077 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1078 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1079 |
#printOn: as basic print mechanism." |
1197 | 1080 |
|
1081 |
%{ /* NOCONTEXT */ |
|
1082 |
||
1083 |
char buffer[64]; |
|
1084 |
REGISTER char *cp; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1085 |
int len; |
1197 | 1086 |
OBJ s; |
1087 |
||
1088 |
/* |
|
1089 |
* actually only needed on sparc: since thisContext is |
|
1090 |
* in a global register, which gets destroyed by printf, |
|
1091 |
* manually save it here - very stupid ... |
|
1092 |
*/ |
|
1093 |
__BEGIN_PROTECT_REGISTERS__ |
|
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
1094 |
len = snprintf(buffer, sizeof(buffer), "%.6g", (float)__shortFloatVal(self)); |
1197 | 1095 |
__END_PROTECT_REGISTERS__ |
1096 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1097 |
if (len >= 0 && len <= sizeof(buffer)-3) { |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1098 |
/* |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1099 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1100 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1101 |
*/ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1102 |
for (cp = buffer; *cp; cp++) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1103 |
if ((*cp == '.') || (*cp == ',') || (*cp == 'E') || (*cp == 'e')) break; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1104 |
} |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1105 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1106 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1107 |
*cp++ = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1108 |
} else { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1109 |
*cp++ = '.'; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1110 |
} |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1111 |
*cp++ = '0'; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1112 |
*cp = '\0'; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1113 |
} else { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1114 |
if (cp && (*cp == '.')) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1115 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1116 |
*cp = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1117 |
} |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1118 |
} |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1119 |
} |
1197 | 1120 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1121 |
s = __MKSTRING(buffer); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1122 |
if (s != nil) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1123 |
RETURN (s); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1124 |
} |
1197 | 1125 |
} |
1126 |
%}. |
|
5432 | 1127 |
^ self asFloat printString |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1128 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1129 |
" |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1130 |
1.234 asShortFloat printString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1131 |
1.0 asShortFloat printString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1132 |
1e10 asShortFloat printString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1133 |
1.2e3 asShortFloat printString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1134 |
1.2e30 asShortFloat printString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1135 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1136 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1137 |
self pi printString. |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1138 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1139 |
DecimalPointCharacterForPrinting := $,. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1140 |
1.234 asShortFloat printString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1141 |
1.0 asShortFloat printString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1142 |
1e10 asShortFloat printString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1143 |
1.2e3 asShortFloat printString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1144 |
1.2e30 asShortFloat printString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1145 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1146 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1147 |
DecimalPointCharacterForPrinting := $. |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1148 |
" |
5718 | 1149 |
! |
1150 |
||
1151 |
printfPrintString:formatString |
|
7746 | 1152 |
"non-standard: return a printed representation of the receiver |
5718 | 1153 |
as specified by formatString, which is defined by printf. |
1154 |
If you use this, be aware, that specifying doubles differs on |
|
11945 | 1155 |
systems; on SYSV machines you have to give something like %lf, |
5718 | 1156 |
while on BSD systems the format string has to be %F. |
1157 |
Also, the resulting string may not be longer than 255 bytes - |
|
1158 |
since thats the (static) size of the buffer. |
|
11924
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1159 |
This method is NONSTANDARD and may be removed without notice. |
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1160 |
WARNNG: this goes directly to the C-printf function and may therefore me inherently unsafe. |
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1161 |
Please use the printf: method, which is safe as it is completely implemented in Smalltalk." |
5718 | 1162 |
|
1163 |
%{ /* STACK: 400 */ |
|
1164 |
char buffer[256]; |
|
1165 |
OBJ s; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1166 |
int len; |
5718 | 1167 |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1168 |
if (__isStringLike(formatString)) { |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1169 |
/* |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1170 |
* actually only needed on sparc: since thisContext is |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1171 |
* in a global register, which gets destroyed by printf, |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1172 |
* manually save it here - very stupid ... |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1173 |
*/ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1174 |
__BEGIN_PROTECT_REGISTERS__ |
5718 | 1175 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1176 |
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
|
1177 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1178 |
__END_PROTECT_REGISTERS__ |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1179 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1180 |
if (len < 0) goto fail; |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1181 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1182 |
s = __MKSTRING_L(buffer, len); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1183 |
if (s != nil) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1184 |
RETURN (s); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1185 |
} |
5718 | 1186 |
} |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1187 |
fail: ; |
5718 | 1188 |
%}. |
1189 |
self primitiveFailed |
|
1190 |
||
1191 |
"ShortFloat pi printfPrintString:'%%lg -> %lg'" |
|
1192 |
"ShortFloat pi printfPrintString:'%%lf -> %lf'" |
|
1193 |
"ShortFloat pi printfPrintString:'%%7.5lg -> %7.5lg'" |
|
1194 |
"ShortFloat pi printfPrintString:'%%G -> %G'" |
|
1195 |
"ShortFloat pi printfPrintString:'%%F -> %F'" |
|
1196 |
"ShortFloat pi printfPrintString:'%%7.5G -> %7.5G'" |
|
1197 |
"ShortFloat pi printfPrintString:'%%7.5F -> %7.5F'" |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1198 |
! |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1199 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1200 |
storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1201 |
"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
|
1202 |
all valid digits are printed. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1203 |
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
|
1204 |
#storeOn: as basic print mechanism." |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1205 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1206 |
%{ /* NOCONTEXT */ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1207 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1208 |
char buffer[64]; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1209 |
REGISTER char *cp; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1210 |
OBJ s; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1211 |
int len; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1212 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1213 |
/* |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1214 |
* build a printf format string |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1215 |
*/ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1216 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1217 |
__BEGIN_PROTECT_REGISTERS__ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1218 |
#ifdef SYSV |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1219 |
len = snprintf(buffer, sizeof(buffer), "%.6lg", (double)__shortFloatVal(self)); |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1220 |
#else |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1221 |
len = snprintf(buffer, sizeof(buffer), "%.6G", (double)__shortFloatVal(self)); |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1222 |
#endif |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1223 |
__END_PROTECT_REGISTERS__ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1224 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1225 |
if (len >= 0 && len < sizeof(buffer)-3) { |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1226 |
/* |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1227 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1228 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1229 |
*/ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1230 |
for (cp = buffer; *cp; cp++) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1231 |
if ((*cp == '.') || (*cp == ',') || (*cp == 'E') || (*cp == 'e')) break; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1232 |
} |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1233 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1234 |
*cp++ = '.'; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1235 |
*cp++ = '0'; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1236 |
*cp = '\0'; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1237 |
} |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1238 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1239 |
s = __MKSTRING(buffer); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1240 |
if (s != nil) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1241 |
RETURN (s); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1242 |
} |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1243 |
} |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1244 |
%}. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1245 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1246 |
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
|
1247 |
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
|
1248 |
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
|
1249 |
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
|
1250 |
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
|
1251 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1252 |
^ ObjectMemory allocationFailureSignal raise. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1253 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1254 |
" |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1255 |
1.0 asShortFloat storeString |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1256 |
1.234 asShortFloat storeString |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1257 |
1e10 asShortFloat storeString |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1258 |
1.2e3 asShortFloat storeString |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1259 |
1.2e30 asShortFloat storeString |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1260 |
Float pi asShortFloat storeString |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1261 |
(1.0 uncheckedDivide:0) asShortFloat storeString |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1262 |
(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
|
1263 |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1264 |
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
|
1265 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1266 |
DecimalPointCharacterForPrinting := $,. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1267 |
1.234 asShortFloat storeString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1268 |
1.0 asShortFloat storeString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1269 |
1e10 asShortFloat storeString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1270 |
1.2e3 asShortFloat storeString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1271 |
1.2e30 asShortFloat storeString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1272 |
(1.0 uncheckedDivide:0) asShortFloat storeString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1273 |
(0.0 uncheckedDivide:0) asShortFloat storeString. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1274 |
DecimalPointCharacterForPrinting := $. |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1275 |
" |
1197 | 1276 |
! ! |
1277 |
||
12628 | 1278 |
!ShortFloat methodsFor:'private accessing'! |
1279 |
||
1280 |
basicAt:index |
|
1281 |
"return an internal byte of the float. |
|
1282 |
The value returned here depends on byte order, float representation etc. |
|
1283 |
Therefore, this method should be used strictly private. |
|
1284 |
||
1285 |
Notice: |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1286 |
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
|
1287 |
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
|
1288 |
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
|
1289 |
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
|
1290 |
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
|
1291 |
values as if this filler wasnt present." |
12628 | 1292 |
|
1293 |
%{ /* NOCONTEXT */ |
|
1294 |
||
1295 |
register int indx; |
|
1296 |
unsigned char *cp; |
|
1297 |
||
1298 |
/* |
|
1299 |
* notice the missing test for self being a nonNilObject - |
|
1300 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
1301 |
* and SmallInteger |
|
1302 |
*/ |
|
1303 |
if (__isSmallInteger(index)) { |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1304 |
indx = __intVal(index) - 1; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1305 |
if (((unsigned)(indx)) < sizeof(float)) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1306 |
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
|
1307 |
RETURN ( __mkSmallInteger(cp[indx] & 0xFF) ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1308 |
} |
12628 | 1309 |
} |
1310 |
%}. |
|
1311 |
^ self indexNotIntegerOrOutOfBounds:index |
|
1312 |
! |
|
1313 |
||
1314 |
basicAt:index put:value |
|
1315 |
"set an internal byte of the float. |
|
1316 |
The value to be stored here depends on byte order, float representation etc. |
|
1317 |
Therefore, this method should be used strictly private. |
|
1318 |
||
1319 |
Notice: |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1320 |
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
|
1321 |
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
|
1322 |
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
|
1323 |
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
|
1324 |
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
|
1325 |
values as if this filler wasnt present." |
12628 | 1326 |
|
1327 |
%{ /* NOCONTEXT */ |
|
1328 |
register int indx, val; |
|
1329 |
unsigned char *cp; |
|
1330 |
||
1331 |
/* |
|
1332 |
* notice the missing test for self being a nonNilObject - |
|
1333 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
1334 |
* and SmallInteger |
|
1335 |
*/ |
|
1336 |
if (__bothSmallInteger(index, value)) { |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1337 |
val = __intVal(value); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1338 |
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
|
1339 |
indx = __intVal(index) - 1; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1340 |
if (((unsigned)(indx)) < sizeof(float)) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1341 |
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
|
1342 |
cp[indx] = val; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1343 |
RETURN ( value ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1344 |
} |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1345 |
} |
12628 | 1346 |
} |
1347 |
%}. |
|
1348 |
value isInteger ifFalse:[ |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1349 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1350 |
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
|
1351 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1352 |
^ self elementNotInteger |
12628 | 1353 |
]. |
1354 |
(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
|
1355 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1356 |
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
|
1357 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1358 |
^ self elementBoundsError:value |
12628 | 1359 |
]. |
1360 |
^ self indexNotIntegerOrOutOfBounds:index |
|
1361 |
! ! |
|
1362 |
||
3897 | 1363 |
!ShortFloat methodsFor:'special access'! |
1364 |
||
1365 |
exponent |
|
1366 |
"extract a normalized floats exponent. |
|
1367 |
The returned value depends on the float-representation of |
|
1368 |
the underlying machine and is therefore highly unportable. |
|
1369 |
This is not for general use. |
|
1370 |
This assumes that the mantissa is normalized to |
|
1371 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
1372 |
||
1373 |
%{ /* NOCONTEXT */ |
|
8913 | 1374 |
int exp; |
3897 | 1375 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1376 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1377 |
frexpf( __shortFloatVal(self), &exp); |
1378 |
#else |
|
1379 |
frexp( (double)(__shortFloatVal(self)), &exp); |
|
1380 |
#endif |
|
14744 | 1381 |
RETURN (__mkSmallInteger(exp)); |
3897 | 1382 |
%}. |
1383 |
||
1384 |
" |
|
11945 | 1385 |
4.0 asShortFloat exponent |
1386 |
2.0 asShortFloat exponent |
|
1387 |
1.0 asShortFloat exponent |
|
1388 |
0.5 asShortFloat exponent |
|
1389 |
0.25 asShortFloat exponent |
|
1390 |
0.00000011111 asShortFloat exponent |
|
3897 | 1391 |
" |
1392 |
! |
|
1393 |
||
1394 |
mantissa |
|
1395 |
"extract a normalized floats mantissa. |
|
1396 |
The returned value depends on the float-representation of |
|
1397 |
the underlying machine and is therefore highly unportable. |
|
1398 |
This is not for general use. |
|
1399 |
This assumes that the mantissa is normalized to |
|
1400 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
1401 |
||
1402 |
%{ /* NOCONTEXT */ |
|
8913 | 1403 |
int exp; |
3897 | 1404 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1405 |
#if defined(__i386__) && defined(__GNUC__) |
14744 | 1406 |
float frac = frexpf(__shortFloatVal(self), &exp); |
1407 |
RETURN (__MKSFLOAT(frac)); |
|
7356 | 1408 |
#else |
14744 | 1409 |
double frac = frexp( (double)(__shortFloatVal(self)), &exp); |
1410 |
RETURN (__MKFLOAT(frac)); |
|
7356 | 1411 |
#endif |
3897 | 1412 |
%}. |
1413 |
||
1414 |
" |
|
11945 | 1415 |
1.0 asShortFloat exponent |
3897 | 1416 |
1.0 asShortFloat mantissa |
1417 |
||
11945 | 1418 |
0.5 asShortFloat exponent |
1419 |
0.5 asShortFloat mantissa |
|
3897 | 1420 |
|
11945 | 1421 |
0.25 asShortFloat exponent |
1422 |
0.25 asShortFloat mantissa |
|
3897 | 1423 |
|
11945 | 1424 |
0.00000011111 asShortFloat exponent |
1425 |
0.00000011111 asShortFloat mantissa |
|
3897 | 1426 |
" |
1427 |
! ! |
|
1428 |
||
1197 | 1429 |
!ShortFloat methodsFor:'testing'! |
1430 |
||
3147 | 1431 |
isFinite |
11945 | 1432 |
"return true, if the receiver is a finite float |
3147 | 1433 |
i.e. not NaN and not infinite." |
1434 |
||
1435 |
%{ /* NOCONTEXT */ |
|
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
1436 |
float fV = __shortFloatVal(self); |
3147 | 1437 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1438 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1439 |
* notice: on machines which do not provide |
11945 | 1440 |
* a isfinite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
1441 |
* 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
|
1442 |
*/ |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1443 |
if (isfinitef(fV)) { RETURN (true); } |
3147 | 1444 |
%}. |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
1445 |
^ false |
3147 | 1446 |
|
1447 |
" |
|
11945 | 1448 |
1.0 asShortFloat isFinite |
1449 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
1450 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
3147 | 1451 |
" |
1452 |
! |
|
1453 |
||
2387 | 1454 |
isNaN |
1455 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
1456 |
These are not created by ST/X float operations (they raise an exception); |
|
1457 |
however, inline C-code could produce them ..." |
|
1458 |
||
1459 |
%{ /* NOCONTEXT */ |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1460 |
float sV = __shortFloatVal(self); |
3139 | 1461 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1462 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1463 |
* notice: on machines which do not provide |
11945 | 1464 |
* a isnan() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
1465 |
* 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
|
1466 |
*/ |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1467 |
if (isnanf(sV)) { RETURN (true); } |
2392 | 1468 |
%}. |
1469 |
^ false |
|
2387 | 1470 |
|
3147 | 1471 |
" |
7356 | 1472 |
1.0 asShortFloat isNaN |
11945 | 1473 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
1474 |
(0.0 asShortFloat uncheckedDivide: 0.0 asShortFloat) isNaN |
|
3147 | 1475 |
" |
2387 | 1476 |
! |
1477 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1478 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1479 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1480 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1481 |
%{ |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1482 |
#if defined(__i386__) && defined(linux) |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1483 |
RETURN ((__ByteArrayInstPtr(self)->ba_element[sizeof(float)-1] == 128) ? true : false ); |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1484 |
#endif |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1485 |
%}. |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1486 |
^ super isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1487 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1488 |
" |
11945 | 1489 |
0.0 asShortFloat isNegativeZero |
1490 |
-0.0 asShortFloat isNegativeZero |
|
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1491 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1492 |
! |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1493 |
|
1197 | 1494 |
negative |
1495 |
"return true if the receiver is less than zero" |
|
1496 |
||
1497 |
%{ /* NOCONTEXT */ |
|
1498 |
||
1499 |
RETURN ( (__shortFloatVal(self) < 0.0) ? true : false ); |
|
5411 | 1500 |
%}. |
1197 | 1501 |
! |
1502 |
||
5357 | 1503 |
numberOfBits |
1504 |
"return the size (in bits) of the real; |
|
1505 |
typically, 32 is returned here, |
|
1506 |
but who knows ..." |
|
1507 |
||
1508 |
%{ /* NOCONTEXT */ |
|
1509 |
||
8913 | 1510 |
RETURN (__mkSmallInteger (sizeof(float) * 8)); |
5357 | 1511 |
%} |
1512 |
||
1513 |
" |
|
11945 | 1514 |
1.2 numberOfBits |
1515 |
1.2 asShortFloat numberOfBits |
|
5357 | 1516 |
" |
1517 |
||
1518 |
! |
|
1519 |
||
1197 | 1520 |
positive |
1521 |
"return true if the receiver is greater or equal to zero" |
|
1522 |
||
1523 |
%{ /* NOCONTEXT */ |
|
1524 |
||
1525 |
RETURN ( (__shortFloatVal(self) >= 0.0) ? true : false ); |
|
1526 |
%} |
|
1880 | 1527 |
! |
1528 |
||
1529 |
strictlyPositive |
|
1530 |
"return true if the receiver is greater than zero" |
|
1531 |
||
1532 |
%{ /* NOCONTEXT */ |
|
1533 |
||
1534 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
1535 |
%} |
|
1197 | 1536 |
! ! |
1537 |
||
5552 | 1538 |
!ShortFloat methodsFor:'truncation & rounding'! |
3897 | 1539 |
|
7356 | 1540 |
ceiling |
1541 |
"return the smallest integer which is greater or equal to the receiver." |
|
1542 |
||
1543 |
|val| |
|
1544 |
||
1545 |
%{ |
|
1546 |
float fVal; |
|
1547 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1548 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1549 |
fVal = ceilf(__shortFloatVal(self)); |
1550 |
#else |
|
1551 |
fVal = (float)(ceil((double)__shortFloatVal(self))); |
|
1552 |
#endif |
|
1553 |
/* |
|
1554 |
* ST-80 (and X3J20) returns integer. |
|
1555 |
*/ |
|
1556 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
11945 | 1557 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1558 |
} |
1559 |
__qMKSFLOAT(val, fVal); |
|
1560 |
%}. |
|
1561 |
^ val asInteger |
|
1562 |
||
1563 |
" |
|
11945 | 1564 |
0.5 asShortFloat ceiling |
1565 |
-0.5 asShortFloat ceiling |
|
7356 | 1566 |
" |
1567 |
! |
|
1568 |
||
1569 |
ceilingAsFloat |
|
1570 |
"return the smallest integer-valued float greater or equal to the receiver. |
|
11945 | 1571 |
This is much like #ceiling, but avoids a (possibly expensive) conversion |
7356 | 1572 |
of the result to an integer. |
1573 |
It may be useful, if the result is to be further used in another float-operation." |
|
1574 |
||
1575 |
%{ /* NOCONTEXT */ |
|
1576 |
float fVal; |
|
1577 |
OBJ v; |
|
1578 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1579 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1580 |
fVal = ceilf(__shortFloatVal(self)); |
1581 |
#else |
|
1582 |
fVal = (float) ceil((double)__shortFloatVal(self)); |
|
1583 |
#endif |
|
1584 |
__qMKSFLOAT(v, fVal); |
|
1585 |
RETURN (v); |
|
1586 |
%} |
|
1587 |
" |
|
11945 | 1588 |
0.5 asShortFloat ceilingAsFloat |
1589 |
-0.5 asShortFloat ceilingAsFloat |
|
1590 |
-1.5 asShortFloat ceilingAsFloat |
|
7356 | 1591 |
" |
1592 |
! |
|
1593 |
||
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1594 |
floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1595 |
"return the integer nearest the receiver towards negative infinity." |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1596 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1597 |
|val| |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1598 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1599 |
%{ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1600 |
float fVal; |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1601 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1602 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1603 |
fVal = floorf(__shortFloatVal(self)); |
1604 |
#else |
|
1605 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
1606 |
#endif |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1607 |
/* |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1608 |
* ST-80 (and X3J20) returns integer. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1609 |
*/ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1610 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
11945 | 1611 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1612 |
} |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1613 |
__qMKSFLOAT(val, fVal); |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1614 |
%}. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1615 |
^ val asInteger |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1616 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1617 |
" |
11945 | 1618 |
0.5 asShortFloat floor |
1619 |
-0.5 asShortFloat floor |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1620 |
" |
7356 | 1621 |
! |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1622 |
|
7356 | 1623 |
floorAsFloat |
1624 |
"return the float which represents the next lower |
|
1625 |
integer nearest the receiver towards negative infinity. |
|
1626 |
Much like floor, but returns a float result - useful if the result |
|
1627 |
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
|
1628 |
|
7356 | 1629 |
%{ /* NOCONTEXT */ |
1630 |
float fVal; |
|
1631 |
OBJ v; |
|
1632 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1633 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1634 |
fVal = floorf(__shortFloatVal(self)); |
1635 |
#else |
|
1636 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
1637 |
#endif |
|
1638 |
__qMKSFLOAT(v, fVal); |
|
1639 |
RETURN ( v ); |
|
1640 |
%} |
|
1641 |
||
1642 |
" |
|
11945 | 1643 |
0.5 asShortFloat floorAsFloat |
1644 |
-0.5 asShortFloat floorAsFloat |
|
7356 | 1645 |
" |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1646 |
! |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1647 |
|
3897 | 1648 |
fractionPart |
1649 |
"extract the after-decimal fraction part. |
|
7356 | 1650 |
such that (self truncated + self fractionPart) = self" |
3897 | 1651 |
|
1652 |
%{ /* NOCONTEXT */ |
|
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1653 |
#ifdef NO_MODFF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1654 |
double dFrac, dTrunc; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1655 |
#else |
14741 | 1656 |
# ifndef __VISUALC__ |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1657 |
float modff(float, float *); |
14741 | 1658 |
# endif |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1659 |
#endif |
14718 | 1660 |
float frac, trunc; |
3897 | 1661 |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1662 |
__threadErrno = 0; |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1663 |
#ifdef NO_MODFF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1664 |
dFrac = modf((double)__shortFloatVal(self), &dTrunc) ; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1665 |
frac = (float)dFrac; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1666 |
#else |
14718 | 1667 |
frac = modff(__shortFloatVal(self), &trunc); |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
1668 |
#endif |
11945 | 1669 |
if (! isnan(frac)) { |
14719 | 1670 |
if (__threadErrno == 0) { |
1671 |
RETURN (__MKSFLOAT(frac)); |
|
1672 |
} |
|
3897 | 1673 |
} |
1674 |
%}. |
|
7403 | 1675 |
^ self class |
14719 | 1676 |
raise:#domainErrorSignal |
1677 |
receiver:self |
|
1678 |
selector:#fractionPart |
|
1679 |
arguments:#() |
|
1680 |
errorString:'bad receiver in fractionPart' |
|
3897 | 1681 |
|
1682 |
" |
|
11945 | 1683 |
1.6 asShortFloat fractionPart + 1.6 asShortFloat truncated |
1684 |
-1.6 asShortFloat fractionPart + -1.6 asShortFloat truncated |
|
7356 | 1685 |
|
14718 | 1686 |
1.0 asShortFloat fractionPart |
1687 |
0.5 asShortFloat fractionPart |
|
1688 |
0.25 asShortFloat fractionPart |
|
1689 |
3.14159 asShortFloat fractionPart |
|
1690 |
12345673.14159 asShortFloat fractionPart |
|
1691 |
123456731231231231.14159 asShortFloat fractionPart |
|
3897 | 1692 |
" |
7356 | 1693 |
! |
3897 | 1694 |
|
7356 | 1695 |
rounded |
1696 |
"return the receiver rounded to the nearest integer" |
|
1697 |
||
1698 |
|val| |
|
1699 |
||
11945 | 1700 |
%{ |
7356 | 1701 |
float fVal; |
1702 |
||
1703 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1704 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1705 |
if (fVal < 0.0) { |
11945 | 1706 |
fVal = ceilf(fVal - (float)0.5); |
7356 | 1707 |
} else { |
11945 | 1708 |
fVal = floorf(fVal + (float)0.5); |
7356 | 1709 |
} |
1710 |
#else |
|
1711 |
if (fVal < 0.0) { |
|
11945 | 1712 |
fVal = (float)ceil((double)fVal - 0.5); |
7356 | 1713 |
} else { |
11945 | 1714 |
fVal = (float)floor((double)fVal + 0.5); |
7356 | 1715 |
} |
1716 |
#endif |
|
1717 |
/* |
|
1718 |
* ST-80 (and X3J20) returns integer. |
|
1719 |
*/ |
|
1720 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
11945 | 1721 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1722 |
} |
1723 |
__qMKSFLOAT(val, fVal); |
|
1724 |
%}. |
|
1725 |
^ val asInteger |
|
1726 |
||
1727 |
" |
|
11945 | 1728 |
0.4 asShortFloat rounded |
1729 |
0.5 asShortFloat rounded |
|
1730 |
0.6 asShortFloat rounded |
|
1731 |
-0.4 asShortFloat rounded |
|
1732 |
-0.5 asShortFloat rounded |
|
1733 |
-0.6 asShortFloat rounded |
|
7356 | 1734 |
" |
1735 |
! |
|
1736 |
||
1737 |
roundedAsFloat |
|
1738 |
"return the receiver rounded to the nearest integer as a float. |
|
11945 | 1739 |
This is much like #rounded, but avoids a (possibly expensive) conversion |
7356 | 1740 |
of the result to an integer. |
1741 |
It may be useful, if the result is to be further used in another float-operation." |
|
1742 |
||
1743 |
%{ /* NOCONTEXT */ |
|
1744 |
float fVal; |
|
1745 |
OBJ v; |
|
1746 |
||
1747 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1748 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1749 |
if (fVal < 0.0) { |
11945 | 1750 |
fVal = ceilf(fVal - (float)0.5); |
7356 | 1751 |
} else { |
11945 | 1752 |
fVal = floorf(fVal + (float)0.5); |
7356 | 1753 |
} |
1754 |
#else |
|
1755 |
if (fVal < 0.0) { |
|
11945 | 1756 |
fVal = (float)ceil((double)fVal - 0.5); |
7356 | 1757 |
} else { |
11945 | 1758 |
fVal = (float)floor((double)fVal + 0.5); |
7356 | 1759 |
} |
1760 |
#endif |
|
1761 |
__qMKSFLOAT(v, fVal); |
|
1762 |
RETURN (v); |
|
1763 |
%} |
|
1764 |
||
1765 |
" |
|
11945 | 1766 |
0.4 asShortFloat rounded |
1767 |
0.5 asShortFloat rounded |
|
1768 |
0.6 asShortFloat rounded |
|
1769 |
-0.4 asShortFloat rounded |
|
1770 |
-0.5 asShortFloat rounded |
|
1771 |
-0.6 asShortFloat rounded |
|
7356 | 1772 |
|
11945 | 1773 |
0.4 asShortFloat roundedAsFloat |
1774 |
0.5 asShortFloat roundedAsFloat |
|
1775 |
0.6 asShortFloat roundedAsFloat |
|
1776 |
-0.4 asShortFloat roundedAsFloat |
|
1777 |
-0.5 asShortFloat roundedAsFloat |
|
1778 |
-0.6 asShortFloat roundedAsFloat |
|
7356 | 1779 |
" |
1780 |
! |
|
1781 |
||
1782 |
truncated |
|
1783 |
"return the receiver truncated towards zero as an integer" |
|
1784 |
||
1785 |
|val| |
|
1786 |
||
11945 | 1787 |
%{ |
7356 | 1788 |
float fVal; |
1789 |
||
1790 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1791 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1792 |
if (fVal < 0.0) { |
11945 | 1793 |
fVal = ceilf(fVal); |
7356 | 1794 |
} else { |
11945 | 1795 |
fVal = floorf(fVal); |
7356 | 1796 |
} |
1797 |
#else |
|
1798 |
if (fVal < 0.0) { |
|
11945 | 1799 |
fVal = (float)ceil((double)fVal); |
7356 | 1800 |
} else { |
11945 | 1801 |
fVal = (float)floor((double)fVal); |
7356 | 1802 |
} |
1803 |
#endif |
|
1804 |
/* |
|
1805 |
* ST-80 (and X3J20) returns integer. |
|
1806 |
*/ |
|
1807 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
11945 | 1808 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1809 |
} |
1810 |
__qMKSFLOAT(val, fVal); |
|
1811 |
%}. |
|
1812 |
^ val asInteger |
|
1813 |
||
1814 |
" |
|
11945 | 1815 |
0.5 asShortFloat truncated |
1816 |
-0.5 asShortFloat truncated |
|
1817 |
0.5 asShortFloat truncatedAsFloat |
|
1818 |
-0.5 asShortFloat truncatedAsFloat |
|
7356 | 1819 |
" |
1820 |
! |
|
1821 |
||
1822 |
truncatedAsFloat |
|
1823 |
"return the receiver truncated towards zero as a float. |
|
11945 | 1824 |
This is much like #truncated, but avoids a (possibly expensive) conversion |
7356 | 1825 |
of the result to an integer. |
11945 | 1826 |
It may be useful, if the result is to be further used in another |
7356 | 1827 |
float-operation." |
1828 |
||
1829 |
%{ /* NOCONTEXT */ |
|
1830 |
float fVal; |
|
1831 |
OBJ v; |
|
1832 |
||
1833 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1834 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1835 |
if (fVal < 0.0) { |
11945 | 1836 |
fVal = ceilf(fVal); |
7356 | 1837 |
} else { |
11945 | 1838 |
fVal = floorf(fVal); |
7356 | 1839 |
} |
1840 |
#else |
|
1841 |
if (fVal < 0.0) { |
|
11945 | 1842 |
fVal = (float)ceil((double)fVal); |
7356 | 1843 |
} else { |
11945 | 1844 |
fVal = (float)floor((double)fVal); |
7356 | 1845 |
} |
1846 |
#endif |
|
1847 |
__qMKSFLOAT(v, fVal); |
|
1848 |
RETURN (v); |
|
1849 |
%} |
|
1850 |
||
1851 |
" |
|
11945 | 1852 |
0.5 asShortFloat truncated |
1853 |
-0.5 asShortFloat truncated |
|
1854 |
0.5 asShortFloat truncatedAsFloat |
|
1855 |
-0.5 asShortFloat truncatedAsFloat |
|
7356 | 1856 |
" |
3897 | 1857 |
! ! |
1858 |
||
1880 | 1859 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 1860 |
|
1861 |
version |
|
14922 | 1862 |
^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.120 2013-03-20 15:01:14 cg Exp $' |
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1863 |
! |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1864 |
|
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1865 |
version_CVS |
14922 | 1866 |
^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.120 2013-03-20 15:01:14 cg Exp $' |
1197 | 1867 |
! ! |
14744 | 1868 |