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