author | Claus Gittinger <cg@exept.de> |
Thu, 20 Nov 1997 19:22:49 +0100 | |
changeset 3115 | 1ecfb9118163 |
parent 3014 | 9934bb63d455 |
child 3118 | ab0dff902c31 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
175 | 3 |
All Rights Reserved |
1 | 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 |
||
54 | 13 |
LimitedPrecisionReal variableByteSubclass:#Float |
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
14 |
instanceVariableNames:'' |
2358 | 15 |
classVariableNames:'DefaultPrintFormat' |
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
16 |
poolDictionaries:'' |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
17 |
category:'Magnitude-Numbers' |
1 | 18 |
! |
19 |
||
701 | 20 |
!Float primitiveDefinitions! |
21 |
%{ |
|
22 |
#include <errno.h> |
|
23 |
||
24 |
/* |
|
25 |
* on some systems errno is a macro ... check for it here |
|
26 |
*/ |
|
27 |
#ifndef errno |
|
28 |
extern errno; |
|
29 |
#endif |
|
30 |
||
2394 | 31 |
#if defined (_AIX) |
32 |
# include <float.h> |
|
33 |
#endif |
|
2395 | 34 |
#if defined(IRIX) |
35 |
# include <nan.h> |
|
36 |
#endif |
|
2397 | 37 |
#if defined(LINUX) |
2396 | 38 |
# include <nan.h> |
39 |
#endif |
|
2399 | 40 |
#if defined(solaris) || defined(sunos) |
2398 | 41 |
# include <nan.h> |
42 |
#endif |
|
2394 | 43 |
|
701 | 44 |
%} |
45 |
! ! |
|
46 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
47 |
!Float class methodsFor:'documentation'! |
68 | 48 |
|
88 | 49 |
copyright |
50 |
" |
|
51 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
175 | 52 |
All Rights Reserved |
88 | 53 |
|
54 |
This software is furnished under a license and may be used |
|
55 |
only in accordance with the terms of that license and with the |
|
56 |
inclusion of the above copyright notice. This software may not |
|
57 |
be provided or otherwise made available to, or used by, any |
|
58 |
other person. No title to or ownership of the software is |
|
59 |
hereby transferred. |
|
60 |
" |
|
61 |
! |
|
62 |
||
68 | 63 |
documentation |
64 |
" |
|
65 |
Floats represent rational numbers with limited precision. In ST/X, Float uses |
|
66 |
the underlying C-compilers double implementation, therefore instances of Float |
|
67 |
are usually represented by the 8-byte IEE double precision float format. |
|
68 |
(but there is no guaranty). |
|
1 | 69 |
|
68 | 70 |
Notice, that Floats are defined as Byte-array to prevent the garbage collector |
71 |
from going into the value ... otherwise I needed a special case in many places. |
|
72 |
||
73 |
Also notice, that ST/X Floats are what Doubles are in ST-80 - this may change |
|
74 |
in one of the next versions (at least on machines, which provide different float |
|
75 |
and double types in their C-compiler. |
|
76 |
||
77 |
WARNING: |
|
78 |
The layout of float instances is known by the runtime system and the compiler; |
|
79 |
you may not add instance variables here. |
|
369 | 80 |
|
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
81 |
Also, subclassing is complicated by the fact, that the VM creates floats as results |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
82 |
of arithmetic operations (even if the operands are subclass-instances). |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
83 |
(It does the float-check by probing a bit in the classes flag instVar). |
85 | 84 |
|
1892 | 85 |
Mixed mode arithmetic: |
86 |
float op float -> float |
|
87 |
float op fix -> float |
|
88 |
float op integer -> float |
|
89 |
float op float -> float |
|
90 |
||
1295 | 91 |
[Class Variables:] |
85 | 92 |
|
1295 | 93 |
[see also:] |
94 |
Number |
|
1893 | 95 |
ShortFloat Fraction FixedPoint Integer |
1295 | 96 |
FloatArray DoubleArray |
97 |
||
98 |
[author:] |
|
99 |
Claus Gittinger |
|
68 | 100 |
" |
2215 | 101 |
! |
102 |
||
103 |
errorHandling |
|
104 |
" |
|
105 |
Floating point error handling and signalling depends on the systems |
|
106 |
(actually: the C-runtime systems) ability to handle floating point errors. |
|
107 |
Most systems report errors by raising an OS floatingPoint exception, |
|
108 |
which is mapped to a fpExceptionInterrupt in ST/X. |
|
109 |
However, some systems do return NaN as result. |
|
110 |
||
111 |
Currently, ST/X does not care specially for these systems - it maybe added |
|
112 |
easily, if there is sufficient customer interest, though. |
|
113 |
||
114 |
Try: |
|
115 |
|f1 f2| |
|
116 |
||
117 |
f1 := 1.0. |
|
118 |
f2 := 0.0. |
|
119 |
f1 / f2 |
|
120 |
||
121 |
or: |
|
122 |
2 arcSin |
|
123 |
" |
|
68 | 124 |
! ! |
1 | 125 |
|
2387 | 126 |
!Float class methodsFor:'initialization'! |
127 |
||
128 |
initialize |
|
129 |
DefaultPrintFormat := '.6' "/ 6 valid digits |
|
130 |
||
131 |
" |
|
132 |
DefaultPrintFormat := '.9' |
|
133 |
DefaultPrintFormat := '.6' |
|
134 |
" |
|
135 |
! ! |
|
136 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
137 |
!Float class methodsFor:'instance creation'! |
1 | 138 |
|
139 |
basicNew |
|
140 |
"return a new float - here we return 0.0 |
|
141 |
- floats are usually NOT created this way ... |
|
142 |
Its implemented here to allow things like binary store & load |
|
143 |
of floats. (but even this support will go away eventually, its not |
|
144 |
a good idea to store the bits of a float - the reader might have a |
|
13 | 145 |
totally different representation - so floats will eventually be |
146 |
binary stored in a device independent format." |
|
1 | 147 |
|
148 |
%{ /* NOCONTEXT */ |
|
1206
9d23c1f5e4d3
use quicker MKFLOAT in FLoat>>basicNew
Claus Gittinger <cg@exept.de>
parents:
1198
diff
changeset
|
149 |
OBJ newFloat; |
9d23c1f5e4d3
use quicker MKFLOAT in FLoat>>basicNew
Claus Gittinger <cg@exept.de>
parents:
1198
diff
changeset
|
150 |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
151 |
__qMKFLOAT(newFloat, 0.0); |
1206
9d23c1f5e4d3
use quicker MKFLOAT in FLoat>>basicNew
Claus Gittinger <cg@exept.de>
parents:
1198
diff
changeset
|
152 |
RETURN (newFloat); |
1 | 153 |
%} |
1206
9d23c1f5e4d3
use quicker MKFLOAT in FLoat>>basicNew
Claus Gittinger <cg@exept.de>
parents:
1198
diff
changeset
|
154 |
|
1 | 155 |
! ! |
156 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
157 |
!Float class methodsFor:'binary storage'! |
701 | 158 |
|
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
159 |
binaryDefinitionFrom:aStream manager: manager |
701 | 160 |
|f| |
161 |
||
162 |
f := self basicNew. |
|
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
163 |
self readBinaryIEEEDoubleFrom:aStream into:f. |
701 | 164 |
^ f |
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
165 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
166 |
"Modified: 16.4.1996 / 21:23:38 / cg" |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
167 |
! |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
168 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
169 |
readBinaryIEEEDoubleFrom:aStream |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
170 |
"read a float from the binary stream, aStream, |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
171 |
interpreting the next bytes as an IEEE formatted 8-byte float" |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
172 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
173 |
|f| |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
174 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
175 |
f := self basicNew. |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
176 |
self readBinaryIEEEDoubleFrom:aStream into:f. |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
177 |
^ f |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
178 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
179 |
"Created: 16.4.1996 / 20:59:59 / cg" |
701 | 180 |
! |
181 |
||
182 |
readBinaryIEEEDoubleFrom:aStream into:aFloat |
|
183 |
"read the receivers value from the binary stream, aStream, |
|
184 |
interpreting the next bytes as an IEEE formatted 8-byte float" |
|
185 |
||
186 |
" |
|
187 |
this implementation is wrong: does not work on non-IEEE machines |
|
188 |
(to date all machines where ST/X is running on use |
|
189 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
190 |
" |
|
191 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
192 |
"swap the bytes" |
|
193 |
8 to:1 by:-1 do:[:i | |
|
194 |
aFloat basicAt:i put:(aStream next) |
|
195 |
]. |
|
196 |
^ self |
|
197 |
]. |
|
198 |
1 to:8 do:[:i | |
|
199 |
aFloat basicAt:i put:aStream next |
|
200 |
] |
|
201 |
! |
|
202 |
||
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
203 |
readBinaryIEEESingleFrom:aStream |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
204 |
"read a float value from the binary stream, aStream, |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
205 |
interpreting the next bytes as an IEEE formatted 4-byte float" |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
206 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
207 |
|f| |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
208 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
209 |
f := self basicNew. |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
210 |
self readBinaryIEEESingleFrom:aStream into:f. |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
211 |
^ f |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
212 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
213 |
"Created: 16.4.1996 / 21:00:35 / cg" |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
214 |
! |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
215 |
|
701 | 216 |
readBinaryIEEESingleFrom:aStream into:aFloat |
217 |
"read a float value from the binary stream, aStream, |
|
218 |
interpreting the next bytes as an IEEE formatted 4-byte float" |
|
219 |
||
220 |
" |
|
221 |
this implementation is wrong: does not work on non-IEEE machines |
|
222 |
(to date all machines where ST/X is running on use |
|
223 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
224 |
" |
|
225 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
226 |
"swap the bytes" |
|
227 |
8 to:4 by:-1 do:[:i | |
|
228 |
aFloat basicAt:i put:(aStream next) |
|
229 |
]. |
|
230 |
^ self |
|
231 |
]. |
|
232 |
1 to:4 do:[:i | |
|
233 |
aFloat basicAt:i put:aStream next |
|
234 |
] |
|
235 |
! |
|
236 |
||
237 |
storeBinaryIEEEDouble:aFloat on:aStream |
|
238 |
"store aFloat as an IEEE formatted 8-byte float |
|
239 |
onto the binary stream, aStream" |
|
240 |
||
241 |
" |
|
242 |
this implementation is wrong: does not work on non-IEEE machines |
|
243 |
(to date all machines where ST/X is running on use |
|
244 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
245 |
" |
|
246 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
247 |
"swap the bytes" |
|
248 |
8 to:1 by:-1 do:[:i | |
|
249 |
aStream nextPut:(aFloat basicAt:i). |
|
250 |
]. |
|
251 |
^ self |
|
252 |
]. |
|
253 |
1 to:8 do:[:i | |
|
254 |
aStream nextPut:(aFloat basicAt:i). |
|
255 |
]. |
|
256 |
! |
|
257 |
||
258 |
storeBinaryIEEESingle:aFloat on:aStream |
|
259 |
"store aFloat as an IEEE formatted 4-byte float |
|
260 |
onto the binary stream, aStream" |
|
261 |
||
262 |
" |
|
263 |
this implementation is wrong: does not work on non-IEEE machines |
|
264 |
(to date all machines where ST/X is running on use |
|
265 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
266 |
" |
|
267 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
268 |
"swap the bytes" |
|
269 |
8 to:4 by:-1 do:[:i | |
|
270 |
aStream nextPut:(aFloat basicAt:i). |
|
271 |
]. |
|
272 |
^ self |
|
273 |
]. |
|
274 |
1 to:4 do:[:i | |
|
275 |
aStream nextPut:(aFloat basicAt:i). |
|
276 |
] |
|
277 |
! ! |
|
278 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
279 |
!Float class methodsFor:'constants'! |
1 | 280 |
|
701 | 281 |
pi |
1260 | 282 |
"return the constant pi as Float" |
1 | 283 |
|
701 | 284 |
^ 3.1415926535897932384626434 |
1260 | 285 |
|
286 |
"Modified: 23.4.1996 / 09:27:02 / cg" |
|
1 | 287 |
! |
288 |
||
289 |
unity |
|
1260 | 290 |
"return the neutral element for multiplication (1.0) as Float" |
1 | 291 |
|
292 |
^ 1.0 |
|
1260 | 293 |
|
294 |
"Modified: 23.4.1996 / 09:27:09 / cg" |
|
1 | 295 |
! |
296 |
||
701 | 297 |
zero |
1260 | 298 |
"return the neutral element for addition (0.0) as Float" |
1 | 299 |
|
701 | 300 |
^ 0.0 |
1260 | 301 |
|
302 |
"Modified: 23.4.1996 / 09:27:15 / cg" |
|
1 | 303 |
! ! |
304 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
305 |
!Float class methodsFor:'queries'! |
3 | 306 |
|
307 |
isBuiltInClass |
|
1264 | 308 |
"return true if this class is known by the run-time-system. |
309 |
Here, true is returned for myself, false for subclasses." |
|
3 | 310 |
|
311 |
^ self == Float |
|
1264 | 312 |
|
313 |
"Modified: 23.4.1996 / 15:59:04 / cg" |
|
3 | 314 |
! ! |
315 |
||
1 | 316 |
!Float methodsFor:'arithmetic'! |
317 |
||
701 | 318 |
* aNumber |
319 |
"return the product of the receiver and the argument, aNumber" |
|
320 |
||
321 |
%{ /* NOCONTEXT */ |
|
322 |
||
323 |
OBJ newFloat; |
|
324 |
double result; |
|
325 |
||
326 |
if (__isSmallInteger(aNumber)) { |
|
1133 | 327 |
result = __floatVal(self) * (double)(__intVal(aNumber)); |
701 | 328 |
retResult: |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
329 |
__qMKFLOAT(newFloat, result); |
701 | 330 |
RETURN ( newFloat ); |
331 |
} |
|
332 |
if (__isFloatLike(aNumber)) { |
|
1133 | 333 |
result = __floatVal(self) * __floatVal(aNumber); |
701 | 334 |
goto retResult; |
335 |
} |
|
336 |
%} |
|
337 |
. |
|
338 |
^ aNumber productFromFloat:self |
|
339 |
! |
|
340 |
||
1 | 341 |
+ aNumber |
342 |
"return the sum of the receiver and the argument, aNumber" |
|
343 |
||
344 |
%{ /* NOCONTEXT */ |
|
13 | 345 |
|
1 | 346 |
OBJ newFloat; |
347 |
double result; |
|
348 |
||
250 | 349 |
if (__isSmallInteger(aNumber)) { |
1133 | 350 |
result = __floatVal(self) + (double)(__intVal(aNumber)); |
1 | 351 |
retResult: |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
352 |
__qMKFLOAT(newFloat, result); |
175 | 353 |
RETURN ( newFloat ); |
1 | 354 |
} |
283 | 355 |
if (__isFloatLike(aNumber)) { |
1133 | 356 |
result = __floatVal(self) + __floatVal(aNumber); |
175 | 357 |
goto retResult; |
1 | 358 |
} |
359 |
%} |
|
360 |
. |
|
361 |
^ aNumber sumFromFloat:self |
|
362 |
! |
|
363 |
||
364 |
- aNumber |
|
365 |
"return the difference of the receiver and the argument, aNumber" |
|
366 |
||
367 |
%{ /* NOCONTEXT */ |
|
13 | 368 |
|
1 | 369 |
OBJ newFloat; |
370 |
double result; |
|
371 |
||
250 | 372 |
if (__isSmallInteger(aNumber)) { |
1133 | 373 |
result = __floatVal(self) - (double)(__intVal(aNumber)); |
1 | 374 |
retResult: |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
375 |
__qMKFLOAT(newFloat, result); |
175 | 376 |
RETURN ( newFloat ); |
1 | 377 |
} |
283 | 378 |
if (__isFloatLike(aNumber)) { |
1133 | 379 |
result = __floatVal(self) - __floatVal(aNumber); |
175 | 380 |
goto retResult; |
1 | 381 |
} |
382 |
%} |
|
383 |
. |
|
384 |
^ aNumber differenceFromFloat:self |
|
385 |
! |
|
386 |
||
387 |
/ aNumber |
|
388 |
"return the quotient of the receiver and the argument, aNumber" |
|
389 |
||
390 |
%{ /* NOCONTEXT */ |
|
391 |
||
392 |
OBJ newFloat; |
|
393 |
double result, val; |
|
394 |
||
250 | 395 |
if (__isSmallInteger(aNumber)) { |
1133 | 396 |
if (aNumber != __MKSMALLINT(0)) { |
397 |
result = __floatVal(self) / ( (double)__intVal(aNumber)) ; |
|
1 | 398 |
retResult: |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
399 |
__qMKFLOAT(newFloat, result); |
175 | 400 |
RETURN ( newFloat ); |
401 |
} |
|
1 | 402 |
} else { |
305 | 403 |
if (__isFloatLike(aNumber)) { |
1133 | 404 |
val = __floatVal(aNumber); |
175 | 405 |
if (val != 0.0) { |
1133 | 406 |
result = __floatVal(self) / val; |
175 | 407 |
goto retResult; |
408 |
} |
|
409 |
} |
|
1 | 410 |
} |
411 |
%} |
|
412 |
. |
|
413 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
142 | 414 |
" |
415 |
No, you shalt not divide by zero |
|
416 |
" |
|
175 | 417 |
^ DivisionByZeroSignal raise. |
1 | 418 |
]. |
419 |
^ aNumber quotientFromFloat:self |
|
420 |
! |
|
421 |
||
422 |
negated |
|
423 |
"return myself negated" |
|
424 |
||
425 |
%{ /* NOCONTEXT */ |
|
13 | 426 |
|
3 | 427 |
OBJ newFloat; |
1133 | 428 |
double rslt = - __floatVal(self); |
3 | 429 |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
430 |
__qMKFLOAT(newFloat, rslt); |
3 | 431 |
RETURN ( newFloat ); |
1 | 432 |
%} |
433 |
! ! |
|
434 |
||
701 | 435 |
!Float methodsFor:'binary storage'! |
436 |
||
437 |
storeBinaryDefinitionOn:stream manager:manager |
|
1260 | 438 |
"store the receiver in a binary format on stream. |
439 |
This is an internal interface for binary storage mechanism." |
|
440 |
||
701 | 441 |
manager putIdOfClass:(self class) on:stream. |
442 |
Float storeBinaryIEEEDouble:self on:stream. |
|
1260 | 443 |
|
444 |
"Modified: 23.4.1996 / 09:29:48 / cg" |
|
701 | 445 |
! ! |
446 |
||
447 |
!Float methodsFor:'coercion and converting'! |
|
1 | 448 |
|
3014 | 449 |
asDouble |
450 |
"ST80 compatibility: return a float with same value - thats me" |
|
451 |
||
452 |
^ self |
|
453 |
! |
|
454 |
||
701 | 455 |
asFloat |
456 |
"return a float with same value - thats me" |
|
457 |
||
458 |
^ self |
|
459 |
! |
|
460 |
||
461 |
asInteger |
|
462 |
"return an integer with same value - might truncate" |
|
463 |
||
1 | 464 |
%{ /* NOCONTEXT */ |
1670 | 465 |
double dVal; |
13 | 466 |
|
1670 | 467 |
dVal = __floatVal(self); |
468 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
|
469 |
RETURN ( __MKSMALLINT( (INT)dVal) ); |
|
701 | 470 |
} |
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
471 |
%}. |
701 | 472 |
^ super asInteger |
473 |
||
474 |
"12345.0 asInteger" |
|
475 |
"1e15 asInteger" |
|
1 | 476 |
! |
477 |
||
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
478 |
asShortFloat |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
479 |
"return a shortFloat with same value as receiver" |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
480 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
481 |
%{ /* NOCONTEXT */ |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
482 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
483 |
OBJ dummy = @global(ShortFloat); |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
484 |
OBJ newFloat; |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
485 |
float fVal = (float)__floatVal(self); |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
486 |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
487 |
__qMKSFLOAT(newFloat, fVal); |
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
488 |
RETURN ( newFloat ); |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
489 |
%} |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
490 |
! |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
491 |
|
701 | 492 |
coerce:aNumber |
493 |
"return aNumber converted into receivers type" |
|
1 | 494 |
|
701 | 495 |
^ aNumber asFloat |
496 |
! |
|
13 | 497 |
|
701 | 498 |
generality |
499 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
500 |
||
501 |
^ 80 |
|
1 | 502 |
! ! |
503 |
||
504 |
!Float methodsFor:'comparing'! |
|
505 |
||
506 |
< aNumber |
|
507 |
"return true, if the argument is greater" |
|
508 |
||
509 |
%{ /* NOCONTEXT */ |
|
13 | 510 |
|
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
511 |
if (aNumber != nil) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
512 |
if (__isSmallInteger(aNumber)) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
513 |
RETURN ( (__floatVal(self) < (double)(__intVal(aNumber))) ? true : false ); |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
514 |
} |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
515 |
if (__qIsFloatLike(aNumber)) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
516 |
RETURN ( (__floatVal(self) < __floatVal(aNumber)) ? true : false ); |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
517 |
} |
1 | 518 |
} |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
519 |
%}. |
1 | 520 |
^ aNumber lessFromFloat:self |
521 |
! |
|
522 |
||
523 |
<= aNumber |
|
524 |
"return true, if the argument is greater or equal" |
|
525 |
||
526 |
%{ /* NOCONTEXT */ |
|
13 | 527 |
|
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
528 |
if (aNumber != nil) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
529 |
if (__isSmallInteger(aNumber)) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
530 |
RETURN ( (__floatVal(self) <= (double)(__intVal(aNumber))) ? true : false ); |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
531 |
} |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
532 |
if (__qIsFloatLike(aNumber)) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
533 |
RETURN ( (__floatVal(self) <= __floatVal(aNumber)) ? true : false ); |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
534 |
} |
1 | 535 |
} |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
536 |
%}. |
1 | 537 |
^ self retry:#<= coercing:aNumber |
538 |
! |
|
539 |
||
540 |
= aNumber |
|
175 | 541 |
"return true, if the arguments value are equal by value" |
1 | 542 |
|
543 |
%{ /* NOCONTEXT */ |
|
13 | 544 |
|
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
545 |
if (aNumber != nil) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
546 |
if (__isSmallInteger(aNumber)) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
547 |
RETURN ( (__floatVal(self) == (double)(__intVal(aNumber))) ? true : false ); |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
548 |
} |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
549 |
if (__qIsFloatLike(aNumber)) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
550 |
RETURN ( (__floatVal(self) == __floatVal(aNumber)) ? true : false ); |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
551 |
} |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
552 |
} else { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
553 |
RETURN (false); |
1 | 554 |
} |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
555 |
%}. |
1 | 556 |
^ self retry:#= coercing:aNumber |
557 |
! |
|
558 |
||
701 | 559 |
> aNumber |
560 |
"return true, if the argument is less" |
|
1 | 561 |
|
562 |
%{ /* NOCONTEXT */ |
|
13 | 563 |
|
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
564 |
if (aNumber != nil) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
565 |
if (__isSmallInteger(aNumber)) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
566 |
RETURN ( (__floatVal(self) > (double)(__intVal(aNumber))) ? true : false ); |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
567 |
} |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
568 |
if (__qIsFloatLike(aNumber)) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
569 |
RETURN ( (__floatVal(self) > __floatVal(aNumber)) ? true : false ); |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
570 |
} |
1 | 571 |
} |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
572 |
%}. |
701 | 573 |
^ self retry:#> coercing:aNumber |
574 |
! |
|
575 |
||
576 |
>= aNumber |
|
577 |
"return true, if the argument is less or equal" |
|
578 |
||
579 |
%{ /* NOCONTEXT */ |
|
580 |
||
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
581 |
if (aNumber != nil) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
582 |
if (__isSmallInteger(aNumber)) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
583 |
RETURN ( (__floatVal(self) >= (double)(__intVal(aNumber))) ? true : false ); |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
584 |
} |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
585 |
if (__qIsFloatLike(aNumber)) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
586 |
RETURN ( (__floatVal(self) >= __floatVal(aNumber)) ? true : false ); |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
587 |
} |
701 | 588 |
} |
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
589 |
%}. |
701 | 590 |
^ self retry:#>= coercing:aNumber |
92 | 591 |
! |
592 |
||
593 |
hash |
|
594 |
"return a number for hashing; redefined, since floats compare |
|
595 |
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same |
|
596 |
as 3 hash." |
|
597 |
||
305 | 598 |
|i| |
92 | 599 |
|
600 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
175 | 601 |
i := self asInteger. |
602 |
self = i ifTrue:[ |
|
603 |
^ i hash |
|
604 |
]. |
|
92 | 605 |
]. |
606 |
||
607 |
" |
|
305 | 608 |
mhmh take some of my value-bits to hash on |
92 | 609 |
" |
305 | 610 |
^ (((self basicAt:8) bitAnd:16r3F) bitShift:24) + |
611 |
((self basicAt:7) bitShift:16) + |
|
612 |
((self basicAt:6) bitShift:8) + |
|
613 |
(self basicAt:5) |
|
92 | 614 |
|
615 |
" |
|
616 |
3 hash |
|
617 |
3.0 hash |
|
618 |
3.1 hash |
|
619 |
3.14159 hash |
|
620 |
31.4159 hash |
|
621 |
3.141591 hash |
|
622 |
1.234567890123456 hash |
|
623 |
1.234567890123457 hash |
|
624 |
Set withAll:#(3 3.0 99 99.0 3.1415) |
|
625 |
" |
|
1 | 626 |
! |
627 |
||
701 | 628 |
~= aNumber |
629 |
"return true, if the arguments value are not equal" |
|
1 | 630 |
|
631 |
%{ /* NOCONTEXT */ |
|
632 |
||
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
633 |
if (aNumber != nil) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
634 |
if (__isSmallInteger(aNumber)) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
635 |
RETURN ( (__floatVal(self) != (double)(__intVal(aNumber))) ? true : false ); |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
636 |
} |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
637 |
if (__qIsFloatLike(aNumber)) { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
638 |
RETURN ( (__floatVal(self) != __floatVal(aNumber)) ? true : false ); |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
639 |
} |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
640 |
} else { |
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
641 |
RETURN (true); |
1 | 642 |
} |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
643 |
%}. |
701 | 644 |
^ self retry:#~= coercing:aNumber |
1 | 645 |
! ! |
646 |
||
647 |
!Float methodsFor:'mathematical functions'! |
|
648 |
||
701 | 649 |
arcCos |
650 |
"return the arccosine of myself as radians" |
|
651 |
||
652 |
%{ /* NOCONTEXT */ |
|
653 |
||
654 |
double acos(); |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
655 |
double rslt; |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
656 |
OBJ newFloat; |
701 | 657 |
|
658 |
errno = 0; |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
659 |
rslt = acos(__floatVal(self)); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
660 |
if (errno == 0) { |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
661 |
__qMKFLOAT(newFloat, rslt); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
662 |
RETURN ( newFloat ); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
663 |
} |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
664 |
%}. |
701 | 665 |
^ DomainErrorSignal raise |
666 |
! |
|
667 |
||
668 |
arcSin |
|
669 |
"return the arcsine of myself as radians" |
|
670 |
||
671 |
%{ /* NOCONTEXT */ |
|
672 |
||
673 |
double asin(); |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
674 |
double rslt; |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
675 |
OBJ newFloat; |
701 | 676 |
|
677 |
errno = 0; |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
678 |
rslt = asin(__floatVal(self)); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
679 |
if (errno == 0) { |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
680 |
__qMKFLOAT(newFloat, rslt); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
681 |
RETURN ( newFloat ); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
682 |
} |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
683 |
%}. |
701 | 684 |
^ DomainErrorSignal raise |
685 |
! |
|
686 |
||
687 |
arcTan |
|
688 |
"return the arctangent of myself as radians" |
|
689 |
||
690 |
%{ /* NOCONTEXT */ |
|
691 |
||
692 |
double atan(); |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
693 |
double rslt; |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
694 |
OBJ newFloat; |
701 | 695 |
|
696 |
errno = 0; |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
697 |
rslt = atan(__floatVal(self)); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
698 |
if (errno == 0) { |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
699 |
__qMKFLOAT(newFloat, rslt); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
700 |
RETURN ( newFloat ); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
701 |
} |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
702 |
%}. |
701 | 703 |
^ DomainErrorSignal raise |
704 |
! |
|
705 |
||
706 |
cos |
|
707 |
"return the cosine of myself interpreted as radians" |
|
708 |
||
709 |
%{ /* NOCONTEXT */ |
|
710 |
||
711 |
double cos(); |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
712 |
double rslt; |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
713 |
OBJ newFloat; |
701 | 714 |
|
715 |
errno = 0; |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
716 |
rslt = cos(__floatVal(self)); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
717 |
if (errno == 0) { |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
718 |
__qMKFLOAT(newFloat, rslt); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
719 |
RETURN ( newFloat ); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
720 |
} |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
721 |
%}. |
701 | 722 |
^ DomainErrorSignal raise |
723 |
! |
|
724 |
||
725 |
exp |
|
726 |
"return e raised to the power of the receiver" |
|
727 |
||
728 |
%{ /* NOCONTEXT */ |
|
729 |
||
730 |
double exp(); |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
731 |
double rslt; |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
732 |
OBJ newFloat; |
701 | 733 |
|
734 |
errno = 0; |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
735 |
rslt = exp(__floatVal(self)); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
736 |
if (errno == 0) { |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
737 |
__qMKFLOAT(newFloat, rslt); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
738 |
RETURN ( newFloat ); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
739 |
} |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
740 |
%}. |
701 | 741 |
^ DomainErrorSignal raise |
742 |
! |
|
743 |
||
1 | 744 |
ln |
745 |
"return the natural logarithm of myself" |
|
746 |
||
747 |
%{ /* NOCONTEXT */ |
|
748 |
||
749 |
double log(); |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
750 |
double rslt; |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
751 |
OBJ newFloat; |
1 | 752 |
|
753 |
errno = 0; |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
754 |
rslt = log(__floatVal(self)); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
755 |
if (errno == 0) { |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
756 |
__qMKFLOAT(newFloat, rslt); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
757 |
RETURN ( newFloat ); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
758 |
} |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
759 |
%}. |
142 | 760 |
" |
761 |
an invalid value for logarithm |
|
762 |
" |
|
131 | 763 |
^ DomainErrorSignal raise |
1 | 764 |
! |
765 |
||
766 |
raisedTo:aNumber |
|
767 |
"return self raised to the power of aNumber" |
|
768 |
|n| |
|
769 |
||
770 |
n := aNumber asFloat. |
|
771 |
%{ |
|
772 |
double pow(); |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
773 |
double rslt; |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
774 |
OBJ newFloat; |
1 | 775 |
|
283 | 776 |
if (__isFloatLike(n)) { |
175 | 777 |
errno = 0; |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
778 |
rslt = pow(__floatVal(self), __floatVal(n)); |
175 | 779 |
errno = 0; /* XXXX */ |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
780 |
if (errno == 0) { |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
781 |
__qMKFLOAT(newFloat, rslt); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
782 |
RETURN ( newFloat ); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
783 |
} |
1 | 784 |
} |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
785 |
%}. |
142 | 786 |
" |
787 |
an invalid argument (not convertable to float ?) |
|
788 |
" |
|
131 | 789 |
^ DomainErrorSignal raise |
1 | 790 |
! |
791 |
||
792 |
sin |
|
793 |
"return the sine of myself interpreted as radians" |
|
794 |
||
795 |
%{ /* NOCONTEXT */ |
|
796 |
||
797 |
double sin(); |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
798 |
double rslt; |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
799 |
OBJ newFloat; |
1 | 800 |
|
801 |
errno = 0; |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
802 |
rslt = sin(__floatVal(self)); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
803 |
if (errno == 0) { |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
804 |
__qMKFLOAT(newFloat, rslt); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
805 |
RETURN ( newFloat ); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
806 |
} |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
807 |
%}. |
131 | 808 |
^ DomainErrorSignal raise |
1 | 809 |
! |
810 |
||
811 |
sqrt |
|
812 |
"return the square root of myself" |
|
813 |
||
814 |
%{ /* NOCONTEXT */ |
|
815 |
||
816 |
double sqrt(); |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
817 |
double rslt; |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
818 |
OBJ newFloat; |
1 | 819 |
|
820 |
errno = 0; |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
821 |
rslt = sqrt(__floatVal(self)); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
822 |
if (errno == 0) { |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
823 |
__qMKFLOAT(newFloat, rslt); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
824 |
RETURN ( newFloat ); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
825 |
} |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
826 |
%}. |
131 | 827 |
^ DomainErrorSignal raise |
701 | 828 |
! |
258 | 829 |
|
701 | 830 |
tan |
831 |
"return the tangent of myself interpreted as radians" |
|
258 | 832 |
|
833 |
%{ /* NOCONTEXT */ |
|
834 |
||
701 | 835 |
double tan(); |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
836 |
double rslt; |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
837 |
OBJ newFloat; |
44 | 838 |
|
701 | 839 |
errno = 0; |
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
840 |
rslt = tan(__floatVal(self)); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
841 |
if (errno == 0) { |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
842 |
__qMKFLOAT(newFloat, rslt); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
843 |
RETURN ( newFloat ); |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
844 |
} |
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
845 |
%}. |
701 | 846 |
^ DomainErrorSignal raise |
13 | 847 |
! ! |
848 |
||
92 | 849 |
!Float methodsFor:'printing & storing'! |
1 | 850 |
|
851 |
printString |
|
2358 | 852 |
"return a printed representation of the receiver; |
853 |
if not specified otherwise (by setting DefaultPrintFormat), |
|
854 |
6 valid digits are printed." |
|
1 | 855 |
|
856 |
%{ /* NOCONTEXT */ |
|
857 |
||
13 | 858 |
char buffer[64]; |
1 | 859 |
REGISTER char *cp; |
314 | 860 |
OBJ s; |
2358 | 861 |
char *fmt; |
862 |
char fmtBuffer[20]; |
|
863 |
||
864 |
if (__isString(@global(DefaultPrintFormat))) { |
|
2892 | 865 |
fmt = (char *) __stringVal(@global(DefaultPrintFormat)); |
2358 | 866 |
} else { |
867 |
/* |
|
868 |
* in case we get called before #initialize ... |
|
869 |
*/ |
|
870 |
fmt = ".6"; |
|
871 |
} |
|
872 |
||
873 |
/* |
|
874 |
* build a printf format string |
|
875 |
*/ |
|
876 |
fmtBuffer[0] = '%'; |
|
877 |
strncpy(fmtBuffer+1, fmt, 10); |
|
878 |
#ifdef SYSV |
|
879 |
strcat(fmtBuffer, "lg"); |
|
880 |
#else |
|
881 |
strcat(fmtBuffer, "G"); |
|
882 |
#endif |
|
314 | 883 |
|
31 | 884 |
/* |
885 |
* actually only needed on sparc: since thisContext is |
|
886 |
* in a global register, which gets destroyed by printf, |
|
887 |
* manually save it here - very stupid ... |
|
888 |
*/ |
|
809
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
889 |
__BEGIN_PROTECT_REGISTERS__ |
1 | 890 |
|
2358 | 891 |
sprintf(buffer, fmtBuffer, __floatVal(self)); |
1 | 892 |
|
809
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
893 |
__END_PROTECT_REGISTERS__ |
1133 | 894 |
|
1 | 895 |
/* |
13 | 896 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
1 | 897 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
898 |
*/ |
|
899 |
for (cp = buffer; *cp; cp++) { |
|
175 | 900 |
if ((*cp == '.') || (*cp == 'e')) break; |
1 | 901 |
} |
902 |
if (! *cp) { |
|
175 | 903 |
*cp++ = '.'; |
904 |
*cp++ = '0'; |
|
905 |
*cp = '\0'; |
|
1 | 906 |
} |
907 |
||
1133 | 908 |
s = __MKSTRING(buffer COMMA_SND); |
314 | 909 |
if (s != nil) { |
343 | 910 |
RETURN (s); |
314 | 911 |
} |
912 |
%}. |
|
913 |
" |
|
914 |
memory allocation (for the new string) failed. |
|
915 |
When we arrive here, there was no memory, even after a garbage collect. |
|
916 |
This means, that the VM wanted to get some more memory from the |
|
917 |
OS, which was not kind enough to give it. |
|
918 |
Bad luck - you should increase the swap space on your machine. |
|
919 |
" |
|
920 |
^ ObjectMemory allocationFailureSignal raise. |
|
921 |
||
1 | 922 |
"1.0 printString" |
923 |
"1.234 printString" |
|
924 |
"1e10 printString" |
|
925 |
"1.2e3 printString" |
|
926 |
"1.2e30 printString" |
|
927 |
! |
|
928 |
||
929 |
printfPrintString:formatString |
|
930 |
"non-portable: return a printed representation of the receiver |
|
931 |
as specified by formatString, which is defined by printf. |
|
31 | 932 |
If you use this, be aware, that specifying doubles differs on |
1 | 933 |
systems; on SYSV machines you have to give something like %lf, |
13 | 934 |
while on BSD systems the format string has to be %F. |
31 | 935 |
Also, the resulting string may not be longer than 255 bytes - |
54 | 936 |
since thats the (static) size of the buffer. |
937 |
This method is NONSTANDARD and may be removed without notice." |
|
1 | 938 |
|
13 | 939 |
%{ /* STACK: 400 */ |
1 | 940 |
char buffer[256]; |
314 | 941 |
OBJ s; |
1 | 942 |
|
54 | 943 |
if (__isString(formatString)) { |
175 | 944 |
/* |
945 |
* actually only needed on sparc: since thisContext is |
|
946 |
* in a global register, which gets destroyed by printf, |
|
947 |
* manually save it here - very stupid ... |
|
948 |
*/ |
|
809
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
949 |
__BEGIN_PROTECT_REGISTERS__ |
31 | 950 |
|
1133 | 951 |
sprintf(buffer, __stringVal(formatString), __floatVal(self)); |
369 | 952 |
|
809
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
953 |
__END_PROTECT_REGISTERS__ |
369 | 954 |
|
1133 | 955 |
s = __MKSTRING(buffer COMMA_SND); |
343 | 956 |
if (s != nil) { |
957 |
RETURN (s); |
|
958 |
} |
|
1 | 959 |
} |
314 | 960 |
%}. |
1 | 961 |
self primitiveFailed |
962 |
||
963 |
"Float pi printfPrintString:'%%lg -> %lg'" |
|
964 |
"Float pi printfPrintString:'%%lf -> %lf'" |
|
965 |
"Float pi printfPrintString:'%%7.5lg -> %7.5lg'" |
|
966 |
"Float pi printfPrintString:'%%7.5lf -> %7.5lf'" |
|
967 |
"Float pi printfPrintString:'%%G -> %G'" |
|
968 |
"Float pi printfPrintString:'%%F -> %F'" |
|
969 |
"Float pi printfPrintString:'%%7.5G -> %7.5G'" |
|
970 |
"Float pi printfPrintString:'%%7.5F -> %7.5F'" |
|
971 |
! ! |
|
701 | 972 |
|
973 |
!Float methodsFor:'private accessing'! |
|
974 |
||
975 |
basicAt:index |
|
976 |
"return an internal byte of the float. |
|
977 |
The value returned here depends on byte order, float representation etc. |
|
978 |
Therefore, this method should be used strictly private. |
|
979 |
||
980 |
Notice: |
|
981 |
the need to redefine this method here is due to the |
|
982 |
inability of many machines to store floats in non-double aligned memory. |
|
983 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
|
984 |
and the actual float is stored at index 5 .. 12. |
|
985 |
To hide this at one place, this method knows about that, and returns |
|
986 |
values as if this filler wasnt present." |
|
987 |
||
988 |
%{ /* NOCONTEXT */ |
|
989 |
||
990 |
register int indx; |
|
991 |
unsigned char *cp; |
|
992 |
||
993 |
/* |
|
994 |
* notice the missing test for self being a nonNilObject - |
|
995 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
996 |
* and SmallInteger |
|
997 |
*/ |
|
998 |
if (__isSmallInteger(index)) { |
|
1133 | 999 |
indx = __intVal(index) - 1; |
701 | 1000 |
if ((indx >= 0) && (indx < sizeof(double))) { |
1133 | 1001 |
cp = (unsigned char *)(& (__FloatInstPtr(self)->f_floatvalue)); |
1002 |
RETURN ( __MKSMALLINT(cp[indx] & 0xFF) ); |
|
701 | 1003 |
} |
1004 |
} |
|
1005 |
%}. |
|
1006 |
index isInteger ifFalse:[ |
|
1007 |
^ self indexNotInteger |
|
1008 |
]. |
|
1009 |
^ self subscriptBoundsError:index |
|
1010 |
! |
|
1011 |
||
1012 |
basicAt:index put:value |
|
1013 |
"set an internal byte of the float. |
|
1014 |
The value to be stored here depends on byte order, float representation etc. |
|
1015 |
Therefore, this method should be used strictly private. |
|
1016 |
||
1017 |
Notice: |
|
1018 |
the need to redefine this method here is due to the |
|
1019 |
inability of many machines to store floats in non-double aligned memory. |
|
1020 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
|
1021 |
and the actual float is stored at index 5 .. 12. |
|
1022 |
To hide this at one place, this method knows about that, and returns |
|
1023 |
values as if this filler wasnt present." |
|
1024 |
||
1025 |
%{ /* NOCONTEXT */ |
|
1026 |
register int indx, val; |
|
1027 |
unsigned char *cp; |
|
1028 |
||
1029 |
/* |
|
1030 |
* notice the missing test for self being a nonNilObject - |
|
1031 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
1032 |
* and SmallInteger |
|
1033 |
*/ |
|
1034 |
if (__bothSmallInteger(index, value)) { |
|
1133 | 1035 |
val = __intVal(value); |
701 | 1036 |
if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) { |
1133 | 1037 |
indx = __intVal(index) - 1; |
701 | 1038 |
if ((indx >= 0) && (indx < sizeof(double))) { |
1133 | 1039 |
cp = (unsigned char *)(& (__FloatInstPtr(self)->f_floatvalue)); |
701 | 1040 |
cp[indx] = val; |
1041 |
RETURN ( value ); |
|
1042 |
} |
|
1043 |
} |
|
1044 |
} |
|
1045 |
%}. |
|
1046 |
index isInteger ifFalse:[ |
|
1047 |
^ self indexNotInteger |
|
1048 |
]. |
|
1049 |
value isInteger ifFalse:[ |
|
1050 |
" |
|
1051 |
the object to store should be an integer number |
|
1052 |
" |
|
1053 |
^ self elementNotInteger |
|
1054 |
]. |
|
1055 |
(value between:0 and:255) ifFalse:[ |
|
1056 |
" |
|
1057 |
the object to store must be a bytes value |
|
1058 |
" |
|
1059 |
^ self elementBoundsError |
|
1060 |
]. |
|
1061 |
^ self subscriptBoundsError:index |
|
1062 |
! ! |
|
1063 |
||
1064 |
!Float methodsFor:'testing'! |
|
1065 |
||
2387 | 1066 |
isNaN |
1067 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
1068 |
These are not created by ST/X float operations (they raise an exception); |
|
1069 |
however, inline C-code could produce them ..." |
|
1070 |
||
1071 |
%{ /* NOCONTEXT */ |
|
1072 |
||
2395 | 1073 |
/* |
1074 |
* sigh - every vendor is playing its own game here ... |
|
1075 |
* Q: what are standards worth, anyway ? |
|
1076 |
*/ |
|
2392 | 1077 |
#ifdef IS_NAN |
1078 |
if (IS_NAN(__floatVal(self))) { RETURN (true); } |
|
2393 | 1079 |
#endif |
1080 |
#ifdef IS_QNAN |
|
2392 | 1081 |
if (IS_QNAN(__floatVal(self))) { RETURN (true); } |
2393 | 1082 |
#endif |
2392 | 1083 |
|
2393 | 1084 |
#ifdef FLT_SNAN |
1085 |
if (__floatVal(self) == FLT_SNAN) { RETURN (true); } |
|
1086 |
#endif |
|
1087 |
#ifdef FLT_QNAN |
|
1088 |
if (__floatVal(self) == FLT_QNAN) { RETURN (true); } |
|
1089 |
#endif |
|
1090 |
||
1091 |
#ifdef _SNANF |
|
2390 | 1092 |
if (__floatVal(self) == _SNAN) { RETURN (true); } |
2393 | 1093 |
#endif |
1094 |
#ifdef _QNANF |
|
2390 | 1095 |
if (__floatVal(self) == _QNAN) { RETURN (true); } |
2393 | 1096 |
#endif |
2392 | 1097 |
|
2395 | 1098 |
#ifdef IsPosNAN |
1099 |
if IsPosNAN(__floatVal(self)) { RETURN (true); } |
|
1100 |
#endif |
|
1101 |
#ifdef IsNegNAN |
|
1102 |
if IsNegNAN(__floatVal(self)) { RETURN (true); } |
|
1103 |
#endif |
|
1104 |
||
2393 | 1105 |
#ifdef NAN |
2392 | 1106 |
if (__floatVal(self) == NAN) { RETURN (true); } |
2390 | 1107 |
#endif |
2399 | 1108 |
#ifdef NaN |
2401 | 1109 |
if (NaN(__shortFloatVal(self))) { RETURN (true); } |
2399 | 1110 |
#endif |
2390 | 1111 |
|
2392 | 1112 |
%}. |
1113 |
^ false |
|
2387 | 1114 |
|
2390 | 1115 |
|
2387 | 1116 |
! |
1117 |
||
701 | 1118 |
negative |
1119 |
"return true if the receiver is less than zero" |
|
1120 |
||
1121 |
%{ /* NOCONTEXT */ |
|
1122 |
||
1133 | 1123 |
RETURN ( (__floatVal(self) < 0.0) ? true : false ); |
701 | 1124 |
%} |
1125 |
! |
|
1126 |
||
1127 |
positive |
|
1128 |
"return true if the receiver is greater or equal to zero" |
|
1129 |
||
1130 |
%{ /* NOCONTEXT */ |
|
1131 |
||
1133 | 1132 |
RETURN ( (__floatVal(self) >= 0.0) ? true : false ); |
701 | 1133 |
%} |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1134 |
! |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1135 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1136 |
strictlyPositive |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1137 |
"return true if the receiver is greater than zero" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1138 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1139 |
%{ /* NOCONTEXT */ |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1140 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1141 |
RETURN ( (__floatVal(self) > 0.0) ? true : false ); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1142 |
%} |
701 | 1143 |
! ! |
1144 |
||
1145 |
!Float methodsFor:'truncation and rounding'! |
|
1146 |
||
1147 |
ceiling |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1148 |
"return the smallest integer which is greater or equal to the receiver." |
701 | 1149 |
|
1150 |
|val| |
|
1151 |
||
1152 |
%{ |
|
1153 |
double ceil(); |
|
1154 |
double dVal; |
|
1155 |
||
1156 |
/* |
|
1157 |
* ST-80 (and X3J20) returns integer. |
|
1158 |
*/ |
|
1133 | 1159 |
dVal = ceil(__floatVal(self)); |
701 | 1160 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1161 |
RETURN ( __MKSMALLINT( (INT) dVal ) ); |
701 | 1162 |
} |
1688
8a42db1eea60
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
1163 |
val = __MKFLOAT(dVal); |
701 | 1164 |
%}. |
1165 |
^ val asInteger |
|
1166 |
! |
|
1167 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1168 |
ceilingAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1169 |
"return the smallest integer-valued float greater or equal to the receiver. |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1170 |
This is much like #ceiling, but avoids a (possibly expensive) conversion |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1171 |
of the result to an integer. |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1172 |
It may be useful, if the result is to be further used in another |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1173 |
float-operation." |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1174 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1175 |
%{ /* NOCONTEXT */ |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1176 |
double ceil(); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1177 |
double dVal; |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1178 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1179 |
/* |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1180 |
* ST-80 (and X3J20) returns integer. |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1181 |
*/ |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1182 |
dVal = ceil(__floatVal(self)); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1183 |
RETURN ( __MKFLOAT(dVal) ); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1184 |
%} |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1185 |
! |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1186 |
|
701 | 1187 |
floor |
1188 |
"return the integer nearest the receiver towards negative infinity." |
|
1189 |
||
1190 |
|val| |
|
1191 |
||
1192 |
%{ |
|
1193 |
double floor(); |
|
1194 |
double dVal; |
|
1195 |
||
1196 |
/* |
|
1197 |
* ST-80 (and X3J20) returns integer. |
|
1198 |
*/ |
|
1133 | 1199 |
dVal = floor(__floatVal(self)); |
701 | 1200 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1201 |
RETURN ( __MKSMALLINT( (INT) dVal ) ); |
701 | 1202 |
} |
1688
8a42db1eea60
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
1203 |
val = __MKFLOAT(dVal); |
701 | 1204 |
%}. |
1205 |
^ val asInteger |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1206 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1207 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1208 |
0.5 floor |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1209 |
0.5 floorAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1210 |
-0.5 floor |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1211 |
-0.5 floorAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1212 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1213 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1214 |
! |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1215 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1216 |
floorAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1217 |
"return the integer nearest the receiver towards negative infinity |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1218 |
as a float. |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1219 |
This is much like #floor, but avoids a (possibly expensive) conversion |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1220 |
of the result to an integer. |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1221 |
It may be useful, if the result is to be further used in another |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1222 |
float-operation." |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1223 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1224 |
%{ /* NOCONTEXT */ |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1225 |
double floor(); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1226 |
double dVal; |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1227 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1228 |
dVal = floor(__floatVal(self)); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1229 |
RETURN ( __MKFLOAT(dVal) ); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1230 |
%} |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1231 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1232 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1233 |
0.5 floor |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1234 |
0.5 floorAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1235 |
-0.5 floor |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1236 |
-0.5 floorAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1237 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1238 |
|
701 | 1239 |
! |
1240 |
||
1241 |
rounded |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1242 |
"return the receiver rounded to the nearest integer" |
701 | 1243 |
|
1244 |
|val| |
|
1245 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1246 |
%{ |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1247 |
double floor(), ceil(); |
701 | 1248 |
double dVal; |
1249 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1250 |
dVal = __floatVal(self); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1251 |
if (dVal < 0.0) { |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1252 |
dVal = ceil(dVal - 0.5); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1253 |
} else { |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1254 |
dVal = floor(dVal + 0.5); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1255 |
} |
701 | 1256 |
/* |
1257 |
* ST-80 (and X3J20) returns integer. |
|
1258 |
*/ |
|
1259 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1260 |
RETURN ( __MKSMALLINT( (INT) dVal ) ); |
701 | 1261 |
} |
1688
8a42db1eea60
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
1262 |
val = __MKFLOAT(dVal); |
701 | 1263 |
%}. |
1264 |
^ val asInteger |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1265 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1266 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1267 |
0.4 rounded |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1268 |
0.5 rounded |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1269 |
0.6 rounded |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1270 |
-0.4 rounded |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1271 |
-0.5 rounded |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1272 |
-0.6 rounded |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1273 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1274 |
! |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1275 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1276 |
roundedAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1277 |
"return the receiver rounded to the nearest integer as a float. |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1278 |
This is much like #rounded, but avoids a (possibly expensive) conversion |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1279 |
of the result to an integer. |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1280 |
It may be useful, if the result is to be further used in another |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1281 |
float-operation." |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1282 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1283 |
|val| |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1284 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1285 |
%{ /* NOCONTEXT */ |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1286 |
double floor(), ceil(); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1287 |
double dVal; |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1288 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1289 |
dVal = __floatVal(self); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1290 |
if (dVal < 0.0) { |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1291 |
dVal = ceil(dVal - 0.5); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1292 |
} else { |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1293 |
dVal = floor(dVal + 0.5); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1294 |
} |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1295 |
RETURN ( __MKFLOAT(dVal) ); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1296 |
%} |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1297 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1298 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1299 |
0.5 rounded |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1300 |
-0.5 rounded |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1301 |
0.5 roundedAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1302 |
-0.5 roundedAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1303 |
" |
701 | 1304 |
! |
1305 |
||
1306 |
truncated |
|
1307 |
"return the receiver truncated towards zero as an integer" |
|
1308 |
||
1309 |
|val| |
|
1310 |
||
1311 |
%{ |
|
1312 |
double floor(), ceil(); |
|
1313 |
double dVal; |
|
1314 |
||
1133 | 1315 |
dVal = __floatVal(self); |
701 | 1316 |
if (dVal < 0.0) { |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1317 |
dVal = ceil(dVal); |
701 | 1318 |
} else { |
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1319 |
dVal = floor(dVal); |
701 | 1320 |
} |
1321 |
||
1322 |
/* |
|
1323 |
* ST-80 (and X3J20) returns integer. |
|
1324 |
*/ |
|
1325 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1326 |
RETURN ( __MKSMALLINT( (INT) dVal ) ); |
701 | 1327 |
} |
1688
8a42db1eea60
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
1328 |
val = __MKFLOAT(dVal); |
701 | 1329 |
%}. |
1330 |
^ val asInteger |
|
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1331 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1332 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1333 |
0.5 truncated |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1334 |
-0.5 truncated |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1335 |
0.5 truncatedAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1336 |
-0.5 truncatedAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1337 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1338 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1339 |
! |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1340 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1341 |
truncatedAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1342 |
"return the receiver truncated towards zero as a float. |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1343 |
This is much like #truncated, but avoids a (possibly expensive) conversion |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1344 |
of the result to an integer. |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1345 |
It may be useful, if the result is to be further used in another |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1346 |
float-operation." |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1347 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1348 |
%{ /* NOCONTEXT */ |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1349 |
double floor(), ceil(); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1350 |
double dVal; |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1351 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1352 |
dVal = __floatVal(self); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1353 |
if (dVal < 0.0) { |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1354 |
dVal = ceil(dVal); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1355 |
} else { |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1356 |
dVal = floor(dVal); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1357 |
} |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1358 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1359 |
RETURN ( __MKFLOAT(dVal) ); |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1360 |
%} |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1361 |
|
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1362 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1363 |
0.5 truncated |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1364 |
-0.5 truncated |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1365 |
0.5 truncatedAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1366 |
-0.5 truncatedAsFloat |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1367 |
" |
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1368 |
|
701 | 1369 |
! ! |
1370 |
||
1879
26df273349c4
added ceilingAsFloat, floorAsFloat, truncatedAsFloat
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1371 |
!Float class methodsFor:'documentation'! |
701 | 1372 |
|
1373 |
version |
|
3115
1ecfb9118163
dont check twice for nil (in compare ops)
Claus Gittinger <cg@exept.de>
parents:
3014
diff
changeset
|
1374 |
^ '$Header: /cvs/stx/stx/libbasic/Float.st,v 1.63 1997-11-20 18:22:49 cg Exp $' |
701 | 1375 |
! ! |
2387 | 1376 |
Float initialize! |