author | Claus Gittinger <cg@exept.de> |
Fri, 02 Aug 2002 19:27:04 +0200 | |
changeset 6707 | 790fc577c403 |
parent 6574 | 788f5a36dcb1 |
child 6890 | 8e74eff54c3d |
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 |
5822
4e56f8aadda5
Get nan.h from bits/nan.h for Linux
Stefan Vogel <sv@exept.de>
parents:
5718
diff
changeset
|
53 |
# include <bits/nan.h> |
4825 | 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 |
|
6063 | 260 |
^ 3.14159265358979323846264338327950288419716939937510582097494459 asShortFloat |
1197 | 261 |
|
1260 | 262 |
"Modified: 23.4.1996 / 09:26:31 / cg" |
1197 | 263 |
! |
264 |
||
6574 | 265 |
precision |
266 |
"answer the precision of a Float |
|
267 |
This is an IEEE float, only the fraction from the normalized mantissa is stored |
|
268 |
and so there is a hidden bit and the mantissa is actually represented |
|
269 |
by 24 binary digits" |
|
270 |
||
271 |
^ 24 |
|
272 |
! |
|
273 |
||
274 |
radix |
|
275 |
"answer the radix of a Float |
|
276 |
This is an IEEE float, which is represented as binary" |
|
277 |
||
278 |
^ 2 |
|
279 |
! |
|
280 |
||
1197 | 281 |
unity |
1260 | 282 |
"return the neutral element for multiplication (1.0) as ShortFloat" |
1197 | 283 |
|
284 |
^ 1.0 asShortFloat |
|
285 |
||
1260 | 286 |
"Modified: 23.4.1996 / 09:26:51 / cg" |
1197 | 287 |
! |
288 |
||
289 |
zero |
|
1260 | 290 |
"return the neutral element for addition (0.0) as ShortFloat" |
1197 | 291 |
|
292 |
^ 0.0 asShortFloat |
|
293 |
||
1260 | 294 |
"Modified: 23.4.1996 / 09:26:45 / cg" |
1197 | 295 |
! ! |
296 |
||
1880 | 297 |
!ShortFloat class methodsFor:'queries'! |
1197 | 298 |
|
299 |
isBuiltInClass |
|
1264 | 300 |
"return true if this class is known by the run-time-system. |
301 |
Here, true is returned for myself, false for subclasses." |
|
1197 | 302 |
|
303 |
^ self == ShortFloat |
|
304 |
||
1264 | 305 |
"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
|
306 |
! |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
307 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
308 |
isIEEEFormat |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
309 |
"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
|
310 |
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
|
311 |
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
|
312 |
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
|
313 |
machine). |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
314 |
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
|
315 |
(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
|
316 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
317 |
^ true "/ this may be a lie |
1197 | 318 |
! ! |
319 |
||
320 |
!ShortFloat methodsFor:'arithmetic'! |
|
321 |
||
322 |
* aNumber |
|
323 |
"return the product of the receiver and the argument, aNumber" |
|
324 |
||
325 |
%{ /* NOCONTEXT */ |
|
326 |
||
327 |
OBJ newFloat; |
|
328 |
float result; |
|
329 |
double dResult; |
|
330 |
||
331 |
if (__isSmallInteger(aNumber)) { |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
332 |
result = __shortFloatVal(self) * (float)(__intVal(aNumber)); |
1197 | 333 |
retResult: |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
334 |
__qMKSFLOAT(newFloat, result); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
335 |
RETURN ( newFloat ); |
3531 | 336 |
} else if (__isShortFloat(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
337 |
result = __shortFloatVal(self) * __shortFloatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
338 |
goto retResult; |
3531 | 339 |
} else if (__isFloatLike(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
340 |
dResult = (double) __shortFloatVal(self)* __floatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
341 |
__qMKFLOAT(newFloat, dResult); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
342 |
RETURN ( newFloat ); |
1197 | 343 |
} |
344 |
%}. |
|
345 |
^ aNumber productFromShortFloat:self |
|
346 |
! |
|
347 |
||
348 |
+ aNumber |
|
349 |
"return the sum of the receiver and the argument, aNumber" |
|
350 |
||
351 |
%{ /* NOCONTEXT */ |
|
352 |
||
353 |
OBJ newFloat; |
|
354 |
float result; |
|
355 |
double dResult; |
|
356 |
||
357 |
if (__isSmallInteger(aNumber)) { |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
358 |
result = __shortFloatVal(self) + (float)(__intVal(aNumber)); |
1197 | 359 |
retResult: |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
360 |
__qMKSFLOAT(newFloat, result); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
361 |
RETURN ( newFloat ); |
3531 | 362 |
} else if (__isShortFloat(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
363 |
result = __shortFloatVal(self) + __shortFloatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
364 |
goto retResult; |
3531 | 365 |
} else if (__isFloatLike(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
366 |
dResult = (double) __shortFloatVal(self) + __floatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
367 |
__qMKFLOAT(newFloat, dResult); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
368 |
RETURN ( newFloat ); |
1197 | 369 |
} |
370 |
%}. |
|
371 |
^ aNumber sumFromShortFloat:self |
|
372 |
! |
|
373 |
||
374 |
- aNumber |
|
375 |
"return the difference of the receiver and the argument, aNumber" |
|
376 |
||
377 |
%{ /* NOCONTEXT */ |
|
378 |
||
379 |
OBJ newFloat; |
|
380 |
float result; |
|
381 |
double dResult; |
|
382 |
||
383 |
if (__isSmallInteger(aNumber)) { |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
384 |
result = __shortFloatVal(self) - (float)(__intVal(aNumber)); |
1197 | 385 |
retResult: |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
386 |
__qMKSFLOAT(newFloat, result); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
387 |
RETURN ( newFloat ); |
3531 | 388 |
} else if (__isShortFloat(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
389 |
result = __shortFloatVal(self) - __shortFloatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
390 |
goto retResult; |
3531 | 391 |
} else if (__isFloatLike(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
392 |
dResult = (double) __shortFloatVal(self) - __floatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
393 |
__qMKFLOAT(newFloat, dResult); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
394 |
RETURN ( newFloat ); |
1197 | 395 |
} |
396 |
%}. |
|
397 |
^ aNumber differenceFromShortFloat:self |
|
398 |
! |
|
399 |
||
400 |
/ aNumber |
|
401 |
"return the quotient of the receiver and the argument, aNumber" |
|
402 |
||
403 |
%{ /* NOCONTEXT */ |
|
404 |
||
405 |
OBJ newFloat; |
|
406 |
float result, val; |
|
407 |
double dResult, dVal; |
|
408 |
||
409 |
if (__isSmallInteger(aNumber)) { |
|
5954 | 410 |
if (aNumber != __MKSMALLINT(0)) { |
411 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
1197 | 412 |
retResult: |
5954 | 413 |
__qMKSFLOAT(newFloat, result); |
414 |
RETURN ( newFloat ); |
|
415 |
} |
|
3528
bc45c489b1b2
oops - division by zero was not handled correctly
Claus Gittinger <cg@exept.de>
parents:
3473
diff
changeset
|
416 |
} else if (__isShortFloat(aNumber)) { |
5954 | 417 |
val = __shortFloatVal(aNumber); |
418 |
if (val != 0.0) { |
|
419 |
result = __shortFloatVal(self) / val; |
|
420 |
goto retResult; |
|
421 |
} |
|
3528
bc45c489b1b2
oops - division by zero was not handled correctly
Claus Gittinger <cg@exept.de>
parents:
3473
diff
changeset
|
422 |
} else if (__isFloatLike(aNumber)) { |
5954 | 423 |
dVal = __floatVal(aNumber); |
424 |
if (dVal != 0.0) { |
|
425 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
426 |
__qMKFLOAT(newFloat, dResult); |
|
427 |
RETURN ( newFloat ); |
|
428 |
} |
|
1197 | 429 |
} |
430 |
%}. |
|
431 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
5954 | 432 |
" |
433 |
No, you shalt not divide by zero |
|
434 |
" |
|
435 |
^ DivisionByZeroSignal raiseRequestWith:thisContext. |
|
1197 | 436 |
]. |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
437 |
^ aNumber quotientFromShortFloat:self |
1197 | 438 |
! |
439 |
||
440 |
negated |
|
441 |
"return myself negated" |
|
442 |
||
443 |
%{ /* NOCONTEXT */ |
|
444 |
OBJ newFloat; |
|
445 |
float rslt = - __shortFloatVal(self); |
|
446 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
447 |
__qMKSFLOAT(newFloat, rslt); |
1197 | 448 |
RETURN ( newFloat ); |
5411 | 449 |
%}. |
450 |
^ 0.0 - self |
|
1197 | 451 |
|
3147 | 452 |
! |
453 |
||
454 |
uncheckedDivide:aNumber |
|
455 |
"return the quotient of the receiver and the argument, aNumber |
|
456 |
Do not check for divide by zero (return NaN or infinity)" |
|
457 |
||
458 |
%{ /* NOCONTEXT */ |
|
459 |
||
460 |
OBJ newFloat; |
|
461 |
float result, val; |
|
462 |
double dResult, dVal; |
|
463 |
||
464 |
if (__isSmallInteger(aNumber)) { |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
465 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
3147 | 466 |
retResult: |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
467 |
__qMKSFLOAT(newFloat, result); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
468 |
RETURN ( newFloat ); |
3529 | 469 |
} else if (__isShortFloat(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
470 |
val = __shortFloatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
471 |
result = __shortFloatVal(self) / val; |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
472 |
goto retResult; |
3529 | 473 |
} else if (__isFloatLike(aNumber)) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
474 |
dVal = __floatVal(aNumber); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
475 |
dResult = (double) __shortFloatVal(self) / dVal; |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
476 |
__qMKFLOAT(newFloat, dResult); |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
477 |
RETURN ( newFloat ); |
3147 | 478 |
} |
479 |
%}. |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
480 |
^ aNumber quotientFromShortFloat:self |
3147 | 481 |
|
482 |
" |
|
483 |
0.0 asShortFloat uncheckedDivide:0 |
|
484 |
1.0 asShortFloat uncheckedDivide:0.0 |
|
485 |
" |
|
1197 | 486 |
! ! |
487 |
||
5257 | 488 |
!ShortFloat methodsFor:'coercing & converting'! |
1197 | 489 |
|
490 |
asFloat |
|
491 |
"return a Float with same value as the receiver" |
|
492 |
||
493 |
%{ /* NOCONTEXT */ |
|
494 |
||
495 |
OBJ newFloat; |
|
496 |
double dVal = (double)__shortFloatVal(self); |
|
497 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
498 |
__qMKFLOAT(newFloat, dVal); |
1197 | 499 |
RETURN ( newFloat ); |
500 |
%} |
|
501 |
||
502 |
" |
|
1583 | 503 |
1.0 asShortFloat asFloat |
1197 | 504 |
" |
505 |
! |
|
506 |
||
507 |
asInteger |
|
508 |
"return an integer with same value - might truncate" |
|
509 |
||
510 |
%{ /* NOCONTEXT */ |
|
1670 | 511 |
float fVal; |
1197 | 512 |
|
1670 | 513 |
fVal = __shortFloatVal(self); |
514 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
3158 | 515 |
RETURN ( __MKSMALLINT( (INT)fVal) ); |
1197 | 516 |
} |
517 |
%}. |
|
518 |
^ super asInteger |
|
519 |
||
520 |
" |
|
521 |
12345.0 asShortFloat asInteger |
|
522 |
1e15 asShortFloat asInteger |
|
523 |
" |
|
524 |
! |
|
525 |
||
526 |
asShortFloat |
|
527 |
"return a ShortFloat with same value as the receiver - thats me" |
|
528 |
||
529 |
^ self |
|
1200 | 530 |
! |
531 |
||
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
532 |
asTrueFraction |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
533 |
"return a fraction with same value as the receiver" |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
534 |
|
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
535 |
^ self asFloat asTrueFraction |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
536 |
! |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4594
diff
changeset
|
537 |
|
1200 | 538 |
generality |
539 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
540 |
||
541 |
^ 70 |
|
542 |
||
543 |
||
1197 | 544 |
! ! |
545 |
||
546 |
!ShortFloat methodsFor:'comparing'! |
|
547 |
||
548 |
< aNumber |
|
549 |
"return true, if the argument is greater" |
|
550 |
||
551 |
%{ /* NOCONTEXT */ |
|
552 |
||
553 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 554 |
RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false ); |
1197 | 555 |
} |
556 |
if (__isFloatLike(aNumber)) { |
|
3158 | 557 |
RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false ); |
1197 | 558 |
} |
559 |
if (__isShortFloat(aNumber)) { |
|
3158 | 560 |
RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false ); |
1197 | 561 |
} |
562 |
%}. |
|
563 |
^ aNumber lessFromShortFloat:self |
|
564 |
||
1200 | 565 |
" |
566 |
1.0 asShortFloat > (1/3) |
|
567 |
" |
|
1197 | 568 |
! |
569 |
||
570 |
<= aNumber |
|
571 |
"return true, if the argument is greater or equal" |
|
572 |
||
573 |
%{ /* NOCONTEXT */ |
|
574 |
||
575 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 576 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 577 |
} |
578 |
if (__isFloatLike(aNumber)) { |
|
3158 | 579 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
1197 | 580 |
} |
581 |
if (__isShortFloat(aNumber)) { |
|
3158 | 582 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
1197 | 583 |
} |
584 |
%}. |
|
585 |
^ self retry:#<= coercing:aNumber |
|
586 |
||
587 |
! |
|
588 |
||
589 |
= aNumber |
|
590 |
"return true, if the arguments value are equal by value" |
|
591 |
||
592 |
%{ /* NOCONTEXT */ |
|
593 |
||
594 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 595 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
1197 | 596 |
} |
597 |
if (__isFloatLike(aNumber)) { |
|
3158 | 598 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
1197 | 599 |
} |
600 |
if (__isShortFloat(aNumber)) { |
|
3158 | 601 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
1197 | 602 |
} |
603 |
%}. |
|
604 |
^ self retry:#= coercing:aNumber |
|
605 |
||
606 |
! |
|
607 |
||
608 |
> aNumber |
|
609 |
"return true, if the argument is less" |
|
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 |
>= aNumber |
|
627 |
"return true, if the argument is less or equal" |
|
628 |
||
629 |
%{ /* NOCONTEXT */ |
|
630 |
||
631 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 632 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 633 |
} |
634 |
if (__isFloatLike(aNumber)) { |
|
3158 | 635 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
1197 | 636 |
} |
637 |
if (__isShortFloat(aNumber)) { |
|
3158 | 638 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
1197 | 639 |
} |
640 |
%}. |
|
641 |
^ self retry:#>= coercing:aNumber |
|
642 |
! |
|
643 |
||
644 |
hash |
|
645 |
"return a number for hashing; redefined, since floats compare |
|
646 |
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
|
647 |
as 3 hash." |
1197 | 648 |
|
649 |
|i| |
|
650 |
||
651 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
652 |
i := self asInteger. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
653 |
self = i ifTrue:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
654 |
^ i hash |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
655 |
]. |
1197 | 656 |
]. |
657 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
658 |
^ self asFloat hash |
1197 | 659 |
|
660 |
" |
|
661 |
1.2345 hash |
|
662 |
1.2345 asShortFloat hash |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
663 |
1.0 hash |
1197 | 664 |
1.0 asShortFloat hash |
4593 | 665 |
0.5 asShortFloat hash |
666 |
0.25 asShortFloat hash |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
667 |
0.5 hash |
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
668 |
0.25 hash |
1197 | 669 |
" |
670 |
! |
|
671 |
||
672 |
~= aNumber |
|
673 |
"return true, if the arguments value are not equal" |
|
674 |
||
675 |
%{ /* NOCONTEXT */ |
|
676 |
||
677 |
if (__isSmallInteger(aNumber)) { |
|
3158 | 678 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
1197 | 679 |
} |
680 |
if (__isFloatLike(aNumber)) { |
|
3158 | 681 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
1197 | 682 |
} |
683 |
if (__isShortFloat(aNumber)) { |
|
3158 | 684 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
1197 | 685 |
} |
686 |
%}. |
|
687 |
^ self retry:#~= coercing:aNumber |
|
688 |
||
689 |
! ! |
|
690 |
||
691 |
!ShortFloat methodsFor:'printing & storing'! |
|
692 |
||
693 |
printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
694 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
695 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
696 |
#printOn: as basic print mechanism." |
1197 | 697 |
|
698 |
%{ /* NOCONTEXT */ |
|
699 |
||
700 |
char buffer[64]; |
|
701 |
REGISTER char *cp; |
|
702 |
OBJ s; |
|
703 |
||
704 |
/* |
|
705 |
* actually only needed on sparc: since thisContext is |
|
706 |
* in a global register, which gets destroyed by printf, |
|
707 |
* manually save it here - very stupid ... |
|
708 |
*/ |
|
709 |
__BEGIN_PROTECT_REGISTERS__ |
|
710 |
||
711 |
#ifdef SYSV |
|
712 |
sprintf(buffer, "%.6lg", (double)__shortFloatVal(self)); |
|
713 |
#else |
|
714 |
sprintf(buffer, "%.6G", (double)__shortFloatVal(self)); |
|
715 |
#endif |
|
716 |
||
717 |
__END_PROTECT_REGISTERS__ |
|
718 |
||
719 |
/* |
|
720 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
|
721 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
|
722 |
*/ |
|
723 |
for (cp = buffer; *cp; cp++) { |
|
5317
6004c4c2a916
oops - printString was wrong when exponent was involved
Claus Gittinger <cg@exept.de>
parents:
5257
diff
changeset
|
724 |
if ((*cp == '.') || (*cp == 'e') || (*cp == 'E')) break; |
1197 | 725 |
} |
726 |
if (! *cp) { |
|
5317
6004c4c2a916
oops - printString was wrong when exponent was involved
Claus Gittinger <cg@exept.de>
parents:
5257
diff
changeset
|
727 |
*cp++ = '.'; |
6004c4c2a916
oops - printString was wrong when exponent was involved
Claus Gittinger <cg@exept.de>
parents:
5257
diff
changeset
|
728 |
*cp++ = '0'; |
6004c4c2a916
oops - printString was wrong when exponent was involved
Claus Gittinger <cg@exept.de>
parents:
5257
diff
changeset
|
729 |
*cp = '\0'; |
1197 | 730 |
} |
731 |
||
732 |
s = __MKSTRING(buffer COMMA_SND); |
|
733 |
if (s != nil) { |
|
5317
6004c4c2a916
oops - printString was wrong when exponent was involved
Claus Gittinger <cg@exept.de>
parents:
5257
diff
changeset
|
734 |
RETURN (s); |
1197 | 735 |
} |
736 |
%}. |
|
5432 | 737 |
^ self asFloat printString |
5718 | 738 |
! |
739 |
||
740 |
printfPrintString:formatString |
|
741 |
"non-portable: return a printed representation of the receiver |
|
742 |
as specified by formatString, which is defined by printf. |
|
743 |
If you use this, be aware, that specifying doubles differs on |
|
744 |
systems; on SYSV machines you have to give something like %lf, |
|
745 |
while on BSD systems the format string has to be %F. |
|
746 |
Also, the resulting string may not be longer than 255 bytes - |
|
747 |
since thats the (static) size of the buffer. |
|
748 |
This method is NONSTANDARD and may be removed without notice." |
|
749 |
||
750 |
%{ /* STACK: 400 */ |
|
751 |
char buffer[256]; |
|
752 |
OBJ s; |
|
753 |
||
754 |
if (__isString(formatString)) { |
|
755 |
sprintf(buffer, __stringVal(formatString), __shortFloatVal(self)); |
|
756 |
||
757 |
s = __MKSTRING(buffer COMMA_SND); |
|
758 |
if (s != nil) { |
|
759 |
RETURN (s); |
|
760 |
} |
|
761 |
} |
|
762 |
%}. |
|
763 |
self primitiveFailed |
|
764 |
||
765 |
"ShortFloat pi printfPrintString:'%%lg -> %lg'" |
|
766 |
"ShortFloat pi printfPrintString:'%%lf -> %lf'" |
|
767 |
"ShortFloat pi printfPrintString:'%%7.5lg -> %7.5lg'" |
|
768 |
"ShortFloat pi printfPrintString:'%%G -> %G'" |
|
769 |
"ShortFloat pi printfPrintString:'%%F -> %F'" |
|
770 |
"ShortFloat pi printfPrintString:'%%7.5G -> %7.5G'" |
|
771 |
"ShortFloat pi printfPrintString:'%%7.5F -> %7.5F'" |
|
1197 | 772 |
! ! |
773 |
||
5884
92d1470a57dc
added #defaultNumberOfDigits
Claus Gittinger <cg@exept.de>
parents:
5822
diff
changeset
|
774 |
!ShortFloat methodsFor:'queries'! |
92d1470a57dc
added #defaultNumberOfDigits
Claus Gittinger <cg@exept.de>
parents:
5822
diff
changeset
|
775 |
|
92d1470a57dc
added #defaultNumberOfDigits
Claus Gittinger <cg@exept.de>
parents:
5822
diff
changeset
|
776 |
defaultNumberOfDigits |
92d1470a57dc
added #defaultNumberOfDigits
Claus Gittinger <cg@exept.de>
parents:
5822
diff
changeset
|
777 |
"Answer how many digits of accuracy this class supports" |
92d1470a57dc
added #defaultNumberOfDigits
Claus Gittinger <cg@exept.de>
parents:
5822
diff
changeset
|
778 |
|
92d1470a57dc
added #defaultNumberOfDigits
Claus Gittinger <cg@exept.de>
parents:
5822
diff
changeset
|
779 |
^ 6 |
92d1470a57dc
added #defaultNumberOfDigits
Claus Gittinger <cg@exept.de>
parents:
5822
diff
changeset
|
780 |
! ! |
92d1470a57dc
added #defaultNumberOfDigits
Claus Gittinger <cg@exept.de>
parents:
5822
diff
changeset
|
781 |
|
3897 | 782 |
!ShortFloat methodsFor:'special access'! |
783 |
||
784 |
exponent |
|
785 |
"extract a normalized floats exponent. |
|
786 |
The returned value depends on the float-representation of |
|
787 |
the underlying machine and is therefore highly unportable. |
|
788 |
This is not for general use. |
|
789 |
This assumes that the mantissa is normalized to |
|
790 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
791 |
||
792 |
%{ /* NOCONTEXT */ |
|
793 |
||
794 |
double frexp(); |
|
795 |
double frac; |
|
796 |
INT exp; |
|
797 |
||
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
798 |
__threadErrno = 0; |
3897 | 799 |
frac = frexp( (double)(__shortFloatVal(self)), &exp); |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
800 |
if (__threadErrno == 0) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
801 |
RETURN (__MKSMALLINT(exp)); |
3897 | 802 |
} |
803 |
%}. |
|
804 |
^ self primitiveFailed |
|
805 |
||
806 |
" |
|
807 |
1.0 asShortFloat exponent |
|
808 |
1.0 asShortFloat exponent |
|
809 |
0.5 asShortFloat exponent |
|
810 |
0.25 asShortFloat exponent |
|
811 |
0.00000011111 asShortFloat exponent |
|
812 |
" |
|
813 |
! |
|
814 |
||
815 |
mantissa |
|
816 |
"extract a normalized floats mantissa. |
|
817 |
The returned value depends on the float-representation of |
|
818 |
the underlying machine and is therefore highly unportable. |
|
819 |
This is not for general use. |
|
820 |
This assumes that the mantissa is normalized to |
|
821 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
822 |
||
823 |
%{ /* NOCONTEXT */ |
|
824 |
||
825 |
double frexp(); |
|
826 |
double frac; |
|
827 |
INT exp; |
|
828 |
||
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
829 |
__threadErrno = 0; |
3897 | 830 |
frac = frexp( (double)(__shortFloatVal(self)), &exp); |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
831 |
if (__threadErrno == 0) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
832 |
RETURN (__MKFLOAT(frac)); |
3897 | 833 |
} |
834 |
%}. |
|
835 |
^ self primitiveFailed |
|
836 |
||
837 |
" |
|
838 |
1.0 asShortFloat exponent |
|
839 |
1.0 asShortFloat mantissa |
|
840 |
||
841 |
0.5 asShortFloat exponent |
|
842 |
0.5 asShortFloat mantissa |
|
843 |
||
844 |
0.25 asShortFloat exponent |
|
845 |
0.25 asShortFloat mantissa |
|
846 |
||
847 |
0.00000011111 asShortFloat exponent |
|
848 |
0.00000011111 asShortFloat mantissa |
|
849 |
" |
|
850 |
! ! |
|
851 |
||
1197 | 852 |
!ShortFloat methodsFor:'testing'! |
853 |
||
3147 | 854 |
isFinite |
855 |
"return true, if the receiver is a finite float |
|
856 |
i.e. not NaN and not infinite." |
|
857 |
||
858 |
%{ /* NOCONTEXT */ |
|
859 |
||
860 |
double dV = (double) __shortFloatVal(self); |
|
861 |
||
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
862 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
863 |
* 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
|
864 |
* a finite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
865 |
* 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
|
866 |
*/ |
3147 | 867 |
if (finite(dV)) { RETURN (true); } |
868 |
%}. |
|
869 |
^false |
|
870 |
||
871 |
" |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
872 |
1.0 asShortFloat isFinite |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
873 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
874 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
3147 | 875 |
" |
876 |
! |
|
877 |
||
2387 | 878 |
isNaN |
879 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
880 |
These are not created by ST/X float operations (they raise an exception); |
|
881 |
however, inline C-code could produce them ..." |
|
882 |
||
883 |
%{ /* NOCONTEXT */ |
|
884 |
||
3139 | 885 |
double dV = (double)(__shortFloatVal(self)); |
886 |
||
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
887 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
888 |
* 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
|
889 |
* a finite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
890 |
* 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
|
891 |
*/ |
3147 | 892 |
if (isnan(dV)) { RETURN (true); } |
893 |
||
894 |
#if 0 /* Currently all our systems support isnan() |
|
895 |
* If not, you have to fix librun/jinterpret.c also. |
|
896 |
*/ |
|
897 |
||
2395 | 898 |
/* |
899 |
* sigh - every vendor is playing its own game here ... |
|
900 |
* Q: what are standards worth, anyway ? |
|
901 |
*/ |
|
3139 | 902 |
#ifdef IS_NAN |
903 |
if (IS_NAN(dV)) { RETURN (true); } |
|
904 |
RETURN (false); |
|
905 |
#endif |
|
906 |
||
2393 | 907 |
#ifdef IS_QNAN |
3139 | 908 |
if (IS_QNAN(dV)) { RETURN (true); } |
909 |
RETURN (false); |
|
2393 | 910 |
#endif |
2392 | 911 |
|
2393 | 912 |
#ifdef FLT_SNAN |
3139 | 913 |
if (dV == FLT_SNAN) { RETURN (true); } |
914 |
RETURN (false); |
|
2393 | 915 |
#endif |
3139 | 916 |
|
2393 | 917 |
#ifdef FLT_QNAN |
3139 | 918 |
if (dV == FLT_QNAN) { RETURN (true); } |
919 |
RETURN (false); |
|
2393 | 920 |
#endif |
921 |
||
922 |
#ifdef _SNANF |
|
3139 | 923 |
if (dV == _SNAN) { RETURN (true); } |
924 |
RETURN (false); |
|
2393 | 925 |
#endif |
3139 | 926 |
|
2393 | 927 |
#ifdef _QNANF |
3139 | 928 |
if (dV == _QNAN) { RETURN (true); } |
929 |
RETURN (false); |
|
2393 | 930 |
#endif |
2392 | 931 |
|
2395 | 932 |
#ifdef IsPosNAN |
3139 | 933 |
if IsPosNAN(dV) { RETURN (true); } |
934 |
RETURN (false); |
|
2395 | 935 |
#endif |
3139 | 936 |
|
2395 | 937 |
#ifdef IsNegNAN |
3139 | 938 |
if IsNegNAN(dV) { RETURN (true); } |
939 |
RETURN (false); |
|
2395 | 940 |
#endif |
941 |
||
2393 | 942 |
#ifdef NAN |
3139 | 943 |
if (dV == NAN) { RETURN (true); } |
944 |
RETURN (false); |
|
2400 | 945 |
#endif |
2389 | 946 |
|
3139 | 947 |
#ifdef NaN |
948 |
if (NaN(dV)) { RETURN (true); } |
|
949 |
RETURN (false); |
|
950 |
#endif |
|
3147 | 951 |
|
952 |
#endif /* 0 */ |
|
2392 | 953 |
%}. |
954 |
^ false |
|
2387 | 955 |
|
3147 | 956 |
" |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
957 |
1.0 asShortFloat isNaN |
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
958 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
3147 | 959 |
" |
2387 | 960 |
! |
961 |
||
1197 | 962 |
negative |
963 |
"return true if the receiver is less than zero" |
|
964 |
||
965 |
%{ /* NOCONTEXT */ |
|
966 |
||
967 |
RETURN ( (__shortFloatVal(self) < 0.0) ? true : false ); |
|
5411 | 968 |
%}. |
969 |
^ self < 0.0 |
|
1197 | 970 |
! |
971 |
||
5357 | 972 |
numberOfBits |
973 |
"return the size (in bits) of the real; |
|
974 |
typically, 32 is returned here, |
|
975 |
but who knows ..." |
|
976 |
||
977 |
%{ /* NOCONTEXT */ |
|
978 |
||
979 |
RETURN (__MKSMALLINT (sizeof(float) * 8)); |
|
980 |
%} |
|
981 |
||
982 |
" |
|
983 |
1.2 numberOfBits |
|
984 |
1.2 asShortFloat numberOfBits |
|
985 |
" |
|
986 |
||
987 |
! |
|
988 |
||
1197 | 989 |
positive |
990 |
"return true if the receiver is greater or equal to zero" |
|
991 |
||
992 |
%{ /* NOCONTEXT */ |
|
993 |
||
994 |
RETURN ( (__shortFloatVal(self) >= 0.0) ? true : false ); |
|
995 |
%} |
|
1880 | 996 |
! |
997 |
||
998 |
strictlyPositive |
|
999 |
"return true if the receiver is greater than zero" |
|
1000 |
||
1001 |
%{ /* NOCONTEXT */ |
|
1002 |
||
1003 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
1004 |
%} |
|
1197 | 1005 |
! ! |
1006 |
||
5552 | 1007 |
!ShortFloat methodsFor:'truncation & rounding'! |
3897 | 1008 |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1009 |
floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1010 |
"return the integer nearest the receiver towards negative infinity." |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1011 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1012 |
|val| |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1013 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1014 |
%{ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1015 |
float fVal; |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1016 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1017 |
/* |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1018 |
* ST-80 (and X3J20) returns integer. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1019 |
*/ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1020 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1021 |
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
|
1022 |
RETURN ( __MKSMALLINT( (INT) fVal ) ); |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1023 |
} |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1024 |
__qMKSFLOAT(val, fVal); |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1025 |
%}. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1026 |
^ val asInteger |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1027 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1028 |
" |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1029 |
0.5 asShortFloat floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1030 |
-0.5 asShortFloat floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1031 |
" |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1032 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1033 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1034 |
! |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1035 |
|
3897 | 1036 |
fractionPart |
1037 |
"extract the after-decimal fraction part. |
|
1038 |
the floats value is |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
1039 |
float truncated + float fractionalPart" |
3897 | 1040 |
|
1041 |
%{ /* NOCONTEXT */ |
|
1042 |
||
1043 |
double modf(); |
|
1044 |
double frac, trunc; |
|
1045 |
||
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1046 |
__threadErrno = 0; |
3897 | 1047 |
frac = modf((double)(__shortFloatVal(self)), &trunc); |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1048 |
if (__threadErrno == 0) { |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
1049 |
RETURN (__MKSFLOAT(frac)); |
3897 | 1050 |
} |
1051 |
%}. |
|
1052 |
^ self primitiveFailed |
|
1053 |
||
1054 |
" |
|
1055 |
1.0 asShortFloat fractionalPart |
|
1056 |
0.5 asShortFloat fractionalPart |
|
1057 |
0.25 asShortFloat fractionalPart |
|
1058 |
3.14159 asShortFloat fractionalPart |
|
1059 |
12345673.14159 asShortFloat fractionalPart |
|
1060 |
123456731231231231.14159 asShortFloat fractionalPart |
|
1061 |
" |
|
1062 |
||
1063 |
! ! |
|
1064 |
||
1880 | 1065 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 1066 |
|
1067 |
version |
|
6574 | 1068 |
^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.67 2002-06-11 21:43:11 stefan Exp $' |
1197 | 1069 |
! ! |