author | Claus Gittinger <cg@exept.de> |
Thu, 07 May 1998 20:02:53 +0200 | |
changeset 3422 | 09c13ab58bb2 |
parent 3414 | d9aefd18c10d |
child 3427 | ba90490c94b6 |
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 |
||
15 |
LimitedPrecisionReal variableByteSubclass:#ShortFloat |
|
16 |
instanceVariableNames:'' |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
17 |
classVariableNames:'' |
1197 | 18 |
poolDictionaries:'' |
19 |
category:'Magnitude-Numbers' |
|
20 |
! |
|
21 |
||
2394 | 22 |
!ShortFloat primitiveDefinitions! |
23 |
%{ |
|
24 |
||
25 |
/* |
|
26 |
* includes, defines, structure definitions |
|
27 |
* and typedefs come here. |
|
28 |
*/ |
|
29 |
||
30 |
#if defined (_AIX) |
|
31 |
# include <float.h> |
|
32 |
#endif |
|
2395 | 33 |
#if defined(IRIX) |
34 |
# include <nan.h> |
|
35 |
#endif |
|
2397 | 36 |
#if defined(LINUX) |
37 |
# include <nan.h> |
|
38 |
#endif |
|
2400 | 39 |
#if defined(solaris) || defined(sunos) |
2398 | 40 |
# include <nan.h> |
41 |
#endif |
|
2394 | 42 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
43 |
#ifdef WIN32 |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
44 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
45 |
* no finite(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
46 |
* no isnan(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
47 |
*/ |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
48 |
# ifndef finite |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
49 |
# define finite(x) 1 |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
50 |
# endif |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
51 |
# ifndef isnan |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
52 |
# define isnan(x) 0 |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
53 |
# endif |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
54 |
#endif |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
55 |
|
3414 | 56 |
#ifdef realIX |
57 |
/* |
|
58 |
* no finite(x) |
|
59 |
*/ |
|
60 |
# ifndef finite |
|
61 |
# define finite(x) 1 |
|
62 |
# endif |
|
63 |
#endif |
|
64 |
||
2394 | 65 |
%} |
66 |
! ! |
|
67 |
||
1880 | 68 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 69 |
|
70 |
copyright |
|
71 |
" |
|
72 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3158 | 73 |
All Rights Reserved |
1197 | 74 |
|
75 |
This software is furnished under a license and may be used |
|
76 |
only in accordance with the terms of that license and with the |
|
77 |
inclusion of the above copyright notice. This software may not |
|
78 |
be provided or otherwise made available to, or used by, any |
|
79 |
other person. No title to or ownership of the software is |
|
80 |
hereby transferred. |
|
81 |
" |
|
82 |
||
83 |
||
84 |
! |
|
85 |
||
86 |
documentation |
|
87 |
" |
|
88 |
ShortFloats represent rational numbers with limited precision. In ST/X, Float uses |
|
89 |
the underlying C-compilers double implementation, while ShortFloats are |
|
90 |
mapped onto C-floats. |
|
91 |
Therefore instances of Float are usually represented by the 8-byte IEE |
|
92 |
double precision float format, while ShortFloats use 4byte IEE format. |
|
93 |
(but there is no guaranty). |
|
94 |
||
95 |
Notice, that ST/X Floats are what Doubles are in ST-80 and ShortFloats are |
|
96 |
ST-80's Floats respectively. |
|
97 |
This may change in one of the next versions (at least on machines, which |
|
98 |
provide different float and double types in their C-compiler. |
|
99 |
||
100 |
WARNING: |
|
101 |
The layout of shortFloat instances is known by the runtime system and the compiler; |
|
102 |
you may not add instance variables here. |
|
103 |
Also, subclassing is complicated by the fact, that the VM creates floats/shortFloats, |
|
104 |
and does its float-checks by an identity compare with the ShortFloat-class. |
|
105 |
(i.e. your subclasses instances may not be recognized as float-like objects, |
|
106 |
thus mixed mode arithmetic will always coerce them, effectively slowing things down). |
|
107 |
||
108 |
This may be changed, to use a flag bit in the class. |
|
1295 | 109 |
|
110 |
[author:] |
|
3158 | 111 |
Claus Gittinger |
1556 | 112 |
|
113 |
[see also:] |
|
3158 | 114 |
Number |
115 |
Float Fraction FixedPoint Integer |
|
1197 | 116 |
" |
117 |
||
118 |
! ! |
|
119 |
||
1880 | 120 |
!ShortFloat class methodsFor:'instance creation'! |
1207 | 121 |
|
122 |
basicNew |
|
123 |
"return a new shortFloat - here we return 0.0 |
|
124 |
- shortFloats are usually NOT created this way ... |
|
125 |
Its implemented here to allow things like binary store & load |
|
126 |
of shortFloats. (but even this support will go away eventually, its not |
|
127 |
a good idea to store the bits of a float - the reader might have a |
|
128 |
totally different representation - so floats will eventually be |
|
129 |
binary stored in a device independent format." |
|
130 |
||
131 |
%{ /* NOCONTEXT */ |
|
132 |
OBJ newFloat; |
|
133 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
134 |
__qMKSFLOAT(newFloat, 0.0); |
1207 | 135 |
RETURN (newFloat); |
136 |
%} |
|
3158 | 137 |
! |
138 |
||
139 |
readFrom:aStringOrStream onError:exceptionBlock |
|
140 |
"read a shortFloat from a string" |
|
141 |
||
142 |
|num| |
|
143 |
||
144 |
num := super readFrom:aStringOrStream onError:nil. |
|
145 |
num isNil ifTrue:[ |
|
146 |
^ exceptionBlock value |
|
147 |
]. |
|
148 |
^ num asShortFloat |
|
149 |
||
150 |
" |
|
151 |
ShortFloat readFrom:'0.1' |
|
152 |
ShortFloat readFrom:'0' |
|
153 |
" |
|
154 |
||
155 |
"Modified: / 7.1.1998 / 16:17:59 / cg" |
|
1207 | 156 |
! ! |
157 |
||
1880 | 158 |
!ShortFloat class methodsFor:'constants'! |
1197 | 159 |
|
160 |
pi |
|
1260 | 161 |
"return the constant pi as ShortFloat" |
1197 | 162 |
|
163 |
^ 3.14159 asShortFloat |
|
164 |
||
1260 | 165 |
"Modified: 23.4.1996 / 09:26:31 / cg" |
1197 | 166 |
! |
167 |
||
168 |
unity |
|
1260 | 169 |
"return the neutral element for multiplication (1.0) as ShortFloat" |
1197 | 170 |
|
171 |
^ 1.0 asShortFloat |
|
172 |
||
1260 | 173 |
"Modified: 23.4.1996 / 09:26:51 / cg" |
1197 | 174 |
! |
175 |
||
176 |
zero |
|
1260 | 177 |
"return the neutral element for addition (0.0) as ShortFloat" |
1197 | 178 |
|
179 |
^ 0.0 asShortFloat |
|
180 |
||
1260 | 181 |
"Modified: 23.4.1996 / 09:26:45 / cg" |
1197 | 182 |
! ! |
183 |
||
1880 | 184 |
!ShortFloat class methodsFor:'queries'! |
1197 | 185 |
|
186 |
isBuiltInClass |
|
1264 | 187 |
"return true if this class is known by the run-time-system. |
188 |
Here, true is returned for myself, false for subclasses." |
|
1197 | 189 |
|
190 |
^ self == ShortFloat |
|
191 |
||
1264 | 192 |
"Modified: 23.4.1996 / 16:00:23 / cg" |
1197 | 193 |
! ! |
194 |
||
195 |
!ShortFloat methodsFor:'arithmetic'! |
|
196 |
||
197 |
* aNumber |
|
198 |
"return the product of the receiver and the argument, aNumber" |
|
199 |
||
200 |
%{ /* NOCONTEXT */ |
|
201 |
||
202 |
OBJ newFloat; |
|
203 |
float result; |
|
204 |
double dResult; |
|
205 |
||
206 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 207 |
result = __shortFloatVal(self) * (float)(__intVal(aNumber)); |
1197 | 208 |
retResult: |
3158 | 209 |
__qMKSFLOAT(newFloat, result); |
210 |
RETURN ( newFloat ); |
|
1197 | 211 |
} |
212 |
if (__isShortFloat(aNumber)) { |
|
3158 | 213 |
result = __shortFloatVal(self) * __shortFloatVal(aNumber); |
214 |
goto retResult; |
|
1197 | 215 |
} |
216 |
if (__isFloatLike(aNumber)) { |
|
3158 | 217 |
dResult = (double) __shortFloatVal(self)* __floatVal(aNumber); |
218 |
__qMKFLOAT(newFloat, dResult); |
|
219 |
RETURN ( newFloat ); |
|
1197 | 220 |
|
221 |
} |
|
222 |
%}. |
|
223 |
^ aNumber productFromShortFloat:self |
|
224 |
! |
|
225 |
||
226 |
+ aNumber |
|
227 |
"return the sum of the receiver and the argument, aNumber" |
|
228 |
||
229 |
%{ /* NOCONTEXT */ |
|
230 |
||
231 |
OBJ newFloat; |
|
232 |
float result; |
|
233 |
double dResult; |
|
234 |
||
235 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 236 |
result = __shortFloatVal(self) + (float)(__intVal(aNumber)); |
1197 | 237 |
retResult: |
3158 | 238 |
__qMKSFLOAT(newFloat, result); |
239 |
RETURN ( newFloat ); |
|
1197 | 240 |
} |
241 |
if (__isShortFloat(aNumber)) { |
|
3158 | 242 |
result = __shortFloatVal(self) + __shortFloatVal(aNumber); |
243 |
goto retResult; |
|
1197 | 244 |
} |
245 |
if (__isFloatLike(aNumber)) { |
|
3158 | 246 |
dResult = (double) __shortFloatVal(self) + __floatVal(aNumber); |
247 |
__qMKFLOAT(newFloat, dResult); |
|
248 |
RETURN ( newFloat ); |
|
1197 | 249 |
|
250 |
} |
|
251 |
%}. |
|
252 |
^ aNumber sumFromShortFloat:self |
|
253 |
! |
|
254 |
||
255 |
- aNumber |
|
256 |
"return the difference of the receiver and the argument, aNumber" |
|
257 |
||
258 |
%{ /* NOCONTEXT */ |
|
259 |
||
260 |
OBJ newFloat; |
|
261 |
float result; |
|
262 |
double dResult; |
|
263 |
||
264 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 265 |
result = __shortFloatVal(self) - (float)(__intVal(aNumber)); |
1197 | 266 |
retResult: |
3158 | 267 |
__qMKSFLOAT(newFloat, result); |
268 |
RETURN ( newFloat ); |
|
1197 | 269 |
} |
270 |
if (__isShortFloat(aNumber)) { |
|
3158 | 271 |
result = __shortFloatVal(self) - __shortFloatVal(aNumber); |
272 |
goto retResult; |
|
1197 | 273 |
} |
274 |
if (__isFloatLike(aNumber)) { |
|
3158 | 275 |
dResult = (double) __shortFloatVal(self) - __floatVal(aNumber); |
276 |
__qMKFLOAT(newFloat, dResult); |
|
277 |
RETURN ( newFloat ); |
|
1197 | 278 |
|
279 |
} |
|
280 |
%}. |
|
281 |
^ aNumber differenceFromShortFloat:self |
|
282 |
! |
|
283 |
||
284 |
/ aNumber |
|
285 |
"return the quotient of the receiver and the argument, aNumber" |
|
286 |
||
287 |
%{ /* NOCONTEXT */ |
|
288 |
||
289 |
OBJ newFloat; |
|
290 |
float result, val; |
|
291 |
double dResult, dVal; |
|
292 |
||
293 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 294 |
if (aNumber != __MKSMALLINT(0)) { |
295 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
1197 | 296 |
retResult: |
3158 | 297 |
__qMKSFLOAT(newFloat, result); |
298 |
RETURN ( newFloat ); |
|
299 |
} |
|
1197 | 300 |
} |
301 |
if (__isShortFloat(aNumber)) { |
|
3158 | 302 |
val = __shortFloatVal(aNumber); |
303 |
if (val != 0.0) { |
|
304 |
result = __shortFloatVal(self) / val; |
|
305 |
goto retResult; |
|
306 |
} |
|
1197 | 307 |
} |
308 |
if (__isFloatLike(aNumber)) { |
|
3158 | 309 |
dVal = __floatVal(aNumber); |
310 |
if (dVal != 0.0) { |
|
311 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
312 |
__qMKFLOAT(newFloat, dResult); |
|
313 |
} |
|
314 |
RETURN ( newFloat ); |
|
1197 | 315 |
|
316 |
} |
|
317 |
%}. |
|
318 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
3158 | 319 |
" |
320 |
No, you shalt not divide by zero |
|
321 |
" |
|
322 |
^ DivisionByZeroSignal raise. |
|
1197 | 323 |
]. |
324 |
^ aNumber quotientFromFloat:self |
|
325 |
||
326 |
! |
|
327 |
||
328 |
negated |
|
329 |
"return myself negated" |
|
330 |
||
331 |
%{ /* NOCONTEXT */ |
|
332 |
OBJ newFloat; |
|
333 |
float rslt = - __shortFloatVal(self); |
|
334 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
335 |
__qMKSFLOAT(newFloat, rslt); |
1197 | 336 |
RETURN ( newFloat ); |
337 |
%} |
|
338 |
||
3147 | 339 |
! |
340 |
||
341 |
uncheckedDivide:aNumber |
|
342 |
"return the quotient of the receiver and the argument, aNumber |
|
343 |
Do not check for divide by zero (return NaN or infinity)" |
|
344 |
||
345 |
%{ /* NOCONTEXT */ |
|
346 |
||
347 |
OBJ newFloat; |
|
348 |
float result, val; |
|
349 |
double dResult, dVal; |
|
350 |
||
351 |
if (__isSmallInteger(aNumber)) { |
|
352 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
353 |
retResult: |
|
354 |
__qMKSFLOAT(newFloat, result); |
|
355 |
RETURN ( newFloat ); |
|
356 |
} |
|
357 |
if (__isShortFloat(aNumber)) { |
|
358 |
val = __shortFloatVal(aNumber); |
|
359 |
result = __shortFloatVal(self) / val; |
|
360 |
goto retResult; |
|
361 |
} |
|
362 |
if (__isFloatLike(aNumber)) { |
|
363 |
dVal = __floatVal(aNumber); |
|
364 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
365 |
__qMKFLOAT(newFloat, dResult); |
|
366 |
RETURN ( newFloat ); |
|
367 |
} |
|
368 |
%}. |
|
369 |
^ aNumber quotientFromFloat:self |
|
370 |
||
371 |
" |
|
372 |
0.0 asShortFloat uncheckedDivide:0 |
|
373 |
1.0 asShortFloat uncheckedDivide:0.0 |
|
374 |
" |
|
375 |
||
1197 | 376 |
! ! |
377 |
||
378 |
!ShortFloat methodsFor:'coercion and converting'! |
|
379 |
||
3014 | 380 |
asDouble |
381 |
"ST80 compatibility - return a Float with same value as the receiver" |
|
382 |
||
383 |
^ self asFloat |
|
384 |
! |
|
385 |
||
1197 | 386 |
asFloat |
387 |
"return a Float with same value as the receiver" |
|
388 |
||
389 |
%{ /* NOCONTEXT */ |
|
390 |
||
391 |
OBJ newFloat; |
|
392 |
double dVal = (double)__shortFloatVal(self); |
|
393 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
394 |
__qMKFLOAT(newFloat, dVal); |
1197 | 395 |
RETURN ( newFloat ); |
396 |
%} |
|
397 |
||
398 |
" |
|
1583 | 399 |
1.0 asShortFloat asFloat |
1197 | 400 |
" |
401 |
! |
|
402 |
||
403 |
asInteger |
|
404 |
"return an integer with same value - might truncate" |
|
405 |
||
406 |
%{ /* NOCONTEXT */ |
|
1670 | 407 |
float fVal; |
1197 | 408 |
|
1670 | 409 |
fVal = __shortFloatVal(self); |
410 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
3158 | 411 |
RETURN ( __MKSMALLINT( (INT)fVal) ); |
1197 | 412 |
} |
413 |
%}. |
|
414 |
^ super asInteger |
|
415 |
||
416 |
" |
|
417 |
12345.0 asShortFloat asInteger |
|
418 |
1e15 asShortFloat asInteger |
|
419 |
" |
|
420 |
! |
|
421 |
||
422 |
asShortFloat |
|
423 |
"return a ShortFloat with same value as the receiver - thats me" |
|
424 |
||
425 |
^ self |
|
1200 | 426 |
! |
427 |
||
428 |
generality |
|
429 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
430 |
||
431 |
^ 70 |
|
432 |
||
433 |
||
1197 | 434 |
! ! |
435 |
||
436 |
!ShortFloat methodsFor:'comparing'! |
|
437 |
||
438 |
< aNumber |
|
439 |
"return true, if the argument is greater" |
|
440 |
||
441 |
%{ /* NOCONTEXT */ |
|
442 |
||
443 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 444 |
RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false ); |
1197 | 445 |
} |
446 |
if (__isFloatLike(aNumber)) { |
|
3158 | 447 |
RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false ); |
1197 | 448 |
} |
449 |
if (__isShortFloat(aNumber)) { |
|
3158 | 450 |
RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false ); |
1197 | 451 |
} |
452 |
%}. |
|
453 |
^ aNumber lessFromShortFloat:self |
|
454 |
||
1200 | 455 |
" |
456 |
1.0 asShortFloat > (1/3) |
|
457 |
" |
|
1197 | 458 |
! |
459 |
||
460 |
<= aNumber |
|
461 |
"return true, if the argument is greater or equal" |
|
462 |
||
463 |
%{ /* NOCONTEXT */ |
|
464 |
||
465 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 466 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 467 |
} |
468 |
if (__isFloatLike(aNumber)) { |
|
3158 | 469 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
1197 | 470 |
} |
471 |
if (__isShortFloat(aNumber)) { |
|
3158 | 472 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
1197 | 473 |
} |
474 |
%}. |
|
475 |
^ self retry:#<= coercing:aNumber |
|
476 |
||
477 |
! |
|
478 |
||
479 |
= aNumber |
|
480 |
"return true, if the arguments value are equal by value" |
|
481 |
||
482 |
%{ /* NOCONTEXT */ |
|
483 |
||
484 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 485 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
1197 | 486 |
} |
487 |
if (__isFloatLike(aNumber)) { |
|
3158 | 488 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
1197 | 489 |
} |
490 |
if (__isShortFloat(aNumber)) { |
|
3158 | 491 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
1197 | 492 |
} |
493 |
%}. |
|
494 |
^ self retry:#= coercing:aNumber |
|
495 |
||
496 |
! |
|
497 |
||
498 |
> aNumber |
|
499 |
"return true, if the argument is less" |
|
500 |
||
501 |
%{ /* NOCONTEXT */ |
|
502 |
||
503 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 504 |
RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false ); |
1197 | 505 |
} |
506 |
if (__isFloatLike(aNumber)) { |
|
3158 | 507 |
RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false ); |
1197 | 508 |
} |
509 |
if (__isShortFloat(aNumber)) { |
|
3158 | 510 |
RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false ); |
1197 | 511 |
} |
512 |
%}. |
|
513 |
^ self retry:#> coercing:aNumber |
|
514 |
! |
|
515 |
||
516 |
>= aNumber |
|
517 |
"return true, if the argument is less or equal" |
|
518 |
||
519 |
%{ /* NOCONTEXT */ |
|
520 |
||
521 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 522 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 523 |
} |
524 |
if (__isFloatLike(aNumber)) { |
|
3158 | 525 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
1197 | 526 |
} |
527 |
if (__isShortFloat(aNumber)) { |
|
3158 | 528 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
1197 | 529 |
} |
530 |
%}. |
|
531 |
^ self retry:#>= coercing:aNumber |
|
532 |
! |
|
533 |
||
534 |
hash |
|
535 |
"return a number for hashing; redefined, since floats compare |
|
536 |
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same |
|
537 |
as 3 hash." |
|
538 |
||
539 |
|i| |
|
540 |
||
541 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
3158 | 542 |
i := self asInteger. |
543 |
self = i ifTrue:[ |
|
544 |
^ i hash |
|
545 |
]. |
|
1197 | 546 |
]. |
547 |
||
548 |
" |
|
549 |
mhmh take some of my value-bits to hash on |
|
550 |
" |
|
551 |
^ (((self basicAt:4) bitAnd:16r3F) bitShift:24) + |
|
552 |
((self basicAt:3) bitShift:16) + |
|
553 |
((self basicAt:2) bitShift:8) + |
|
554 |
(self basicAt:1) |
|
555 |
||
556 |
" |
|
557 |
1.2345 hash |
|
558 |
1.2345 asShortFloat hash |
|
559 |
1.0 hash |
|
560 |
1.0 asShortFloat hash |
|
561 |
" |
|
562 |
! |
|
563 |
||
564 |
~= aNumber |
|
565 |
"return true, if the arguments value are not equal" |
|
566 |
||
567 |
%{ /* NOCONTEXT */ |
|
568 |
||
569 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 570 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
1197 | 571 |
} |
572 |
if (__isFloatLike(aNumber)) { |
|
3158 | 573 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
1197 | 574 |
} |
575 |
if (__isShortFloat(aNumber)) { |
|
3158 | 576 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
1197 | 577 |
} |
578 |
%}. |
|
579 |
^ self retry:#~= coercing:aNumber |
|
580 |
||
581 |
! ! |
|
582 |
||
583 |
!ShortFloat methodsFor:'printing & storing'! |
|
584 |
||
585 |
printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
586 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
587 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
588 |
#printOn: as basic print mechanism." |
1197 | 589 |
|
590 |
%{ /* NOCONTEXT */ |
|
591 |
||
592 |
char buffer[64]; |
|
593 |
REGISTER char *cp; |
|
594 |
OBJ s; |
|
595 |
||
596 |
/* |
|
597 |
* actually only needed on sparc: since thisContext is |
|
598 |
* in a global register, which gets destroyed by printf, |
|
599 |
* manually save it here - very stupid ... |
|
600 |
*/ |
|
601 |
__BEGIN_PROTECT_REGISTERS__ |
|
602 |
||
603 |
#ifdef SYSV |
|
604 |
sprintf(buffer, "%.6lg", (double)__shortFloatVal(self)); |
|
605 |
#else |
|
606 |
sprintf(buffer, "%.6G", (double)__shortFloatVal(self)); |
|
607 |
#endif |
|
608 |
||
609 |
__END_PROTECT_REGISTERS__ |
|
610 |
||
611 |
/* |
|
612 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
|
613 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
|
614 |
*/ |
|
615 |
for (cp = buffer; *cp; cp++) { |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
616 |
if ((*cp == '.') || (*cp == 'e')) break; |
1197 | 617 |
} |
618 |
if (! *cp) { |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
619 |
*cp++ = '.'; |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
620 |
*cp++ = '0'; |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
621 |
*cp = '\0'; |
1197 | 622 |
} |
623 |
||
624 |
s = __MKSTRING(buffer COMMA_SND); |
|
625 |
if (s != nil) { |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
626 |
RETURN (s); |
1197 | 627 |
} |
628 |
%}. |
|
629 |
" |
|
630 |
memory allocation (for the new string) failed. |
|
631 |
When we arrive here, there was no memory, even after a garbage collect. |
|
632 |
This means, that the VM wanted to get some more memory from the |
|
633 |
OS, which was not kind enough to give it. |
|
634 |
Bad luck - you should increase the swap space on your machine. |
|
635 |
" |
|
636 |
^ ObjectMemory allocationFailureSignal raise. |
|
637 |
! ! |
|
638 |
||
639 |
!ShortFloat methodsFor:'testing'! |
|
640 |
||
3147 | 641 |
isFinite |
642 |
"return true, if the receiver is a finite float |
|
643 |
i.e. not NaN and not infinite." |
|
644 |
||
645 |
%{ /* NOCONTEXT */ |
|
646 |
||
647 |
double dV = (double) __shortFloatVal(self); |
|
648 |
||
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
649 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
650 |
* 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
|
651 |
* a finite() macro or function (WIN32), |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
652 |
* this may always return true here ... |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
653 |
*/ |
3147 | 654 |
if (finite(dV)) { RETURN (true); } |
655 |
%}. |
|
656 |
^false |
|
657 |
||
658 |
" |
|
659 |
1.0 asShortFloat isFinite |
|
660 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
661 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
662 |
" |
|
663 |
! |
|
664 |
||
2387 | 665 |
isNaN |
666 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
667 |
These are not created by ST/X float operations (they raise an exception); |
|
668 |
however, inline C-code could produce them ..." |
|
669 |
||
670 |
%{ /* NOCONTEXT */ |
|
671 |
||
3139 | 672 |
double dV = (double)(__shortFloatVal(self)); |
673 |
||
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
674 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
675 |
* 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
|
676 |
* a finite() macro or function (WIN32), |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
677 |
* this may always return false here ... |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
678 |
*/ |
3147 | 679 |
if (isnan(dV)) { RETURN (true); } |
680 |
||
681 |
#if 0 /* Currently all our systems support isnan() |
|
682 |
* If not, you have to fix librun/jinterpret.c also. |
|
683 |
*/ |
|
684 |
||
2395 | 685 |
/* |
686 |
* sigh - every vendor is playing its own game here ... |
|
687 |
* Q: what are standards worth, anyway ? |
|
688 |
*/ |
|
3139 | 689 |
#ifdef IS_NAN |
690 |
if (IS_NAN(dV)) { RETURN (true); } |
|
691 |
RETURN (false); |
|
692 |
#endif |
|
693 |
||
2393 | 694 |
#ifdef IS_QNAN |
3139 | 695 |
if (IS_QNAN(dV)) { RETURN (true); } |
696 |
RETURN (false); |
|
2393 | 697 |
#endif |
2392 | 698 |
|
2393 | 699 |
#ifdef FLT_SNAN |
3139 | 700 |
if (dV == FLT_SNAN) { RETURN (true); } |
701 |
RETURN (false); |
|
2393 | 702 |
#endif |
3139 | 703 |
|
2393 | 704 |
#ifdef FLT_QNAN |
3139 | 705 |
if (dV == FLT_QNAN) { RETURN (true); } |
706 |
RETURN (false); |
|
2393 | 707 |
#endif |
708 |
||
709 |
#ifdef _SNANF |
|
3139 | 710 |
if (dV == _SNAN) { RETURN (true); } |
711 |
RETURN (false); |
|
2393 | 712 |
#endif |
3139 | 713 |
|
2393 | 714 |
#ifdef _QNANF |
3139 | 715 |
if (dV == _QNAN) { RETURN (true); } |
716 |
RETURN (false); |
|
2393 | 717 |
#endif |
2392 | 718 |
|
2395 | 719 |
#ifdef IsPosNAN |
3139 | 720 |
if IsPosNAN(dV) { RETURN (true); } |
721 |
RETURN (false); |
|
2395 | 722 |
#endif |
3139 | 723 |
|
2395 | 724 |
#ifdef IsNegNAN |
3139 | 725 |
if IsNegNAN(dV) { RETURN (true); } |
726 |
RETURN (false); |
|
2395 | 727 |
#endif |
728 |
||
2393 | 729 |
#ifdef NAN |
3139 | 730 |
if (dV == NAN) { RETURN (true); } |
731 |
RETURN (false); |
|
2400 | 732 |
#endif |
2389 | 733 |
|
3139 | 734 |
#ifdef NaN |
735 |
if (NaN(dV)) { RETURN (true); } |
|
736 |
RETURN (false); |
|
737 |
#endif |
|
3147 | 738 |
|
739 |
#endif /* 0 */ |
|
2392 | 740 |
%}. |
741 |
^ false |
|
2387 | 742 |
|
3147 | 743 |
" |
744 |
1.0 asShortFloat isNaN |
|
745 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
|
746 |
" |
|
2387 | 747 |
! |
748 |
||
1197 | 749 |
negative |
750 |
"return true if the receiver is less than zero" |
|
751 |
||
752 |
%{ /* NOCONTEXT */ |
|
753 |
||
754 |
RETURN ( (__shortFloatVal(self) < 0.0) ? true : false ); |
|
755 |
%} |
|
756 |
! |
|
757 |
||
758 |
positive |
|
759 |
"return true if the receiver is greater or equal to zero" |
|
760 |
||
761 |
%{ /* NOCONTEXT */ |
|
762 |
||
763 |
RETURN ( (__shortFloatVal(self) >= 0.0) ? true : false ); |
|
764 |
%} |
|
1880 | 765 |
! |
766 |
||
767 |
strictlyPositive |
|
768 |
"return true if the receiver is greater than zero" |
|
769 |
||
770 |
%{ /* NOCONTEXT */ |
|
771 |
||
772 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
773 |
%} |
|
1197 | 774 |
! ! |
775 |
||
1880 | 776 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 777 |
|
778 |
version |
|
3414 | 779 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/SFloat.st,v 1.30 1998-05-01 10:50:26 cg Exp $' |
1197 | 780 |
! ! |