author | Claus Gittinger <cg@exept.de> |
Fri, 08 May 1998 14:04:07 +0200 | |
changeset 3427 | ba90490c94b6 |
parent 3414 | d9aefd18c10d |
child 3429 | 50c2ce8d5a5f |
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); |
3427
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
422 |
printf("sself: %f min: %f max: %f\n", fVal, (float)_MIN_INT, (float)_MAX_INT); |
1670 | 423 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
3427
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
424 |
printf("in range\n"); |
3158 | 425 |
RETURN ( __MKSMALLINT( (INT)fVal) ); |
1197 | 426 |
} |
3427
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
427 |
printf("not in range\n"); |
1197 | 428 |
%}. |
429 |
^ super asInteger |
|
430 |
||
431 |
" |
|
432 |
12345.0 asShortFloat asInteger |
|
433 |
1e15 asShortFloat asInteger |
|
434 |
" |
|
435 |
! |
|
436 |
||
437 |
asShortFloat |
|
438 |
"return a ShortFloat with same value as the receiver - thats me" |
|
439 |
||
440 |
^ self |
|
1200 | 441 |
! |
442 |
||
443 |
generality |
|
444 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
445 |
||
446 |
^ 70 |
|
447 |
||
448 |
||
1197 | 449 |
! ! |
450 |
||
451 |
!ShortFloat methodsFor:'comparing'! |
|
452 |
||
453 |
< aNumber |
|
454 |
"return true, if the argument is greater" |
|
455 |
||
456 |
%{ /* NOCONTEXT */ |
|
457 |
||
458 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 459 |
RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false ); |
1197 | 460 |
} |
461 |
if (__isFloatLike(aNumber)) { |
|
3158 | 462 |
RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false ); |
1197 | 463 |
} |
464 |
if (__isShortFloat(aNumber)) { |
|
3158 | 465 |
RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false ); |
1197 | 466 |
} |
467 |
%}. |
|
468 |
^ aNumber lessFromShortFloat:self |
|
469 |
||
1200 | 470 |
" |
471 |
1.0 asShortFloat > (1/3) |
|
472 |
" |
|
1197 | 473 |
! |
474 |
||
475 |
<= aNumber |
|
476 |
"return true, if the argument is greater or equal" |
|
477 |
||
478 |
%{ /* NOCONTEXT */ |
|
479 |
||
480 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 481 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 482 |
} |
483 |
if (__isFloatLike(aNumber)) { |
|
3158 | 484 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
1197 | 485 |
} |
486 |
if (__isShortFloat(aNumber)) { |
|
3158 | 487 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
1197 | 488 |
} |
489 |
%}. |
|
490 |
^ self retry:#<= coercing:aNumber |
|
491 |
||
492 |
! |
|
493 |
||
494 |
= aNumber |
|
495 |
"return true, if the arguments value are equal by value" |
|
496 |
||
497 |
%{ /* NOCONTEXT */ |
|
498 |
||
499 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 500 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
1197 | 501 |
} |
502 |
if (__isFloatLike(aNumber)) { |
|
3158 | 503 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
1197 | 504 |
} |
505 |
if (__isShortFloat(aNumber)) { |
|
3158 | 506 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
1197 | 507 |
} |
508 |
%}. |
|
509 |
^ self retry:#= coercing:aNumber |
|
510 |
||
511 |
! |
|
512 |
||
513 |
> aNumber |
|
514 |
"return true, if the argument is less" |
|
515 |
||
516 |
%{ /* NOCONTEXT */ |
|
517 |
||
518 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 519 |
RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false ); |
1197 | 520 |
} |
521 |
if (__isFloatLike(aNumber)) { |
|
3158 | 522 |
RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false ); |
1197 | 523 |
} |
524 |
if (__isShortFloat(aNumber)) { |
|
3158 | 525 |
RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false ); |
1197 | 526 |
} |
527 |
%}. |
|
528 |
^ self retry:#> coercing:aNumber |
|
529 |
! |
|
530 |
||
531 |
>= aNumber |
|
532 |
"return true, if the argument is less or equal" |
|
533 |
||
534 |
%{ /* NOCONTEXT */ |
|
535 |
||
536 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 537 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 538 |
} |
539 |
if (__isFloatLike(aNumber)) { |
|
3158 | 540 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
1197 | 541 |
} |
542 |
if (__isShortFloat(aNumber)) { |
|
3158 | 543 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
1197 | 544 |
} |
545 |
%}. |
|
546 |
^ self retry:#>= coercing:aNumber |
|
547 |
! |
|
548 |
||
549 |
hash |
|
550 |
"return a number for hashing; redefined, since floats compare |
|
551 |
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same |
|
552 |
as 3 hash." |
|
553 |
||
554 |
|i| |
|
555 |
||
556 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
3158 | 557 |
i := self asInteger. |
558 |
self = i ifTrue:[ |
|
559 |
^ i hash |
|
560 |
]. |
|
1197 | 561 |
]. |
562 |
||
563 |
" |
|
564 |
mhmh take some of my value-bits to hash on |
|
565 |
" |
|
566 |
^ (((self basicAt:4) bitAnd:16r3F) bitShift:24) + |
|
567 |
((self basicAt:3) bitShift:16) + |
|
568 |
((self basicAt:2) bitShift:8) + |
|
569 |
(self basicAt:1) |
|
570 |
||
571 |
" |
|
572 |
1.2345 hash |
|
573 |
1.2345 asShortFloat hash |
|
574 |
1.0 hash |
|
575 |
1.0 asShortFloat hash |
|
576 |
" |
|
577 |
! |
|
578 |
||
579 |
~= aNumber |
|
580 |
"return true, if the arguments value are not equal" |
|
581 |
||
582 |
%{ /* NOCONTEXT */ |
|
583 |
||
584 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 585 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
1197 | 586 |
} |
587 |
if (__isFloatLike(aNumber)) { |
|
3158 | 588 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
1197 | 589 |
} |
590 |
if (__isShortFloat(aNumber)) { |
|
3158 | 591 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
1197 | 592 |
} |
593 |
%}. |
|
594 |
^ self retry:#~= coercing:aNumber |
|
595 |
||
596 |
! ! |
|
597 |
||
598 |
!ShortFloat methodsFor:'printing & storing'! |
|
599 |
||
600 |
printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
601 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
602 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
603 |
#printOn: as basic print mechanism." |
1197 | 604 |
|
605 |
%{ /* NOCONTEXT */ |
|
606 |
||
607 |
char buffer[64]; |
|
608 |
REGISTER char *cp; |
|
609 |
OBJ s; |
|
610 |
||
611 |
/* |
|
612 |
* actually only needed on sparc: since thisContext is |
|
613 |
* in a global register, which gets destroyed by printf, |
|
614 |
* manually save it here - very stupid ... |
|
615 |
*/ |
|
616 |
__BEGIN_PROTECT_REGISTERS__ |
|
617 |
||
618 |
#ifdef SYSV |
|
619 |
sprintf(buffer, "%.6lg", (double)__shortFloatVal(self)); |
|
620 |
#else |
|
621 |
sprintf(buffer, "%.6G", (double)__shortFloatVal(self)); |
|
622 |
#endif |
|
623 |
||
624 |
__END_PROTECT_REGISTERS__ |
|
625 |
||
626 |
/* |
|
627 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
|
628 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
|
629 |
*/ |
|
630 |
for (cp = buffer; *cp; cp++) { |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
631 |
if ((*cp == '.') || (*cp == 'e')) break; |
1197 | 632 |
} |
633 |
if (! *cp) { |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
634 |
*cp++ = '.'; |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
635 |
*cp++ = '0'; |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
636 |
*cp = '\0'; |
1197 | 637 |
} |
638 |
||
639 |
s = __MKSTRING(buffer COMMA_SND); |
|
640 |
if (s != nil) { |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
641 |
RETURN (s); |
1197 | 642 |
} |
643 |
%}. |
|
644 |
" |
|
645 |
memory allocation (for the new string) failed. |
|
646 |
When we arrive here, there was no memory, even after a garbage collect. |
|
647 |
This means, that the VM wanted to get some more memory from the |
|
648 |
OS, which was not kind enough to give it. |
|
649 |
Bad luck - you should increase the swap space on your machine. |
|
650 |
" |
|
651 |
^ ObjectMemory allocationFailureSignal raise. |
|
652 |
! ! |
|
653 |
||
654 |
!ShortFloat methodsFor:'testing'! |
|
655 |
||
3147 | 656 |
isFinite |
657 |
"return true, if the receiver is a finite float |
|
658 |
i.e. not NaN and not infinite." |
|
659 |
||
660 |
%{ /* NOCONTEXT */ |
|
661 |
||
662 |
double dV = (double) __shortFloatVal(self); |
|
663 |
||
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
664 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
665 |
* 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
|
666 |
* 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
|
667 |
* 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
|
668 |
*/ |
3147 | 669 |
if (finite(dV)) { RETURN (true); } |
670 |
%}. |
|
671 |
^false |
|
672 |
||
673 |
" |
|
674 |
1.0 asShortFloat isFinite |
|
675 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
676 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
677 |
" |
|
678 |
! |
|
679 |
||
2387 | 680 |
isNaN |
681 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
682 |
These are not created by ST/X float operations (they raise an exception); |
|
683 |
however, inline C-code could produce them ..." |
|
684 |
||
685 |
%{ /* NOCONTEXT */ |
|
686 |
||
3139 | 687 |
double dV = (double)(__shortFloatVal(self)); |
688 |
||
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
689 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
690 |
* 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
|
691 |
* 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
|
692 |
* 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
|
693 |
*/ |
3147 | 694 |
if (isnan(dV)) { RETURN (true); } |
695 |
||
696 |
#if 0 /* Currently all our systems support isnan() |
|
697 |
* If not, you have to fix librun/jinterpret.c also. |
|
698 |
*/ |
|
699 |
||
2395 | 700 |
/* |
701 |
* sigh - every vendor is playing its own game here ... |
|
702 |
* Q: what are standards worth, anyway ? |
|
703 |
*/ |
|
3139 | 704 |
#ifdef IS_NAN |
705 |
if (IS_NAN(dV)) { RETURN (true); } |
|
706 |
RETURN (false); |
|
707 |
#endif |
|
708 |
||
2393 | 709 |
#ifdef IS_QNAN |
3139 | 710 |
if (IS_QNAN(dV)) { RETURN (true); } |
711 |
RETURN (false); |
|
2393 | 712 |
#endif |
2392 | 713 |
|
2393 | 714 |
#ifdef FLT_SNAN |
3139 | 715 |
if (dV == FLT_SNAN) { RETURN (true); } |
716 |
RETURN (false); |
|
2393 | 717 |
#endif |
3139 | 718 |
|
2393 | 719 |
#ifdef FLT_QNAN |
3139 | 720 |
if (dV == FLT_QNAN) { RETURN (true); } |
721 |
RETURN (false); |
|
2393 | 722 |
#endif |
723 |
||
724 |
#ifdef _SNANF |
|
3139 | 725 |
if (dV == _SNAN) { RETURN (true); } |
726 |
RETURN (false); |
|
2393 | 727 |
#endif |
3139 | 728 |
|
2393 | 729 |
#ifdef _QNANF |
3139 | 730 |
if (dV == _QNAN) { RETURN (true); } |
731 |
RETURN (false); |
|
2393 | 732 |
#endif |
2392 | 733 |
|
2395 | 734 |
#ifdef IsPosNAN |
3139 | 735 |
if IsPosNAN(dV) { RETURN (true); } |
736 |
RETURN (false); |
|
2395 | 737 |
#endif |
3139 | 738 |
|
2395 | 739 |
#ifdef IsNegNAN |
3139 | 740 |
if IsNegNAN(dV) { RETURN (true); } |
741 |
RETURN (false); |
|
2395 | 742 |
#endif |
743 |
||
2393 | 744 |
#ifdef NAN |
3139 | 745 |
if (dV == NAN) { RETURN (true); } |
746 |
RETURN (false); |
|
2400 | 747 |
#endif |
2389 | 748 |
|
3139 | 749 |
#ifdef NaN |
750 |
if (NaN(dV)) { RETURN (true); } |
|
751 |
RETURN (false); |
|
752 |
#endif |
|
3147 | 753 |
|
754 |
#endif /* 0 */ |
|
2392 | 755 |
%}. |
756 |
^ false |
|
2387 | 757 |
|
3147 | 758 |
" |
759 |
1.0 asShortFloat isNaN |
|
760 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
|
761 |
" |
|
2387 | 762 |
! |
763 |
||
1197 | 764 |
negative |
765 |
"return true if the receiver is less than zero" |
|
766 |
||
767 |
%{ /* NOCONTEXT */ |
|
768 |
||
769 |
RETURN ( (__shortFloatVal(self) < 0.0) ? true : false ); |
|
770 |
%} |
|
771 |
! |
|
772 |
||
773 |
positive |
|
774 |
"return true if the receiver is greater or equal to zero" |
|
775 |
||
776 |
%{ /* NOCONTEXT */ |
|
777 |
||
778 |
RETURN ( (__shortFloatVal(self) >= 0.0) ? true : false ); |
|
779 |
%} |
|
1880 | 780 |
! |
781 |
||
782 |
strictlyPositive |
|
783 |
"return true if the receiver is greater than zero" |
|
784 |
||
785 |
%{ /* NOCONTEXT */ |
|
786 |
||
787 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
788 |
%} |
|
1197 | 789 |
! ! |
790 |
||
1880 | 791 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 792 |
|
793 |
version |
|
3427
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
794 |
^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.31 1998-05-08 12:04:05 cg Exp $' |
1197 | 795 |
! ! |