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