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