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
|
1
|
14 |
instanceVariableNames:''
|
85
|
15 |
classVariableNames:'LastErrorNumber'
|
1
|
16 |
poolDictionaries:''
|
|
17 |
category:'Magnitude-Numbers'
|
|
18 |
!
|
|
19 |
|
|
20 |
Float comment:'
|
5
|
21 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
175
|
22 |
All Rights Reserved
|
92
|
23 |
|
250
|
24 |
$Header: /cvs/stx/stx/libbasic/Float.st,v 1.21 1995-02-15 10:24:23 claus Exp $
|
68
|
25 |
'!
|
|
26 |
|
|
27 |
!Float class methodsFor:'documentation'!
|
|
28 |
|
88
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
175
|
32 |
All Rights Reserved
|
88
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
250
|
45 |
$Header: /cvs/stx/stx/libbasic/Float.st,v 1.21 1995-02-15 10:24:23 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
68
|
49 |
documentation
|
|
50 |
"
|
|
51 |
Floats represent rational numbers with limited precision. In ST/X, Float uses
|
|
52 |
the underlying C-compilers double implementation, therefore instances of Float
|
|
53 |
are usually represented by the 8-byte IEE double precision float format.
|
|
54 |
(but there is no guaranty).
|
1
|
55 |
|
68
|
56 |
Notice, that Floats are defined as Byte-array to prevent the garbage collector
|
|
57 |
from going into the value ... otherwise I needed a special case in many places.
|
|
58 |
|
|
59 |
Also notice, that ST/X Floats are what Doubles are in ST-80 - this may change
|
|
60 |
in one of the next versions (at least on machines, which provide different float
|
|
61 |
and double types in their C-compiler.
|
|
62 |
|
|
63 |
WARNING:
|
|
64 |
The layout of float instances is known by the runtime system and the compiler;
|
|
65 |
you may not add instance variables here.
|
|
66 |
Also, subclassing is complicated by the fact, that the VM creates floats, and does
|
|
67 |
its float-checks by an identity compare with the Float-class. (i.e. your subclasses
|
|
68 |
instances will not be recognized as float-like objects, thus mixed mode arithmetic
|
|
69 |
will always coerce them, effectively slowing things down.
|
|
70 |
This may be changed, to use a flag bit in the class.
|
85
|
71 |
|
|
72 |
Class Variables:
|
|
73 |
|
175
|
74 |
LastErrorNumber <Integer> value of errno (after a trig- or other math err)
|
68
|
75 |
"
|
|
76 |
! !
|
1
|
77 |
|
224
|
78 |
!Float primitiveDefinitions!
|
|
79 |
|
|
80 |
%{
|
|
81 |
#include <errno.h>
|
|
82 |
|
|
83 |
/*
|
|
84 |
* on some systems errno is a macro ... check for it here
|
|
85 |
*/
|
|
86 |
#ifndef errno
|
|
87 |
extern errno;
|
|
88 |
#endif
|
|
89 |
|
|
90 |
%}
|
|
91 |
! !
|
|
92 |
|
1
|
93 |
!Float class methodsFor:'instance creation'!
|
|
94 |
|
|
95 |
basicNew
|
|
96 |
"return a new float - here we return 0.0
|
|
97 |
- floats are usually NOT created this way ...
|
|
98 |
Its implemented here to allow things like binary store & load
|
|
99 |
of floats. (but even this support will go away eventually, its not
|
|
100 |
a good idea to store the bits of a float - the reader might have a
|
13
|
101 |
totally different representation - so floats will eventually be
|
|
102 |
binary stored in a device independent format."
|
1
|
103 |
|
|
104 |
%{ /* NOCONTEXT */
|
|
105 |
RETURN ( _MKFLOAT((0.0) COMMA_SND) );
|
|
106 |
%}
|
|
107 |
! !
|
|
108 |
|
|
109 |
!Float class methodsFor:'constants'!
|
|
110 |
|
|
111 |
zero
|
|
112 |
"return the neutral element for addition"
|
|
113 |
|
|
114 |
^ 0.0
|
|
115 |
!
|
|
116 |
|
|
117 |
unity
|
|
118 |
"return the neutral element for multiplication"
|
|
119 |
|
|
120 |
^ 1.0
|
|
121 |
!
|
|
122 |
|
|
123 |
pi
|
|
124 |
"return the constant pi"
|
|
125 |
|
|
126 |
^ 3.1415926535897932384626434
|
|
127 |
! !
|
|
128 |
|
3
|
129 |
!Float class methodsFor:'queries'!
|
|
130 |
|
|
131 |
isBuiltInClass
|
|
132 |
"this class is known by the run-time-system"
|
|
133 |
|
|
134 |
^ self == Float
|
85
|
135 |
!
|
|
136 |
|
|
137 |
lastErrorNumber
|
|
138 |
"return the value of errno after an error"
|
|
139 |
|
|
140 |
^ LastErrorNumber
|
|
141 |
|
|
142 |
"
|
|
143 |
2 arcSin.
|
|
144 |
OperatingSystem errorTextForNumber:(Float lastErrorNumber)
|
|
145 |
"
|
3
|
146 |
! !
|
|
147 |
|
1
|
148 |
!Float methodsFor:'arithmetic'!
|
|
149 |
|
|
150 |
+ aNumber
|
|
151 |
"return the sum of the receiver and the argument, aNumber"
|
|
152 |
|
|
153 |
%{ /* NOCONTEXT */
|
13
|
154 |
|
1
|
155 |
OBJ newFloat;
|
|
156 |
double result;
|
|
157 |
|
250
|
158 |
if (__isSmallInteger(aNumber)) {
|
175
|
159 |
result = _floatVal(self) + (double)(_intVal(aNumber));
|
1
|
160 |
retResult:
|
175
|
161 |
_qMKFLOAT(newFloat, result, SENDER);
|
|
162 |
RETURN ( newFloat );
|
1
|
163 |
}
|
|
164 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
175
|
165 |
result = _floatVal(self) + _floatVal(aNumber);
|
|
166 |
goto retResult;
|
1
|
167 |
}
|
|
168 |
%}
|
|
169 |
.
|
|
170 |
^ aNumber sumFromFloat:self
|
|
171 |
!
|
|
172 |
|
|
173 |
- aNumber
|
|
174 |
"return the difference of the receiver and the argument, aNumber"
|
|
175 |
|
|
176 |
%{ /* NOCONTEXT */
|
13
|
177 |
|
1
|
178 |
OBJ newFloat;
|
|
179 |
double result;
|
|
180 |
|
250
|
181 |
if (__isSmallInteger(aNumber)) {
|
175
|
182 |
result = _floatVal(self) - (double)(_intVal(aNumber));
|
1
|
183 |
retResult:
|
175
|
184 |
_qMKFLOAT(newFloat, result, SENDER);
|
|
185 |
RETURN ( newFloat );
|
1
|
186 |
}
|
|
187 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
175
|
188 |
result = _floatVal(self) - _floatVal(aNumber);
|
|
189 |
goto retResult;
|
1
|
190 |
}
|
|
191 |
%}
|
|
192 |
.
|
|
193 |
^ aNumber differenceFromFloat:self
|
|
194 |
!
|
|
195 |
|
|
196 |
* aNumber
|
|
197 |
"return the product of the receiver and the argument, aNumber"
|
|
198 |
|
|
199 |
%{ /* NOCONTEXT */
|
|
200 |
|
|
201 |
OBJ newFloat;
|
|
202 |
double result;
|
|
203 |
|
250
|
204 |
if (__isSmallInteger(aNumber)) {
|
175
|
205 |
result = _floatVal(self) * (double)(_intVal(aNumber));
|
1
|
206 |
retResult:
|
175
|
207 |
_qMKFLOAT(newFloat, result, SENDER);
|
|
208 |
RETURN ( newFloat );
|
1
|
209 |
}
|
|
210 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
175
|
211 |
result = _floatVal(self) * _floatVal(aNumber);
|
|
212 |
goto retResult;
|
1
|
213 |
}
|
|
214 |
%}
|
|
215 |
.
|
|
216 |
^ aNumber productFromFloat:self
|
|
217 |
!
|
|
218 |
|
|
219 |
/ aNumber
|
|
220 |
"return the quotient of the receiver and the argument, aNumber"
|
|
221 |
|
|
222 |
%{ /* NOCONTEXT */
|
|
223 |
|
|
224 |
OBJ newFloat;
|
|
225 |
double result, val;
|
|
226 |
|
250
|
227 |
if (__isSmallInteger(aNumber)) {
|
175
|
228 |
if (aNumber != _MKSMALLINT(0)) {
|
|
229 |
result = _floatVal(self) / ( (double)_intVal(aNumber)) ;
|
1
|
230 |
retResult:
|
175
|
231 |
_qMKFLOAT(newFloat, result, SENDER);
|
|
232 |
RETURN ( newFloat );
|
|
233 |
}
|
1
|
234 |
} else {
|
175
|
235 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
|
236 |
val = _floatVal(aNumber);
|
|
237 |
if (val != 0.0) {
|
|
238 |
result = _floatVal(self) / val;
|
|
239 |
goto retResult;
|
|
240 |
}
|
|
241 |
}
|
1
|
242 |
}
|
|
243 |
%}
|
|
244 |
.
|
|
245 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[
|
142
|
246 |
"
|
|
247 |
No, you shalt not divide by zero
|
|
248 |
"
|
175
|
249 |
^ DivisionByZeroSignal raise.
|
1
|
250 |
].
|
|
251 |
^ aNumber quotientFromFloat:self
|
|
252 |
!
|
|
253 |
|
|
254 |
negated
|
|
255 |
"return myself negated"
|
|
256 |
|
|
257 |
%{ /* NOCONTEXT */
|
13
|
258 |
|
3
|
259 |
OBJ newFloat;
|
|
260 |
|
|
261 |
_qMKFLOAT(newFloat, - _floatVal(self), SENDER);
|
|
262 |
RETURN ( newFloat );
|
1
|
263 |
%}
|
|
264 |
! !
|
|
265 |
|
|
266 |
!Float methodsFor:'testing'!
|
|
267 |
|
|
268 |
positive
|
|
269 |
"return true if the receiver is greater or equal to zero"
|
|
270 |
|
|
271 |
%{ /* NOCONTEXT */
|
13
|
272 |
|
1
|
273 |
RETURN ( (_floatVal(self) >= 0.0) ? true : false );
|
|
274 |
%}
|
|
275 |
!
|
|
276 |
|
|
277 |
negative
|
|
278 |
"return true if the receiver is less than zero"
|
|
279 |
|
|
280 |
%{ /* NOCONTEXT */
|
13
|
281 |
|
1
|
282 |
RETURN ( (_floatVal(self) < 0.0) ? true : false );
|
|
283 |
%}
|
|
284 |
! !
|
|
285 |
|
|
286 |
!Float methodsFor:'comparing'!
|
|
287 |
|
|
288 |
< aNumber
|
|
289 |
"return true, if the argument is greater"
|
|
290 |
|
|
291 |
%{ /* NOCONTEXT */
|
13
|
292 |
|
250
|
293 |
if (__isSmallInteger(aNumber)) {
|
175
|
294 |
RETURN ( (_floatVal(self) < (double)(_intVal(aNumber))) ? true : false );
|
1
|
295 |
}
|
|
296 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
175
|
297 |
RETURN ( (_floatVal(self) < _floatVal(aNumber)) ? true : false );
|
1
|
298 |
}
|
|
299 |
%}
|
|
300 |
.
|
|
301 |
^ aNumber lessFromFloat:self
|
|
302 |
!
|
|
303 |
|
|
304 |
> aNumber
|
|
305 |
"return true, if the argument is less"
|
|
306 |
|
|
307 |
%{ /* NOCONTEXT */
|
13
|
308 |
|
250
|
309 |
if (__isSmallInteger(aNumber)) {
|
175
|
310 |
RETURN ( (_floatVal(self) > (double)(_intVal(aNumber))) ? true : false );
|
1
|
311 |
}
|
|
312 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
175
|
313 |
RETURN ( (_floatVal(self) > _floatVal(aNumber)) ? true : false );
|
1
|
314 |
}
|
|
315 |
%}
|
|
316 |
.
|
|
317 |
^ self retry:#> coercing:aNumber
|
|
318 |
!
|
|
319 |
|
|
320 |
<= aNumber
|
|
321 |
"return true, if the argument is greater or equal"
|
|
322 |
|
|
323 |
%{ /* NOCONTEXT */
|
13
|
324 |
|
250
|
325 |
if (__isSmallInteger(aNumber)) {
|
175
|
326 |
RETURN ( (_floatVal(self) <= (double)(_intVal(aNumber))) ? true : false );
|
1
|
327 |
}
|
|
328 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
175
|
329 |
RETURN ( (_floatVal(self) <= _floatVal(aNumber)) ? true : false );
|
1
|
330 |
}
|
|
331 |
%}
|
|
332 |
.
|
|
333 |
^ self retry:#<= coercing:aNumber
|
|
334 |
!
|
|
335 |
|
|
336 |
>= aNumber
|
|
337 |
"return true, if the argument is less or equal"
|
|
338 |
|
|
339 |
%{ /* NOCONTEXT */
|
13
|
340 |
|
250
|
341 |
if (__isSmallInteger(aNumber)) {
|
175
|
342 |
RETURN ( (_floatVal(self) >= (double)(_intVal(aNumber))) ? true : false );
|
1
|
343 |
}
|
|
344 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
175
|
345 |
RETURN ( (_floatVal(self) >= _floatVal(aNumber)) ? true : false );
|
1
|
346 |
}
|
|
347 |
%}
|
|
348 |
.
|
|
349 |
^ self retry:#>= coercing:aNumber
|
|
350 |
!
|
|
351 |
|
|
352 |
= aNumber
|
175
|
353 |
"return true, if the arguments value are equal by value"
|
1
|
354 |
|
|
355 |
%{ /* NOCONTEXT */
|
13
|
356 |
|
250
|
357 |
if (__isSmallInteger(aNumber)) {
|
175
|
358 |
RETURN ( (_floatVal(self) == (double)(_intVal(aNumber))) ? true : false );
|
1
|
359 |
}
|
|
360 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
175
|
361 |
RETURN ( (_floatVal(self) == _floatVal(aNumber)) ? true : false );
|
1
|
362 |
}
|
|
363 |
%}
|
|
364 |
.
|
|
365 |
^ self retry:#= coercing:aNumber
|
|
366 |
!
|
|
367 |
|
|
368 |
~= aNumber
|
|
369 |
"return true, if the arguments value are not equal"
|
|
370 |
|
|
371 |
%{ /* NOCONTEXT */
|
13
|
372 |
|
250
|
373 |
if (__isSmallInteger(aNumber)) {
|
175
|
374 |
RETURN ( (_floatVal(self) != (double)(_intVal(aNumber))) ? true : false );
|
1
|
375 |
}
|
|
376 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
175
|
377 |
RETURN ( (_floatVal(self) != _floatVal(aNumber)) ? true : false );
|
1
|
378 |
}
|
|
379 |
%}
|
|
380 |
.
|
|
381 |
^ self retry:#~= coercing:aNumber
|
92
|
382 |
!
|
|
383 |
|
|
384 |
hash
|
|
385 |
"return a number for hashing; redefined, since floats compare
|
|
386 |
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same
|
|
387 |
as 3 hash."
|
|
388 |
|
|
389 |
|i idx|
|
|
390 |
|
|
391 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[
|
175
|
392 |
i := self asInteger.
|
|
393 |
self = i ifTrue:[
|
|
394 |
^ i hash
|
|
395 |
].
|
92
|
396 |
].
|
|
397 |
|
|
398 |
"
|
|
399 |
mhmh take my value-bits to hash on
|
|
400 |
"
|
|
401 |
idx := self basicSize.
|
|
402 |
^ (((self basicAt:idx) bitAnd:16r3F) bitShift:24) +
|
|
403 |
((self basicAt:idx-1) bitShift:16) +
|
|
404 |
((self basicAt:idx-2) bitShift:8) +
|
|
405 |
(self basicAt:idx-3)
|
|
406 |
|
|
407 |
"
|
|
408 |
3 hash
|
|
409 |
3.0 hash
|
|
410 |
3.1 hash
|
|
411 |
3.14159 hash
|
|
412 |
31.4159 hash
|
|
413 |
3.141591 hash
|
|
414 |
1.234567890123456 hash
|
|
415 |
1.234567890123457 hash
|
|
416 |
Set withAll:#(3 3.0 99 99.0 3.1415)
|
|
417 |
"
|
1
|
418 |
! !
|
|
419 |
|
|
420 |
!Float methodsFor:'coercion and converting'!
|
|
421 |
|
|
422 |
coerce:aNumber
|
|
423 |
"return aNumber converted into receivers type"
|
|
424 |
|
|
425 |
^ aNumber asFloat
|
|
426 |
!
|
|
427 |
|
|
428 |
generality
|
|
429 |
"return the generality value - see ArithmeticValue>>retry:coercing:"
|
41
|
430 |
|
1
|
431 |
^ 80
|
|
432 |
!
|
|
433 |
|
|
434 |
asFloat
|
|
435 |
"return a float with same value - thats me"
|
|
436 |
|
|
437 |
^ self
|
|
438 |
!
|
|
439 |
|
|
440 |
asInteger
|
|
441 |
"return an integer with same value - might truncate"
|
|
442 |
|
|
443 |
|l v sign|
|
|
444 |
|
|
445 |
%{ /* NOCONTEXT */
|
|
446 |
|
|
447 |
if ((_floatVal(self) >= (double)_MIN_INT)
|
|
448 |
&& (_floatVal(self) <= (double)_MAX_INT)) {
|
175
|
449 |
RETURN ( _MKSMALLINT( (INT)_floatVal(self)) );
|
1
|
450 |
}
|
|
451 |
%}
|
|
452 |
.
|
54
|
453 |
^ super asInteger
|
1
|
454 |
|
|
455 |
"12345.0 asInteger"
|
|
456 |
"1e15 asInteger"
|
|
457 |
! !
|
|
458 |
|
|
459 |
!Float methodsFor:'truncation and rounding'!
|
|
460 |
|
|
461 |
truncated
|
68
|
462 |
"return the receiver truncated towards zero as an integer"
|
1
|
463 |
|
|
464 |
|val|
|
|
465 |
|
|
466 |
%{
|
|
467 |
double floor(), ceil();
|
|
468 |
double dVal;
|
|
469 |
|
|
470 |
dVal = _floatVal(self);
|
|
471 |
if (dVal < 0.0) {
|
175
|
472 |
dVal = ceil(dVal);
|
1
|
473 |
} else {
|
175
|
474 |
dVal = floor(dVal);
|
1
|
475 |
}
|
|
476 |
|
|
477 |
/*
|
68
|
478 |
* ST-80 (and X3J20) returns integer.
|
1
|
479 |
*/
|
|
480 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) {
|
175
|
481 |
RETURN ( _MKSMALLINT( (INT) dVal ) );
|
1
|
482 |
}
|
|
483 |
val = _MKFLOAT(dVal COMMA_CON);
|
68
|
484 |
%}.
|
1
|
485 |
^ val asInteger
|
|
486 |
!
|
|
487 |
|
|
488 |
rounded
|
68
|
489 |
"return the receiver rounded to the nearest integer as an integer"
|
1
|
490 |
|
|
491 |
|val|
|
|
492 |
|
|
493 |
%{
|
|
494 |
double floor();
|
|
495 |
double dVal;
|
|
496 |
|
|
497 |
/*
|
68
|
498 |
* ST-80 (and X3J20) returns integer.
|
1
|
499 |
*/
|
|
500 |
dVal = floor(_floatVal(self) + 0.5);
|
|
501 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) {
|
175
|
502 |
RETURN ( _MKSMALLINT( (INT) dVal ) );
|
1
|
503 |
}
|
|
504 |
val = _MKFLOAT(dVal COMMA_CON);
|
68
|
505 |
%}.
|
1
|
506 |
^ val asInteger
|
|
507 |
!
|
|
508 |
|
|
509 |
floor
|
68
|
510 |
"return the integer nearest the receiver towards negative infinity."
|
|
511 |
|
|
512 |
|val|
|
1
|
513 |
|
68
|
514 |
%{
|
|
515 |
double floor();
|
|
516 |
double dVal;
|
1
|
517 |
|
68
|
518 |
/*
|
|
519 |
* ST-80 (and X3J20) returns integer.
|
|
520 |
*/
|
|
521 |
dVal = floor(_floatVal(self));
|
|
522 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) {
|
175
|
523 |
RETURN ( _MKSMALLINT( (INT) dVal ) );
|
68
|
524 |
}
|
|
525 |
val = _MKFLOAT(dVal COMMA_CON);
|
|
526 |
%}.
|
|
527 |
^ val asInteger
|
1
|
528 |
!
|
|
529 |
|
|
530 |
ceiling
|
|
531 |
"return the smallest integer-valued float greater or equal to the receiver"
|
|
532 |
|
68
|
533 |
|val|
|
1
|
534 |
|
68
|
535 |
%{
|
1
|
536 |
double ceil();
|
68
|
537 |
double dVal;
|
1
|
538 |
|
68
|
539 |
/*
|
|
540 |
* ST-80 (and X3J20) returns integer.
|
|
541 |
*/
|
|
542 |
dVal = ceil(_floatVal(self));
|
|
543 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) {
|
175
|
544 |
RETURN ( _MKSMALLINT( (INT) dVal ) );
|
68
|
545 |
}
|
|
546 |
val = _MKFLOAT(dVal COMMA_CON);
|
|
547 |
%}.
|
|
548 |
^ val asInteger
|
1
|
549 |
! !
|
|
550 |
|
|
551 |
!Float methodsFor:'mathematical functions'!
|
|
552 |
|
|
553 |
ln
|
|
554 |
"return the natural logarithm of myself"
|
|
555 |
|
|
556 |
%{ /* NOCONTEXT */
|
|
557 |
|
|
558 |
double log();
|
|
559 |
double result;
|
|
560 |
|
|
561 |
errno = 0;
|
|
562 |
result = log(_floatVal(self));
|
|
563 |
if (errno == 0)
|
175
|
564 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
565 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
566 |
%}
|
|
567 |
.
|
142
|
568 |
"
|
|
569 |
an invalid value for logarithm
|
|
570 |
"
|
131
|
571 |
^ DomainErrorSignal raise
|
1
|
572 |
!
|
|
573 |
|
|
574 |
raisedTo:aNumber
|
|
575 |
"return self raised to the power of aNumber"
|
|
576 |
|n|
|
|
577 |
|
|
578 |
n := aNumber asFloat.
|
|
579 |
%{
|
|
580 |
double pow();
|
|
581 |
double result;
|
|
582 |
|
54
|
583 |
if (__isFloat(n)) {
|
175
|
584 |
errno = 0;
|
|
585 |
result = pow(_floatVal(self), _floatVal(n));
|
|
586 |
errno = 0; /* XXXX */
|
|
587 |
if (errno == 0)
|
|
588 |
RETURN ( _MKFLOAT(result COMMA_CON) );
|
|
589 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
590 |
}
|
|
591 |
%}
|
|
592 |
.
|
142
|
593 |
"
|
|
594 |
an invalid argument (not convertable to float ?)
|
|
595 |
"
|
131
|
596 |
^ DomainErrorSignal raise
|
1
|
597 |
!
|
|
598 |
|
|
599 |
exp
|
|
600 |
"return e raised to the power of the receiver"
|
|
601 |
|
|
602 |
%{ /* NOCONTEXT */
|
|
603 |
|
|
604 |
double exp();
|
|
605 |
double result;
|
|
606 |
|
|
607 |
errno = 0;
|
|
608 |
result = exp(_floatVal(self));
|
|
609 |
if (errno == 0)
|
175
|
610 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
611 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
612 |
%}
|
|
613 |
.
|
131
|
614 |
^ DomainErrorSignal raise
|
1
|
615 |
!
|
|
616 |
|
|
617 |
sin
|
|
618 |
"return the sine of myself interpreted as radians"
|
|
619 |
|
|
620 |
%{ /* NOCONTEXT */
|
|
621 |
|
|
622 |
double sin();
|
|
623 |
double result;
|
|
624 |
|
|
625 |
errno = 0;
|
|
626 |
result = sin(_floatVal(self));
|
|
627 |
if (errno == 0)
|
175
|
628 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
629 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
630 |
%}
|
|
631 |
.
|
131
|
632 |
^ DomainErrorSignal raise
|
1
|
633 |
!
|
|
634 |
|
|
635 |
cos
|
|
636 |
"return the cosine of myself interpreted as radians"
|
|
637 |
|
|
638 |
%{ /* NOCONTEXT */
|
|
639 |
|
|
640 |
double cos();
|
|
641 |
double result;
|
|
642 |
|
|
643 |
errno = 0;
|
|
644 |
result = cos(_floatVal(self));
|
|
645 |
if (errno == 0)
|
175
|
646 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
647 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
648 |
%}
|
|
649 |
.
|
131
|
650 |
^ DomainErrorSignal raise
|
1
|
651 |
!
|
|
652 |
|
|
653 |
tan
|
|
654 |
"return the tangent of myself interpreted as radians"
|
|
655 |
|
|
656 |
%{ /* NOCONTEXT */
|
|
657 |
|
|
658 |
double tan();
|
|
659 |
double result;
|
|
660 |
|
|
661 |
errno = 0;
|
|
662 |
result = tan(_floatVal(self));
|
|
663 |
if (errno == 0)
|
175
|
664 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
665 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
666 |
%}
|
|
667 |
.
|
131
|
668 |
^ DomainErrorSignal raise
|
1
|
669 |
!
|
|
670 |
|
|
671 |
arcSin
|
|
672 |
"return the arcsine of myself as radians"
|
|
673 |
|
|
674 |
%{ /* NOCONTEXT */
|
|
675 |
|
|
676 |
double asin();
|
|
677 |
double result;
|
|
678 |
|
|
679 |
errno = 0;
|
|
680 |
result = asin(_floatVal(self));
|
|
681 |
if (errno == 0)
|
175
|
682 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
683 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
684 |
%}
|
|
685 |
.
|
131
|
686 |
^ DomainErrorSignal raise
|
1
|
687 |
!
|
|
688 |
|
|
689 |
arcCos
|
|
690 |
"return the arccosine of myself as radians"
|
|
691 |
|
|
692 |
%{ /* NOCONTEXT */
|
|
693 |
|
|
694 |
double acos();
|
|
695 |
double result;
|
|
696 |
|
|
697 |
errno = 0;
|
|
698 |
result = acos(_floatVal(self));
|
|
699 |
if (errno == 0)
|
175
|
700 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
701 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
702 |
%}
|
|
703 |
.
|
131
|
704 |
^ DomainErrorSignal raise
|
1
|
705 |
!
|
|
706 |
|
|
707 |
arcTan
|
|
708 |
"return the arctangent of myself as radians"
|
|
709 |
|
|
710 |
%{ /* NOCONTEXT */
|
|
711 |
|
|
712 |
double atan();
|
|
713 |
double result;
|
|
714 |
|
|
715 |
errno = 0;
|
|
716 |
result = atan(_floatVal(self));
|
|
717 |
if (errno == 0)
|
175
|
718 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
719 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
720 |
%}
|
|
721 |
.
|
131
|
722 |
^ DomainErrorSignal raise
|
1
|
723 |
!
|
|
724 |
|
|
725 |
sqrt
|
|
726 |
"return the square root of myself"
|
|
727 |
|
|
728 |
%{ /* NOCONTEXT */
|
|
729 |
|
|
730 |
double sqrt();
|
|
731 |
double result;
|
|
732 |
|
|
733 |
errno = 0;
|
|
734 |
result = sqrt(_floatVal(self));
|
|
735 |
if (errno == 0)
|
175
|
736 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
737 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
738 |
%}
|
|
739 |
.
|
131
|
740 |
^ DomainErrorSignal raise
|
1
|
741 |
! !
|
|
742 |
|
13
|
743 |
!Float class methodsFor:'binary storage'!
|
|
744 |
|
44
|
745 |
storeBinaryIEEESingle:aFloat on:aStream
|
|
746 |
"store aFloat as an IEEE formatted 4-byte float
|
|
747 |
onto the binary stream, aStream"
|
|
748 |
|
142
|
749 |
"
|
|
750 |
this implementation is wrong: does not work on non-IEEE machines
|
|
751 |
(to date all machines where ST/X is running on use
|
|
752 |
IEEE float format. Need more here, when porting ST/X to 370's)
|
|
753 |
"
|
77
|
754 |
UninterpretedBytes isBigEndian ifFalse:[
|
175
|
755 |
"swap the bytes"
|
|
756 |
8 to:4 by:-1 do:[:i |
|
|
757 |
aStream nextPut:(aFloat basicAt:i).
|
|
758 |
].
|
|
759 |
^ self
|
44
|
760 |
].
|
77
|
761 |
1 to:4 do:[:i |
|
175
|
762 |
aStream nextPut:(aFloat basicAt:i).
|
44
|
763 |
]
|
|
764 |
!
|
|
765 |
|
|
766 |
storeBinaryIEEEDouble:aFloat on:aStream
|
|
767 |
"store aFloat as an IEEE formatted 8-byte float
|
|
768 |
onto the binary stream, aStream"
|
|
769 |
|
142
|
770 |
"
|
|
771 |
this implementation is wrong: does not work on non-IEEE machines
|
|
772 |
(to date all machines where ST/X is running on use
|
|
773 |
IEEE float format. Need more here, when porting ST/X to 370's)
|
|
774 |
"
|
77
|
775 |
UninterpretedBytes isBigEndian ifFalse:[
|
175
|
776 |
"swap the bytes"
|
|
777 |
8 to:1 by:-1 do:[:i |
|
|
778 |
aStream nextPut:(aFloat basicAt:i).
|
|
779 |
].
|
|
780 |
^ self
|
44
|
781 |
].
|
|
782 |
1 to:8 do:[:i |
|
175
|
783 |
aStream nextPut:(aFloat basicAt:i).
|
44
|
784 |
].
|
|
785 |
!
|
|
786 |
|
|
787 |
readBinaryIEEESingleFrom:aStream into:aFloat
|
|
788 |
"read a float value from the binary stream, aStream,
|
|
789 |
interpreting the next bytes as an IEEE formatted 4-byte float"
|
|
790 |
|
142
|
791 |
"
|
|
792 |
this implementation is wrong: does not work on non-IEEE machines
|
|
793 |
(to date all machines where ST/X is running on use
|
|
794 |
IEEE float format. Need more here, when porting ST/X to 370's)
|
|
795 |
"
|
77
|
796 |
UninterpretedBytes isBigEndian ifFalse:[
|
175
|
797 |
"swap the bytes"
|
|
798 |
8 to:4 by:-1 do:[:i |
|
|
799 |
aFloat basicAt:i put:(aStream next)
|
|
800 |
].
|
|
801 |
^ self
|
44
|
802 |
].
|
77
|
803 |
1 to:4 do:[:i |
|
175
|
804 |
aFloat basicAt:i put:aStream next
|
44
|
805 |
]
|
|
806 |
!
|
|
807 |
|
|
808 |
readBinaryIEEEDoubleFrom:aStream into:aFloat
|
|
809 |
"read the receivers value from the binary stream, aStream,
|
|
810 |
interpreting the next bytes as an IEEE formatted 8-byte float"
|
|
811 |
|
142
|
812 |
"
|
|
813 |
this implementation is wrong: does not work on non-IEEE machines
|
|
814 |
(to date all machines where ST/X is running on use
|
|
815 |
IEEE float format. Need more here, when porting ST/X to 370's)
|
|
816 |
"
|
77
|
817 |
UninterpretedBytes isBigEndian ifFalse:[
|
175
|
818 |
"swap the bytes"
|
|
819 |
8 to:1 by:-1 do:[:i |
|
|
820 |
aFloat basicAt:i put:(aStream next)
|
|
821 |
].
|
|
822 |
^ self
|
44
|
823 |
].
|
|
824 |
1 to:8 do:[:i |
|
175
|
825 |
aFloat basicAt:i put:aStream next
|
44
|
826 |
]
|
|
827 |
!
|
|
828 |
|
13
|
829 |
binaryDefinitionFrom: stream manager: manager
|
44
|
830 |
|f|
|
13
|
831 |
|
|
832 |
f := self basicNew.
|
44
|
833 |
self readBinaryIEEEDoubleFrom:stream into:f.
|
13
|
834 |
^ f
|
|
835 |
! !
|
|
836 |
|
|
837 |
!Float methodsFor:'binary storage'!
|
|
838 |
|
|
839 |
storeBinaryDefinitionOn: stream manager: manager
|
|
840 |
manager putIdOf: self class on: stream.
|
44
|
841 |
Float storeBinaryIEEEDouble:self on:stream.
|
13
|
842 |
! !
|
|
843 |
|
92
|
844 |
!Float methodsFor:'printing & storing'!
|
1
|
845 |
|
|
846 |
printString
|
|
847 |
"return a printed representation of the receiver"
|
|
848 |
|
|
849 |
%{ /* NOCONTEXT */
|
|
850 |
|
13
|
851 |
char buffer[64];
|
1
|
852 |
REGISTER char *cp;
|
54
|
853 |
#ifdef THISCONTEXT_IN_REGISTER
|
31
|
854 |
/*
|
|
855 |
* actually only needed on sparc: since thisContext is
|
|
856 |
* in a global register, which gets destroyed by printf,
|
|
857 |
* manually save it here - very stupid ...
|
|
858 |
*/
|
1
|
859 |
OBJ sav = __thisContext;
|
54
|
860 |
#endif
|
1
|
861 |
|
|
862 |
#ifdef SYSV
|
|
863 |
sprintf(buffer, "%.6lg", _floatVal(self));
|
|
864 |
#else
|
|
865 |
sprintf(buffer, "%.6G", _floatVal(self));
|
|
866 |
#endif
|
|
867 |
|
54
|
868 |
#ifdef THISCONTEXT_IN_REGISTER
|
1
|
869 |
__thisContext = sav;
|
54
|
870 |
#endif
|
1
|
871 |
/*
|
13
|
872 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does)
|
1
|
873 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not)
|
|
874 |
*/
|
|
875 |
for (cp = buffer; *cp; cp++) {
|
175
|
876 |
if ((*cp == '.') || (*cp == 'e')) break;
|
1
|
877 |
}
|
|
878 |
if (! *cp) {
|
175
|
879 |
*cp++ = '.';
|
|
880 |
*cp++ = '0';
|
|
881 |
*cp = '\0';
|
1
|
882 |
}
|
|
883 |
|
|
884 |
RETURN ( _MKSTRING(buffer COMMA_SND) );
|
|
885 |
%}
|
|
886 |
"1.0 printString"
|
|
887 |
"1.234 printString"
|
|
888 |
"1e10 printString"
|
|
889 |
"1.2e3 printString"
|
|
890 |
"1.2e30 printString"
|
|
891 |
!
|
|
892 |
|
|
893 |
printfPrintString:formatString
|
|
894 |
"non-portable: return a printed representation of the receiver
|
|
895 |
as specified by formatString, which is defined by printf.
|
31
|
896 |
If you use this, be aware, that specifying doubles differs on
|
1
|
897 |
systems; on SYSV machines you have to give something like %lf,
|
13
|
898 |
while on BSD systems the format string has to be %F.
|
31
|
899 |
Also, the resulting string may not be longer than 255 bytes -
|
54
|
900 |
since thats the (static) size of the buffer.
|
|
901 |
This method is NONSTANDARD and may be removed without notice."
|
1
|
902 |
|
13
|
903 |
%{ /* STACK: 400 */
|
1
|
904 |
char buffer[256];
|
|
905 |
|
54
|
906 |
if (__isString(formatString)) {
|
175
|
907 |
/*
|
|
908 |
* actually only needed on sparc: since thisContext is
|
|
909 |
* in a global register, which gets destroyed by printf,
|
|
910 |
* manually save it here - very stupid ...
|
|
911 |
*/
|
|
912 |
OBJ sav = __thisContext;
|
31
|
913 |
|
175
|
914 |
sprintf(buffer, _stringVal(formatString), _floatVal(self));
|
|
915 |
__thisContext = sav;
|
|
916 |
RETURN ( _MKSTRING(buffer COMMA_SND) );
|
1
|
917 |
}
|
|
918 |
%}
|
|
919 |
.
|
|
920 |
self primitiveFailed
|
|
921 |
|
|
922 |
"Float pi printfPrintString:'%%lg -> %lg'"
|
|
923 |
"Float pi printfPrintString:'%%lf -> %lf'"
|
|
924 |
"Float pi printfPrintString:'%%7.5lg -> %7.5lg'"
|
|
925 |
"Float pi printfPrintString:'%%7.5lf -> %7.5lf'"
|
|
926 |
"Float pi printfPrintString:'%%G -> %G'"
|
|
927 |
"Float pi printfPrintString:'%%F -> %F'"
|
|
928 |
"Float pi printfPrintString:'%%7.5G -> %7.5G'"
|
|
929 |
"Float pi printfPrintString:'%%7.5F -> %7.5F'"
|
|
930 |
! !
|