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