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