author | Claus Gittinger <cg@exept.de> |
Thu, 12 Aug 1999 10:46:07 +0200 | |
changeset 4591 | 64d6624dae5e |
parent 4455 | 2d31d0d986be |
child 4593 | 1023fa0c779e |
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 |
||
4297 | 148 |
fastFromString:aString at:startIndex |
149 |
"return the next ShortFloat from the string starting at startIndex. |
|
150 |
No spaces are skipped. |
|
151 |
||
152 |
This is a specially tuned entry (using a low-level C-call), which |
|
153 |
returns garbage if the argument string is not a valid float number. |
|
154 |
It has been added to allow higher speed string decomposition into |
|
155 |
numbers." |
|
156 |
%{ |
|
157 |
if (__isString(aString) && __isSmallInteger(startIndex)) { |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
158 |
char *cp = (char *)(__stringVal(aString)); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
159 |
int idx = __intVal(startIndex) - 1; |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
160 |
double atof(); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
161 |
double val; |
4297 | 162 |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
163 |
if ((unsigned)idx < __stringSize(aString)) { |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
164 |
val = atof(cp + idx); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
165 |
RETURN (__MKSFLOAT(val)); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
166 |
} |
4297 | 167 |
} |
168 |
%}. |
|
169 |
self primitiveFailed. |
|
170 |
||
171 |
" |
|
172 |
ShortFloat fastFromString:'123.45' at:1 |
|
173 |
ShortFloat fastFromString:'123.45' at:2 |
|
174 |
ShortFloat fastFromString:'123.45E4' at:1 |
|
175 |
ShortFloat fastFromString:'hello123.45E4' at:6 |
|
176 |
ShortFloat fastFromString:'12345' at:1 |
|
177 |
ShortFloat fastFromString:'12345' at:2 |
|
178 |
ShortFloat fastFromString:'12345' at:3 |
|
179 |
ShortFloat fastFromString:'12345' at:4 |
|
180 |
ShortFloat fastFromString:'12345' at:5 |
|
181 |
ShortFloat fastFromString:'12345' at:6 |
|
182 |
ShortFloat fastFromString:'12345' at:0 |
|
183 |
ShortFloat fastFromString:'hello123.45E4' at:1 |
|
184 |
||
185 |
Time millisecondsToRun:[ |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
186 |
100000 timesRepeat:[ |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
187 |
ShortFloat readFrom:'123.45' |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
188 |
] |
4297 | 189 |
] |
190 |
" |
|
191 |
||
192 |
" |
|
193 |
Time millisecondsToRun:[ |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
194 |
100000 timesRepeat:[ |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
195 |
ShortFloat fastFromString:'123.45' at:1 |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
196 |
] |
4297 | 197 |
] |
198 |
" |
|
199 |
||
200 |
||
201 |
||
202 |
! |
|
203 |
||
3158 | 204 |
readFrom:aStringOrStream onError:exceptionBlock |
205 |
"read a shortFloat from a string" |
|
206 |
||
207 |
|num| |
|
208 |
||
209 |
num := super readFrom:aStringOrStream onError:nil. |
|
210 |
num isNil ifTrue:[ |
|
211 |
^ exceptionBlock value |
|
212 |
]. |
|
213 |
^ num asShortFloat |
|
214 |
||
215 |
" |
|
216 |
ShortFloat readFrom:'0.1' |
|
217 |
ShortFloat readFrom:'0' |
|
218 |
" |
|
219 |
||
220 |
"Modified: / 7.1.1998 / 16:17:59 / cg" |
|
1207 | 221 |
! ! |
222 |
||
1880 | 223 |
!ShortFloat class methodsFor:'constants'! |
1197 | 224 |
|
225 |
pi |
|
1260 | 226 |
"return the constant pi as ShortFloat" |
1197 | 227 |
|
228 |
^ 3.14159 asShortFloat |
|
229 |
||
1260 | 230 |
"Modified: 23.4.1996 / 09:26:31 / cg" |
1197 | 231 |
! |
232 |
||
233 |
unity |
|
1260 | 234 |
"return the neutral element for multiplication (1.0) as ShortFloat" |
1197 | 235 |
|
236 |
^ 1.0 asShortFloat |
|
237 |
||
1260 | 238 |
"Modified: 23.4.1996 / 09:26:51 / cg" |
1197 | 239 |
! |
240 |
||
241 |
zero |
|
1260 | 242 |
"return the neutral element for addition (0.0) as ShortFloat" |
1197 | 243 |
|
244 |
^ 0.0 asShortFloat |
|
245 |
||
1260 | 246 |
"Modified: 23.4.1996 / 09:26:45 / cg" |
1197 | 247 |
! ! |
248 |
||
1880 | 249 |
!ShortFloat class methodsFor:'queries'! |
1197 | 250 |
|
251 |
isBuiltInClass |
|
1264 | 252 |
"return true if this class is known by the run-time-system. |
253 |
Here, true is returned for myself, false for subclasses." |
|
1197 | 254 |
|
255 |
^ self == ShortFloat |
|
256 |
||
1264 | 257 |
"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
|
258 |
! |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
259 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
260 |
isIEEEFormat |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
261 |
"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
|
262 |
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
|
263 |
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
|
264 |
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
|
265 |
machine). |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
266 |
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
|
267 |
(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
|
268 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
269 |
^ true "/ this may be a lie |
1197 | 270 |
! ! |
271 |
||
272 |
!ShortFloat methodsFor:'arithmetic'! |
|
273 |
||
274 |
* aNumber |
|
275 |
"return the product of the receiver and the argument, aNumber" |
|
276 |
||
277 |
%{ /* NOCONTEXT */ |
|
278 |
||
279 |
OBJ newFloat; |
|
280 |
float result; |
|
281 |
double dResult; |
|
282 |
||
283 |
if (__isSmallInteger(aNumber)) { |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
284 |
result = __shortFloatVal(self) * (float)(__intVal(aNumber)); |
1197 | 285 |
retResult: |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
286 |
__qMKSFLOAT(newFloat, result); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
287 |
RETURN ( newFloat ); |
3531 | 288 |
} else if (__isShortFloat(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
289 |
result = __shortFloatVal(self) * __shortFloatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
290 |
goto retResult; |
3531 | 291 |
} else if (__isFloatLike(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
292 |
dResult = (double) __shortFloatVal(self)* __floatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
293 |
__qMKFLOAT(newFloat, dResult); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
294 |
RETURN ( newFloat ); |
1197 | 295 |
} |
296 |
%}. |
|
297 |
^ aNumber productFromShortFloat:self |
|
298 |
! |
|
299 |
||
300 |
+ aNumber |
|
301 |
"return the sum of the receiver and the argument, aNumber" |
|
302 |
||
303 |
%{ /* NOCONTEXT */ |
|
304 |
||
305 |
OBJ newFloat; |
|
306 |
float result; |
|
307 |
double dResult; |
|
308 |
||
309 |
if (__isSmallInteger(aNumber)) { |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
310 |
result = __shortFloatVal(self) + (float)(__intVal(aNumber)); |
1197 | 311 |
retResult: |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
312 |
__qMKSFLOAT(newFloat, result); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
313 |
RETURN ( newFloat ); |
3531 | 314 |
} else if (__isShortFloat(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
315 |
result = __shortFloatVal(self) + __shortFloatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
316 |
goto retResult; |
3531 | 317 |
} else if (__isFloatLike(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
318 |
dResult = (double) __shortFloatVal(self) + __floatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
319 |
__qMKFLOAT(newFloat, dResult); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
320 |
RETURN ( newFloat ); |
1197 | 321 |
} |
322 |
%}. |
|
323 |
^ aNumber sumFromShortFloat:self |
|
324 |
! |
|
325 |
||
326 |
- aNumber |
|
327 |
"return the difference of the receiver and the argument, aNumber" |
|
328 |
||
329 |
%{ /* NOCONTEXT */ |
|
330 |
||
331 |
OBJ newFloat; |
|
332 |
float result; |
|
333 |
double dResult; |
|
334 |
||
335 |
if (__isSmallInteger(aNumber)) { |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
336 |
result = __shortFloatVal(self) - (float)(__intVal(aNumber)); |
1197 | 337 |
retResult: |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
338 |
__qMKSFLOAT(newFloat, result); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
339 |
RETURN ( newFloat ); |
3531 | 340 |
} else if (__isShortFloat(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
341 |
result = __shortFloatVal(self) - __shortFloatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
342 |
goto retResult; |
3531 | 343 |
} else if (__isFloatLike(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
344 |
dResult = (double) __shortFloatVal(self) - __floatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
345 |
__qMKFLOAT(newFloat, dResult); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
346 |
RETURN ( newFloat ); |
1197 | 347 |
} |
348 |
%}. |
|
349 |
^ aNumber differenceFromShortFloat:self |
|
350 |
! |
|
351 |
||
352 |
/ aNumber |
|
353 |
"return the quotient of the receiver and the argument, aNumber" |
|
354 |
||
355 |
%{ /* NOCONTEXT */ |
|
356 |
||
357 |
OBJ newFloat; |
|
358 |
float result, val; |
|
359 |
double dResult, dVal; |
|
360 |
||
361 |
if (__isSmallInteger(aNumber)) { |
|
4455
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
362 |
if (aNumber != __MKSMALLINT(0)) { |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
363 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
1197 | 364 |
retResult: |
4455
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
365 |
__qMKSFLOAT(newFloat, result); |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
366 |
RETURN ( newFloat ); |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
367 |
} |
3528
bc45c489b1b2
oops - division by zero was not handled correctly
Claus Gittinger <cg@exept.de>
parents:
3473
diff
changeset
|
368 |
} else if (__isShortFloat(aNumber)) { |
4455
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
369 |
val = __shortFloatVal(aNumber); |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
370 |
if (val != 0.0) { |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
371 |
result = __shortFloatVal(self) / val; |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
372 |
goto retResult; |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
373 |
} |
3528
bc45c489b1b2
oops - division by zero was not handled correctly
Claus Gittinger <cg@exept.de>
parents:
3473
diff
changeset
|
374 |
} else if (__isFloatLike(aNumber)) { |
4455
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
375 |
dVal = __floatVal(aNumber); |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
376 |
if (dVal != 0.0) { |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
377 |
dResult = (double) __shortFloatVal(self) / dVal; |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
378 |
__qMKFLOAT(newFloat, dResult); |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
379 |
RETURN ( newFloat ); |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
380 |
} |
1197 | 381 |
} |
382 |
%}. |
|
383 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
4455
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
384 |
" |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
385 |
No, you shalt not divide by zero |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
386 |
" |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
4305
diff
changeset
|
387 |
^ DivisionByZeroSignal raiseRequest. |
1197 | 388 |
]. |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
389 |
^ aNumber quotientFromShortFloat:self |
1197 | 390 |
|
391 |
! |
|
392 |
||
393 |
negated |
|
394 |
"return myself negated" |
|
395 |
||
396 |
%{ /* NOCONTEXT */ |
|
397 |
OBJ newFloat; |
|
398 |
float rslt = - __shortFloatVal(self); |
|
399 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
400 |
__qMKSFLOAT(newFloat, rslt); |
1197 | 401 |
RETURN ( newFloat ); |
402 |
%} |
|
403 |
||
3147 | 404 |
! |
405 |
||
406 |
uncheckedDivide:aNumber |
|
407 |
"return the quotient of the receiver and the argument, aNumber |
|
408 |
Do not check for divide by zero (return NaN or infinity)" |
|
409 |
||
410 |
%{ /* NOCONTEXT */ |
|
411 |
||
412 |
OBJ newFloat; |
|
413 |
float result, val; |
|
414 |
double dResult, dVal; |
|
415 |
||
416 |
if (__isSmallInteger(aNumber)) { |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
417 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
3147 | 418 |
retResult: |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
419 |
__qMKSFLOAT(newFloat, result); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
420 |
RETURN ( newFloat ); |
3529 | 421 |
} else if (__isShortFloat(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
422 |
val = __shortFloatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
423 |
result = __shortFloatVal(self) / val; |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
424 |
goto retResult; |
3529 | 425 |
} else if (__isFloatLike(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
426 |
dVal = __floatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
427 |
dResult = (double) __shortFloatVal(self) / dVal; |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
428 |
__qMKFLOAT(newFloat, dResult); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
429 |
RETURN ( newFloat ); |
3147 | 430 |
} |
431 |
%}. |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
432 |
^ aNumber quotientFromShortFloat:self |
3147 | 433 |
|
434 |
" |
|
435 |
0.0 asShortFloat uncheckedDivide:0 |
|
436 |
1.0 asShortFloat uncheckedDivide:0.0 |
|
437 |
" |
|
1197 | 438 |
! ! |
439 |
||
440 |
!ShortFloat methodsFor:'coercion and converting'! |
|
441 |
||
442 |
asFloat |
|
443 |
"return a Float with same value as the receiver" |
|
444 |
||
445 |
%{ /* NOCONTEXT */ |
|
446 |
||
447 |
OBJ newFloat; |
|
448 |
double dVal = (double)__shortFloatVal(self); |
|
449 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
450 |
__qMKFLOAT(newFloat, dVal); |
1197 | 451 |
RETURN ( newFloat ); |
452 |
%} |
|
453 |
||
454 |
" |
|
1583 | 455 |
1.0 asShortFloat asFloat |
1197 | 456 |
" |
457 |
! |
|
458 |
||
459 |
asInteger |
|
460 |
"return an integer with same value - might truncate" |
|
461 |
||
462 |
%{ /* NOCONTEXT */ |
|
1670 | 463 |
float fVal; |
1197 | 464 |
|
1670 | 465 |
fVal = __shortFloatVal(self); |
466 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
3158 | 467 |
RETURN ( __MKSMALLINT( (INT)fVal) ); |
1197 | 468 |
} |
469 |
%}. |
|
470 |
^ super asInteger |
|
471 |
||
472 |
" |
|
473 |
12345.0 asShortFloat asInteger |
|
474 |
1e15 asShortFloat asInteger |
|
475 |
" |
|
476 |
! |
|
477 |
||
478 |
asShortFloat |
|
479 |
"return a ShortFloat with same value as the receiver - thats me" |
|
480 |
||
481 |
^ self |
|
1200 | 482 |
! |
483 |
||
484 |
generality |
|
485 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
486 |
||
487 |
^ 70 |
|
488 |
||
489 |
||
1197 | 490 |
! ! |
491 |
||
492 |
!ShortFloat methodsFor:'comparing'! |
|
493 |
||
494 |
< aNumber |
|
495 |
"return true, if the argument is greater" |
|
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 |
^ aNumber lessFromShortFloat:self |
|
510 |
||
1200 | 511 |
" |
512 |
1.0 asShortFloat > (1/3) |
|
513 |
" |
|
1197 | 514 |
! |
515 |
||
516 |
<= aNumber |
|
517 |
"return true, if the argument is greater or equal" |
|
518 |
||
519 |
%{ /* NOCONTEXT */ |
|
520 |
||
521 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 522 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 523 |
} |
524 |
if (__isFloatLike(aNumber)) { |
|
3158 | 525 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
1197 | 526 |
} |
527 |
if (__isShortFloat(aNumber)) { |
|
3158 | 528 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
1197 | 529 |
} |
530 |
%}. |
|
531 |
^ self retry:#<= coercing:aNumber |
|
532 |
||
533 |
! |
|
534 |
||
535 |
= aNumber |
|
536 |
"return true, if the arguments value are equal by value" |
|
537 |
||
538 |
%{ /* NOCONTEXT */ |
|
539 |
||
540 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 541 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
1197 | 542 |
} |
543 |
if (__isFloatLike(aNumber)) { |
|
3158 | 544 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
1197 | 545 |
} |
546 |
if (__isShortFloat(aNumber)) { |
|
3158 | 547 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
1197 | 548 |
} |
549 |
%}. |
|
550 |
^ self retry:#= coercing:aNumber |
|
551 |
||
552 |
! |
|
553 |
||
554 |
> aNumber |
|
555 |
"return true, if the argument is less" |
|
556 |
||
557 |
%{ /* NOCONTEXT */ |
|
558 |
||
559 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 560 |
RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false ); |
1197 | 561 |
} |
562 |
if (__isFloatLike(aNumber)) { |
|
3158 | 563 |
RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false ); |
1197 | 564 |
} |
565 |
if (__isShortFloat(aNumber)) { |
|
3158 | 566 |
RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false ); |
1197 | 567 |
} |
568 |
%}. |
|
569 |
^ self retry:#> coercing:aNumber |
|
570 |
! |
|
571 |
||
572 |
>= aNumber |
|
573 |
"return true, if the argument is less or equal" |
|
574 |
||
575 |
%{ /* NOCONTEXT */ |
|
576 |
||
577 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 578 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 579 |
} |
580 |
if (__isFloatLike(aNumber)) { |
|
3158 | 581 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
1197 | 582 |
} |
583 |
if (__isShortFloat(aNumber)) { |
|
3158 | 584 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
1197 | 585 |
} |
586 |
%}. |
|
587 |
^ self retry:#>= coercing:aNumber |
|
588 |
! |
|
589 |
||
590 |
hash |
|
591 |
"return a number for hashing; redefined, since floats compare |
|
592 |
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same |
|
593 |
as 3 hash." |
|
594 |
||
595 |
|i| |
|
596 |
||
597 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
4591 | 598 |
i := self asInteger. |
599 |
self = i ifTrue:[ |
|
600 |
^ i hash |
|
601 |
]. |
|
1197 | 602 |
]. |
603 |
||
604 |
" |
|
605 |
mhmh take some of my value-bits to hash on |
|
606 |
" |
|
4591 | 607 |
^ (((self basicAt:4) bitAnd:16r1F) bitShift:24) + |
1197 | 608 |
((self basicAt:3) bitShift:16) + |
609 |
((self basicAt:2) bitShift:8) + |
|
610 |
(self basicAt:1) |
|
611 |
||
612 |
" |
|
613 |
1.2345 hash |
|
614 |
1.2345 asShortFloat hash |
|
615 |
1.0 hash |
|
616 |
1.0 asShortFloat hash |
|
617 |
" |
|
618 |
! |
|
619 |
||
620 |
~= aNumber |
|
621 |
"return true, if the arguments value are not equal" |
|
622 |
||
623 |
%{ /* NOCONTEXT */ |
|
624 |
||
625 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 626 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
1197 | 627 |
} |
628 |
if (__isFloatLike(aNumber)) { |
|
3158 | 629 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
1197 | 630 |
} |
631 |
if (__isShortFloat(aNumber)) { |
|
3158 | 632 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
1197 | 633 |
} |
634 |
%}. |
|
635 |
^ self retry:#~= coercing:aNumber |
|
636 |
||
637 |
! ! |
|
638 |
||
639 |
!ShortFloat methodsFor:'printing & storing'! |
|
640 |
||
641 |
printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
642 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
643 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
644 |
#printOn: as basic print mechanism." |
1197 | 645 |
|
646 |
%{ /* NOCONTEXT */ |
|
647 |
||
648 |
char buffer[64]; |
|
649 |
REGISTER char *cp; |
|
650 |
OBJ s; |
|
651 |
||
652 |
/* |
|
653 |
* actually only needed on sparc: since thisContext is |
|
654 |
* in a global register, which gets destroyed by printf, |
|
655 |
* manually save it here - very stupid ... |
|
656 |
*/ |
|
657 |
__BEGIN_PROTECT_REGISTERS__ |
|
658 |
||
659 |
#ifdef SYSV |
|
660 |
sprintf(buffer, "%.6lg", (double)__shortFloatVal(self)); |
|
661 |
#else |
|
662 |
sprintf(buffer, "%.6G", (double)__shortFloatVal(self)); |
|
663 |
#endif |
|
664 |
||
665 |
__END_PROTECT_REGISTERS__ |
|
666 |
||
667 |
/* |
|
668 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
|
669 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
|
670 |
*/ |
|
671 |
for (cp = buffer; *cp; cp++) { |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
672 |
if ((*cp == '.') || (*cp == 'e')) break; |
1197 | 673 |
} |
674 |
if (! *cp) { |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
675 |
*cp++ = '.'; |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
676 |
*cp++ = '0'; |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
677 |
*cp = '\0'; |
1197 | 678 |
} |
679 |
||
680 |
s = __MKSTRING(buffer COMMA_SND); |
|
681 |
if (s != nil) { |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
682 |
RETURN (s); |
1197 | 683 |
} |
684 |
%}. |
|
685 |
" |
|
686 |
memory allocation (for the new string) failed. |
|
687 |
When we arrive here, there was no memory, even after a garbage collect. |
|
688 |
This means, that the VM wanted to get some more memory from the |
|
689 |
OS, which was not kind enough to give it. |
|
690 |
Bad luck - you should increase the swap space on your machine. |
|
691 |
" |
|
692 |
^ ObjectMemory allocationFailureSignal raise. |
|
693 |
! ! |
|
694 |
||
3897 | 695 |
!ShortFloat methodsFor:'special access'! |
696 |
||
697 |
exponent |
|
698 |
"extract a normalized floats exponent. |
|
699 |
The returned value depends on the float-representation of |
|
700 |
the underlying machine and is therefore highly unportable. |
|
701 |
This is not for general use. |
|
702 |
This assumes that the mantissa is normalized to |
|
703 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
704 |
||
705 |
%{ /* NOCONTEXT */ |
|
706 |
||
707 |
double frexp(); |
|
708 |
double frac; |
|
709 |
INT exp; |
|
710 |
||
711 |
errno = 0; |
|
712 |
frac = frexp( (double)(__shortFloatVal(self)), &exp); |
|
713 |
if (errno == 0) { |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
714 |
RETURN (__MKSMALLINT(exp)); |
3897 | 715 |
} |
716 |
%}. |
|
717 |
^ self primitiveFailed |
|
718 |
||
719 |
" |
|
720 |
1.0 asShortFloat exponent |
|
721 |
1.0 asShortFloat exponent |
|
722 |
0.5 asShortFloat exponent |
|
723 |
0.25 asShortFloat exponent |
|
724 |
0.00000011111 asShortFloat exponent |
|
725 |
" |
|
726 |
! |
|
727 |
||
728 |
mantissa |
|
729 |
"extract a normalized floats mantissa. |
|
730 |
The returned value depends on the float-representation of |
|
731 |
the underlying machine and is therefore highly unportable. |
|
732 |
This is not for general use. |
|
733 |
This assumes that the mantissa is normalized to |
|
734 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
735 |
||
736 |
%{ /* NOCONTEXT */ |
|
737 |
||
738 |
double frexp(); |
|
739 |
double frac; |
|
740 |
INT exp; |
|
741 |
||
742 |
errno = 0; |
|
743 |
frac = frexp( (double)(__shortFloatVal(self)), &exp); |
|
744 |
if (errno == 0) { |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
745 |
RETURN (__MKFLOAT(frac)); |
3897 | 746 |
} |
747 |
%}. |
|
748 |
^ self primitiveFailed |
|
749 |
||
750 |
" |
|
751 |
1.0 asShortFloat exponent |
|
752 |
1.0 asShortFloat mantissa |
|
753 |
||
754 |
0.5 asShortFloat exponent |
|
755 |
0.5 asShortFloat mantissa |
|
756 |
||
757 |
0.25 asShortFloat exponent |
|
758 |
0.25 asShortFloat mantissa |
|
759 |
||
760 |
0.00000011111 asShortFloat exponent |
|
761 |
0.00000011111 asShortFloat mantissa |
|
762 |
" |
|
763 |
! ! |
|
764 |
||
1197 | 765 |
!ShortFloat methodsFor:'testing'! |
766 |
||
3147 | 767 |
isFinite |
768 |
"return true, if the receiver is a finite float |
|
769 |
i.e. not NaN and not infinite." |
|
770 |
||
771 |
%{ /* NOCONTEXT */ |
|
772 |
||
773 |
double dV = (double) __shortFloatVal(self); |
|
774 |
||
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
775 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
776 |
* 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
|
777 |
* a finite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
778 |
* this may always ret true here ... |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
779 |
*/ |
3147 | 780 |
if (finite(dV)) { RETURN (true); } |
781 |
%}. |
|
782 |
^false |
|
783 |
||
784 |
" |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
785 |
1.0 asShortFloat isFinite |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
786 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
787 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
3147 | 788 |
" |
789 |
! |
|
790 |
||
2387 | 791 |
isNaN |
792 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
793 |
These are not created by ST/X float operations (they raise an exception); |
|
794 |
however, inline C-code could produce them ..." |
|
795 |
||
796 |
%{ /* NOCONTEXT */ |
|
797 |
||
3139 | 798 |
double dV = (double)(__shortFloatVal(self)); |
799 |
||
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
800 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
801 |
* 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
|
802 |
* a finite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
803 |
* this may always ret false here ... |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
804 |
*/ |
3147 | 805 |
if (isnan(dV)) { RETURN (true); } |
806 |
||
807 |
#if 0 /* Currently all our systems support isnan() |
|
808 |
* If not, you have to fix librun/jinterpret.c also. |
|
809 |
*/ |
|
810 |
||
2395 | 811 |
/* |
812 |
* sigh - every vendor is playing its own game here ... |
|
813 |
* Q: what are standards worth, anyway ? |
|
814 |
*/ |
|
3139 | 815 |
#ifdef IS_NAN |
816 |
if (IS_NAN(dV)) { RETURN (true); } |
|
817 |
RETURN (false); |
|
818 |
#endif |
|
819 |
||
2393 | 820 |
#ifdef IS_QNAN |
3139 | 821 |
if (IS_QNAN(dV)) { RETURN (true); } |
822 |
RETURN (false); |
|
2393 | 823 |
#endif |
2392 | 824 |
|
2393 | 825 |
#ifdef FLT_SNAN |
3139 | 826 |
if (dV == FLT_SNAN) { RETURN (true); } |
827 |
RETURN (false); |
|
2393 | 828 |
#endif |
3139 | 829 |
|
2393 | 830 |
#ifdef FLT_QNAN |
3139 | 831 |
if (dV == FLT_QNAN) { RETURN (true); } |
832 |
RETURN (false); |
|
2393 | 833 |
#endif |
834 |
||
835 |
#ifdef _SNANF |
|
3139 | 836 |
if (dV == _SNAN) { RETURN (true); } |
837 |
RETURN (false); |
|
2393 | 838 |
#endif |
3139 | 839 |
|
2393 | 840 |
#ifdef _QNANF |
3139 | 841 |
if (dV == _QNAN) { RETURN (true); } |
842 |
RETURN (false); |
|
2393 | 843 |
#endif |
2392 | 844 |
|
2395 | 845 |
#ifdef IsPosNAN |
3139 | 846 |
if IsPosNAN(dV) { RETURN (true); } |
847 |
RETURN (false); |
|
2395 | 848 |
#endif |
3139 | 849 |
|
2395 | 850 |
#ifdef IsNegNAN |
3139 | 851 |
if IsNegNAN(dV) { RETURN (true); } |
852 |
RETURN (false); |
|
2395 | 853 |
#endif |
854 |
||
2393 | 855 |
#ifdef NAN |
3139 | 856 |
if (dV == NAN) { RETURN (true); } |
857 |
RETURN (false); |
|
2400 | 858 |
#endif |
2389 | 859 |
|
3139 | 860 |
#ifdef NaN |
861 |
if (NaN(dV)) { RETURN (true); } |
|
862 |
RETURN (false); |
|
863 |
#endif |
|
3147 | 864 |
|
865 |
#endif /* 0 */ |
|
2392 | 866 |
%}. |
867 |
^ false |
|
2387 | 868 |
|
3147 | 869 |
" |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
870 |
1.0 asShortFloat isNaN |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
871 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
3147 | 872 |
" |
2387 | 873 |
! |
874 |
||
1197 | 875 |
negative |
876 |
"return true if the receiver is less than zero" |
|
877 |
||
878 |
%{ /* NOCONTEXT */ |
|
879 |
||
880 |
RETURN ( (__shortFloatVal(self) < 0.0) ? true : false ); |
|
881 |
%} |
|
882 |
! |
|
883 |
||
884 |
positive |
|
885 |
"return true if the receiver is greater or equal to zero" |
|
886 |
||
887 |
%{ /* NOCONTEXT */ |
|
888 |
||
889 |
RETURN ( (__shortFloatVal(self) >= 0.0) ? true : false ); |
|
890 |
%} |
|
1880 | 891 |
! |
892 |
||
893 |
strictlyPositive |
|
894 |
"return true if the receiver is greater than zero" |
|
895 |
||
896 |
%{ /* NOCONTEXT */ |
|
897 |
||
898 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
899 |
%} |
|
1197 | 900 |
! ! |
901 |
||
3897 | 902 |
!ShortFloat methodsFor:'truncation and rounding'! |
903 |
||
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
904 |
floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
905 |
"return the integer nearest the receiver towards negative infinity." |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
906 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
907 |
|val| |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
908 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
909 |
%{ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
910 |
float fVal; |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
911 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
912 |
/* |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
913 |
* ST-80 (and X3J20) returns integer. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
914 |
*/ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
915 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
916 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
917 |
RETURN ( __MKSMALLINT( (INT) fVal ) ); |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
918 |
} |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
919 |
__qMKSFLOAT(val, fVal); |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
920 |
%}. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
921 |
^ val asInteger |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
922 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
923 |
" |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
924 |
0.5 asShortFloat floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
925 |
-0.5 asShortFloat floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
926 |
" |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
927 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
928 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
929 |
! |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
930 |
|
3897 | 931 |
fractionPart |
932 |
"extract the after-decimal fraction part. |
|
933 |
the floats value is |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
934 |
float truncated + float fractionalPart" |
3897 | 935 |
|
936 |
%{ /* NOCONTEXT */ |
|
937 |
||
938 |
double modf(); |
|
939 |
double frac, trunc; |
|
940 |
||
941 |
errno = 0; |
|
942 |
frac = modf((double)(__shortFloatVal(self)), &trunc); |
|
943 |
if (errno == 0) { |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
944 |
RETURN (__MKSFLOAT(frac)); |
3897 | 945 |
} |
946 |
%}. |
|
947 |
^ self primitiveFailed |
|
948 |
||
949 |
" |
|
950 |
1.0 asShortFloat fractionalPart |
|
951 |
0.5 asShortFloat fractionalPart |
|
952 |
0.25 asShortFloat fractionalPart |
|
953 |
3.14159 asShortFloat fractionalPart |
|
954 |
12345673.14159 asShortFloat fractionalPart |
|
955 |
123456731231231231.14159 asShortFloat fractionalPart |
|
956 |
" |
|
957 |
||
958 |
! ! |
|
959 |
||
1880 | 960 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 961 |
|
962 |
version |
|
4591 | 963 |
^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.49 1999-08-12 08:46:07 cg Exp $' |
1197 | 964 |
! ! |