author | Claus Gittinger <cg@exept.de> |
Fri, 08 Jul 2005 19:15:03 +0200 | |
changeset 8913 | b9498d27a554 |
parent 8901 | 824a89d0b5c7 |
child 8980 | abee9fcb0385 |
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 |
*/ |
7419 | 66 |
# ifndef isnan |
67 |
# define isnan(x) \ |
|
68 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
|
69 |
(((unsigned int *)(&x))[1] == 0xFFF80000)) |
|
70 |
# endif |
|
71 |
||
72 |
# ifndef isnanf |
|
73 |
# define isnanf(x) \ |
|
74 |
(((unsigned int *)(&x))[0] == 0xFFC00000) |
|
75 |
# endif |
|
76 |
||
77 |
# ifndef isPositiveInfinity |
|
78 |
# define isPositiveInfinity(x) \ |
|
79 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
|
80 |
(((unsigned int *)(&x))[1] == 0x7FF00000)) |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
81 |
# endif |
7419 | 82 |
|
83 |
# ifndef isNegativeInfinity |
|
84 |
# define isPositiveInfinity(x) \ |
|
85 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
|
86 |
(((unsigned int *)(&x))[1] == 0xFFF00000)) |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
87 |
# endif |
7419 | 88 |
|
89 |
# ifndef isinf |
|
90 |
# define isinf(x) \ |
|
91 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
|
92 |
((((unsigned int *)(&x))[1] & 0x7FF00000) == 0x7FF00000)) |
|
93 |
# endif |
|
94 |
||
95 |
# ifndef finite |
|
96 |
# define finite(x) (!isinf(x) && !isnan(x)) |
|
97 |
# endif |
|
98 |
||
99 |
# define NO_ASINH |
|
100 |
# define NO_ACOSH |
|
101 |
# define NO_ATANH |
|
8706 | 102 |
#endif /* WIN32 */ |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
103 |
|
3414 | 104 |
#ifdef realIX |
105 |
/* |
|
106 |
* no finite(x) |
|
107 |
*/ |
|
108 |
# ifndef finite |
|
109 |
# define finite(x) 1 |
|
110 |
# endif |
|
8706 | 111 |
#endif /* realIX */ |
112 |
||
113 |
#ifdef OSX |
|
114 |
# ifndef isnanf |
|
115 |
# define isnanf(x) isnan(x) |
|
116 |
# endif |
|
117 |
#endif /* OSX */ |
|
3414 | 118 |
|
2394 | 119 |
%} |
120 |
! ! |
|
121 |
||
1880 | 122 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 123 |
|
124 |
copyright |
|
125 |
" |
|
126 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3158 | 127 |
All Rights Reserved |
1197 | 128 |
|
129 |
This software is furnished under a license and may be used |
|
130 |
only in accordance with the terms of that license and with the |
|
131 |
inclusion of the above copyright notice. This software may not |
|
132 |
be provided or otherwise made available to, or used by, any |
|
133 |
other person. No title to or ownership of the software is |
|
134 |
hereby transferred. |
|
135 |
" |
|
136 |
||
137 |
||
138 |
! |
|
139 |
||
140 |
documentation |
|
141 |
" |
|
7380 | 142 |
ShortFloats represent rational numbers with limited precision. |
143 |
They use the C-compilers 'float' format, which is usually the IEE single float format. |
|
144 |
||
145 |
In contrast to Floats (which use the C-compilers 64bit 'double' format), |
|
146 |
ShortFloats give you 32 bit floats. |
|
1197 | 147 |
|
148 |
Notice, that ST/X Floats are what Doubles are in ST-80 and ShortFloats are |
|
149 |
ST-80's Floats respectively. |
|
150 |
This may change in one of the next versions (at least on machines, which |
|
151 |
provide different float and double types in their C-compiler. |
|
152 |
||
153 |
WARNING: |
|
7380 | 154 |
The layout of shortFloat instances is known by the runtime system and the compiler; |
155 |
you may not add instance variables here. |
|
156 |
Also, subclassing is complicated by the fact, that the VM creates floats/shortFloats, |
|
157 |
and does some of its float-checks by an identity compare with the ShortFloat-class. |
|
158 |
(i.e. your subclasses instances may not be recognized as float-like objects, |
|
159 |
thus mixed mode arithmetic will always coerce them, effectively slowing things down). |
|
160 |
This may be changed, to use a flag bit in the class. |
|
1197 | 161 |
|
7380 | 162 |
Mixed mode arithmetic: |
163 |
shortFloat op shortFloat -> shortFloat |
|
164 |
shortFloat op fix -> shortFloat |
|
165 |
shortFloat op fraction -> shortFloat |
|
166 |
shortFloat op integer -> shortFloat |
|
167 |
shortFloat op longFloat -> longFloat |
|
168 |
shortFloat op float -> float |
|
169 |
shortFloat op complex -> complex |
|
1295 | 170 |
|
7405 | 171 |
Representation: |
172 |
32bit single precision IEE floats |
|
173 |
23 bit mantissa, |
|
174 |
8 bit exponent, |
|
175 |
6 decimal digits (approx) |
|
176 |
||
177 |
Range and Precision of Storage Formats: see LimitedPrecisionReal >> documentation |
|
178 |
||
1295 | 179 |
[author:] |
7380 | 180 |
Claus Gittinger |
1556 | 181 |
|
182 |
[see also:] |
|
7380 | 183 |
Number |
184 |
Float LongFloat Fraction FixedPoint Integer Complex |
|
185 |
FloatArray DoubleArray |
|
1197 | 186 |
" |
187 |
! ! |
|
188 |
||
1880 | 189 |
!ShortFloat class methodsFor:'instance creation'! |
1207 | 190 |
|
191 |
basicNew |
|
192 |
"return a new shortFloat - here we return 0.0 |
|
193 |
- shortFloats are usually NOT created this way ... |
|
194 |
Its implemented here to allow things like binary store & load |
|
195 |
of shortFloats. (but even this support will go away eventually, its not |
|
196 |
a good idea to store the bits of a float - the reader might have a |
|
197 |
totally different representation - so floats will eventually be |
|
198 |
binary stored in a device independent format." |
|
199 |
||
200 |
%{ /* NOCONTEXT */ |
|
201 |
OBJ newFloat; |
|
202 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
203 |
__qMKSFLOAT(newFloat, 0.0); |
1207 | 204 |
RETURN (newFloat); |
205 |
%} |
|
3158 | 206 |
! |
207 |
||
4297 | 208 |
fastFromString:aString at:startIndex |
209 |
"return the next ShortFloat from the string starting at startIndex. |
|
210 |
No spaces are skipped. |
|
211 |
||
212 |
This is a specially tuned entry (using a low-level C-call), which |
|
213 |
returns garbage if the argument string is not a valid float number. |
|
214 |
It has been added to allow higher speed string decomposition into |
|
215 |
numbers." |
|
216 |
%{ |
|
217 |
if (__isString(aString) && __isSmallInteger(startIndex)) { |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
218 |
char *cp = (char *)(__stringVal(aString)); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
219 |
int idx = __intVal(startIndex) - 1; |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
220 |
double atof(); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
221 |
double val; |
4297 | 222 |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
223 |
if ((unsigned)idx < __stringSize(aString)) { |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
224 |
val = atof(cp + idx); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
225 |
RETURN (__MKSFLOAT(val)); |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
226 |
} |
4297 | 227 |
} |
228 |
%}. |
|
229 |
self primitiveFailed. |
|
230 |
||
231 |
" |
|
232 |
ShortFloat fastFromString:'123.45' at:1 |
|
233 |
ShortFloat fastFromString:'123.45' at:2 |
|
234 |
ShortFloat fastFromString:'123.45E4' at:1 |
|
235 |
ShortFloat fastFromString:'hello123.45E4' at:6 |
|
236 |
ShortFloat fastFromString:'12345' at:1 |
|
237 |
ShortFloat fastFromString:'12345' at:2 |
|
238 |
ShortFloat fastFromString:'12345' at:3 |
|
239 |
ShortFloat fastFromString:'12345' at:4 |
|
240 |
ShortFloat fastFromString:'12345' at:5 |
|
241 |
ShortFloat fastFromString:'12345' at:6 |
|
242 |
ShortFloat fastFromString:'12345' at:0 |
|
243 |
ShortFloat fastFromString:'hello123.45E4' at:1 |
|
244 |
||
245 |
Time millisecondsToRun:[ |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
246 |
100000 timesRepeat:[ |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
247 |
ShortFloat readFrom:'123.45' |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
248 |
] |
4297 | 249 |
] |
250 |
" |
|
251 |
||
252 |
" |
|
253 |
Time millisecondsToRun:[ |
|
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
254 |
100000 timesRepeat:[ |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
255 |
ShortFloat fastFromString:'123.45' at:1 |
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
256 |
] |
4297 | 257 |
] |
258 |
" |
|
259 |
||
260 |
||
261 |
||
262 |
! |
|
263 |
||
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
264 |
readFrom:aStringOrStream |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
265 |
"read a shortFloat from a string" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
266 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
267 |
|num| |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
268 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
269 |
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
|
270 |
num notNil ifTrue:[ |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
271 |
num := num asShortFloat |
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
272 |
]. |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
273 |
^ num |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
274 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
275 |
" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
276 |
ShortFloat readFrom:'0.1' |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
277 |
ShortFloat readFrom:'0' |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
278 |
ShortFloat readFrom:'.123' |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
279 |
ShortFloat readFrom:'-.123' |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
280 |
ShortFloat readFrom:'1e4' |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
281 |
" |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
282 |
|
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
283 |
"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
|
284 |
! |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
285 |
|
3158 | 286 |
readFrom:aStringOrStream onError:exceptionBlock |
287 |
"read a shortFloat from a string" |
|
288 |
||
289 |
|num| |
|
290 |
||
291 |
num := super readFrom:aStringOrStream onError:nil. |
|
292 |
num isNil ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
293 |
^ exceptionBlock value |
3158 | 294 |
]. |
295 |
^ num asShortFloat |
|
296 |
||
297 |
" |
|
298 |
ShortFloat readFrom:'0.1' |
|
299 |
ShortFloat readFrom:'0' |
|
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4615
diff
changeset
|
300 |
ShortFloat readFrom:'.123' |
3158 | 301 |
" |
302 |
||
303 |
"Modified: / 7.1.1998 / 16:17:59 / cg" |
|
1207 | 304 |
! ! |
305 |
||
7356 | 306 |
!ShortFloat class methodsFor:'binary storage'! |
307 |
||
308 |
readBinaryIEEESingleFrom:aStream |
|
309 |
"read a float value from the binary stream, aStream, |
|
310 |
interpreting the next bytes as an IEEE formatted 4-byte float" |
|
1197 | 311 |
|
7356 | 312 |
|f| |
1197 | 313 |
|
7356 | 314 |
f := ShortFloat basicNew. |
315 |
self readBinaryIEEESingleFrom:aStream into:f. |
|
316 |
^ f |
|
1197 | 317 |
|
7356 | 318 |
"Created: 16.4.1996 / 21:00:35 / cg" |
1197 | 319 |
! |
320 |
||
7356 | 321 |
readBinaryIEEESingleFrom:aStream into:aFloat |
322 |
"read a float value from the binary stream, aStream, |
|
323 |
interpreting the next bytes as an IEEE formatted 4-byte float" |
|
324 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
325 |
aFloat class == self ifFalse:[self error:'no a ShortFloat']. |
6574 | 326 |
|
7356 | 327 |
" |
328 |
this implementation is wrong: does not work on non-IEEE machines |
|
329 |
(to date all machines where ST/X is running on use |
|
330 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
331 |
" |
|
332 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
333 |
||
334 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
335 |
"swap the bytes" |
|
336 |
8 to:4 by:-1 do:[:i | |
|
337 |
aFloat basicAt:i put:(aStream next) |
|
338 |
]. |
|
339 |
^ self |
|
340 |
]. |
|
341 |
1 to:4 do:[:i | |
|
342 |
aFloat basicAt:i put:aStream next |
|
343 |
] |
|
6574 | 344 |
! |
345 |
||
7356 | 346 |
storeBinaryIEEESingle:aFloat on:aStream |
347 |
"store aFloat as an IEEE formatted 4-byte float |
|
348 |
onto the binary stream, aStream" |
|
349 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
350 |
|float| |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
351 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
352 |
float := aFloat asShortFloat. |
7356 | 353 |
|
354 |
" |
|
355 |
this implementation is wrong: does not work on non-IEEE machines |
|
356 |
(to date all machines where ST/X is running on use |
|
357 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
358 |
" |
|
359 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
6574 | 360 |
|
7356 | 361 |
UninterpretedBytes isBigEndian ifFalse:[ |
362 |
"swap the bytes" |
|
363 |
8 to:4 by:-1 do:[:i | |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
364 |
aStream nextPut:(float basicAt:i). |
7356 | 365 |
]. |
366 |
^ self |
|
367 |
]. |
|
368 |
1 to:4 do:[:i | |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
369 |
aStream nextPut:(float basicAt:i). |
7356 | 370 |
] |
371 |
! ! |
|
372 |
||
373 |
!ShortFloat class methodsFor:'constants'! |
|
6574 | 374 |
|
7392 | 375 |
e |
376 |
"return the constant e as ShortFloat" |
|
377 |
||
378 |
^ Float e asShortFloat |
|
379 |
! |
|
380 |
||
381 |
pi |
|
382 |
"return the constant pi as ShortFloat" |
|
383 |
||
384 |
^ Float pi asShortFloat |
|
385 |
! |
|
386 |
||
1197 | 387 |
unity |
1260 | 388 |
"return the neutral element for multiplication (1.0) as ShortFloat" |
1197 | 389 |
|
390 |
^ 1.0 asShortFloat |
|
391 |
||
1260 | 392 |
"Modified: 23.4.1996 / 09:26:51 / cg" |
1197 | 393 |
! |
394 |
||
395 |
zero |
|
1260 | 396 |
"return the neutral element for addition (0.0) as ShortFloat" |
1197 | 397 |
|
398 |
^ 0.0 asShortFloat |
|
399 |
||
1260 | 400 |
"Modified: 23.4.1996 / 09:26:45 / cg" |
1197 | 401 |
! ! |
402 |
||
1880 | 403 |
!ShortFloat class methodsFor:'queries'! |
1197 | 404 |
|
6898 | 405 |
exponentCharacter |
406 |
^ $e |
|
407 |
! |
|
408 |
||
1197 | 409 |
isBuiltInClass |
1264 | 410 |
"return true if this class is known by the run-time-system. |
411 |
Here, true is returned for myself, false for subclasses." |
|
1197 | 412 |
|
413 |
^ self == ShortFloat |
|
414 |
||
1264 | 415 |
"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
|
416 |
! |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
417 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
418 |
isIEEEFormat |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
419 |
"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
|
420 |
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
|
421 |
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
|
422 |
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
|
423 |
machine). |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
424 |
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
|
425 |
(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
|
426 |
|
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
427 |
^ true "/ this may be a lie |
7356 | 428 |
! |
429 |
||
430 |
numBitsInExponent |
|
431 |
"answer the number of bits in the exponent |
|
432 |
This is an IEEE float, where 8 bits are available: |
|
433 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
|
434 |
" |
|
435 |
||
436 |
^ 8 |
|
437 |
! |
|
438 |
||
439 |
numBitsInMantissa |
|
440 |
"answer the number of bits in the mantissa. |
|
441 |
This is an IEEE float, where 23 bits (the hidden one is not counted here) are available: |
|
442 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
|
443 |
" |
|
444 |
||
445 |
^ 23 |
|
446 |
! |
|
447 |
||
448 |
precision |
|
449 |
"answer the precision of a ShortFloat (in bits) |
|
450 |
This is an IEEE float, only the fraction from the normalized mantissa is stored |
|
451 |
and so there is a hidden bit and the mantissa is actually represented by 24 binary digits" |
|
452 |
||
453 |
^ 24 |
|
454 |
! |
|
455 |
||
456 |
radix |
|
457 |
"answer the radix of a ShortFloats exponent |
|
458 |
This is an IEEE float, which is represented as binary" |
|
459 |
||
8635 | 460 |
^ 2 "must be careful here, whenever ST/X is used on VAX or a 370" |
1197 | 461 |
! ! |
462 |
||
463 |
!ShortFloat methodsFor:'arithmetic'! |
|
464 |
||
465 |
* aNumber |
|
466 |
"return the product of the receiver and the argument, aNumber" |
|
467 |
||
468 |
%{ /* NOCONTEXT */ |
|
469 |
||
470 |
OBJ newFloat; |
|
471 |
float result; |
|
472 |
double dResult; |
|
473 |
||
474 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 475 |
result = __shortFloatVal(self) * (float)(__intVal(aNumber)); |
1197 | 476 |
retResult: |
7356 | 477 |
__qMKSFLOAT(newFloat, result); |
478 |
RETURN ( newFloat ); |
|
479 |
} |
|
480 |
if (__isShortFloat(aNumber)) { |
|
481 |
result = __shortFloatVal(self) * __shortFloatVal(aNumber); |
|
482 |
goto retResult; |
|
483 |
} |
|
484 |
if (__isFloatLike(aNumber)) { |
|
485 |
dResult = (double) __shortFloatVal(self)* __floatVal(aNumber); |
|
486 |
__qMKFLOAT(newFloat, dResult); |
|
487 |
RETURN ( newFloat ); |
|
1197 | 488 |
} |
489 |
%}. |
|
490 |
^ aNumber productFromShortFloat:self |
|
491 |
! |
|
492 |
||
493 |
+ aNumber |
|
494 |
"return the sum of the receiver and the argument, aNumber" |
|
495 |
||
496 |
%{ /* NOCONTEXT */ |
|
497 |
||
498 |
OBJ newFloat; |
|
499 |
float result; |
|
500 |
double dResult; |
|
501 |
||
502 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 503 |
result = __shortFloatVal(self) + (float)(__intVal(aNumber)); |
1197 | 504 |
retResult: |
7356 | 505 |
__qMKSFLOAT(newFloat, result); |
506 |
RETURN ( newFloat ); |
|
507 |
} |
|
508 |
if (__isShortFloat(aNumber)) { |
|
509 |
result = __shortFloatVal(self) + __shortFloatVal(aNumber); |
|
510 |
goto retResult; |
|
511 |
} |
|
512 |
if (__isFloatLike(aNumber)) { |
|
513 |
dResult = (double) __shortFloatVal(self) + __floatVal(aNumber); |
|
514 |
__qMKFLOAT(newFloat, dResult); |
|
515 |
RETURN ( newFloat ); |
|
1197 | 516 |
} |
517 |
%}. |
|
518 |
^ aNumber sumFromShortFloat:self |
|
519 |
! |
|
520 |
||
521 |
- aNumber |
|
522 |
"return the difference of the receiver and the argument, aNumber" |
|
523 |
||
524 |
%{ /* NOCONTEXT */ |
|
525 |
||
526 |
OBJ newFloat; |
|
527 |
float result; |
|
528 |
double dResult; |
|
529 |
||
530 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 531 |
result = __shortFloatVal(self) - (float)(__intVal(aNumber)); |
1197 | 532 |
retResult: |
7356 | 533 |
__qMKSFLOAT(newFloat, result); |
534 |
RETURN ( newFloat ); |
|
535 |
} |
|
536 |
if (__isShortFloat(aNumber)) { |
|
537 |
result = __shortFloatVal(self) - __shortFloatVal(aNumber); |
|
538 |
goto retResult; |
|
539 |
} |
|
540 |
if (__isFloatLike(aNumber)) { |
|
541 |
dResult = (double) __shortFloatVal(self) - __floatVal(aNumber); |
|
542 |
__qMKFLOAT(newFloat, dResult); |
|
543 |
RETURN ( newFloat ); |
|
1197 | 544 |
} |
545 |
%}. |
|
546 |
^ aNumber differenceFromShortFloat:self |
|
547 |
! |
|
548 |
||
549 |
/ aNumber |
|
550 |
"return the quotient of the receiver and the argument, aNumber" |
|
551 |
||
552 |
%{ /* NOCONTEXT */ |
|
553 |
||
554 |
OBJ newFloat; |
|
555 |
float result, val; |
|
556 |
double dResult, dVal; |
|
557 |
||
558 |
if (__isSmallInteger(aNumber)) { |
|
8913 | 559 |
if (aNumber != __mkSmallInteger(0)) { |
5954 | 560 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
1197 | 561 |
retResult: |
5954 | 562 |
__qMKSFLOAT(newFloat, result); |
563 |
RETURN ( newFloat ); |
|
564 |
} |
|
7356 | 565 |
} |
566 |
if (__isShortFloat(aNumber)) { |
|
5954 | 567 |
val = __shortFloatVal(aNumber); |
568 |
if (val != 0.0) { |
|
569 |
result = __shortFloatVal(self) / val; |
|
570 |
goto retResult; |
|
571 |
} |
|
7356 | 572 |
} |
573 |
if (__isFloatLike(aNumber)) { |
|
5954 | 574 |
dVal = __floatVal(aNumber); |
575 |
if (dVal != 0.0) { |
|
576 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
577 |
__qMKFLOAT(newFloat, dResult); |
|
578 |
RETURN ( newFloat ); |
|
579 |
} |
|
1197 | 580 |
} |
581 |
%}. |
|
582 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
5954 | 583 |
" |
584 |
No, you shalt not divide by zero |
|
585 |
" |
|
7141
033f2c26d8e6
replaced refs to DivisionByZeroSignal with ZeroDivide
Claus Gittinger <cg@exept.de>
parents:
6898
diff
changeset
|
586 |
^ ZeroDivide raiseRequestWith:thisContext. |
1197 | 587 |
]. |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
588 |
^ aNumber quotientFromShortFloat:self |
1197 | 589 |
! |
590 |
||
7725 | 591 |
abs |
592 |
"return the absolute value of the receiver |
|
593 |
reimplemented here for speed" |
|
594 |
||
595 |
%{ /* NOCONTEXT */ |
|
596 |
||
597 |
OBJ newFloat; |
|
598 |
float val = __shortFloatVal(self); |
|
599 |
||
600 |
if (val < 0.0) { |
|
601 |
__qMKSFLOAT(newFloat, -val); |
|
602 |
RETURN ( newFloat ); |
|
603 |
} |
|
604 |
RETURN (self); |
|
605 |
%}. |
|
606 |
||
607 |
" |
|
608 |
3.0 asShortFloat abs |
|
609 |
-3.0 asShortFloat abs |
|
610 |
" |
|
611 |
! |
|
612 |
||
1197 | 613 |
negated |
614 |
"return myself negated" |
|
615 |
||
616 |
%{ /* NOCONTEXT */ |
|
617 |
OBJ newFloat; |
|
618 |
float rslt = - __shortFloatVal(self); |
|
619 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
620 |
__qMKSFLOAT(newFloat, rslt); |
1197 | 621 |
RETURN ( newFloat ); |
5411 | 622 |
%}. |
623 |
^ 0.0 - self |
|
1197 | 624 |
|
3147 | 625 |
! |
626 |
||
627 |
uncheckedDivide:aNumber |
|
7356 | 628 |
"return the quotient of the receiver and the argument, aNumber. |
7471 | 629 |
Do not check for divide by zero (return NaN or Infinity). |
7356 | 630 |
This operation is provided for emulators of other languages/semantics, |
631 |
where no exception is raised for these results (i.e. Java). |
|
632 |
Its only defined if the arguments type is the same as the receivers." |
|
3147 | 633 |
|
634 |
%{ /* NOCONTEXT */ |
|
635 |
||
636 |
OBJ newFloat; |
|
637 |
float result, val; |
|
638 |
double dResult, dVal; |
|
639 |
||
640 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 641 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
3147 | 642 |
retResult: |
7356 | 643 |
__qMKSFLOAT(newFloat, result); |
644 |
RETURN ( newFloat ); |
|
645 |
} |
|
646 |
if (__isShortFloat(aNumber)) { |
|
647 |
val = __shortFloatVal(aNumber); |
|
648 |
result = __shortFloatVal(self) / val; |
|
649 |
goto retResult; |
|
650 |
} |
|
651 |
if (__isFloatLike(aNumber)) { |
|
652 |
dVal = __floatVal(aNumber); |
|
653 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
654 |
__qMKFLOAT(newFloat, dResult); |
|
655 |
RETURN ( newFloat ); |
|
3147 | 656 |
} |
657 |
%}. |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
658 |
^ aNumber quotientFromShortFloat:self |
3147 | 659 |
|
660 |
" |
|
661 |
0.0 asShortFloat uncheckedDivide:0 |
|
662 |
1.0 asShortFloat uncheckedDivide:0.0 |
|
663 |
" |
|
1197 | 664 |
! ! |
665 |
||
5257 | 666 |
!ShortFloat methodsFor:'coercing & converting'! |
1197 | 667 |
|
668 |
asFloat |
|
669 |
"return a Float with same value as the receiver" |
|
670 |
||
671 |
%{ /* NOCONTEXT */ |
|
672 |
||
673 |
OBJ newFloat; |
|
674 |
double dVal = (double)__shortFloatVal(self); |
|
675 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
676 |
__qMKFLOAT(newFloat, dVal); |
1197 | 677 |
RETURN ( newFloat ); |
678 |
%} |
|
679 |
||
680 |
" |
|
1583 | 681 |
1.0 asShortFloat asFloat |
1197 | 682 |
" |
683 |
! |
|
684 |
||
685 |
asInteger |
|
686 |
"return an integer with same value - might truncate" |
|
687 |
||
688 |
%{ /* NOCONTEXT */ |
|
1670 | 689 |
float fVal; |
1197 | 690 |
|
1670 | 691 |
fVal = __shortFloatVal(self); |
7419 | 692 |
#ifdef WIN32 |
693 |
if (! isnanf(fVal)) |
|
694 |
#endif |
|
695 |
{ |
|
696 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
8913 | 697 |
RETURN ( __mkSmallInteger( (INT)fVal) ); |
7419 | 698 |
} |
1197 | 699 |
} |
700 |
%}. |
|
701 |
^ super asInteger |
|
702 |
||
703 |
" |
|
704 |
12345.0 asShortFloat asInteger |
|
705 |
1e15 asShortFloat asInteger |
|
706 |
" |
|
707 |
! |
|
708 |
||
7437
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
709 |
asLongFloat |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
710 |
^ LongFloat fromShortFloat:self |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
711 |
! |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
712 |
|
1197 | 713 |
asShortFloat |
714 |
"return a ShortFloat with same value as the receiver - thats me" |
|
715 |
||
716 |
^ self |
|
1200 | 717 |
! |
718 |
||
7356 | 719 |
coerce:aNumber |
720 |
"return aNumber converted into receivers type" |
|
721 |
||
722 |
^ aNumber asShortFloat |
|
723 |
! |
|
724 |
||
1200 | 725 |
generality |
726 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
727 |
||
728 |
^ 70 |
|
729 |
||
730 |
||
1197 | 731 |
! ! |
732 |
||
733 |
!ShortFloat methodsFor:'comparing'! |
|
734 |
||
735 |
< aNumber |
|
736 |
"return true, if the argument is greater" |
|
737 |
||
738 |
%{ /* NOCONTEXT */ |
|
739 |
||
740 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 741 |
RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false ); |
1197 | 742 |
} |
7356 | 743 |
if (aNumber != nil) { |
744 |
if (__qIsFloatLike(aNumber)) { |
|
745 |
RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false ); |
|
746 |
} |
|
747 |
if (__qIsShortFloat(aNumber)) { |
|
748 |
RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false ); |
|
749 |
} |
|
1197 | 750 |
} |
751 |
%}. |
|
752 |
^ aNumber lessFromShortFloat:self |
|
753 |
||
1200 | 754 |
" |
755 |
1.0 asShortFloat > (1/3) |
|
756 |
" |
|
1197 | 757 |
! |
758 |
||
759 |
<= aNumber |
|
760 |
"return true, if the argument is greater or equal" |
|
761 |
||
762 |
%{ /* NOCONTEXT */ |
|
763 |
||
764 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 765 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 766 |
} |
7356 | 767 |
if (aNumber != nil) { |
768 |
if (__qIsFloatLike(aNumber)) { |
|
769 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
|
770 |
} |
|
771 |
if (__qIsShortFloat(aNumber)) { |
|
772 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
|
773 |
} |
|
774 |
} |
|
1197 | 775 |
%}. |
776 |
^ self retry:#<= coercing:aNumber |
|
777 |
! |
|
778 |
||
779 |
= aNumber |
|
7356 | 780 |
"return true, if the argument represents the same numeric value |
781 |
as the receiver, false otherwise" |
|
1197 | 782 |
|
783 |
%{ /* NOCONTEXT */ |
|
784 |
||
785 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 786 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
787 |
} |
|
788 |
if (aNumber == nil) { |
|
789 |
RETURN (false); |
|
1197 | 790 |
} |
7356 | 791 |
if (__qIsFloatLike(aNumber)) { |
792 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
|
1197 | 793 |
} |
7356 | 794 |
if (__qIsShortFloat(aNumber)) { |
795 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
|
1197 | 796 |
} |
797 |
%}. |
|
7356 | 798 |
^ aNumber equalFromShortFloat:self |
1197 | 799 |
! |
800 |
||
801 |
> aNumber |
|
802 |
"return true, if the argument is less" |
|
803 |
||
804 |
%{ /* NOCONTEXT */ |
|
805 |
||
806 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 807 |
RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false ); |
1197 | 808 |
} |
7356 | 809 |
if (aNumber != nil) { |
810 |
if (__qIsFloatLike(aNumber)) { |
|
811 |
RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false ); |
|
812 |
} |
|
813 |
if (__qIsShortFloat(aNumber)) { |
|
814 |
RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false ); |
|
815 |
} |
|
1197 | 816 |
} |
817 |
%}. |
|
818 |
^ self retry:#> coercing:aNumber |
|
819 |
! |
|
820 |
||
821 |
>= aNumber |
|
822 |
"return true, if the argument is less or equal" |
|
823 |
||
824 |
%{ /* NOCONTEXT */ |
|
825 |
||
826 |
if (__isSmallInteger(aNumber)) { |
|
7356 | 827 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 828 |
} |
7356 | 829 |
if (aNumber != nil) { |
830 |
if (__qIsFloatLike(aNumber)) { |
|
831 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
|
832 |
} |
|
833 |
if (__qIsShortFloat(aNumber)) { |
|
834 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
|
835 |
} |
|
1197 | 836 |
} |
837 |
%}. |
|
838 |
^ self retry:#>= coercing:aNumber |
|
839 |
! |
|
840 |
||
841 |
hash |
|
842 |
"return a number for hashing; redefined, since floats compare |
|
843 |
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
|
844 |
as 3 hash." |
1197 | 845 |
|
846 |
|i| |
|
847 |
||
848 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
849 |
i := self asInteger. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
850 |
self = i ifTrue:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
851 |
^ i hash |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
852 |
]. |
1197 | 853 |
]. |
854 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
855 |
^ self asFloat hash |
1197 | 856 |
|
857 |
" |
|
858 |
1.2345 hash |
|
859 |
1.2345 asShortFloat hash |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
860 |
1.0 hash |
1197 | 861 |
1.0 asShortFloat hash |
4593 | 862 |
0.5 asShortFloat hash |
863 |
0.25 asShortFloat hash |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
864 |
0.5 hash |
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
865 |
0.25 hash |
1197 | 866 |
" |
867 |
! |
|
868 |
||
869 |
~= aNumber |
|
870 |
"return true, if the arguments value are not equal" |
|
871 |
||
872 |
%{ /* NOCONTEXT */ |
|
873 |
||
7356 | 874 |
if (aNumber != nil) { |
875 |
if (__isSmallInteger(aNumber)) { |
|
876 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
|
877 |
} |
|
878 |
if (__qIsFloatLike(aNumber)) { |
|
879 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
|
880 |
} |
|
881 |
if (__qIsShortFloat(aNumber)) { |
|
882 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
|
883 |
} |
|
884 |
} else { |
|
885 |
RETURN ( true ); |
|
1197 | 886 |
} |
887 |
%}. |
|
7356 | 888 |
^ super ~= aNumber |
1197 | 889 |
! ! |
890 |
||
891 |
!ShortFloat methodsFor:'printing & storing'! |
|
892 |
||
893 |
printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
894 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
895 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
896 |
#printOn: as basic print mechanism." |
1197 | 897 |
|
898 |
%{ /* NOCONTEXT */ |
|
899 |
||
900 |
char buffer[64]; |
|
901 |
REGISTER char *cp; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
902 |
int len; |
1197 | 903 |
OBJ s; |
904 |
||
905 |
/* |
|
906 |
* actually only needed on sparc: since thisContext is |
|
907 |
* in a global register, which gets destroyed by printf, |
|
908 |
* manually save it here - very stupid ... |
|
909 |
*/ |
|
910 |
__BEGIN_PROTECT_REGISTERS__ |
|
911 |
||
912 |
#ifdef SYSV |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
913 |
len = snprintf(buffer, sizeof(buffer), "%.6lg", (double)__shortFloatVal(self)); |
1197 | 914 |
#else |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
915 |
len = snprintf(buffer, sizeof(buffer), "%.6G", (double)__shortFloatVal(self)); |
1197 | 916 |
#endif |
917 |
||
918 |
__END_PROTECT_REGISTERS__ |
|
919 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
920 |
if (len >= 0 && len <= sizeof(buffer)-3) { |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
921 |
/* |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
922 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
923 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
924 |
*/ |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
925 |
for (cp = buffer; *cp; cp++) { |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
926 |
if ((*cp == '.') || (*cp == 'E') || (*cp == 'e')) break; |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
927 |
} |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
928 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
929 |
if (__isCharacter(@global(DecimalPointCharacter))) { |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
930 |
*cp++ = __intVal(__characterVal(@global(DecimalPointCharacter))); |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
931 |
} else { |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
932 |
*cp++ = '.'; |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
933 |
} |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
934 |
*cp++ = '0'; |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
935 |
*cp = '\0'; |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
936 |
} else { |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
937 |
if (cp && (*cp == '.')) { |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
938 |
if (__isCharacter(@global(DecimalPointCharacter))) { |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
939 |
*cp = __intVal(__characterVal(@global(DecimalPointCharacter))); |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
940 |
} |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
941 |
} |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
942 |
} |
1197 | 943 |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
944 |
s = __MKSTRING(buffer); |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
945 |
if (s != nil) { |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
946 |
RETURN (s); |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
947 |
} |
1197 | 948 |
} |
949 |
%}. |
|
5432 | 950 |
^ self asFloat printString |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
951 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
952 |
" |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
953 |
DecimalPointCharacter := $,. |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
954 |
1.234 asShortFloat printString. |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
955 |
1.0 asShortFloat printString. |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
956 |
1e10 asShortFloat printString. |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
957 |
1.2e3 asShortFloat printString. |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
958 |
1.2e30 asShortFloat printString. |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
959 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
960 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
961 |
DecimalPointCharacter := $. |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
962 |
" |
5718 | 963 |
! |
964 |
||
965 |
printfPrintString:formatString |
|
7746 | 966 |
"non-standard: return a printed representation of the receiver |
5718 | 967 |
as specified by formatString, which is defined by printf. |
968 |
If you use this, be aware, that specifying doubles differs on |
|
969 |
systems; on SYSV machines you have to give something like %lf, |
|
970 |
while on BSD systems the format string has to be %F. |
|
971 |
Also, the resulting string may not be longer than 255 bytes - |
|
972 |
since thats the (static) size of the buffer. |
|
973 |
This method is NONSTANDARD and may be removed without notice." |
|
974 |
||
975 |
%{ /* STACK: 400 */ |
|
976 |
char buffer[256]; |
|
977 |
OBJ s; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
978 |
int len; |
5718 | 979 |
|
980 |
if (__isString(formatString)) { |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
981 |
/* |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
982 |
* actually only needed on sparc: since thisContext is |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
983 |
* in a global register, which gets destroyed by printf, |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
984 |
* manually save it here - very stupid ... |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
985 |
*/ |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
986 |
__BEGIN_PROTECT_REGISTERS__ |
5718 | 987 |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
988 |
len = snprintf(buffer, sizeof(buffer), __stringVal(formatString), __shortFloatVal(self)); |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
989 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
990 |
__END_PROTECT_REGISTERS__ |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
991 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
992 |
if (len < 0) goto fail; |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
993 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
994 |
s = __MKSTRING_L(buffer, len); |
5718 | 995 |
if (s != nil) { |
996 |
RETURN (s); |
|
997 |
} |
|
998 |
} |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
999 |
fail: ; |
5718 | 1000 |
%}. |
1001 |
self primitiveFailed |
|
1002 |
||
1003 |
"ShortFloat pi printfPrintString:'%%lg -> %lg'" |
|
1004 |
"ShortFloat pi printfPrintString:'%%lf -> %lf'" |
|
1005 |
"ShortFloat pi printfPrintString:'%%7.5lg -> %7.5lg'" |
|
1006 |
"ShortFloat pi printfPrintString:'%%G -> %G'" |
|
1007 |
"ShortFloat pi printfPrintString:'%%F -> %F'" |
|
1008 |
"ShortFloat pi printfPrintString:'%%7.5G -> %7.5G'" |
|
1009 |
"ShortFloat pi printfPrintString:'%%7.5F -> %7.5F'" |
|
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1010 |
! |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1011 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1012 |
storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1013 |
"return a printed representation of the receiver; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1014 |
all valid digits are printed. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1015 |
LimitedPrecisonReal and its subclasses use #storeString instead of |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1016 |
#storeOn: as basic print mechanism." |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1017 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1018 |
%{ /* NOCONTEXT */ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1019 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1020 |
char buffer[64]; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1021 |
REGISTER char *cp; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1022 |
OBJ s; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1023 |
int len; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1024 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1025 |
/* |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1026 |
* build a printf format string |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1027 |
*/ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1028 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1029 |
__BEGIN_PROTECT_REGISTERS__ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1030 |
#ifdef SYSV |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1031 |
len = snprintf(buffer, sizeof(buffer), "%.6lg", (double)__shortFloatVal(self)); |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1032 |
#else |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1033 |
len = snprintf(buffer, sizeof(buffer), "%.6G", (double)__shortFloatVal(self)); |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1034 |
#endif |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1035 |
__END_PROTECT_REGISTERS__ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1036 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1037 |
if (len >= 0 && len < sizeof(buffer)-3) { |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1038 |
/* |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1039 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1040 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1041 |
*/ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1042 |
for (cp = buffer; *cp; cp++) { |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1043 |
if ((*cp == '.') || (*cp == 'E') || (*cp == 'e')) break; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1044 |
} |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1045 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1046 |
*cp++ = '.'; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1047 |
*cp++ = '0'; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1048 |
*cp = '\0'; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1049 |
} |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1050 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1051 |
s = __MKSTRING(buffer); |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1052 |
if (s != nil) { |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1053 |
RETURN (s); |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1054 |
} |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1055 |
} |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1056 |
%}. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1057 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1058 |
memory allocation (for the new string) failed. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1059 |
When we arrive here, there was no memory, even after a garbage collect. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1060 |
This means, that the VM wanted to get some more memory from the |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1061 |
OS, which was not kind enough to give it. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1062 |
Bad luck - you should increase the swap space on your machine. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1063 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1064 |
^ ObjectMemory allocationFailureSignal raise. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1065 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1066 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1067 |
1.0 asShortFloat storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1068 |
1.234 asShortFloat storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1069 |
1e10 asShortFloat storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1070 |
1.2e3 asShortFloat storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1071 |
1.2e30 asShortFloat storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1072 |
Float pi asShortFloat storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1073 |
(1.0 uncheckedDivide:0) asShortFloat storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1074 |
(0.0 uncheckedDivide:0) asShortFloat storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1075 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1076 |
DecimalPointCharacter := $,. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1077 |
1.234 asShortFloat storeString. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1078 |
1.0 asShortFloat storeString. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1079 |
1e10 asShortFloat storeString. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1080 |
1.2e3 asShortFloat storeString. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1081 |
1.2e30 asShortFloat storeString. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1082 |
(1.0 uncheckedDivide:0) asShortFloat storeString. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1083 |
(0.0 uncheckedDivide:0) asShortFloat storeString. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1084 |
DecimalPointCharacter := $. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1085 |
" |
1197 | 1086 |
! ! |
1087 |
||
3897 | 1088 |
!ShortFloat methodsFor:'special access'! |
1089 |
||
1090 |
exponent |
|
1091 |
"extract a normalized floats exponent. |
|
1092 |
The returned value depends on the float-representation of |
|
1093 |
the underlying machine and is therefore highly unportable. |
|
1094 |
This is not for general use. |
|
1095 |
This assumes that the mantissa is normalized to |
|
1096 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
1097 |
||
1098 |
%{ /* NOCONTEXT */ |
|
1099 |
||
1100 |
double frexp(); |
|
8913 | 1101 |
int exp; |
3897 | 1102 |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1103 |
__threadErrno = 0; |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1104 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1105 |
frexpf( __shortFloatVal(self), &exp); |
1106 |
#else |
|
1107 |
frexp( (double)(__shortFloatVal(self)), &exp); |
|
1108 |
#endif |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1109 |
if (__threadErrno == 0) { |
8913 | 1110 |
RETURN (__mkSmallInteger(exp)); |
3897 | 1111 |
} |
1112 |
%}. |
|
7387 | 1113 |
^ super exponent |
3897 | 1114 |
|
1115 |
" |
|
7356 | 1116 |
4.0 asShortFloat exponent |
1117 |
2.0 asShortFloat exponent |
|
3897 | 1118 |
1.0 asShortFloat exponent |
1119 |
0.5 asShortFloat exponent |
|
1120 |
0.25 asShortFloat exponent |
|
1121 |
0.00000011111 asShortFloat exponent |
|
1122 |
" |
|
1123 |
! |
|
1124 |
||
1125 |
mantissa |
|
1126 |
"extract a normalized floats mantissa. |
|
1127 |
The returned value depends on the float-representation of |
|
1128 |
the underlying machine and is therefore highly unportable. |
|
1129 |
This is not for general use. |
|
1130 |
This assumes that the mantissa is normalized to |
|
1131 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
1132 |
||
1133 |
%{ /* NOCONTEXT */ |
|
8913 | 1134 |
int exp; |
3897 | 1135 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1136 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1137 |
float frac; |
1138 |
||
1139 |
__threadErrno = 0; |
|
1140 |
frac = frexpf( (double)__shortFloatVal(self), &exp); |
|
1141 |
if (__threadErrno == 0) { |
|
1142 |
RETURN (__MKSFLOAT(frac)); |
|
1143 |
} |
|
1144 |
#else |
|
3897 | 1145 |
double frexp(); |
1146 |
double frac; |
|
1147 |
||
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1148 |
__threadErrno = 0; |
3897 | 1149 |
frac = frexp( (double)(__shortFloatVal(self)), &exp); |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1150 |
if (__threadErrno == 0) { |
7356 | 1151 |
RETURN (__MKFLOAT(frac)); |
3897 | 1152 |
} |
7356 | 1153 |
#endif |
3897 | 1154 |
%}. |
1155 |
^ self primitiveFailed |
|
1156 |
||
1157 |
" |
|
1158 |
1.0 asShortFloat exponent |
|
1159 |
1.0 asShortFloat mantissa |
|
1160 |
||
1161 |
0.5 asShortFloat exponent |
|
1162 |
0.5 asShortFloat mantissa |
|
1163 |
||
1164 |
0.25 asShortFloat exponent |
|
1165 |
0.25 asShortFloat mantissa |
|
1166 |
||
1167 |
0.00000011111 asShortFloat exponent |
|
1168 |
0.00000011111 asShortFloat mantissa |
|
1169 |
" |
|
1170 |
! ! |
|
1171 |
||
1197 | 1172 |
!ShortFloat methodsFor:'testing'! |
1173 |
||
3147 | 1174 |
isFinite |
1175 |
"return true, if the receiver is a finite float |
|
1176 |
i.e. not NaN and not infinite." |
|
1177 |
||
1178 |
%{ /* NOCONTEXT */ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1179 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1180 |
if (finitef(__shortFloatVal(self))) { RETURN (true); } |
1181 |
#else |
|
3147 | 1182 |
double dV = (double) __shortFloatVal(self); |
1183 |
||
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1184 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1185 |
* 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
|
1186 |
* a finite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
1187 |
* 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
|
1188 |
*/ |
3147 | 1189 |
if (finite(dV)) { RETURN (true); } |
7356 | 1190 |
#endif |
3147 | 1191 |
%}. |
1192 |
^false |
|
1193 |
||
1194 |
" |
|
7356 | 1195 |
1.0 asShortFloat isFinite |
1196 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
1197 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
3147 | 1198 |
" |
1199 |
! |
|
1200 |
||
2387 | 1201 |
isNaN |
1202 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
1203 |
These are not created by ST/X float operations (they raise an exception); |
|
1204 |
however, inline C-code could produce them ..." |
|
1205 |
||
1206 |
%{ /* NOCONTEXT */ |
|
7408 | 1207 |
#if defined(__GNUC__) |
7356 | 1208 |
if (isnanf(__shortFloatVal(self))) { RETURN (true); } |
7408 | 1209 |
# else |
2387 | 1210 |
|
3139 | 1211 |
double dV = (double)(__shortFloatVal(self)); |
1212 |
||
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1213 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1214 |
* 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
|
1215 |
* a finite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
1216 |
* 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
|
1217 |
*/ |
3147 | 1218 |
if (isnan(dV)) { RETURN (true); } |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1219 |
#endif /* __GNUC__ */ |
2392 | 1220 |
%}. |
1221 |
^ false |
|
2387 | 1222 |
|
3147 | 1223 |
" |
7356 | 1224 |
1.0 asShortFloat isNaN |
1225 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
|
3147 | 1226 |
" |
2387 | 1227 |
! |
1228 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1229 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1230 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1231 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1232 |
%{ |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1233 |
#if defined(__i386__) && defined(linux) |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1234 |
RETURN ((__ByteArrayInstPtr(self)->ba_element[sizeof(float)-1] == 128) ? true : false ); |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1235 |
#endif |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1236 |
%}. |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1237 |
^ super isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1238 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1239 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1240 |
0.0 asShortFloat isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1241 |
-0.0 asShortFloat isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1242 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1243 |
! |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1244 |
|
1197 | 1245 |
negative |
1246 |
"return true if the receiver is less than zero" |
|
1247 |
||
1248 |
%{ /* NOCONTEXT */ |
|
1249 |
||
1250 |
RETURN ( (__shortFloatVal(self) < 0.0) ? true : false ); |
|
5411 | 1251 |
%}. |
1197 | 1252 |
! |
1253 |
||
5357 | 1254 |
numberOfBits |
1255 |
"return the size (in bits) of the real; |
|
1256 |
typically, 32 is returned here, |
|
1257 |
but who knows ..." |
|
1258 |
||
1259 |
%{ /* NOCONTEXT */ |
|
1260 |
||
8913 | 1261 |
RETURN (__mkSmallInteger (sizeof(float) * 8)); |
5357 | 1262 |
%} |
1263 |
||
1264 |
" |
|
1265 |
1.2 numberOfBits |
|
1266 |
1.2 asShortFloat numberOfBits |
|
1267 |
" |
|
1268 |
||
1269 |
! |
|
1270 |
||
1197 | 1271 |
positive |
1272 |
"return true if the receiver is greater or equal to zero" |
|
1273 |
||
1274 |
%{ /* NOCONTEXT */ |
|
1275 |
||
1276 |
RETURN ( (__shortFloatVal(self) >= 0.0) ? true : false ); |
|
1277 |
%} |
|
1880 | 1278 |
! |
1279 |
||
1280 |
strictlyPositive |
|
1281 |
"return true if the receiver is greater than zero" |
|
1282 |
||
1283 |
%{ /* NOCONTEXT */ |
|
1284 |
||
1285 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
1286 |
%} |
|
1197 | 1287 |
! ! |
1288 |
||
5552 | 1289 |
!ShortFloat methodsFor:'truncation & rounding'! |
3897 | 1290 |
|
7356 | 1291 |
ceiling |
1292 |
"return the smallest integer which is greater or equal to the receiver." |
|
1293 |
||
1294 |
|val| |
|
1295 |
||
1296 |
%{ |
|
1297 |
float fVal; |
|
1298 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1299 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1300 |
fVal = ceilf(__shortFloatVal(self)); |
1301 |
#else |
|
1302 |
fVal = (float)(ceil((double)__shortFloatVal(self))); |
|
1303 |
#endif |
|
1304 |
/* |
|
1305 |
* ST-80 (and X3J20) returns integer. |
|
1306 |
*/ |
|
1307 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
8913 | 1308 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1309 |
} |
1310 |
__qMKSFLOAT(val, fVal); |
|
1311 |
%}. |
|
1312 |
^ val asInteger |
|
1313 |
||
1314 |
" |
|
1315 |
0.5 asShortFloat ceiling |
|
1316 |
-0.5 asShortFloat ceiling |
|
1317 |
" |
|
1318 |
! |
|
1319 |
||
1320 |
ceilingAsFloat |
|
1321 |
"return the smallest integer-valued float greater or equal to the receiver. |
|
1322 |
This is much like #ceiling, but avoids a (possibly expensive) conversion |
|
1323 |
of the result to an integer. |
|
1324 |
It may be useful, if the result is to be further used in another float-operation." |
|
1325 |
||
1326 |
%{ /* NOCONTEXT */ |
|
1327 |
float fVal; |
|
1328 |
OBJ v; |
|
1329 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1330 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1331 |
fVal = ceilf(__shortFloatVal(self)); |
1332 |
#else |
|
1333 |
fVal = (float) ceil((double)__shortFloatVal(self)); |
|
1334 |
#endif |
|
1335 |
__qMKSFLOAT(v, fVal); |
|
1336 |
RETURN (v); |
|
1337 |
%} |
|
1338 |
" |
|
1339 |
0.5 asShortFloat ceilingAsFloat |
|
1340 |
-0.5 asShortFloat ceilingAsFloat |
|
1341 |
-1.5 asShortFloat ceilingAsFloat |
|
1342 |
" |
|
1343 |
! |
|
1344 |
||
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1345 |
floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1346 |
"return the integer nearest the receiver towards negative infinity." |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1347 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1348 |
|val| |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1349 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1350 |
%{ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1351 |
float fVal; |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1352 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1353 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1354 |
fVal = floorf(__shortFloatVal(self)); |
1355 |
#else |
|
1356 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
1357 |
#endif |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1358 |
/* |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1359 |
* ST-80 (and X3J20) returns integer. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1360 |
*/ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1361 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
8913 | 1362 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1363 |
} |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1364 |
__qMKSFLOAT(val, fVal); |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1365 |
%}. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1366 |
^ val asInteger |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1367 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1368 |
" |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1369 |
0.5 asShortFloat floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1370 |
-0.5 asShortFloat floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1371 |
" |
7356 | 1372 |
! |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1373 |
|
7356 | 1374 |
floorAsFloat |
1375 |
"return the float which represents the next lower |
|
1376 |
integer nearest the receiver towards negative infinity. |
|
1377 |
Much like floor, but returns a float result - useful if the result |
|
1378 |
will be used in another float operation, to avoid costy int-conversion." |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1379 |
|
7356 | 1380 |
%{ /* NOCONTEXT */ |
1381 |
float fVal; |
|
1382 |
OBJ v; |
|
1383 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1384 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1385 |
fVal = floorf(__shortFloatVal(self)); |
1386 |
#else |
|
1387 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
1388 |
#endif |
|
1389 |
__qMKSFLOAT(v, fVal); |
|
1390 |
RETURN ( v ); |
|
1391 |
%} |
|
1392 |
||
1393 |
" |
|
1394 |
0.5 asShortFloat floorAsFloat |
|
1395 |
-0.5 asShortFloat floorAsFloat |
|
1396 |
" |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1397 |
! |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
1398 |
|
3897 | 1399 |
fractionPart |
1400 |
"extract the after-decimal fraction part. |
|
7356 | 1401 |
such that (self truncated + self fractionPart) = self" |
3897 | 1402 |
|
1403 |
%{ /* NOCONTEXT */ |
|
1404 |
||
1405 |
double modf(); |
|
1406 |
double frac, trunc; |
|
1407 |
||
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1408 |
__threadErrno = 0; |
3897 | 1409 |
frac = modf((double)(__shortFloatVal(self)), &trunc); |
7403 | 1410 |
if (! isnan(frac)) { |
1411 |
if (__threadErrno == 0) { |
|
1412 |
RETURN (__MKSFLOAT((float)frac)); |
|
1413 |
} |
|
3897 | 1414 |
} |
1415 |
%}. |
|
7403 | 1416 |
^ self class |
1417 |
raise:#domainErrorSignal |
|
1418 |
receiver:self |
|
1419 |
selector:#fractionPart |
|
1420 |
arguments:#() |
|
1421 |
errorString:'bad receiver in fractionPart' |
|
3897 | 1422 |
|
1423 |
" |
|
7356 | 1424 |
1.6 asShortFloat fractionPart + 1.6 asShortFloat truncated |
1425 |
-1.6 asShortFloat fractionPart + -1.6 asShortFloat truncated |
|
1426 |
||
3897 | 1427 |
1.0 asShortFloat fractionalPart |
1428 |
0.5 asShortFloat fractionalPart |
|
1429 |
0.25 asShortFloat fractionalPart |
|
1430 |
3.14159 asShortFloat fractionalPart |
|
1431 |
12345673.14159 asShortFloat fractionalPart |
|
1432 |
123456731231231231.14159 asShortFloat fractionalPart |
|
1433 |
" |
|
7356 | 1434 |
! |
3897 | 1435 |
|
7356 | 1436 |
rounded |
1437 |
"return the receiver rounded to the nearest integer" |
|
1438 |
||
1439 |
|val| |
|
1440 |
||
1441 |
%{ |
|
1442 |
float fVal; |
|
1443 |
||
1444 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1445 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1446 |
if (fVal < 0.0) { |
1447 |
fVal = ceilf(fVal - (float)0.5); |
|
1448 |
} else { |
|
1449 |
fVal = floorf(fVal + (float)0.5); |
|
1450 |
} |
|
1451 |
#else |
|
1452 |
if (fVal < 0.0) { |
|
1453 |
fVal = (float)ceil((double)fVal - 0.5); |
|
1454 |
} else { |
|
1455 |
fVal = (float)floor((double)fVal + 0.5); |
|
1456 |
} |
|
1457 |
#endif |
|
1458 |
/* |
|
1459 |
* ST-80 (and X3J20) returns integer. |
|
1460 |
*/ |
|
1461 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
8913 | 1462 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1463 |
} |
1464 |
__qMKSFLOAT(val, fVal); |
|
1465 |
%}. |
|
1466 |
^ val asInteger |
|
1467 |
||
1468 |
" |
|
1469 |
0.4 asShortFloat rounded |
|
1470 |
0.5 asShortFloat rounded |
|
1471 |
0.6 asShortFloat rounded |
|
1472 |
-0.4 asShortFloat rounded |
|
1473 |
-0.5 asShortFloat rounded |
|
1474 |
-0.6 asShortFloat rounded |
|
1475 |
" |
|
1476 |
! |
|
1477 |
||
1478 |
roundedAsFloat |
|
1479 |
"return the receiver rounded to the nearest integer as a float. |
|
1480 |
This is much like #rounded, but avoids a (possibly expensive) conversion |
|
1481 |
of the result to an integer. |
|
1482 |
It may be useful, if the result is to be further used in another float-operation." |
|
1483 |
||
1484 |
%{ /* NOCONTEXT */ |
|
1485 |
float fVal; |
|
1486 |
OBJ v; |
|
1487 |
||
1488 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1489 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1490 |
if (fVal < 0.0) { |
1491 |
fVal = ceilf(fVal - (float)0.5); |
|
1492 |
} else { |
|
1493 |
fVal = floorf(fVal + (float)0.5); |
|
1494 |
} |
|
1495 |
#else |
|
1496 |
if (fVal < 0.0) { |
|
1497 |
fVal = (float)ceil((double)fVal - 0.5); |
|
1498 |
} else { |
|
1499 |
fVal = (float)floor((double)fVal + 0.5); |
|
1500 |
} |
|
1501 |
#endif |
|
1502 |
__qMKSFLOAT(v, fVal); |
|
1503 |
RETURN (v); |
|
1504 |
%} |
|
1505 |
||
1506 |
" |
|
1507 |
0.4 asShortFloat rounded |
|
1508 |
0.5 asShortFloat rounded |
|
1509 |
0.6 asShortFloat rounded |
|
1510 |
-0.4 asShortFloat rounded |
|
1511 |
-0.5 asShortFloat rounded |
|
1512 |
-0.6 asShortFloat rounded |
|
1513 |
||
1514 |
0.4 asShortFloat roundedAsFloat |
|
1515 |
0.5 asShortFloat roundedAsFloat |
|
1516 |
0.6 asShortFloat roundedAsFloat |
|
1517 |
-0.4 asShortFloat roundedAsFloat |
|
1518 |
-0.5 asShortFloat roundedAsFloat |
|
1519 |
-0.6 asShortFloat roundedAsFloat |
|
1520 |
" |
|
1521 |
! |
|
1522 |
||
1523 |
truncated |
|
1524 |
"return the receiver truncated towards zero as an integer" |
|
1525 |
||
1526 |
|val| |
|
1527 |
||
1528 |
%{ |
|
1529 |
float fVal; |
|
1530 |
||
1531 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1532 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1533 |
if (fVal < 0.0) { |
1534 |
fVal = ceilf(fVal); |
|
1535 |
} else { |
|
1536 |
fVal = floorf(fVal); |
|
1537 |
} |
|
1538 |
#else |
|
1539 |
if (fVal < 0.0) { |
|
1540 |
fVal = (float)ceil((double)fVal); |
|
1541 |
} else { |
|
1542 |
fVal = (float)floor((double)fVal); |
|
1543 |
} |
|
1544 |
#endif |
|
1545 |
/* |
|
1546 |
* ST-80 (and X3J20) returns integer. |
|
1547 |
*/ |
|
1548 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
8913 | 1549 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 1550 |
} |
1551 |
__qMKSFLOAT(val, fVal); |
|
1552 |
%}. |
|
1553 |
^ val asInteger |
|
1554 |
||
1555 |
" |
|
1556 |
0.5 asShortFloat truncated |
|
1557 |
-0.5 asShortFloat truncated |
|
1558 |
0.5 asShortFloat truncatedAsFloat |
|
1559 |
-0.5 asShortFloat truncatedAsFloat |
|
1560 |
" |
|
1561 |
! |
|
1562 |
||
1563 |
truncatedAsFloat |
|
1564 |
"return the receiver truncated towards zero as a float. |
|
1565 |
This is much like #truncated, but avoids a (possibly expensive) conversion |
|
1566 |
of the result to an integer. |
|
1567 |
It may be useful, if the result is to be further used in another |
|
1568 |
float-operation." |
|
1569 |
||
1570 |
%{ /* NOCONTEXT */ |
|
1571 |
float fVal; |
|
1572 |
OBJ v; |
|
1573 |
||
1574 |
fVal = __shortFloatVal(self); |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1575 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1576 |
if (fVal < 0.0) { |
1577 |
fVal = ceilf(fVal); |
|
1578 |
} else { |
|
1579 |
fVal = floorf(fVal); |
|
1580 |
} |
|
1581 |
#else |
|
1582 |
if (fVal < 0.0) { |
|
1583 |
fVal = (float)ceil((double)fVal); |
|
1584 |
} else { |
|
1585 |
fVal = (float)floor((double)fVal); |
|
1586 |
} |
|
1587 |
#endif |
|
1588 |
__qMKSFLOAT(v, fVal); |
|
1589 |
RETURN (v); |
|
1590 |
%} |
|
1591 |
||
1592 |
" |
|
1593 |
0.5 asShortFloat truncated |
|
1594 |
-0.5 asShortFloat truncated |
|
1595 |
0.5 asShortFloat truncatedAsFloat |
|
1596 |
-0.5 asShortFloat truncatedAsFloat |
|
1597 |
" |
|
3897 | 1598 |
! ! |
1599 |
||
1880 | 1600 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 1601 |
|
1602 |
version |
|
8913 | 1603 |
^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.89 2005-07-08 17:15:02 cg Exp $' |
1197 | 1604 |
! ! |