author | Claus Gittinger <cg@exept.de> |
Tue, 17 Jun 2003 22:13:34 +0200 | |
changeset 7412 | daae29634e02 |
parent 7408 | 43bd10f2f2e6 |
child 7419 | 44b2f66791f1 |
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 |
" |
|
12 |
||
13 |
||
14 |
||
5411 | 15 |
"{ Package: 'stx:libbasic' }" |
16 |
||
1197 | 17 |
LimitedPrecisionReal variableByteSubclass:#ShortFloat |
18 |
instanceVariableNames:'' |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
19 |
classVariableNames:'' |
1197 | 20 |
poolDictionaries:'' |
21 |
category:'Magnitude-Numbers' |
|
22 |
! |
|
23 |
||
2394 | 24 |
!ShortFloat primitiveDefinitions! |
25 |
%{ |
|
26 |
||
3471 | 27 |
#include <errno.h> |
28 |
||
29 |
#ifndef __OPTIMIZE__ |
|
30 |
# define __OPTIMIZE__ |
|
31 |
#endif |
|
4825 | 32 |
|
33 |
#define __USE_ISOC9X 1 |
|
3471 | 34 |
#include <math.h> |
35 |
||
36 |
/* |
|
37 |
* on some systems errno is a macro ... check for it here |
|
38 |
*/ |
|
39 |
#ifndef errno |
|
40 |
extern errno; |
|
41 |
#endif |
|
42 |
||
2394 | 43 |
#if defined (_AIX) |
44 |
# include <float.h> |
|
45 |
#endif |
|
4825 | 46 |
|
2395 | 47 |
#if defined(IRIX) |
48 |
# include <nan.h> |
|
49 |
#endif |
|
4825 | 50 |
|
2397 | 51 |
#if defined(LINUX) |
4825 | 52 |
# ifndef NAN |
5822
4e56f8aadda5
Get nan.h from bits/nan.h for Linux
Stefan Vogel <sv@exept.de>
parents:
5718
diff
changeset
|
53 |
# include <bits/nan.h> |
4825 | 54 |
# endif |
2397 | 55 |
#endif |
4825 | 56 |
|
2400 | 57 |
#if defined(solaris) || defined(sunos) |
2398 | 58 |
# include <nan.h> |
59 |
#endif |
|
2394 | 60 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
61 |
#ifdef WIN32 |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
62 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
63 |
* no finite(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
64 |
* no isnan(x) ? |
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 |
# ifndef finite |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
67 |
# define finite(x) 1 |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
68 |
# endif |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
69 |
# ifndef isnan |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
70 |
# define isnan(x) 0 |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
71 |
# endif |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
72 |
#endif |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
73 |
|
3414 | 74 |
#ifdef realIX |
75 |
/* |
|
76 |
* no finite(x) |
|
77 |
*/ |
|
78 |
# ifndef finite |
|
79 |
# define finite(x) 1 |
|
80 |
# endif |
|
81 |
#endif |
|
82 |
||
2394 | 83 |
%} |
84 |
! ! |
|
85 |
||
1880 | 86 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 87 |
|
88 |
copyright |
|
89 |
" |
|
90 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3158 | 91 |
All Rights Reserved |
1197 | 92 |
|
93 |
This software is furnished under a license and may be used |
|
94 |
only in accordance with the terms of that license and with the |
|
95 |
inclusion of the above copyright notice. This software may not |
|
96 |
be provided or otherwise made available to, or used by, any |
|
97 |
other person. No title to or ownership of the software is |
|
98 |
hereby transferred. |
|
99 |
" |
|
100 |
||
101 |
||
102 |
! |
|
103 |
||
104 |
documentation |
|
105 |
" |
|
7380 | 106 |
ShortFloats represent rational numbers with limited precision. |
107 |
They use the C-compilers 'float' format, which is usually the IEE single float format. |
|
108 |
||
109 |
In contrast to Floats (which use the C-compilers 64bit 'double' format), |
|
110 |
ShortFloats give you 32 bit floats. |
|
1197 | 111 |
|
112 |
Notice, that ST/X Floats are what Doubles are in ST-80 and ShortFloats are |
|
113 |
ST-80's Floats respectively. |
|
114 |
This may change in one of the next versions (at least on machines, which |
|
115 |
provide different float and double types in their C-compiler. |
|
116 |
||
117 |
WARNING: |
|
7380 | 118 |
The layout of shortFloat instances is known by the runtime system and the compiler; |
119 |
you may not add instance variables here. |
|
120 |
Also, subclassing is complicated by the fact, that the VM creates floats/shortFloats, |
|
121 |
and does some of its float-checks by an identity compare with the ShortFloat-class. |
|
122 |
(i.e. your subclasses instances may not be recognized as float-like objects, |
|
123 |
thus mixed mode arithmetic will always coerce them, effectively slowing things down). |
|
124 |
This may be changed, to use a flag bit in the class. |
|
1197 | 125 |
|
7380 | 126 |
Mixed mode arithmetic: |
127 |
shortFloat op shortFloat -> shortFloat |
|
128 |
shortFloat op fix -> shortFloat |
|
129 |
shortFloat op fraction -> shortFloat |
|
130 |
shortFloat op integer -> shortFloat |
|
131 |
shortFloat op longFloat -> longFloat |
|
132 |
shortFloat op float -> float |
|
133 |
shortFloat op complex -> complex |
|
1295 | 134 |
|
7405 | 135 |
Representation: |
136 |
32bit single precision IEE floats |
|
137 |
23 bit mantissa, |
|
138 |
8 bit exponent, |
|
139 |
6 decimal digits (approx) |
|
140 |
||
141 |
Range and Precision of Storage Formats: see LimitedPrecisionReal >> documentation |
|
142 |
||
1295 | 143 |
[author:] |
7380 | 144 |
Claus Gittinger |
1556 | 145 |
|
146 |
[see also:] |
|
7380 | 147 |
Number |
148 |
Float LongFloat Fraction FixedPoint Integer Complex |
|
149 |
FloatArray DoubleArray |
|
1197 | 150 |
" |
151 |
! ! |
|
152 |
||
1880 | 153 |
!ShortFloat class methodsFor:'instance creation'! |
1207 | 154 |
|
155 |
basicNew |
|
156 |
"return a new shortFloat - here we return 0.0 |
|
157 |
- shortFloats are usually NOT created this way ... |
|
158 |
Its implemented here to allow things like binary store & load |
|
159 |
of shortFloats. (but even this support will go away eventually, its not |
|
160 |
a good idea to store the bits of a float - the reader might have a |
|
161 |
totally different representation - so floats will eventually be |
|
162 |
binary stored in a device independent format." |
|
163 |
||
164 |
%{ /* NOCONTEXT */ |
|
165 |
OBJ newFloat; |
|
166 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
167 |
__qMKSFLOAT(newFloat, 0.0); |
1207 | 168 |
RETURN (newFloat); |
169 |
%} |
|
3158 | 170 |
! |
171 |
||
4297 | 172 |
fastFromString:aString at:startIndex |
173 |
"return the next ShortFloat from the string starting at startIndex. |
|
174 |
No spaces are skipped. |
|
175 |
||
176 |
This is a specially tuned entry (using a low-level C-call), which |
|
177 |
returns garbage if the argument string is not a valid float number. |
|
178 |
It has been added to allow higher speed string decomposition into |
|
179 |
numbers." |
|
180 |
%{ |
|
181 |
if (__isString(aString) && __isSmallInteger(startIndex)) { |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
182 |
char *cp = (char *)(__stringVal(aString)); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
183 |
int idx = __intVal(startIndex) - 1; |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
184 |
double atof(); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
185 |
double val; |
4297 | 186 |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
187 |
if ((unsigned)idx < __stringSize(aString)) { |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
188 |
val = atof(cp + idx); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
189 |
RETURN (__MKSFLOAT(val)); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
190 |
} |
4297 | 191 |
} |
192 |
%}. |
|
193 |
self primitiveFailed. |
|
194 |
||
195 |
" |
|
196 |
ShortFloat fastFromString:'123.45' at:1 |
|
197 |
ShortFloat fastFromString:'123.45' at:2 |
|
198 |
ShortFloat fastFromString:'123.45E4' at:1 |
|
199 |
ShortFloat fastFromString:'hello123.45E4' at:6 |
|
200 |
ShortFloat fastFromString:'12345' at:1 |
|
201 |
ShortFloat fastFromString:'12345' at:2 |
|
202 |
ShortFloat fastFromString:'12345' at:3 |
|
203 |
ShortFloat fastFromString:'12345' at:4 |
|
204 |
ShortFloat fastFromString:'12345' at:5 |
|
205 |
ShortFloat fastFromString:'12345' at:6 |
|
206 |
ShortFloat fastFromString:'12345' at:0 |
|
207 |
ShortFloat fastFromString:'hello123.45E4' at:1 |
|
208 |
||
209 |
Time millisecondsToRun:[ |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
210 |
100000 timesRepeat:[ |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
211 |
ShortFloat readFrom:'123.45' |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
212 |
] |
4297 | 213 |
] |
214 |
" |
|
215 |
||
216 |
" |
|
217 |
Time millisecondsToRun:[ |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
218 |
100000 timesRepeat:[ |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
219 |
ShortFloat fastFromString:'123.45' at:1 |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
220 |
] |
4297 | 221 |
] |
222 |
" |
|
223 |
||
224 |
||
225 |
||
226 |
! |
|
227 |
||
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
228 |
readFrom:aStringOrStream |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
229 |
"read a shortFloat from a string" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
230 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
231 |
|num| |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
232 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
233 |
num := super readFrom:aStringOrStream onError:[self error:'conversion error for: ' , self name]. |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
234 |
num notNil ifTrue:[ |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
235 |
num := num asShortFloat |
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
236 |
]. |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
237 |
^ num |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
238 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
239 |
" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
240 |
ShortFloat readFrom:'0.1' |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
241 |
ShortFloat readFrom:'0' |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
242 |
ShortFloat readFrom:'.123' |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
243 |
ShortFloat readFrom:'-.123' |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
244 |
ShortFloat readFrom:'1e4' |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
245 |
" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
246 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
247 |
"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
|
248 |
! |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
249 |
|
3158 | 250 |
readFrom:aStringOrStream onError:exceptionBlock |
251 |
"read a shortFloat from a string" |
|
252 |
||
253 |
|num| |
|
254 |
||
255 |
num := super readFrom:aStringOrStream onError:nil. |
|
256 |
num isNil ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
257 |
^ exceptionBlock value |
3158 | 258 |
]. |
259 |
^ num asShortFloat |
|
260 |
||
261 |
" |
|
262 |
ShortFloat readFrom:'0.1' |
|
263 |
ShortFloat readFrom:'0' |
|
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
264 |
ShortFloat readFrom:'.123' |
3158 | 265 |
" |
266 |
||
267 |
"Modified: / 7.1.1998 / 16:17:59 / cg" |
|
1207 | 268 |
! ! |
269 |
||
7356 | 270 |
!ShortFloat class methodsFor:'binary storage'! |
271 |
||
272 |
readBinaryIEEESingleFrom:aStream |
|
273 |
"read a float value from the binary stream, aStream, |
|
274 |
interpreting the next bytes as an IEEE formatted 4-byte float" |
|
1197 | 275 |
|
7356 | 276 |
|f| |
1197 | 277 |
|
7356 | 278 |
f := ShortFloat basicNew. |
279 |
self readBinaryIEEESingleFrom:aStream into:f. |
|
280 |
^ f |
|
1197 | 281 |
|
7356 | 282 |
"Created: 16.4.1996 / 21:00:35 / cg" |
1197 | 283 |
! |
284 |
||
7356 | 285 |
readBinaryIEEESingleFrom:aStream into:aFloat |
286 |
"read a float value from the binary stream, aStream, |
|
287 |
interpreting the next bytes as an IEEE formatted 4-byte float" |
|
288 |
||
289 |
aFloat isShortFloat ifFalse:[self error]. |
|
6574 | 290 |
|
7356 | 291 |
" |
292 |
this implementation is wrong: does not work on non-IEEE machines |
|
293 |
(to date all machines where ST/X is running on use |
|
294 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
295 |
" |
|
296 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
297 |
||
298 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
299 |
"swap the bytes" |
|
300 |
8 to:4 by:-1 do:[:i | |
|
301 |
aFloat basicAt:i put:(aStream next) |
|
302 |
]. |
|
303 |
^ self |
|
304 |
]. |
|
305 |
1 to:4 do:[:i | |
|
306 |
aFloat basicAt:i put:aStream next |
|
307 |
] |
|
6574 | 308 |
! |
309 |
||
7356 | 310 |
storeBinaryIEEESingle:aFloat on:aStream |
311 |
"store aFloat as an IEEE formatted 4-byte float |
|
312 |
onto the binary stream, aStream" |
|
313 |
||
314 |
aFloat isShortFloat ifFalse:[self error]. |
|
315 |
||
316 |
" |
|
317 |
this implementation is wrong: does not work on non-IEEE machines |
|
318 |
(to date all machines where ST/X is running on use |
|
319 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
320 |
" |
|
321 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
6574 | 322 |
|
7356 | 323 |
UninterpretedBytes isBigEndian ifFalse:[ |
324 |
"swap the bytes" |
|
325 |
8 to:4 by:-1 do:[:i | |
|
326 |
aStream nextPut:(aFloat basicAt:i). |
|
327 |
]. |
|
328 |
^ self |
|
329 |
]. |
|
330 |
1 to:4 do:[:i | |
|
331 |
aStream nextPut:(aFloat basicAt:i). |
|
332 |
] |
|
333 |
! ! |
|
334 |
||
335 |
!ShortFloat class methodsFor:'constants'! |
|
6574 | 336 |
|
7392 | 337 |
e |
338 |
"return the constant e as ShortFloat" |
|
339 |
||
340 |
^ Float e asShortFloat |
|
341 |
! |
|
342 |
||
343 |
pi |
|
344 |
"return the constant pi as ShortFloat" |
|
345 |
||
346 |
^ Float pi asShortFloat |
|
347 |
! |
|
348 |
||
1197 | 349 |
unity |
1260 | 350 |
"return the neutral element for multiplication (1.0) as ShortFloat" |
1197 | 351 |
|
352 |
^ 1.0 asShortFloat |
|
353 |
||
1260 | 354 |
"Modified: 23.4.1996 / 09:26:51 / cg" |
1197 | 355 |
! |
356 |
||
357 |
zero |
|
1260 | 358 |
"return the neutral element for addition (0.0) as ShortFloat" |
1197 | 359 |
|
360 |
^ 0.0 asShortFloat |
|
361 |
||
1260 | 362 |
"Modified: 23.4.1996 / 09:26:45 / cg" |
1197 | 363 |
! ! |
364 |
||
1880 | 365 |
!ShortFloat class methodsFor:'queries'! |
1197 | 366 |
|
6898 | 367 |
exponentCharacter |
368 |
^ $e |
|
369 |
! |
|
370 |
||
1197 | 371 |
isBuiltInClass |
1264 | 372 |
"return true if this class is known by the run-time-system. |
373 |
Here, true is returned for myself, false for subclasses." |
|
1197 | 374 |
|
375 |
^ self == ShortFloat |
|
376 |
||
1264 | 377 |
"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
|
378 |
! |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
379 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
380 |
isIEEEFormat |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
381 |
"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
|
382 |
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
|
383 |
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
|
384 |
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
|
385 |
machine). |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
386 |
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
|
387 |
(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
|
388 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
389 |
^ true "/ this may be a lie |
7356 | 390 |
! |
391 |
||
392 |
numBitsInExponent |
|
393 |
"answer the number of bits in the exponent |
|
394 |
This is an IEEE float, where 8 bits are available: |
|
395 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
|
396 |
" |
|
397 |
||
398 |
^ 8 |
|
399 |
! |
|
400 |
||
401 |
numBitsInMantissa |
|
402 |
"answer the number of bits in the mantissa. |
|
403 |
This is an IEEE float, where 23 bits (the hidden one is not counted here) are available: |
|
404 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
|
405 |
" |
|
406 |
||
407 |
^ 23 |
|
408 |
! |
|
409 |
||
410 |
precision |
|
411 |
"answer the precision of a ShortFloat (in bits) |
|
412 |
This is an IEEE float, only the fraction from the normalized mantissa is stored |
|
413 |
and so there is a hidden bit and the mantissa is actually represented by 24 binary digits" |
|
414 |
||
415 |
^ 24 |
|
416 |
! |
|
417 |
||
418 |
radix |
|
419 |
"answer the radix of a ShortFloats exponent |
|
420 |
This is an IEEE float, which is represented as binary" |
|
421 |
||
422 |
^ 2 "must be careful here, whenever ST/X is used on VAX or a 370" |
|
1197 | 423 |
! ! |
424 |
||
425 |
!ShortFloat methodsFor:'arithmetic'! |
|
426 |
||
427 |
* aNumber |
|
428 |
"return the product of the receiver and the argument, aNumber" |
|
429 |
||
430 |
%{ /* NOCONTEXT */ |
|
431 |
||
432 |
OBJ newFloat; |
|
433 |
float result; |
|
434 |
double dResult; |
|
435 |
||
436 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 437 |
result = __shortFloatVal(self) * (float)(__intVal(aNumber)); |
1197 | 438 |
retResult: |
7356 | 439 |
__qMKSFLOAT(newFloat, result); |
440 |
RETURN ( newFloat ); |
|
441 |
} |
|
442 |
if (__isShortFloat(aNumber)) { |
|
443 |
result = __shortFloatVal(self) * __shortFloatVal(aNumber); |
|
444 |
goto retResult; |
|
445 |
} |
|
446 |
if (__isFloatLike(aNumber)) { |
|
447 |
dResult = (double) __shortFloatVal(self)* __floatVal(aNumber); |
|
448 |
__qMKFLOAT(newFloat, dResult); |
|
449 |
RETURN ( newFloat ); |
|
1197 | 450 |
} |
451 |
%}. |
|
452 |
^ aNumber productFromShortFloat:self |
|
453 |
! |
|
454 |
||
455 |
+ aNumber |
|
456 |
"return the sum of the receiver and the argument, aNumber" |
|
457 |
||
458 |
%{ /* NOCONTEXT */ |
|
459 |
||
460 |
OBJ newFloat; |
|
461 |
float result; |
|
462 |
double dResult; |
|
463 |
||
464 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 465 |
result = __shortFloatVal(self) + (float)(__intVal(aNumber)); |
1197 | 466 |
retResult: |
7356 | 467 |
__qMKSFLOAT(newFloat, result); |
468 |
RETURN ( newFloat ); |
|
469 |
} |
|
470 |
if (__isShortFloat(aNumber)) { |
|
471 |
result = __shortFloatVal(self) + __shortFloatVal(aNumber); |
|
472 |
goto retResult; |
|
473 |
} |
|
474 |
if (__isFloatLike(aNumber)) { |
|
475 |
dResult = (double) __shortFloatVal(self) + __floatVal(aNumber); |
|
476 |
__qMKFLOAT(newFloat, dResult); |
|
477 |
RETURN ( newFloat ); |
|
1197 | 478 |
} |
479 |
%}. |
|
480 |
^ aNumber sumFromShortFloat:self |
|
481 |
! |
|
482 |
||
483 |
- aNumber |
|
484 |
"return the difference of the receiver and the argument, aNumber" |
|
485 |
||
486 |
%{ /* NOCONTEXT */ |
|
487 |
||
488 |
OBJ newFloat; |
|
489 |
float result; |
|
490 |
double dResult; |
|
491 |
||
492 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 493 |
result = __shortFloatVal(self) - (float)(__intVal(aNumber)); |
1197 | 494 |
retResult: |
7356 | 495 |
__qMKSFLOAT(newFloat, result); |
496 |
RETURN ( newFloat ); |
|
497 |
} |
|
498 |
if (__isShortFloat(aNumber)) { |
|
499 |
result = __shortFloatVal(self) - __shortFloatVal(aNumber); |
|
500 |
goto retResult; |
|
501 |
} |
|
502 |
if (__isFloatLike(aNumber)) { |
|
503 |
dResult = (double) __shortFloatVal(self) - __floatVal(aNumber); |
|
504 |
__qMKFLOAT(newFloat, dResult); |
|
505 |
RETURN ( newFloat ); |
|
1197 | 506 |
} |
507 |
%}. |
|
508 |
^ aNumber differenceFromShortFloat:self |
|
509 |
! |
|
510 |
||
511 |
/ aNumber |
|
512 |
"return the quotient of the receiver and the argument, aNumber" |
|
513 |
||
514 |
%{ /* NOCONTEXT */ |
|
515 |
||
516 |
OBJ newFloat; |
|
517 |
float result, val; |
|
518 |
double dResult, dVal; |
|
519 |
||
520 |
if (__isSmallInteger(aNumber)) { |
|
5954 | 521 |
if (aNumber != __MKSMALLINT(0)) { |
522 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
1197 | 523 |
retResult: |
5954 | 524 |
__qMKSFLOAT(newFloat, result); |
525 |
RETURN ( newFloat ); |
|
526 |
} |
|
7356 | 527 |
} |
528 |
if (__isShortFloat(aNumber)) { |
|
5954 | 529 |
val = __shortFloatVal(aNumber); |
530 |
if (val != 0.0) { |
|
531 |
result = __shortFloatVal(self) / val; |
|
532 |
goto retResult; |
|
533 |
} |
|
7356 | 534 |
} |
535 |
if (__isFloatLike(aNumber)) { |
|
5954 | 536 |
dVal = __floatVal(aNumber); |
537 |
if (dVal != 0.0) { |
|
538 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
539 |
__qMKFLOAT(newFloat, dResult); |
|
540 |
RETURN ( newFloat ); |
|
541 |
} |
|
1197 | 542 |
} |
543 |
%}. |
|
544 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
5954 | 545 |
" |
546 |
No, you shalt not divide by zero |
|
547 |
" |
|
7141
033f2c26d8e6
replaced refs to DivisionByZeroSignal with ZeroDivide
Claus Gittinger <cg@exept.de>
parents:
6898
diff
changeset
|
548 |
^ ZeroDivide raiseRequestWith:thisContext. |
1197 | 549 |
]. |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
550 |
^ aNumber quotientFromShortFloat:self |
1197 | 551 |
! |
552 |
||
553 |
negated |
|
554 |
"return myself negated" |
|
555 |
||
556 |
%{ /* NOCONTEXT */ |
|
557 |
OBJ newFloat; |
|
558 |
float rslt = - __shortFloatVal(self); |
|
559 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
560 |
__qMKSFLOAT(newFloat, rslt); |
1197 | 561 |
RETURN ( newFloat ); |
5411 | 562 |
%}. |
563 |
^ 0.0 - self |
|
1197 | 564 |
|
3147 | 565 |
! |
566 |
||
567 |
uncheckedDivide:aNumber |
|
7356 | 568 |
"return the quotient of the receiver and the argument, aNumber. |
569 |
Do not check for divide by zero (return NaN or infinity). |
|
570 |
This operation is provided for emulators of other languages/semantics, |
|
571 |
where no exception is raised for these results (i.e. Java). |
|
572 |
Its only defined if the arguments type is the same as the receivers." |
|
3147 | 573 |
|
574 |
%{ /* NOCONTEXT */ |
|
575 |
||
576 |
OBJ newFloat; |
|
577 |
float result, val; |
|
578 |
double dResult, dVal; |
|
579 |
||
580 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 581 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
3147 | 582 |
retResult: |
7356 | 583 |
__qMKSFLOAT(newFloat, result); |
584 |
RETURN ( newFloat ); |
|
585 |
} |
|
586 |
if (__isShortFloat(aNumber)) { |
|
587 |
val = __shortFloatVal(aNumber); |
|
588 |
result = __shortFloatVal(self) / val; |
|
589 |
goto retResult; |
|
590 |
} |
|
591 |
if (__isFloatLike(aNumber)) { |
|
592 |
dVal = __floatVal(aNumber); |
|
593 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
594 |
__qMKFLOAT(newFloat, dResult); |
|
595 |
RETURN ( newFloat ); |
|
3147 | 596 |
} |
597 |
%}. |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
598 |
^ aNumber quotientFromShortFloat:self |
3147 | 599 |
|
600 |
" |
|
601 |
0.0 asShortFloat uncheckedDivide:0 |
|
602 |
1.0 asShortFloat uncheckedDivide:0.0 |
|
603 |
" |
|
1197 | 604 |
! ! |
605 |
||
5257 | 606 |
!ShortFloat methodsFor:'coercing & converting'! |
1197 | 607 |
|
608 |
asFloat |
|
609 |
"return a Float with same value as the receiver" |
|
610 |
||
611 |
%{ /* NOCONTEXT */ |
|
612 |
||
613 |
OBJ newFloat; |
|
614 |
double dVal = (double)__shortFloatVal(self); |
|
615 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
616 |
__qMKFLOAT(newFloat, dVal); |
1197 | 617 |
RETURN ( newFloat ); |
618 |
%} |
|
619 |
||
620 |
" |
|
1583 | 621 |
1.0 asShortFloat asFloat |
1197 | 622 |
" |
623 |
! |
|
624 |
||
625 |
asInteger |
|
626 |
"return an integer with same value - might truncate" |
|
627 |
||
628 |
%{ /* NOCONTEXT */ |
|
1670 | 629 |
float fVal; |
1197 | 630 |
|
1670 | 631 |
fVal = __shortFloatVal(self); |
632 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
3158 | 633 |
RETURN ( __MKSMALLINT( (INT)fVal) ); |
1197 | 634 |
} |
635 |
%}. |
|
636 |
^ super asInteger |
|
637 |
||
638 |
" |
|
639 |
12345.0 asShortFloat asInteger |
|
640 |
1e15 asShortFloat asInteger |
|
641 |
" |
|
642 |
! |
|
643 |
||
644 |
asShortFloat |
|
645 |
"return a ShortFloat with same value as the receiver - thats me" |
|
646 |
||
647 |
^ self |
|
1200 | 648 |
! |
649 |
||
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
650 |
asTrueFraction |
7356 | 651 |
"return a fraction or integer with the same value as the receiver" |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
652 |
|
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
653 |
^ self asFloat asTrueFraction |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
654 |
! |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
655 |
|
7356 | 656 |
coerce:aNumber |
657 |
"return aNumber converted into receivers type" |
|
658 |
||
659 |
^ aNumber asShortFloat |
|
660 |
! |
|
661 |
||
1200 | 662 |
generality |
663 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
664 |
||
665 |
^ 70 |
|
666 |
||
667 |
||
1197 | 668 |
! ! |
669 |
||
670 |
!ShortFloat methodsFor:'comparing'! |
|
671 |
||
672 |
< aNumber |
|
673 |
"return true, if the argument is greater" |
|
674 |
||
675 |
%{ /* NOCONTEXT */ |
|
676 |
||
677 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 678 |
RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false ); |
1197 | 679 |
} |
7356 | 680 |
if (aNumber != nil) { |
681 |
if (__qIsFloatLike(aNumber)) { |
|
682 |
RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false ); |
|
683 |
} |
|
684 |
if (__qIsShortFloat(aNumber)) { |
|
685 |
RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false ); |
|
686 |
} |
|
1197 | 687 |
} |
688 |
%}. |
|
689 |
^ aNumber lessFromShortFloat:self |
|
690 |
||
1200 | 691 |
" |
692 |
1.0 asShortFloat > (1/3) |
|
693 |
" |
|
1197 | 694 |
! |
695 |
||
696 |
<= aNumber |
|
697 |
"return true, if the argument is greater or equal" |
|
698 |
||
699 |
%{ /* NOCONTEXT */ |
|
700 |
||
701 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 702 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 703 |
} |
7356 | 704 |
if (aNumber != nil) { |
705 |
if (__qIsFloatLike(aNumber)) { |
|
706 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
|
707 |
} |
|
708 |
if (__qIsShortFloat(aNumber)) { |
|
709 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
|
710 |
} |
|
711 |
} |
|
1197 | 712 |
%}. |
713 |
^ self retry:#<= coercing:aNumber |
|
714 |
! |
|
715 |
||
716 |
= aNumber |
|
7356 | 717 |
"return true, if the argument represents the same numeric value |
718 |
as the receiver, false otherwise" |
|
1197 | 719 |
|
720 |
%{ /* NOCONTEXT */ |
|
721 |
||
722 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 723 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
724 |
} |
|
725 |
if (aNumber == nil) { |
|
726 |
RETURN (false); |
|
1197 | 727 |
} |
7356 | 728 |
if (__qIsFloatLike(aNumber)) { |
729 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
|
1197 | 730 |
} |
7356 | 731 |
if (__qIsShortFloat(aNumber)) { |
732 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
|
1197 | 733 |
} |
734 |
%}. |
|
7356 | 735 |
^ aNumber equalFromShortFloat:self |
1197 | 736 |
! |
737 |
||
738 |
> aNumber |
|
739 |
"return true, if the argument is less" |
|
740 |
||
741 |
%{ /* NOCONTEXT */ |
|
742 |
||
743 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 744 |
RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false ); |
1197 | 745 |
} |
7356 | 746 |
if (aNumber != nil) { |
747 |
if (__qIsFloatLike(aNumber)) { |
|
748 |
RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false ); |
|
749 |
} |
|
750 |
if (__qIsShortFloat(aNumber)) { |
|
751 |
RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false ); |
|
752 |
} |
|
1197 | 753 |
} |
754 |
%}. |
|
755 |
^ self retry:#> coercing:aNumber |
|
756 |
! |
|
757 |
||
758 |
>= aNumber |
|
759 |
"return true, if the argument is less or equal" |
|
760 |
||
761 |
%{ /* NOCONTEXT */ |
|
762 |
||
763 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 764 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 765 |
} |
7356 | 766 |
if (aNumber != nil) { |
767 |
if (__qIsFloatLike(aNumber)) { |
|
768 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
|
769 |
} |
|
770 |
if (__qIsShortFloat(aNumber)) { |
|
771 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
|
772 |
} |
|
1197 | 773 |
} |
774 |
%}. |
|
775 |
^ self retry:#>= coercing:aNumber |
|
776 |
! |
|
777 |
||
778 |
hash |
|
779 |
"return a number for hashing; redefined, since floats compare |
|
780 |
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
|
781 |
as 3 hash." |
1197 | 782 |
|
783 |
|i| |
|
784 |
||
785 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
786 |
i := self asInteger. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
787 |
self = i ifTrue:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
788 |
^ i hash |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
789 |
]. |
1197 | 790 |
]. |
791 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
792 |
^ self asFloat hash |
1197 | 793 |
|
794 |
" |
|
795 |
1.2345 hash |
|
796 |
1.2345 asShortFloat hash |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
797 |
1.0 hash |
1197 | 798 |
1.0 asShortFloat hash |
4593 | 799 |
0.5 asShortFloat hash |
800 |
0.25 asShortFloat hash |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
801 |
0.5 hash |
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
802 |
0.25 hash |
1197 | 803 |
" |
804 |
! |
|
805 |
||
806 |
~= aNumber |
|
807 |
"return true, if the arguments value are not equal" |
|
808 |
||
809 |
%{ /* NOCONTEXT */ |
|
810 |
||
7356 | 811 |
if (aNumber != nil) { |
812 |
if (__isSmallInteger(aNumber)) { |
|
813 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
|
814 |
} |
|
815 |
if (__qIsFloatLike(aNumber)) { |
|
816 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
|
817 |
} |
|
818 |
if (__qIsShortFloat(aNumber)) { |
|
819 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
|
820 |
} |
|
821 |
} else { |
|
822 |
RETURN ( true ); |
|
1197 | 823 |
} |
824 |
%}. |
|
7356 | 825 |
^ super ~= aNumber |
1197 | 826 |
! ! |
827 |
||
828 |
!ShortFloat methodsFor:'printing & storing'! |
|
829 |
||
830 |
printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
831 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
832 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
833 |
#printOn: as basic print mechanism." |
1197 | 834 |
|
835 |
%{ /* NOCONTEXT */ |
|
836 |
||
837 |
char buffer[64]; |
|
838 |
REGISTER char *cp; |
|
839 |
OBJ s; |
|
840 |
||
841 |
/* |
|
842 |
* actually only needed on sparc: since thisContext is |
|
843 |
* in a global register, which gets destroyed by printf, |
|
844 |
* manually save it here - very stupid ... |
|
845 |
*/ |
|
846 |
__BEGIN_PROTECT_REGISTERS__ |
|
847 |
||
848 |
#ifdef SYSV |
|
849 |
sprintf(buffer, "%.6lg", (double)__shortFloatVal(self)); |
|
850 |
#else |
|
851 |
sprintf(buffer, "%.6G", (double)__shortFloatVal(self)); |
|
852 |
#endif |
|
853 |
||
854 |
__END_PROTECT_REGISTERS__ |
|
855 |
||
856 |
/* |
|
857 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
|
858 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
|
859 |
*/ |
|
860 |
for (cp = buffer; *cp; cp++) { |
|
5317
6004c4c2a916
oops - printString was wrong when exponent was involved
Claus Gittinger <cg@exept.de>
parents:
5257
diff
changeset
|
861 |
if ((*cp == '.') || (*cp == 'e') || (*cp == 'E')) break; |
1197 | 862 |
} |
863 |
if (! *cp) { |
|
5317
6004c4c2a916
oops - printString was wrong when exponent was involved
Claus Gittinger <cg@exept.de>
parents:
5257
diff
changeset
|
864 |
*cp++ = '.'; |
6004c4c2a916
oops - printString was wrong when exponent was involved
Claus Gittinger <cg@exept.de>
parents:
5257
diff
changeset
|
865 |
*cp++ = '0'; |
6004c4c2a916
oops - printString was wrong when exponent was involved
Claus Gittinger <cg@exept.de>
parents:
5257
diff
changeset
|
866 |
*cp = '\0'; |
1197 | 867 |
} |
868 |
||
869 |
s = __MKSTRING(buffer COMMA_SND); |
|
870 |
if (s != nil) { |
|
5317
6004c4c2a916
oops - printString was wrong when exponent was involved
Claus Gittinger <cg@exept.de>
parents:
5257
diff
changeset
|
871 |
RETURN (s); |
1197 | 872 |
} |
873 |
%}. |
|
5432 | 874 |
^ self asFloat printString |
5718 | 875 |
! |
876 |
||
877 |
printfPrintString:formatString |
|
878 |
"non-portable: return a printed representation of the receiver |
|
879 |
as specified by formatString, which is defined by printf. |
|
880 |
If you use this, be aware, that specifying doubles differs on |
|
881 |
systems; on SYSV machines you have to give something like %lf, |
|
882 |
while on BSD systems the format string has to be %F. |
|
883 |
Also, the resulting string may not be longer than 255 bytes - |
|
884 |
since thats the (static) size of the buffer. |
|
885 |
This method is NONSTANDARD and may be removed without notice." |
|
886 |
||
887 |
%{ /* STACK: 400 */ |
|
888 |
char buffer[256]; |
|
889 |
OBJ s; |
|
890 |
||
891 |
if (__isString(formatString)) { |
|
892 |
sprintf(buffer, __stringVal(formatString), __shortFloatVal(self)); |
|
893 |
||
894 |
s = __MKSTRING(buffer COMMA_SND); |
|
895 |
if (s != nil) { |
|
896 |
RETURN (s); |
|
897 |
} |
|
898 |
} |
|
899 |
%}. |
|
900 |
self primitiveFailed |
|
901 |
||
902 |
"ShortFloat pi printfPrintString:'%%lg -> %lg'" |
|
903 |
"ShortFloat pi printfPrintString:'%%lf -> %lf'" |
|
904 |
"ShortFloat pi printfPrintString:'%%7.5lg -> %7.5lg'" |
|
905 |
"ShortFloat pi printfPrintString:'%%G -> %G'" |
|
906 |
"ShortFloat pi printfPrintString:'%%F -> %F'" |
|
907 |
"ShortFloat pi printfPrintString:'%%7.5G -> %7.5G'" |
|
908 |
"ShortFloat pi printfPrintString:'%%7.5F -> %7.5F'" |
|
1197 | 909 |
! ! |
910 |
||
3897 | 911 |
!ShortFloat methodsFor:'special access'! |
912 |
||
913 |
exponent |
|
914 |
"extract a normalized floats exponent. |
|
915 |
The returned value depends on the float-representation of |
|
916 |
the underlying machine and is therefore highly unportable. |
|
917 |
This is not for general use. |
|
918 |
This assumes that the mantissa is normalized to |
|
919 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
920 |
||
921 |
%{ /* NOCONTEXT */ |
|
922 |
||
923 |
double frexp(); |
|
924 |
INT exp; |
|
925 |
||
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
926 |
__threadErrno = 0; |
7356 | 927 |
#if defined(i386) && defined(__GNUC__) |
928 |
frexpf( __shortFloatVal(self), &exp); |
|
929 |
#else |
|
930 |
frexp( (double)(__shortFloatVal(self)), &exp); |
|
931 |
#endif |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
932 |
if (__threadErrno == 0) { |
7356 | 933 |
RETURN (__MKSMALLINT(exp)); |
3897 | 934 |
} |
935 |
%}. |
|
7387 | 936 |
^ super exponent |
3897 | 937 |
|
938 |
" |
|
7356 | 939 |
4.0 asShortFloat exponent |
940 |
2.0 asShortFloat exponent |
|
3897 | 941 |
1.0 asShortFloat exponent |
942 |
0.5 asShortFloat exponent |
|
943 |
0.25 asShortFloat exponent |
|
944 |
0.00000011111 asShortFloat exponent |
|
945 |
" |
|
946 |
! |
|
947 |
||
948 |
mantissa |
|
949 |
"extract a normalized floats mantissa. |
|
950 |
The returned value depends on the float-representation of |
|
951 |
the underlying machine and is therefore highly unportable. |
|
952 |
This is not for general use. |
|
953 |
This assumes that the mantissa is normalized to |
|
954 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
955 |
||
956 |
%{ /* NOCONTEXT */ |
|
7356 | 957 |
INT exp; |
3897 | 958 |
|
7356 | 959 |
#if defined(i386) && defined(__GNUC__) |
960 |
float frac; |
|
961 |
||
962 |
__threadErrno = 0; |
|
963 |
frac = frexpf( (double)__shortFloatVal(self), &exp); |
|
964 |
if (__threadErrno == 0) { |
|
965 |
RETURN (__MKSFLOAT(frac)); |
|
966 |
} |
|
967 |
#else |
|
3897 | 968 |
double frexp(); |
969 |
double frac; |
|
970 |
||
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
971 |
__threadErrno = 0; |
3897 | 972 |
frac = frexp( (double)(__shortFloatVal(self)), &exp); |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
973 |
if (__threadErrno == 0) { |
7356 | 974 |
RETURN (__MKFLOAT(frac)); |
3897 | 975 |
} |
7356 | 976 |
#endif |
3897 | 977 |
%}. |
978 |
^ self primitiveFailed |
|
979 |
||
980 |
" |
|
981 |
1.0 asShortFloat exponent |
|
982 |
1.0 asShortFloat mantissa |
|
983 |
||
984 |
0.5 asShortFloat exponent |
|
985 |
0.5 asShortFloat mantissa |
|
986 |
||
987 |
0.25 asShortFloat exponent |
|
988 |
0.25 asShortFloat mantissa |
|
989 |
||
990 |
0.00000011111 asShortFloat exponent |
|
991 |
0.00000011111 asShortFloat mantissa |
|
992 |
" |
|
993 |
! ! |
|
994 |
||
1197 | 995 |
!ShortFloat methodsFor:'testing'! |
996 |
||
3147 | 997 |
isFinite |
998 |
"return true, if the receiver is a finite float |
|
999 |
i.e. not NaN and not infinite." |
|
1000 |
||
1001 |
%{ /* NOCONTEXT */ |
|
7356 | 1002 |
#if defined(i386) && defined(__GNUC__) |
1003 |
if (finitef(__shortFloatVal(self))) { RETURN (true); } |
|
1004 |
#else |
|
3147 | 1005 |
double dV = (double) __shortFloatVal(self); |
1006 |
||
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1007 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1008 |
* notice: on machines which do not provide |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1009 |
* a finite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
1010 |
* 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
|
1011 |
*/ |
3147 | 1012 |
if (finite(dV)) { RETURN (true); } |
7356 | 1013 |
#endif |
3147 | 1014 |
%}. |
1015 |
^false |
|
1016 |
||
1017 |
" |
|
7356 | 1018 |
1.0 asShortFloat isFinite |
1019 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
1020 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
3147 | 1021 |
" |
1022 |
! |
|
1023 |
||
2387 | 1024 |
isNaN |
1025 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
1026 |
These are not created by ST/X float operations (they raise an exception); |
|
1027 |
however, inline C-code could produce them ..." |
|
1028 |
||
1029 |
%{ /* NOCONTEXT */ |
|
7408 | 1030 |
#if defined(__GNUC__) |
7356 | 1031 |
if (isnanf(__shortFloatVal(self))) { RETURN (true); } |
7408 | 1032 |
# else |
2387 | 1033 |
|
3139 | 1034 |
double dV = (double)(__shortFloatVal(self)); |
1035 |
||
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1036 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1037 |
* notice: on machines which do not provide |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1038 |
* a finite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
1039 |
* 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
|
1040 |
*/ |
3147 | 1041 |
if (isnan(dV)) { RETURN (true); } |
7356 | 1042 |
#endif /* i386 */ |
2392 | 1043 |
%}. |
1044 |
^ false |
|
2387 | 1045 |
|
3147 | 1046 |
" |
7356 | 1047 |
1.0 asShortFloat isNaN |
1048 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
|
3147 | 1049 |
" |
2387 | 1050 |
! |
1051 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1052 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1053 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1054 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1055 |
%{ |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1056 |
#if defined(i386) && defined(linux) |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1057 |
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
|
1058 |
#endif |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1059 |
%}. |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1060 |
^ super isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1061 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1062 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1063 |
0.0 asShortFloat isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1064 |
-0.0 asShortFloat isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1065 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1066 |
! |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1067 |
|
1197 | 1068 |
negative |
1069 |
"return true if the receiver is less than zero" |
|
1070 |
||
1071 |
%{ /* NOCONTEXT */ |
|
1072 |
||
1073 |
RETURN ( (__shortFloatVal(self) < 0.0) ? true : false ); |
|
5411 | 1074 |
%}. |
1197 | 1075 |
! |
1076 |
||
5357 | 1077 |
numberOfBits |
1078 |
"return the size (in bits) of the real; |
|
1079 |
typically, 32 is returned here, |
|
1080 |
but who knows ..." |
|
1081 |
||
1082 |
%{ /* NOCONTEXT */ |
|
1083 |
||
1084 |
RETURN (__MKSMALLINT (sizeof(float) * 8)); |
|
1085 |
%} |
|
1086 |
||
1087 |
" |
|
1088 |
1.2 numberOfBits |
|
1089 |
1.2 asShortFloat numberOfBits |
|
1090 |
" |
|
1091 |
||
1092 |
! |
|
1093 |
||
1197 | 1094 |
positive |
1095 |
"return true if the receiver is greater or equal to zero" |
|
1096 |
||
1097 |
%{ /* NOCONTEXT */ |
|
1098 |
||
1099 |
RETURN ( (__shortFloatVal(self) >= 0.0) ? true : false ); |
|
1100 |
%} |
|
1880 | 1101 |
! |
1102 |
||
1103 |
strictlyPositive |
|
1104 |
"return true if the receiver is greater than zero" |
|
1105 |
||
1106 |
%{ /* NOCONTEXT */ |
|
1107 |
||
1108 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
1109 |
%} |
|
1197 | 1110 |
! ! |
1111 |
||
5552 | 1112 |
!ShortFloat methodsFor:'truncation & rounding'! |
3897 | 1113 |
|
7356 | 1114 |
ceiling |
1115 |
"return the smallest integer which is greater or equal to the receiver." |
|
1116 |
||
1117 |
|val| |
|
1118 |
||
1119 |
%{ |
|
1120 |
float fVal; |
|
1121 |
||
1122 |
#if defined(i386) && defined(__GNUC__) |
|
1123 |
fVal = ceilf(__shortFloatVal(self)); |
|
1124 |
#else |
|
1125 |
fVal = (float)(ceil((double)__shortFloatVal(self))); |
|
1126 |
#endif |
|
1127 |
/* |
|
1128 |
* ST-80 (and X3J20) returns integer. |
|
1129 |
*/ |
|
1130 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
1131 |
RETURN ( __MKSMALLINT( (INT) fVal ) ); |
|
1132 |
} |
|
1133 |
__qMKSFLOAT(val, fVal); |
|
1134 |
%}. |
|
1135 |
^ val asInteger |
|
1136 |
||
1137 |
" |
|
1138 |
0.5 asShortFloat ceiling |
|
1139 |
-0.5 asShortFloat ceiling |
|
1140 |
" |
|
1141 |
! |
|
1142 |
||
1143 |
ceilingAsFloat |
|
1144 |
"return the smallest integer-valued float greater or equal to the receiver. |
|
1145 |
This is much like #ceiling, but avoids a (possibly expensive) conversion |
|
1146 |
of the result to an integer. |
|
1147 |
It may be useful, if the result is to be further used in another float-operation." |
|
1148 |
||
1149 |
%{ /* NOCONTEXT */ |
|
1150 |
float fVal; |
|
1151 |
OBJ v; |
|
1152 |
||
1153 |
#if defined(i386) && defined(__GNUC__) |
|
1154 |
fVal = ceilf(__shortFloatVal(self)); |
|
1155 |
#else |
|
1156 |
fVal = (float) ceil((double)__shortFloatVal(self)); |
|
1157 |
#endif |
|
1158 |
__qMKSFLOAT(v, fVal); |
|
1159 |
RETURN (v); |
|
1160 |
%} |
|
1161 |
" |
|
1162 |
0.5 asShortFloat ceilingAsFloat |
|
1163 |
-0.5 asShortFloat ceilingAsFloat |
|
1164 |
-1.5 asShortFloat ceilingAsFloat |
|
1165 |
" |
|
1166 |
! |
|
1167 |
||
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1168 |
floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1169 |
"return the integer nearest the receiver towards negative infinity." |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1170 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1171 |
|val| |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1172 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1173 |
%{ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1174 |
float fVal; |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1175 |
|
7356 | 1176 |
#if defined(i386) && defined(__GNUC__) |
1177 |
fVal = floorf(__shortFloatVal(self)); |
|
1178 |
#else |
|
1179 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
1180 |
#endif |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1181 |
/* |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1182 |
* ST-80 (and X3J20) returns integer. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1183 |
*/ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1184 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
7356 | 1185 |
RETURN ( __MKSMALLINT( (INT) fVal ) ); |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1186 |
} |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1187 |
__qMKSFLOAT(val, fVal); |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1188 |
%}. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1189 |
^ val asInteger |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1190 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1191 |
" |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1192 |
0.5 asShortFloat floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1193 |
-0.5 asShortFloat floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1194 |
" |
7356 | 1195 |
! |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1196 |
|
7356 | 1197 |
floorAsFloat |
1198 |
"return the float which represents the next lower |
|
1199 |
integer nearest the receiver towards negative infinity. |
|
1200 |
Much like floor, but returns a float result - useful if the result |
|
1201 |
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
|
1202 |
|
7356 | 1203 |
%{ /* NOCONTEXT */ |
1204 |
float fVal; |
|
1205 |
OBJ v; |
|
1206 |
||
1207 |
#if defined(i386) && defined(__GNUC__) |
|
1208 |
fVal = floorf(__shortFloatVal(self)); |
|
1209 |
#else |
|
1210 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
1211 |
#endif |
|
1212 |
__qMKSFLOAT(v, fVal); |
|
1213 |
RETURN ( v ); |
|
1214 |
%} |
|
1215 |
||
1216 |
" |
|
1217 |
0.5 asShortFloat floorAsFloat |
|
1218 |
-0.5 asShortFloat floorAsFloat |
|
1219 |
" |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1220 |
! |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1221 |
|
3897 | 1222 |
fractionPart |
1223 |
"extract the after-decimal fraction part. |
|
7356 | 1224 |
such that (self truncated + self fractionPart) = self" |
3897 | 1225 |
|
1226 |
%{ /* NOCONTEXT */ |
|
1227 |
||
1228 |
double modf(); |
|
1229 |
double frac, trunc; |
|
1230 |
||
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1231 |
__threadErrno = 0; |
3897 | 1232 |
frac = modf((double)(__shortFloatVal(self)), &trunc); |
7403 | 1233 |
if (! isnan(frac)) { |
1234 |
if (__threadErrno == 0) { |
|
1235 |
RETURN (__MKSFLOAT((float)frac)); |
|
1236 |
} |
|
3897 | 1237 |
} |
1238 |
%}. |
|
7403 | 1239 |
^ self class |
1240 |
raise:#domainErrorSignal |
|
1241 |
receiver:self |
|
1242 |
selector:#fractionPart |
|
1243 |
arguments:#() |
|
1244 |
errorString:'bad receiver in fractionPart' |
|
3897 | 1245 |
|
1246 |
" |
|
7356 | 1247 |
1.6 asShortFloat fractionPart + 1.6 asShortFloat truncated |
1248 |
-1.6 asShortFloat fractionPart + -1.6 asShortFloat truncated |
|
1249 |
||
3897 | 1250 |
1.0 asShortFloat fractionalPart |
1251 |
0.5 asShortFloat fractionalPart |
|
1252 |
0.25 asShortFloat fractionalPart |
|
1253 |
3.14159 asShortFloat fractionalPart |
|
1254 |
12345673.14159 asShortFloat fractionalPart |
|
1255 |
123456731231231231.14159 asShortFloat fractionalPart |
|
1256 |
" |
|
7356 | 1257 |
! |
3897 | 1258 |
|
7356 | 1259 |
rounded |
1260 |
"return the receiver rounded to the nearest integer" |
|
1261 |
||
1262 |
|val| |
|
1263 |
||
1264 |
%{ |
|
1265 |
float fVal; |
|
1266 |
||
1267 |
fVal = __shortFloatVal(self); |
|
1268 |
#if defined(i386) && defined(__GNUC__) |
|
1269 |
if (fVal < 0.0) { |
|
1270 |
fVal = ceilf(fVal - (float)0.5); |
|
1271 |
} else { |
|
1272 |
fVal = floorf(fVal + (float)0.5); |
|
1273 |
} |
|
1274 |
#else |
|
1275 |
if (fVal < 0.0) { |
|
1276 |
fVal = (float)ceil((double)fVal - 0.5); |
|
1277 |
} else { |
|
1278 |
fVal = (float)floor((double)fVal + 0.5); |
|
1279 |
} |
|
1280 |
#endif |
|
1281 |
/* |
|
1282 |
* ST-80 (and X3J20) returns integer. |
|
1283 |
*/ |
|
1284 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
1285 |
RETURN ( __MKSMALLINT( (INT) fVal ) ); |
|
1286 |
} |
|
1287 |
__qMKSFLOAT(val, fVal); |
|
1288 |
%}. |
|
1289 |
^ val asInteger |
|
1290 |
||
1291 |
" |
|
1292 |
0.4 asShortFloat rounded |
|
1293 |
0.5 asShortFloat rounded |
|
1294 |
0.6 asShortFloat rounded |
|
1295 |
-0.4 asShortFloat rounded |
|
1296 |
-0.5 asShortFloat rounded |
|
1297 |
-0.6 asShortFloat rounded |
|
1298 |
" |
|
1299 |
! |
|
1300 |
||
1301 |
roundedAsFloat |
|
1302 |
"return the receiver rounded to the nearest integer as a float. |
|
1303 |
This is much like #rounded, but avoids a (possibly expensive) conversion |
|
1304 |
of the result to an integer. |
|
1305 |
It may be useful, if the result is to be further used in another float-operation." |
|
1306 |
||
1307 |
%{ /* NOCONTEXT */ |
|
1308 |
float fVal; |
|
1309 |
OBJ v; |
|
1310 |
||
1311 |
fVal = __shortFloatVal(self); |
|
1312 |
#if defined(i386) && defined(__GNUC__) |
|
1313 |
if (fVal < 0.0) { |
|
1314 |
fVal = ceilf(fVal - (float)0.5); |
|
1315 |
} else { |
|
1316 |
fVal = floorf(fVal + (float)0.5); |
|
1317 |
} |
|
1318 |
#else |
|
1319 |
if (fVal < 0.0) { |
|
1320 |
fVal = (float)ceil((double)fVal - 0.5); |
|
1321 |
} else { |
|
1322 |
fVal = (float)floor((double)fVal + 0.5); |
|
1323 |
} |
|
1324 |
#endif |
|
1325 |
__qMKSFLOAT(v, fVal); |
|
1326 |
RETURN (v); |
|
1327 |
%} |
|
1328 |
||
1329 |
" |
|
1330 |
0.4 asShortFloat rounded |
|
1331 |
0.5 asShortFloat rounded |
|
1332 |
0.6 asShortFloat rounded |
|
1333 |
-0.4 asShortFloat rounded |
|
1334 |
-0.5 asShortFloat rounded |
|
1335 |
-0.6 asShortFloat rounded |
|
1336 |
||
1337 |
0.4 asShortFloat roundedAsFloat |
|
1338 |
0.5 asShortFloat roundedAsFloat |
|
1339 |
0.6 asShortFloat roundedAsFloat |
|
1340 |
-0.4 asShortFloat roundedAsFloat |
|
1341 |
-0.5 asShortFloat roundedAsFloat |
|
1342 |
-0.6 asShortFloat roundedAsFloat |
|
1343 |
" |
|
1344 |
! |
|
1345 |
||
1346 |
truncated |
|
1347 |
"return the receiver truncated towards zero as an integer" |
|
1348 |
||
1349 |
|val| |
|
1350 |
||
1351 |
%{ |
|
1352 |
float fVal; |
|
1353 |
||
1354 |
fVal = __shortFloatVal(self); |
|
1355 |
#if defined(i386) && defined(__GNUC__) |
|
1356 |
if (fVal < 0.0) { |
|
1357 |
fVal = ceilf(fVal); |
|
1358 |
} else { |
|
1359 |
fVal = floorf(fVal); |
|
1360 |
} |
|
1361 |
#else |
|
1362 |
if (fVal < 0.0) { |
|
1363 |
fVal = (float)ceil((double)fVal); |
|
1364 |
} else { |
|
1365 |
fVal = (float)floor((double)fVal); |
|
1366 |
} |
|
1367 |
#endif |
|
1368 |
/* |
|
1369 |
* ST-80 (and X3J20) returns integer. |
|
1370 |
*/ |
|
1371 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
1372 |
RETURN ( __MKSMALLINT( (INT) fVal ) ); |
|
1373 |
} |
|
1374 |
__qMKSFLOAT(val, fVal); |
|
1375 |
%}. |
|
1376 |
^ val asInteger |
|
1377 |
||
1378 |
" |
|
1379 |
0.5 asShortFloat truncated |
|
1380 |
-0.5 asShortFloat truncated |
|
1381 |
0.5 asShortFloat truncatedAsFloat |
|
1382 |
-0.5 asShortFloat truncatedAsFloat |
|
1383 |
" |
|
1384 |
! |
|
1385 |
||
1386 |
truncatedAsFloat |
|
1387 |
"return the receiver truncated towards zero as a float. |
|
1388 |
This is much like #truncated, but avoids a (possibly expensive) conversion |
|
1389 |
of the result to an integer. |
|
1390 |
It may be useful, if the result is to be further used in another |
|
1391 |
float-operation." |
|
1392 |
||
1393 |
%{ /* NOCONTEXT */ |
|
1394 |
float fVal; |
|
1395 |
OBJ v; |
|
1396 |
||
1397 |
fVal = __shortFloatVal(self); |
|
1398 |
#if defined(i386) && defined(__GNUC__) |
|
1399 |
if (fVal < 0.0) { |
|
1400 |
fVal = ceilf(fVal); |
|
1401 |
} else { |
|
1402 |
fVal = floorf(fVal); |
|
1403 |
} |
|
1404 |
#else |
|
1405 |
if (fVal < 0.0) { |
|
1406 |
fVal = (float)ceil((double)fVal); |
|
1407 |
} else { |
|
1408 |
fVal = (float)floor((double)fVal); |
|
1409 |
} |
|
1410 |
#endif |
|
1411 |
__qMKSFLOAT(v, fVal); |
|
1412 |
RETURN (v); |
|
1413 |
%} |
|
1414 |
||
1415 |
" |
|
1416 |
0.5 asShortFloat truncated |
|
1417 |
-0.5 asShortFloat truncated |
|
1418 |
0.5 asShortFloat truncatedAsFloat |
|
1419 |
-0.5 asShortFloat truncatedAsFloat |
|
1420 |
" |
|
3897 | 1421 |
! ! |
1422 |
||
1880 | 1423 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 1424 |
|
1425 |
version |
|
7408 | 1426 |
^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.78 2003-06-17 16:10:28 cg Exp $' |
1197 | 1427 |
! ! |