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