author | Claus Gittinger <cg@exept.de> |
Fri, 08 May 1998 14:15:34 +0200 | |
changeset 3429 | 50c2ce8d5a5f |
parent 3427 | ba90490c94b6 |
child 3470 | fa7cda7fb45e |
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" |
3427
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
193 |
! |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
194 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
195 |
isIEEEFormat |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
196 |
"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
|
197 |
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
|
198 |
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
|
199 |
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
|
200 |
machine). |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
201 |
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
|
202 |
(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
|
203 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
204 |
^ true "/ this may be a lie |
1197 | 205 |
! ! |
206 |
||
207 |
!ShortFloat methodsFor:'arithmetic'! |
|
208 |
||
209 |
* aNumber |
|
210 |
"return the product of the receiver and the argument, aNumber" |
|
211 |
||
212 |
%{ /* NOCONTEXT */ |
|
213 |
||
214 |
OBJ newFloat; |
|
215 |
float result; |
|
216 |
double dResult; |
|
217 |
||
218 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 219 |
result = __shortFloatVal(self) * (float)(__intVal(aNumber)); |
1197 | 220 |
retResult: |
3158 | 221 |
__qMKSFLOAT(newFloat, result); |
222 |
RETURN ( newFloat ); |
|
1197 | 223 |
} |
224 |
if (__isShortFloat(aNumber)) { |
|
3158 | 225 |
result = __shortFloatVal(self) * __shortFloatVal(aNumber); |
226 |
goto retResult; |
|
1197 | 227 |
} |
228 |
if (__isFloatLike(aNumber)) { |
|
3158 | 229 |
dResult = (double) __shortFloatVal(self)* __floatVal(aNumber); |
230 |
__qMKFLOAT(newFloat, dResult); |
|
231 |
RETURN ( newFloat ); |
|
1197 | 232 |
|
233 |
} |
|
234 |
%}. |
|
235 |
^ aNumber productFromShortFloat:self |
|
236 |
! |
|
237 |
||
238 |
+ aNumber |
|
239 |
"return the sum of the receiver and the argument, aNumber" |
|
240 |
||
241 |
%{ /* NOCONTEXT */ |
|
242 |
||
243 |
OBJ newFloat; |
|
244 |
float result; |
|
245 |
double dResult; |
|
246 |
||
247 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 248 |
result = __shortFloatVal(self) + (float)(__intVal(aNumber)); |
1197 | 249 |
retResult: |
3158 | 250 |
__qMKSFLOAT(newFloat, result); |
251 |
RETURN ( newFloat ); |
|
1197 | 252 |
} |
253 |
if (__isShortFloat(aNumber)) { |
|
3158 | 254 |
result = __shortFloatVal(self) + __shortFloatVal(aNumber); |
255 |
goto retResult; |
|
1197 | 256 |
} |
257 |
if (__isFloatLike(aNumber)) { |
|
3158 | 258 |
dResult = (double) __shortFloatVal(self) + __floatVal(aNumber); |
259 |
__qMKFLOAT(newFloat, dResult); |
|
260 |
RETURN ( newFloat ); |
|
1197 | 261 |
|
262 |
} |
|
263 |
%}. |
|
264 |
^ aNumber sumFromShortFloat:self |
|
265 |
! |
|
266 |
||
267 |
- aNumber |
|
268 |
"return the difference of the receiver and the argument, aNumber" |
|
269 |
||
270 |
%{ /* NOCONTEXT */ |
|
271 |
||
272 |
OBJ newFloat; |
|
273 |
float result; |
|
274 |
double dResult; |
|
275 |
||
276 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 277 |
result = __shortFloatVal(self) - (float)(__intVal(aNumber)); |
1197 | 278 |
retResult: |
3158 | 279 |
__qMKSFLOAT(newFloat, result); |
280 |
RETURN ( newFloat ); |
|
1197 | 281 |
} |
282 |
if (__isShortFloat(aNumber)) { |
|
3158 | 283 |
result = __shortFloatVal(self) - __shortFloatVal(aNumber); |
284 |
goto retResult; |
|
1197 | 285 |
} |
286 |
if (__isFloatLike(aNumber)) { |
|
3158 | 287 |
dResult = (double) __shortFloatVal(self) - __floatVal(aNumber); |
288 |
__qMKFLOAT(newFloat, dResult); |
|
289 |
RETURN ( newFloat ); |
|
1197 | 290 |
|
291 |
} |
|
292 |
%}. |
|
293 |
^ aNumber differenceFromShortFloat:self |
|
294 |
! |
|
295 |
||
296 |
/ aNumber |
|
297 |
"return the quotient of the receiver and the argument, aNumber" |
|
298 |
||
299 |
%{ /* NOCONTEXT */ |
|
300 |
||
301 |
OBJ newFloat; |
|
302 |
float result, val; |
|
303 |
double dResult, dVal; |
|
304 |
||
305 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 306 |
if (aNumber != __MKSMALLINT(0)) { |
307 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
1197 | 308 |
retResult: |
3158 | 309 |
__qMKSFLOAT(newFloat, result); |
310 |
RETURN ( newFloat ); |
|
311 |
} |
|
1197 | 312 |
} |
313 |
if (__isShortFloat(aNumber)) { |
|
3158 | 314 |
val = __shortFloatVal(aNumber); |
315 |
if (val != 0.0) { |
|
316 |
result = __shortFloatVal(self) / val; |
|
317 |
goto retResult; |
|
318 |
} |
|
1197 | 319 |
} |
320 |
if (__isFloatLike(aNumber)) { |
|
3158 | 321 |
dVal = __floatVal(aNumber); |
322 |
if (dVal != 0.0) { |
|
323 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
324 |
__qMKFLOAT(newFloat, dResult); |
|
325 |
} |
|
326 |
RETURN ( newFloat ); |
|
1197 | 327 |
|
328 |
} |
|
329 |
%}. |
|
330 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
3158 | 331 |
" |
332 |
No, you shalt not divide by zero |
|
333 |
" |
|
334 |
^ DivisionByZeroSignal raise. |
|
1197 | 335 |
]. |
336 |
^ aNumber quotientFromFloat:self |
|
337 |
||
338 |
! |
|
339 |
||
340 |
negated |
|
341 |
"return myself negated" |
|
342 |
||
343 |
%{ /* NOCONTEXT */ |
|
344 |
OBJ newFloat; |
|
345 |
float rslt = - __shortFloatVal(self); |
|
346 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
347 |
__qMKSFLOAT(newFloat, rslt); |
1197 | 348 |
RETURN ( newFloat ); |
349 |
%} |
|
350 |
||
3147 | 351 |
! |
352 |
||
353 |
uncheckedDivide:aNumber |
|
354 |
"return the quotient of the receiver and the argument, aNumber |
|
355 |
Do not check for divide by zero (return NaN or infinity)" |
|
356 |
||
357 |
%{ /* NOCONTEXT */ |
|
358 |
||
359 |
OBJ newFloat; |
|
360 |
float result, val; |
|
361 |
double dResult, dVal; |
|
362 |
||
363 |
if (__isSmallInteger(aNumber)) { |
|
364 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
365 |
retResult: |
|
366 |
__qMKSFLOAT(newFloat, result); |
|
367 |
RETURN ( newFloat ); |
|
368 |
} |
|
369 |
if (__isShortFloat(aNumber)) { |
|
370 |
val = __shortFloatVal(aNumber); |
|
371 |
result = __shortFloatVal(self) / val; |
|
372 |
goto retResult; |
|
373 |
} |
|
374 |
if (__isFloatLike(aNumber)) { |
|
375 |
dVal = __floatVal(aNumber); |
|
376 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
377 |
__qMKFLOAT(newFloat, dResult); |
|
378 |
RETURN ( newFloat ); |
|
379 |
} |
|
380 |
%}. |
|
381 |
^ aNumber quotientFromFloat:self |
|
382 |
||
383 |
" |
|
384 |
0.0 asShortFloat uncheckedDivide:0 |
|
385 |
1.0 asShortFloat uncheckedDivide:0.0 |
|
386 |
" |
|
387 |
||
1197 | 388 |
! ! |
389 |
||
390 |
!ShortFloat methodsFor:'coercion and converting'! |
|
391 |
||
3014 | 392 |
asDouble |
393 |
"ST80 compatibility - return a Float with same value as the receiver" |
|
394 |
||
395 |
^ self asFloat |
|
396 |
! |
|
397 |
||
1197 | 398 |
asFloat |
399 |
"return a Float with same value as the receiver" |
|
400 |
||
401 |
%{ /* NOCONTEXT */ |
|
402 |
||
403 |
OBJ newFloat; |
|
404 |
double dVal = (double)__shortFloatVal(self); |
|
405 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
406 |
__qMKFLOAT(newFloat, dVal); |
1197 | 407 |
RETURN ( newFloat ); |
408 |
%} |
|
409 |
||
410 |
" |
|
1583 | 411 |
1.0 asShortFloat asFloat |
1197 | 412 |
" |
413 |
! |
|
414 |
||
415 |
asInteger |
|
416 |
"return an integer with same value - might truncate" |
|
417 |
||
418 |
%{ /* NOCONTEXT */ |
|
1670 | 419 |
float fVal; |
1197 | 420 |
|
1670 | 421 |
fVal = __shortFloatVal(self); |
422 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
3158 | 423 |
RETURN ( __MKSMALLINT( (INT)fVal) ); |
1197 | 424 |
} |
425 |
%}. |
|
426 |
^ super asInteger |
|
427 |
||
428 |
" |
|
429 |
12345.0 asShortFloat asInteger |
|
430 |
1e15 asShortFloat asInteger |
|
431 |
" |
|
432 |
! |
|
433 |
||
434 |
asShortFloat |
|
435 |
"return a ShortFloat with same value as the receiver - thats me" |
|
436 |
||
437 |
^ self |
|
1200 | 438 |
! |
439 |
||
440 |
generality |
|
441 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
442 |
||
443 |
^ 70 |
|
444 |
||
445 |
||
1197 | 446 |
! ! |
447 |
||
448 |
!ShortFloat methodsFor:'comparing'! |
|
449 |
||
450 |
< aNumber |
|
451 |
"return true, if the argument is greater" |
|
452 |
||
453 |
%{ /* NOCONTEXT */ |
|
454 |
||
455 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 456 |
RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false ); |
1197 | 457 |
} |
458 |
if (__isFloatLike(aNumber)) { |
|
3158 | 459 |
RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false ); |
1197 | 460 |
} |
461 |
if (__isShortFloat(aNumber)) { |
|
3158 | 462 |
RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false ); |
1197 | 463 |
} |
464 |
%}. |
|
465 |
^ aNumber lessFromShortFloat:self |
|
466 |
||
1200 | 467 |
" |
468 |
1.0 asShortFloat > (1/3) |
|
469 |
" |
|
1197 | 470 |
! |
471 |
||
472 |
<= aNumber |
|
473 |
"return true, if the argument is greater or equal" |
|
474 |
||
475 |
%{ /* NOCONTEXT */ |
|
476 |
||
477 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 478 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 479 |
} |
480 |
if (__isFloatLike(aNumber)) { |
|
3158 | 481 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
1197 | 482 |
} |
483 |
if (__isShortFloat(aNumber)) { |
|
3158 | 484 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
1197 | 485 |
} |
486 |
%}. |
|
487 |
^ self retry:#<= coercing:aNumber |
|
488 |
||
489 |
! |
|
490 |
||
491 |
= aNumber |
|
492 |
"return true, if the arguments value are equal by value" |
|
493 |
||
494 |
%{ /* NOCONTEXT */ |
|
495 |
||
496 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 497 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
1197 | 498 |
} |
499 |
if (__isFloatLike(aNumber)) { |
|
3158 | 500 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
1197 | 501 |
} |
502 |
if (__isShortFloat(aNumber)) { |
|
3158 | 503 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
1197 | 504 |
} |
505 |
%}. |
|
506 |
^ self retry:#= coercing:aNumber |
|
507 |
||
508 |
! |
|
509 |
||
510 |
> aNumber |
|
511 |
"return true, if the argument is less" |
|
512 |
||
513 |
%{ /* NOCONTEXT */ |
|
514 |
||
515 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 516 |
RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false ); |
1197 | 517 |
} |
518 |
if (__isFloatLike(aNumber)) { |
|
3158 | 519 |
RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false ); |
1197 | 520 |
} |
521 |
if (__isShortFloat(aNumber)) { |
|
3158 | 522 |
RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false ); |
1197 | 523 |
} |
524 |
%}. |
|
525 |
^ self retry:#> coercing:aNumber |
|
526 |
! |
|
527 |
||
528 |
>= aNumber |
|
529 |
"return true, if the argument is less or equal" |
|
530 |
||
531 |
%{ /* NOCONTEXT */ |
|
532 |
||
533 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 534 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 535 |
} |
536 |
if (__isFloatLike(aNumber)) { |
|
3158 | 537 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
1197 | 538 |
} |
539 |
if (__isShortFloat(aNumber)) { |
|
3158 | 540 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
1197 | 541 |
} |
542 |
%}. |
|
543 |
^ self retry:#>= coercing:aNumber |
|
544 |
! |
|
545 |
||
546 |
hash |
|
547 |
"return a number for hashing; redefined, since floats compare |
|
548 |
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same |
|
549 |
as 3 hash." |
|
550 |
||
551 |
|i| |
|
552 |
||
553 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
3158 | 554 |
i := self asInteger. |
555 |
self = i ifTrue:[ |
|
556 |
^ i hash |
|
557 |
]. |
|
1197 | 558 |
]. |
559 |
||
560 |
" |
|
561 |
mhmh take some of my value-bits to hash on |
|
562 |
" |
|
563 |
^ (((self basicAt:4) bitAnd:16r3F) bitShift:24) + |
|
564 |
((self basicAt:3) bitShift:16) + |
|
565 |
((self basicAt:2) bitShift:8) + |
|
566 |
(self basicAt:1) |
|
567 |
||
568 |
" |
|
569 |
1.2345 hash |
|
570 |
1.2345 asShortFloat hash |
|
571 |
1.0 hash |
|
572 |
1.0 asShortFloat hash |
|
573 |
" |
|
574 |
! |
|
575 |
||
576 |
~= aNumber |
|
577 |
"return true, if the arguments value are not equal" |
|
578 |
||
579 |
%{ /* NOCONTEXT */ |
|
580 |
||
581 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 582 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
1197 | 583 |
} |
584 |
if (__isFloatLike(aNumber)) { |
|
3158 | 585 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
1197 | 586 |
} |
587 |
if (__isShortFloat(aNumber)) { |
|
3158 | 588 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
1197 | 589 |
} |
590 |
%}. |
|
591 |
^ self retry:#~= coercing:aNumber |
|
592 |
||
593 |
! ! |
|
594 |
||
595 |
!ShortFloat methodsFor:'printing & storing'! |
|
596 |
||
597 |
printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
598 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
599 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
600 |
#printOn: as basic print mechanism." |
1197 | 601 |
|
602 |
%{ /* NOCONTEXT */ |
|
603 |
||
604 |
char buffer[64]; |
|
605 |
REGISTER char *cp; |
|
606 |
OBJ s; |
|
607 |
||
608 |
/* |
|
609 |
* actually only needed on sparc: since thisContext is |
|
610 |
* in a global register, which gets destroyed by printf, |
|
611 |
* manually save it here - very stupid ... |
|
612 |
*/ |
|
613 |
__BEGIN_PROTECT_REGISTERS__ |
|
614 |
||
615 |
#ifdef SYSV |
|
616 |
sprintf(buffer, "%.6lg", (double)__shortFloatVal(self)); |
|
617 |
#else |
|
618 |
sprintf(buffer, "%.6G", (double)__shortFloatVal(self)); |
|
619 |
#endif |
|
620 |
||
621 |
__END_PROTECT_REGISTERS__ |
|
622 |
||
623 |
/* |
|
624 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
|
625 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
|
626 |
*/ |
|
627 |
for (cp = buffer; *cp; cp++) { |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
628 |
if ((*cp == '.') || (*cp == 'e')) break; |
1197 | 629 |
} |
630 |
if (! *cp) { |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
631 |
*cp++ = '.'; |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
632 |
*cp++ = '0'; |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
633 |
*cp = '\0'; |
1197 | 634 |
} |
635 |
||
636 |
s = __MKSTRING(buffer COMMA_SND); |
|
637 |
if (s != nil) { |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
638 |
RETURN (s); |
1197 | 639 |
} |
640 |
%}. |
|
641 |
" |
|
642 |
memory allocation (for the new string) failed. |
|
643 |
When we arrive here, there was no memory, even after a garbage collect. |
|
644 |
This means, that the VM wanted to get some more memory from the |
|
645 |
OS, which was not kind enough to give it. |
|
646 |
Bad luck - you should increase the swap space on your machine. |
|
647 |
" |
|
648 |
^ ObjectMemory allocationFailureSignal raise. |
|
649 |
! ! |
|
650 |
||
651 |
!ShortFloat methodsFor:'testing'! |
|
652 |
||
3147 | 653 |
isFinite |
654 |
"return true, if the receiver is a finite float |
|
655 |
i.e. not NaN and not infinite." |
|
656 |
||
657 |
%{ /* NOCONTEXT */ |
|
658 |
||
659 |
double dV = (double) __shortFloatVal(self); |
|
660 |
||
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
661 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
662 |
* 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
|
663 |
* 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
|
664 |
* 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
|
665 |
*/ |
3147 | 666 |
if (finite(dV)) { RETURN (true); } |
667 |
%}. |
|
668 |
^false |
|
669 |
||
670 |
" |
|
671 |
1.0 asShortFloat isFinite |
|
672 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
673 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
674 |
" |
|
675 |
! |
|
676 |
||
2387 | 677 |
isNaN |
678 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
679 |
These are not created by ST/X float operations (they raise an exception); |
|
680 |
however, inline C-code could produce them ..." |
|
681 |
||
682 |
%{ /* NOCONTEXT */ |
|
683 |
||
3139 | 684 |
double dV = (double)(__shortFloatVal(self)); |
685 |
||
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
686 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
687 |
* 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
|
688 |
* 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
|
689 |
* 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
|
690 |
*/ |
3147 | 691 |
if (isnan(dV)) { RETURN (true); } |
692 |
||
693 |
#if 0 /* Currently all our systems support isnan() |
|
694 |
* If not, you have to fix librun/jinterpret.c also. |
|
695 |
*/ |
|
696 |
||
2395 | 697 |
/* |
698 |
* sigh - every vendor is playing its own game here ... |
|
699 |
* Q: what are standards worth, anyway ? |
|
700 |
*/ |
|
3139 | 701 |
#ifdef IS_NAN |
702 |
if (IS_NAN(dV)) { RETURN (true); } |
|
703 |
RETURN (false); |
|
704 |
#endif |
|
705 |
||
2393 | 706 |
#ifdef IS_QNAN |
3139 | 707 |
if (IS_QNAN(dV)) { RETURN (true); } |
708 |
RETURN (false); |
|
2393 | 709 |
#endif |
2392 | 710 |
|
2393 | 711 |
#ifdef FLT_SNAN |
3139 | 712 |
if (dV == FLT_SNAN) { RETURN (true); } |
713 |
RETURN (false); |
|
2393 | 714 |
#endif |
3139 | 715 |
|
2393 | 716 |
#ifdef FLT_QNAN |
3139 | 717 |
if (dV == FLT_QNAN) { RETURN (true); } |
718 |
RETURN (false); |
|
2393 | 719 |
#endif |
720 |
||
721 |
#ifdef _SNANF |
|
3139 | 722 |
if (dV == _SNAN) { RETURN (true); } |
723 |
RETURN (false); |
|
2393 | 724 |
#endif |
3139 | 725 |
|
2393 | 726 |
#ifdef _QNANF |
3139 | 727 |
if (dV == _QNAN) { RETURN (true); } |
728 |
RETURN (false); |
|
2393 | 729 |
#endif |
2392 | 730 |
|
2395 | 731 |
#ifdef IsPosNAN |
3139 | 732 |
if IsPosNAN(dV) { RETURN (true); } |
733 |
RETURN (false); |
|
2395 | 734 |
#endif |
3139 | 735 |
|
2395 | 736 |
#ifdef IsNegNAN |
3139 | 737 |
if IsNegNAN(dV) { RETURN (true); } |
738 |
RETURN (false); |
|
2395 | 739 |
#endif |
740 |
||
2393 | 741 |
#ifdef NAN |
3139 | 742 |
if (dV == NAN) { RETURN (true); } |
743 |
RETURN (false); |
|
2400 | 744 |
#endif |
2389 | 745 |
|
3139 | 746 |
#ifdef NaN |
747 |
if (NaN(dV)) { RETURN (true); } |
|
748 |
RETURN (false); |
|
749 |
#endif |
|
3147 | 750 |
|
751 |
#endif /* 0 */ |
|
2392 | 752 |
%}. |
753 |
^ false |
|
2387 | 754 |
|
3147 | 755 |
" |
756 |
1.0 asShortFloat isNaN |
|
757 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
|
758 |
" |
|
2387 | 759 |
! |
760 |
||
1197 | 761 |
negative |
762 |
"return true if the receiver is less than zero" |
|
763 |
||
764 |
%{ /* NOCONTEXT */ |
|
765 |
||
766 |
RETURN ( (__shortFloatVal(self) < 0.0) ? true : false ); |
|
767 |
%} |
|
768 |
! |
|
769 |
||
770 |
positive |
|
771 |
"return true if the receiver is greater or equal to zero" |
|
772 |
||
773 |
%{ /* NOCONTEXT */ |
|
774 |
||
775 |
RETURN ( (__shortFloatVal(self) >= 0.0) ? true : false ); |
|
776 |
%} |
|
1880 | 777 |
! |
778 |
||
779 |
strictlyPositive |
|
780 |
"return true if the receiver is greater than zero" |
|
781 |
||
782 |
%{ /* NOCONTEXT */ |
|
783 |
||
784 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
785 |
%} |
|
1197 | 786 |
! ! |
787 |
||
1880 | 788 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 789 |
|
790 |
version |
|
3429
50c2ce8d5a5f
oops - leftover debug prints
Claus Gittinger <cg@exept.de>
parents:
3427
diff
changeset
|
791 |
^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.32 1998-05-08 12:15:34 cg Exp $' |
1197 | 792 |
! ! |