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