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