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 |
|
305
|
24 |
$Header: /cvs/stx/stx/libbasic/Float.st,v 1.24 1995-03-09 09:45:42 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 |
"
|
305
|
45 |
$Header: /cvs/stx/stx/libbasic/Float.st,v 1.24 1995-03-09 09:45:42 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 |
}
|
283
|
164 |
if (__isFloatLike(aNumber)) {
|
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 |
}
|
283
|
187 |
if (__isFloatLike(aNumber)) {
|
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 |
}
|
283
|
210 |
if (__isFloatLike(aNumber)) {
|
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 {
|
305
|
235 |
if (__isFloatLike(aNumber)) {
|
175
|
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 |
}
|
283
|
296 |
if (__isFloatLike(aNumber)) {
|
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 |
}
|
283
|
312 |
if (__isFloatLike(aNumber)) {
|
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 |
}
|
283
|
328 |
if (__isFloatLike(aNumber)) {
|
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 |
}
|
283
|
344 |
if (__isFloatLike(aNumber)) {
|
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 |
}
|
283
|
360 |
if (__isFloatLike(aNumber)) {
|
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 |
}
|
283
|
376 |
if (__isFloatLike(aNumber)) {
|
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 |
|
305
|
389 |
|i|
|
92
|
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 |
"
|
305
|
399 |
mhmh take some of my value-bits to hash on
|
92
|
400 |
"
|
305
|
401 |
^ (((self basicAt:8) bitAnd:16r3F) bitShift:24) +
|
|
402 |
((self basicAt:7) bitShift:16) +
|
|
403 |
((self basicAt:6) bitShift:8) +
|
|
404 |
(self basicAt:5)
|
92
|
405 |
|
|
406 |
"
|
|
407 |
3 hash
|
|
408 |
3.0 hash
|
|
409 |
3.1 hash
|
|
410 |
3.14159 hash
|
|
411 |
31.4159 hash
|
|
412 |
3.141591 hash
|
|
413 |
1.234567890123456 hash
|
|
414 |
1.234567890123457 hash
|
|
415 |
Set withAll:#(3 3.0 99 99.0 3.1415)
|
|
416 |
"
|
1
|
417 |
! !
|
|
418 |
|
|
419 |
!Float methodsFor:'coercion and converting'!
|
|
420 |
|
|
421 |
coerce:aNumber
|
|
422 |
"return aNumber converted into receivers type"
|
|
423 |
|
|
424 |
^ aNumber asFloat
|
|
425 |
!
|
|
426 |
|
|
427 |
generality
|
|
428 |
"return the generality value - see ArithmeticValue>>retry:coercing:"
|
41
|
429 |
|
1
|
430 |
^ 80
|
|
431 |
!
|
|
432 |
|
|
433 |
asFloat
|
|
434 |
"return a float with same value - thats me"
|
|
435 |
|
|
436 |
^ self
|
|
437 |
!
|
|
438 |
|
|
439 |
asInteger
|
|
440 |
"return an integer with same value - might truncate"
|
|
441 |
|
|
442 |
|l v sign|
|
|
443 |
|
|
444 |
%{ /* NOCONTEXT */
|
|
445 |
|
|
446 |
if ((_floatVal(self) >= (double)_MIN_INT)
|
|
447 |
&& (_floatVal(self) <= (double)_MAX_INT)) {
|
175
|
448 |
RETURN ( _MKSMALLINT( (INT)_floatVal(self)) );
|
1
|
449 |
}
|
|
450 |
%}
|
|
451 |
.
|
54
|
452 |
^ super asInteger
|
1
|
453 |
|
|
454 |
"12345.0 asInteger"
|
|
455 |
"1e15 asInteger"
|
|
456 |
! !
|
|
457 |
|
|
458 |
!Float methodsFor:'truncation and rounding'!
|
|
459 |
|
|
460 |
truncated
|
68
|
461 |
"return the receiver truncated towards zero as an integer"
|
1
|
462 |
|
|
463 |
|val|
|
|
464 |
|
|
465 |
%{
|
|
466 |
double floor(), ceil();
|
|
467 |
double dVal;
|
|
468 |
|
|
469 |
dVal = _floatVal(self);
|
|
470 |
if (dVal < 0.0) {
|
175
|
471 |
dVal = ceil(dVal);
|
1
|
472 |
} else {
|
175
|
473 |
dVal = floor(dVal);
|
1
|
474 |
}
|
|
475 |
|
|
476 |
/*
|
68
|
477 |
* ST-80 (and X3J20) returns integer.
|
1
|
478 |
*/
|
|
479 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) {
|
175
|
480 |
RETURN ( _MKSMALLINT( (INT) dVal ) );
|
1
|
481 |
}
|
|
482 |
val = _MKFLOAT(dVal COMMA_CON);
|
68
|
483 |
%}.
|
1
|
484 |
^ val asInteger
|
|
485 |
!
|
|
486 |
|
|
487 |
rounded
|
68
|
488 |
"return the receiver rounded to the nearest integer as an integer"
|
1
|
489 |
|
|
490 |
|val|
|
|
491 |
|
|
492 |
%{
|
|
493 |
double floor();
|
|
494 |
double dVal;
|
|
495 |
|
|
496 |
/*
|
68
|
497 |
* ST-80 (and X3J20) returns integer.
|
1
|
498 |
*/
|
|
499 |
dVal = floor(_floatVal(self) + 0.5);
|
|
500 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) {
|
175
|
501 |
RETURN ( _MKSMALLINT( (INT) dVal ) );
|
1
|
502 |
}
|
|
503 |
val = _MKFLOAT(dVal COMMA_CON);
|
68
|
504 |
%}.
|
1
|
505 |
^ val asInteger
|
|
506 |
!
|
|
507 |
|
|
508 |
floor
|
68
|
509 |
"return the integer nearest the receiver towards negative infinity."
|
|
510 |
|
|
511 |
|val|
|
1
|
512 |
|
68
|
513 |
%{
|
|
514 |
double floor();
|
|
515 |
double dVal;
|
1
|
516 |
|
68
|
517 |
/*
|
|
518 |
* ST-80 (and X3J20) returns integer.
|
|
519 |
*/
|
|
520 |
dVal = floor(_floatVal(self));
|
|
521 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) {
|
175
|
522 |
RETURN ( _MKSMALLINT( (INT) dVal ) );
|
68
|
523 |
}
|
|
524 |
val = _MKFLOAT(dVal COMMA_CON);
|
|
525 |
%}.
|
|
526 |
^ val asInteger
|
1
|
527 |
!
|
|
528 |
|
|
529 |
ceiling
|
|
530 |
"return the smallest integer-valued float greater or equal to the receiver"
|
|
531 |
|
68
|
532 |
|val|
|
1
|
533 |
|
68
|
534 |
%{
|
1
|
535 |
double ceil();
|
68
|
536 |
double dVal;
|
1
|
537 |
|
68
|
538 |
/*
|
|
539 |
* ST-80 (and X3J20) returns integer.
|
|
540 |
*/
|
|
541 |
dVal = ceil(_floatVal(self));
|
|
542 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) {
|
175
|
543 |
RETURN ( _MKSMALLINT( (INT) dVal ) );
|
68
|
544 |
}
|
|
545 |
val = _MKFLOAT(dVal COMMA_CON);
|
|
546 |
%}.
|
|
547 |
^ val asInteger
|
1
|
548 |
! !
|
|
549 |
|
|
550 |
!Float methodsFor:'mathematical functions'!
|
|
551 |
|
|
552 |
ln
|
|
553 |
"return the natural logarithm of myself"
|
|
554 |
|
|
555 |
%{ /* NOCONTEXT */
|
|
556 |
|
|
557 |
double log();
|
|
558 |
double result;
|
|
559 |
|
|
560 |
errno = 0;
|
|
561 |
result = log(_floatVal(self));
|
|
562 |
if (errno == 0)
|
175
|
563 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
564 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
565 |
%}
|
|
566 |
.
|
142
|
567 |
"
|
|
568 |
an invalid value for logarithm
|
|
569 |
"
|
131
|
570 |
^ DomainErrorSignal raise
|
1
|
571 |
!
|
|
572 |
|
|
573 |
raisedTo:aNumber
|
|
574 |
"return self raised to the power of aNumber"
|
|
575 |
|n|
|
|
576 |
|
|
577 |
n := aNumber asFloat.
|
|
578 |
%{
|
|
579 |
double pow();
|
|
580 |
double result;
|
|
581 |
|
283
|
582 |
if (__isFloatLike(n)) {
|
175
|
583 |
errno = 0;
|
|
584 |
result = pow(_floatVal(self), _floatVal(n));
|
|
585 |
errno = 0; /* XXXX */
|
|
586 |
if (errno == 0)
|
|
587 |
RETURN ( _MKFLOAT(result COMMA_CON) );
|
|
588 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
589 |
}
|
|
590 |
%}
|
|
591 |
.
|
142
|
592 |
"
|
|
593 |
an invalid argument (not convertable to float ?)
|
|
594 |
"
|
131
|
595 |
^ DomainErrorSignal raise
|
1
|
596 |
!
|
|
597 |
|
|
598 |
exp
|
|
599 |
"return e raised to the power of the receiver"
|
|
600 |
|
|
601 |
%{ /* NOCONTEXT */
|
|
602 |
|
|
603 |
double exp();
|
|
604 |
double result;
|
|
605 |
|
|
606 |
errno = 0;
|
|
607 |
result = exp(_floatVal(self));
|
|
608 |
if (errno == 0)
|
175
|
609 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
610 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
611 |
%}
|
|
612 |
.
|
131
|
613 |
^ DomainErrorSignal raise
|
1
|
614 |
!
|
|
615 |
|
|
616 |
sin
|
|
617 |
"return the sine of myself interpreted as radians"
|
|
618 |
|
|
619 |
%{ /* NOCONTEXT */
|
|
620 |
|
|
621 |
double sin();
|
|
622 |
double result;
|
|
623 |
|
|
624 |
errno = 0;
|
|
625 |
result = sin(_floatVal(self));
|
|
626 |
if (errno == 0)
|
175
|
627 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
628 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
629 |
%}
|
|
630 |
.
|
131
|
631 |
^ DomainErrorSignal raise
|
1
|
632 |
!
|
|
633 |
|
|
634 |
cos
|
|
635 |
"return the cosine of myself interpreted as radians"
|
|
636 |
|
|
637 |
%{ /* NOCONTEXT */
|
|
638 |
|
|
639 |
double cos();
|
|
640 |
double result;
|
|
641 |
|
|
642 |
errno = 0;
|
|
643 |
result = cos(_floatVal(self));
|
|
644 |
if (errno == 0)
|
175
|
645 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
646 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
647 |
%}
|
|
648 |
.
|
131
|
649 |
^ DomainErrorSignal raise
|
1
|
650 |
!
|
|
651 |
|
|
652 |
tan
|
|
653 |
"return the tangent of myself interpreted as radians"
|
|
654 |
|
|
655 |
%{ /* NOCONTEXT */
|
|
656 |
|
|
657 |
double tan();
|
|
658 |
double result;
|
|
659 |
|
|
660 |
errno = 0;
|
|
661 |
result = tan(_floatVal(self));
|
|
662 |
if (errno == 0)
|
175
|
663 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
664 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
665 |
%}
|
|
666 |
.
|
131
|
667 |
^ DomainErrorSignal raise
|
1
|
668 |
!
|
|
669 |
|
|
670 |
arcSin
|
|
671 |
"return the arcsine of myself as radians"
|
|
672 |
|
|
673 |
%{ /* NOCONTEXT */
|
|
674 |
|
|
675 |
double asin();
|
|
676 |
double result;
|
|
677 |
|
|
678 |
errno = 0;
|
|
679 |
result = asin(_floatVal(self));
|
|
680 |
if (errno == 0)
|
175
|
681 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
682 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
683 |
%}
|
|
684 |
.
|
131
|
685 |
^ DomainErrorSignal raise
|
1
|
686 |
!
|
|
687 |
|
|
688 |
arcCos
|
|
689 |
"return the arccosine of myself as radians"
|
|
690 |
|
|
691 |
%{ /* NOCONTEXT */
|
|
692 |
|
|
693 |
double acos();
|
|
694 |
double result;
|
|
695 |
|
|
696 |
errno = 0;
|
|
697 |
result = acos(_floatVal(self));
|
|
698 |
if (errno == 0)
|
175
|
699 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
700 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
701 |
%}
|
|
702 |
.
|
131
|
703 |
^ DomainErrorSignal raise
|
1
|
704 |
!
|
|
705 |
|
|
706 |
arcTan
|
|
707 |
"return the arctangent of myself as radians"
|
|
708 |
|
|
709 |
%{ /* NOCONTEXT */
|
|
710 |
|
|
711 |
double atan();
|
|
712 |
double result;
|
|
713 |
|
|
714 |
errno = 0;
|
|
715 |
result = atan(_floatVal(self));
|
|
716 |
if (errno == 0)
|
175
|
717 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
718 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
719 |
%}
|
|
720 |
.
|
131
|
721 |
^ DomainErrorSignal raise
|
1
|
722 |
!
|
|
723 |
|
|
724 |
sqrt
|
|
725 |
"return the square root of myself"
|
|
726 |
|
|
727 |
%{ /* NOCONTEXT */
|
|
728 |
|
|
729 |
double sqrt();
|
|
730 |
double result;
|
|
731 |
|
|
732 |
errno = 0;
|
|
733 |
result = sqrt(_floatVal(self));
|
|
734 |
if (errno == 0)
|
175
|
735 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
85
|
736 |
Float_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
737 |
%}
|
|
738 |
.
|
131
|
739 |
^ DomainErrorSignal raise
|
1
|
740 |
! !
|
|
741 |
|
258
|
742 |
!Float methodsFor:'private accessing'!
|
|
743 |
|
|
744 |
basicAt:index
|
|
745 |
"return an internal byte of the float.
|
|
746 |
The value returned here depends on byte order, float representation etc.
|
|
747 |
Therefore, this method should be used strictly private.
|
|
748 |
|
|
749 |
Notice:
|
|
750 |
the need to redefine this method here is due to the
|
|
751 |
inability of many machines to store floats in non-double aligned memory.
|
|
752 |
Therefore, on some machines, the first 4 bytes of a float are left unused,
|
|
753 |
and the actual float is stored at index 5 .. 12.
|
|
754 |
To hide this at one place, this method knows about that, and returns
|
|
755 |
values as if this filler wasnt present."
|
|
756 |
|
|
757 |
%{ /* NOCONTEXT */
|
|
758 |
|
|
759 |
register int indx;
|
|
760 |
unsigned char *cp;
|
|
761 |
|
|
762 |
/*
|
|
763 |
* notice the missing test for self being a nonNilObject -
|
|
764 |
* this can be done since basicAt: is defined both in UndefinedObject
|
|
765 |
* and SmallInteger
|
|
766 |
*/
|
|
767 |
if (__isSmallInteger(index)) {
|
|
768 |
indx = _intVal(index) - 1;
|
|
769 |
if ((indx >= 0) && (indx < sizeof(double))) {
|
|
770 |
cp = (unsigned char *)(& (_FloatInstPtr(self)->f_floatvalue));
|
|
771 |
RETURN ( _MKSMALLINT(cp[indx] & 0xFF) );
|
|
772 |
}
|
|
773 |
}
|
|
774 |
%}.
|
|
775 |
index isInteger ifFalse:[
|
|
776 |
^ self indexNotInteger
|
|
777 |
].
|
|
778 |
^ self subscriptBoundsError:index
|
|
779 |
!
|
|
780 |
|
|
781 |
basicAt:index put:value
|
|
782 |
"set an internal byte of the float.
|
|
783 |
The value to be stored here depends on byte order, float representation etc.
|
|
784 |
Therefore, this method should be used strictly private.
|
|
785 |
|
|
786 |
Notice:
|
|
787 |
the need to redefine this method here is due to the
|
|
788 |
inability of many machines to store floats in non-double aligned memory.
|
|
789 |
Therefore, on some machines, the first 4 bytes of a float are left unused,
|
|
790 |
and the actual float is stored at index 5 .. 12.
|
|
791 |
To hide this at one place, this method knows about that, and returns
|
|
792 |
values as if this filler wasnt present."
|
|
793 |
|
|
794 |
%{ /* NOCONTEXT */
|
|
795 |
register int indx, val;
|
|
796 |
unsigned char *cp;
|
|
797 |
|
|
798 |
/*
|
|
799 |
* notice the missing test for self being a nonNilObject -
|
|
800 |
* this can be done since basicAt: is defined both in UndefinedObject
|
|
801 |
* and SmallInteger
|
|
802 |
*/
|
|
803 |
if (__bothSmallInteger(index, value)) {
|
|
804 |
val = _intVal(value);
|
|
805 |
if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) {
|
|
806 |
indx = _intVal(index) - 1;
|
|
807 |
if ((indx >= 0) && (indx < sizeof(double))) {
|
|
808 |
cp = (unsigned char *)(& (_FloatInstPtr(self)->f_floatvalue));
|
|
809 |
cp[indx] = val;
|
|
810 |
RETURN ( value );
|
|
811 |
}
|
|
812 |
}
|
|
813 |
}
|
|
814 |
%}.
|
|
815 |
index isInteger ifFalse:[
|
|
816 |
^ self indexNotInteger
|
|
817 |
].
|
|
818 |
value isInteger ifFalse:[
|
|
819 |
"
|
|
820 |
the object to store should be an integer number
|
|
821 |
"
|
|
822 |
^ self elementNotInteger
|
|
823 |
].
|
|
824 |
(value between:0 and:255) ifFalse:[
|
|
825 |
"
|
|
826 |
the object to store must be a bytes value
|
|
827 |
"
|
|
828 |
^ self elementBoundsError
|
|
829 |
].
|
|
830 |
^ self subscriptBoundsError:index
|
|
831 |
! !
|
|
832 |
|
13
|
833 |
!Float class methodsFor:'binary storage'!
|
|
834 |
|
44
|
835 |
storeBinaryIEEESingle:aFloat on:aStream
|
|
836 |
"store aFloat as an IEEE formatted 4-byte float
|
|
837 |
onto the binary stream, aStream"
|
|
838 |
|
142
|
839 |
"
|
|
840 |
this implementation is wrong: does not work on non-IEEE machines
|
|
841 |
(to date all machines where ST/X is running on use
|
|
842 |
IEEE float format. Need more here, when porting ST/X to 370's)
|
|
843 |
"
|
77
|
844 |
UninterpretedBytes isBigEndian ifFalse:[
|
175
|
845 |
"swap the bytes"
|
|
846 |
8 to:4 by:-1 do:[:i |
|
|
847 |
aStream nextPut:(aFloat basicAt:i).
|
|
848 |
].
|
|
849 |
^ self
|
44
|
850 |
].
|
77
|
851 |
1 to:4 do:[:i |
|
175
|
852 |
aStream nextPut:(aFloat basicAt:i).
|
44
|
853 |
]
|
|
854 |
!
|
|
855 |
|
|
856 |
storeBinaryIEEEDouble:aFloat on:aStream
|
|
857 |
"store aFloat as an IEEE formatted 8-byte float
|
|
858 |
onto the binary stream, aStream"
|
|
859 |
|
142
|
860 |
"
|
|
861 |
this implementation is wrong: does not work on non-IEEE machines
|
|
862 |
(to date all machines where ST/X is running on use
|
|
863 |
IEEE float format. Need more here, when porting ST/X to 370's)
|
|
864 |
"
|
77
|
865 |
UninterpretedBytes isBigEndian ifFalse:[
|
175
|
866 |
"swap the bytes"
|
|
867 |
8 to:1 by:-1 do:[:i |
|
|
868 |
aStream nextPut:(aFloat basicAt:i).
|
|
869 |
].
|
|
870 |
^ self
|
44
|
871 |
].
|
|
872 |
1 to:8 do:[:i |
|
175
|
873 |
aStream nextPut:(aFloat basicAt:i).
|
44
|
874 |
].
|
|
875 |
!
|
|
876 |
|
|
877 |
readBinaryIEEESingleFrom:aStream into:aFloat
|
|
878 |
"read a float value from the binary stream, aStream,
|
|
879 |
interpreting the next bytes as an IEEE formatted 4-byte float"
|
|
880 |
|
142
|
881 |
"
|
|
882 |
this implementation is wrong: does not work on non-IEEE machines
|
|
883 |
(to date all machines where ST/X is running on use
|
|
884 |
IEEE float format. Need more here, when porting ST/X to 370's)
|
|
885 |
"
|
77
|
886 |
UninterpretedBytes isBigEndian ifFalse:[
|
175
|
887 |
"swap the bytes"
|
|
888 |
8 to:4 by:-1 do:[:i |
|
|
889 |
aFloat basicAt:i put:(aStream next)
|
|
890 |
].
|
|
891 |
^ self
|
44
|
892 |
].
|
77
|
893 |
1 to:4 do:[:i |
|
175
|
894 |
aFloat basicAt:i put:aStream next
|
44
|
895 |
]
|
|
896 |
!
|
|
897 |
|
|
898 |
readBinaryIEEEDoubleFrom:aStream into:aFloat
|
|
899 |
"read the receivers value from the binary stream, aStream,
|
|
900 |
interpreting the next bytes as an IEEE formatted 8-byte float"
|
|
901 |
|
142
|
902 |
"
|
|
903 |
this implementation is wrong: does not work on non-IEEE machines
|
|
904 |
(to date all machines where ST/X is running on use
|
|
905 |
IEEE float format. Need more here, when porting ST/X to 370's)
|
|
906 |
"
|
77
|
907 |
UninterpretedBytes isBigEndian ifFalse:[
|
175
|
908 |
"swap the bytes"
|
|
909 |
8 to:1 by:-1 do:[:i |
|
|
910 |
aFloat basicAt:i put:(aStream next)
|
|
911 |
].
|
|
912 |
^ self
|
44
|
913 |
].
|
|
914 |
1 to:8 do:[:i |
|
175
|
915 |
aFloat basicAt:i put:aStream next
|
44
|
916 |
]
|
|
917 |
!
|
|
918 |
|
13
|
919 |
binaryDefinitionFrom: stream manager: manager
|
44
|
920 |
|f|
|
13
|
921 |
|
|
922 |
f := self basicNew.
|
44
|
923 |
self readBinaryIEEEDoubleFrom:stream into:f.
|
13
|
924 |
^ f
|
|
925 |
! !
|
|
926 |
|
|
927 |
!Float methodsFor:'binary storage'!
|
|
928 |
|
|
929 |
storeBinaryDefinitionOn: stream manager: manager
|
|
930 |
manager putIdOf: self class on: stream.
|
44
|
931 |
Float storeBinaryIEEEDouble:self on:stream.
|
13
|
932 |
! !
|
|
933 |
|
92
|
934 |
!Float methodsFor:'printing & storing'!
|
1
|
935 |
|
|
936 |
printString
|
|
937 |
"return a printed representation of the receiver"
|
|
938 |
|
|
939 |
%{ /* NOCONTEXT */
|
|
940 |
|
13
|
941 |
char buffer[64];
|
1
|
942 |
REGISTER char *cp;
|
54
|
943 |
#ifdef THISCONTEXT_IN_REGISTER
|
31
|
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 |
*/
|
1
|
949 |
OBJ sav = __thisContext;
|
54
|
950 |
#endif
|
1
|
951 |
|
|
952 |
#ifdef SYSV
|
|
953 |
sprintf(buffer, "%.6lg", _floatVal(self));
|
|
954 |
#else
|
|
955 |
sprintf(buffer, "%.6G", _floatVal(self));
|
|
956 |
#endif
|
|
957 |
|
54
|
958 |
#ifdef THISCONTEXT_IN_REGISTER
|
1
|
959 |
__thisContext = sav;
|
54
|
960 |
#endif
|
1
|
961 |
/*
|
13
|
962 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does)
|
1
|
963 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not)
|
|
964 |
*/
|
|
965 |
for (cp = buffer; *cp; cp++) {
|
175
|
966 |
if ((*cp == '.') || (*cp == 'e')) break;
|
1
|
967 |
}
|
|
968 |
if (! *cp) {
|
175
|
969 |
*cp++ = '.';
|
|
970 |
*cp++ = '0';
|
|
971 |
*cp = '\0';
|
1
|
972 |
}
|
|
973 |
|
|
974 |
RETURN ( _MKSTRING(buffer COMMA_SND) );
|
|
975 |
%}
|
|
976 |
"1.0 printString"
|
|
977 |
"1.234 printString"
|
|
978 |
"1e10 printString"
|
|
979 |
"1.2e3 printString"
|
|
980 |
"1.2e30 printString"
|
|
981 |
!
|
|
982 |
|
|
983 |
printfPrintString:formatString
|
|
984 |
"non-portable: return a printed representation of the receiver
|
|
985 |
as specified by formatString, which is defined by printf.
|
31
|
986 |
If you use this, be aware, that specifying doubles differs on
|
1
|
987 |
systems; on SYSV machines you have to give something like %lf,
|
13
|
988 |
while on BSD systems the format string has to be %F.
|
31
|
989 |
Also, the resulting string may not be longer than 255 bytes -
|
54
|
990 |
since thats the (static) size of the buffer.
|
|
991 |
This method is NONSTANDARD and may be removed without notice."
|
1
|
992 |
|
13
|
993 |
%{ /* STACK: 400 */
|
1
|
994 |
char buffer[256];
|
|
995 |
|
54
|
996 |
if (__isString(formatString)) {
|
175
|
997 |
/*
|
|
998 |
* actually only needed on sparc: since thisContext is
|
|
999 |
* in a global register, which gets destroyed by printf,
|
|
1000 |
* manually save it here - very stupid ...
|
|
1001 |
*/
|
|
1002 |
OBJ sav = __thisContext;
|
31
|
1003 |
|
175
|
1004 |
sprintf(buffer, _stringVal(formatString), _floatVal(self));
|
|
1005 |
__thisContext = sav;
|
|
1006 |
RETURN ( _MKSTRING(buffer COMMA_SND) );
|
1
|
1007 |
}
|
|
1008 |
%}
|
|
1009 |
.
|
|
1010 |
self primitiveFailed
|
|
1011 |
|
|
1012 |
"Float pi printfPrintString:'%%lg -> %lg'"
|
|
1013 |
"Float pi printfPrintString:'%%lf -> %lf'"
|
|
1014 |
"Float pi printfPrintString:'%%7.5lg -> %7.5lg'"
|
|
1015 |
"Float pi printfPrintString:'%%7.5lf -> %7.5lf'"
|
|
1016 |
"Float pi printfPrintString:'%%G -> %G'"
|
|
1017 |
"Float pi printfPrintString:'%%F -> %F'"
|
|
1018 |
"Float pi printfPrintString:'%%7.5G -> %7.5G'"
|
|
1019 |
"Float pi printfPrintString:'%%7.5F -> %7.5F'"
|
|
1020 |
! !
|