1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1988-93 by Claus Gittinger
|
|
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 |
|
|
22 |
COPYRIGHT (c) 1988-93 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
3
|
25 |
$Header: /cvs/stx/stx/libbasic/Float.st,v 1.3 1993-10-13 00:16:02 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
|
|
45 |
totally different representation."
|
|
46 |
|
|
47 |
%{ /* NOCONTEXT */
|
|
48 |
RETURN ( _MKFLOAT((0.0) COMMA_SND) );
|
|
49 |
%}
|
|
50 |
! !
|
|
51 |
|
|
52 |
!Float class methodsFor:'constants'!
|
|
53 |
|
|
54 |
zero
|
|
55 |
"return the neutral element for addition"
|
|
56 |
|
|
57 |
^ 0.0
|
|
58 |
!
|
|
59 |
|
|
60 |
unity
|
|
61 |
"return the neutral element for multiplication"
|
|
62 |
|
|
63 |
^ 1.0
|
|
64 |
!
|
|
65 |
|
|
66 |
pi
|
|
67 |
"return the constant pi"
|
|
68 |
|
|
69 |
^ 3.1415926535897932384626434
|
|
70 |
! !
|
|
71 |
|
3
|
72 |
!Float class methodsFor:'queries'!
|
|
73 |
|
|
74 |
isBuiltInClass
|
|
75 |
"this class is known by the run-time-system"
|
|
76 |
|
|
77 |
^ self == Float
|
|
78 |
! !
|
|
79 |
|
1
|
80 |
!Float methodsFor:'copying'!
|
|
81 |
|
|
82 |
shallowCopy
|
|
83 |
"return a shallow copy of myself
|
|
84 |
- reimplemented here since Floats are kind of kludgy"
|
|
85 |
|
|
86 |
^ self
|
|
87 |
!
|
|
88 |
|
|
89 |
deepCopy
|
|
90 |
"return a deep copy of myself
|
|
91 |
- reimplemented here since Floats are kind of kludgy"
|
|
92 |
|
|
93 |
^ self
|
|
94 |
! !
|
|
95 |
|
|
96 |
!Float methodsFor:'accessing'!
|
|
97 |
|
|
98 |
size
|
|
99 |
"redefined since floats are kludgy (ByteArry)"
|
|
100 |
|
|
101 |
^ 0
|
|
102 |
!
|
|
103 |
|
|
104 |
at:index
|
|
105 |
"redefined to prevent access to individual bytes in a float"
|
|
106 |
|
|
107 |
self error:'not allowed for floats'
|
|
108 |
!
|
|
109 |
|
|
110 |
at:index put:aValue
|
|
111 |
"redefined to prevent access to individual bytes in a float"
|
|
112 |
|
|
113 |
self error:'not allowed for floats'
|
|
114 |
! !
|
|
115 |
|
|
116 |
!Float methodsFor:'arithmetic'!
|
|
117 |
|
|
118 |
+ aNumber
|
|
119 |
"return the sum of the receiver and the argument, aNumber"
|
|
120 |
|
|
121 |
%{ /* NOCONTEXT */
|
|
122 |
OBJ newFloat;
|
|
123 |
double result;
|
|
124 |
|
|
125 |
if (_isSmallInteger(aNumber)) {
|
|
126 |
result = _floatVal(self) + (double)(_intVal(aNumber));
|
|
127 |
retResult:
|
3
|
128 |
_qMKFLOAT(newFloat, result, SENDER);
|
1
|
129 |
RETURN ( newFloat );
|
|
130 |
}
|
|
131 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
|
132 |
result = _floatVal(self) + _floatVal(aNumber);
|
|
133 |
goto retResult;
|
|
134 |
}
|
|
135 |
%}
|
|
136 |
.
|
|
137 |
^ aNumber sumFromFloat:self
|
|
138 |
!
|
|
139 |
|
|
140 |
- aNumber
|
|
141 |
"return the difference of the receiver and the argument, aNumber"
|
|
142 |
|
|
143 |
%{ /* NOCONTEXT */
|
|
144 |
OBJ newFloat;
|
|
145 |
double result;
|
|
146 |
|
|
147 |
if (_isSmallInteger(aNumber)) {
|
|
148 |
result = _floatVal(self) - (double)(_intVal(aNumber));
|
|
149 |
retResult:
|
3
|
150 |
_qMKFLOAT(newFloat, result, SENDER);
|
1
|
151 |
RETURN ( newFloat );
|
|
152 |
}
|
|
153 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
|
154 |
result = _floatVal(self) - _floatVal(aNumber);
|
|
155 |
goto retResult;
|
|
156 |
}
|
|
157 |
%}
|
|
158 |
.
|
|
159 |
^ aNumber differenceFromFloat:self
|
|
160 |
!
|
|
161 |
|
|
162 |
* aNumber
|
|
163 |
"return the product of the receiver and the argument, aNumber"
|
|
164 |
|
|
165 |
%{ /* NOCONTEXT */
|
|
166 |
|
|
167 |
OBJ newFloat;
|
|
168 |
double result;
|
|
169 |
|
|
170 |
if (_isSmallInteger(aNumber)) {
|
|
171 |
result = _floatVal(self) * (double)(_intVal(aNumber));
|
|
172 |
retResult:
|
3
|
173 |
_qMKFLOAT(newFloat, result, SENDER);
|
1
|
174 |
RETURN ( newFloat );
|
|
175 |
}
|
|
176 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
|
177 |
result = _floatVal(self) * _floatVal(aNumber);
|
|
178 |
goto retResult;
|
|
179 |
}
|
|
180 |
%}
|
|
181 |
.
|
|
182 |
^ aNumber productFromFloat:self
|
|
183 |
!
|
|
184 |
|
|
185 |
/ aNumber
|
|
186 |
"return the quotient of the receiver and the argument, aNumber"
|
|
187 |
|
|
188 |
%{ /* NOCONTEXT */
|
|
189 |
|
|
190 |
OBJ newFloat;
|
|
191 |
double result, val;
|
|
192 |
|
|
193 |
if (_isSmallInteger(aNumber)) {
|
|
194 |
if (aNumber != _MKSMALLINT(0)) {
|
|
195 |
result = _floatVal(self) / ( (double)_intVal(aNumber)) ;
|
|
196 |
retResult:
|
3
|
197 |
_qMKFLOAT(newFloat, result, SENDER);
|
1
|
198 |
RETURN ( newFloat );
|
|
199 |
}
|
|
200 |
} else {
|
|
201 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
|
202 |
val = _floatVal(aNumber);
|
|
203 |
if (val != 0.0) {
|
|
204 |
result = _floatVal(self) / val;
|
|
205 |
goto retResult;
|
|
206 |
}
|
|
207 |
}
|
|
208 |
}
|
|
209 |
%}
|
|
210 |
.
|
|
211 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[
|
|
212 |
DivisionByZeroSignal raise.
|
|
213 |
^ self
|
|
214 |
].
|
|
215 |
^ aNumber quotientFromFloat:self
|
|
216 |
!
|
|
217 |
|
|
218 |
// aNumber
|
|
219 |
"return the integer quotient of dividing the receiver by aNumber with
|
|
220 |
truncation towards negative infinity."
|
|
221 |
|
|
222 |
^ (self / aNumber) floor asInteger
|
|
223 |
!
|
|
224 |
|
|
225 |
\\ aNumber
|
|
226 |
"return the integer remainder of dividing the receiver by aNumber with
|
|
227 |
truncation towards negative infinity."
|
|
228 |
|
|
229 |
^ (self - ((self / aNumber) floor * aNumber)) floor asInteger
|
|
230 |
!
|
|
231 |
|
|
232 |
negated
|
|
233 |
"return myself negated"
|
|
234 |
|
|
235 |
%{ /* NOCONTEXT */
|
3
|
236 |
OBJ newFloat;
|
|
237 |
|
|
238 |
_qMKFLOAT(newFloat, - _floatVal(self), SENDER);
|
|
239 |
RETURN ( newFloat );
|
1
|
240 |
%}
|
|
241 |
! !
|
|
242 |
|
|
243 |
!Float methodsFor:'testing'!
|
|
244 |
|
|
245 |
positive
|
|
246 |
"return true if the receiver is greater or equal to zero"
|
|
247 |
|
|
248 |
%{ /* NOCONTEXT */
|
|
249 |
RETURN ( (_floatVal(self) >= 0.0) ? true : false );
|
|
250 |
%}
|
|
251 |
!
|
|
252 |
|
|
253 |
negative
|
|
254 |
"return true if the receiver is less than zero"
|
|
255 |
|
|
256 |
%{ /* NOCONTEXT */
|
|
257 |
RETURN ( (_floatVal(self) < 0.0) ? true : false );
|
|
258 |
%}
|
|
259 |
! !
|
|
260 |
|
|
261 |
!Float methodsFor:'comparing'!
|
|
262 |
|
|
263 |
< aNumber
|
|
264 |
"return true, if the argument is greater"
|
|
265 |
|
|
266 |
%{ /* NOCONTEXT */
|
|
267 |
if (_isSmallInteger(aNumber)) {
|
|
268 |
RETURN ( (_floatVal(self) < (double)(_intVal(aNumber))) ? true : false );
|
|
269 |
}
|
|
270 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
|
271 |
RETURN ( (_floatVal(self) < _floatVal(aNumber)) ? true : false );
|
|
272 |
}
|
|
273 |
%}
|
|
274 |
.
|
|
275 |
^ aNumber lessFromFloat:self
|
|
276 |
!
|
|
277 |
|
|
278 |
> aNumber
|
|
279 |
"return true, if the argument is less"
|
|
280 |
|
|
281 |
%{ /* NOCONTEXT */
|
|
282 |
if (_isSmallInteger(aNumber)) {
|
|
283 |
RETURN ( (_floatVal(self) > (double)(_intVal(aNumber))) ? true : false );
|
|
284 |
}
|
|
285 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
|
286 |
RETURN ( (_floatVal(self) > _floatVal(aNumber)) ? true : false );
|
|
287 |
}
|
|
288 |
%}
|
|
289 |
.
|
|
290 |
^ self retry:#> coercing:aNumber
|
|
291 |
!
|
|
292 |
|
|
293 |
<= aNumber
|
|
294 |
"return true, if the argument is greater or equal"
|
|
295 |
|
|
296 |
%{ /* NOCONTEXT */
|
|
297 |
if (_isSmallInteger(aNumber)) {
|
|
298 |
RETURN ( (_floatVal(self) <= (double)(_intVal(aNumber))) ? true : false );
|
|
299 |
}
|
|
300 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
|
301 |
RETURN ( (_floatVal(self) <= _floatVal(aNumber)) ? true : false );
|
|
302 |
}
|
|
303 |
%}
|
|
304 |
.
|
|
305 |
^ self retry:#<= coercing:aNumber
|
|
306 |
!
|
|
307 |
|
|
308 |
>= aNumber
|
|
309 |
"return true, if the argument is less or equal"
|
|
310 |
|
|
311 |
%{ /* NOCONTEXT */
|
|
312 |
if (_isSmallInteger(aNumber)) {
|
|
313 |
RETURN ( (_floatVal(self) >= (double)(_intVal(aNumber))) ? true : false );
|
|
314 |
}
|
|
315 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
|
316 |
RETURN ( (_floatVal(self) >= _floatVal(aNumber)) ? true : false );
|
|
317 |
}
|
|
318 |
%}
|
|
319 |
.
|
|
320 |
^ self retry:#>= coercing:aNumber
|
|
321 |
!
|
|
322 |
|
|
323 |
= aNumber
|
|
324 |
"return true, if the arguments value are equal"
|
|
325 |
|
|
326 |
%{ /* NOCONTEXT */
|
|
327 |
if (_isSmallInteger(aNumber)) {
|
|
328 |
RETURN ( (_floatVal(self) == (double)(_intVal(aNumber))) ? true : false );
|
|
329 |
}
|
|
330 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
|
331 |
RETURN ( (_floatVal(self) == _floatVal(aNumber)) ? true : false );
|
|
332 |
}
|
|
333 |
%}
|
|
334 |
.
|
|
335 |
^ self retry:#= coercing:aNumber
|
|
336 |
!
|
|
337 |
|
|
338 |
~= aNumber
|
|
339 |
"return true, if the arguments value are not equal"
|
|
340 |
|
|
341 |
%{ /* NOCONTEXT */
|
|
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 |
!Float methodsFor:'coercion and converting'!
|
|
354 |
|
|
355 |
coerce:aNumber
|
|
356 |
"return aNumber converted into receivers type"
|
|
357 |
|
|
358 |
^ aNumber asFloat
|
|
359 |
!
|
|
360 |
|
|
361 |
generality
|
|
362 |
"return the generality value - see ArithmeticValue>>retry:coercing:"
|
|
363 |
^ 80
|
|
364 |
!
|
|
365 |
|
|
366 |
asFloat
|
|
367 |
"return a float with same value - thats me"
|
|
368 |
|
|
369 |
^ self
|
|
370 |
!
|
|
371 |
|
|
372 |
asFraction
|
|
373 |
"return a corresponding fraction
|
|
374 |
- notice, that fract asFloat asFract does not always return
|
|
375 |
a good fraction ..."
|
|
376 |
|
|
377 |
|fract digits power num denom|
|
|
378 |
|
|
379 |
"we (indirectly) use printf which knows the precision of floats"
|
|
380 |
|
|
381 |
fract := self fractionPart.
|
|
382 |
digits := fract printString copyFrom:3.
|
|
383 |
power := digits size.
|
|
384 |
num := (self - fract) asInteger.
|
|
385 |
denom := (10 raisedToInteger:power).
|
|
386 |
num := num * denom.
|
|
387 |
num := num + (Integer readFromString:digits).
|
|
388 |
^ (Fraction numerator:num denominator:denom) reduced
|
|
389 |
|
|
390 |
"0.3 asFraction"
|
|
391 |
"0.5 asFraction"
|
|
392 |
"(1/5) asFloat asFraction"
|
|
393 |
"(1/8) asFloat asFraction"
|
|
394 |
"(1/12) asFloat asFraction -> inexact result due to float errors"
|
|
395 |
"(1/13) asFloat asFraction -> inexact result due to float errors"
|
|
396 |
!
|
|
397 |
|
|
398 |
asInteger
|
|
399 |
"return an integer with same value - might truncate"
|
|
400 |
|
|
401 |
|l v sign|
|
|
402 |
|
|
403 |
%{ /* NOCONTEXT */
|
|
404 |
|
|
405 |
if ((_floatVal(self) >= (double)_MIN_INT)
|
|
406 |
&& (_floatVal(self) <= (double)_MAX_INT)) {
|
|
407 |
RETURN ( _MKSMALLINT( (INT)_floatVal(self)) );
|
|
408 |
}
|
|
409 |
%}
|
|
410 |
.
|
|
411 |
"this is stupid code - rounding errors accumulate; fix later"
|
|
412 |
|
|
413 |
sign := self sign.
|
|
414 |
v := self abs.
|
|
415 |
(v >= 10) ifTrue:[
|
|
416 |
l := (v / 10.0) asInteger * 10
|
|
417 |
] ifFalse:[
|
|
418 |
l := 0
|
|
419 |
].
|
|
420 |
v := v - ((v / 10.0) floor * 10.0) floor.
|
|
421 |
l := l + v truncated.
|
|
422 |
^ l * sign
|
|
423 |
|
|
424 |
"12345.0 asInteger"
|
|
425 |
"1e15 asInteger"
|
|
426 |
! !
|
|
427 |
|
|
428 |
!Float methodsFor:'double dispatching'!
|
|
429 |
|
|
430 |
sumFromFraction:aFraction
|
|
431 |
"sent when a fraction does not know how to add the recevier, a float"
|
|
432 |
|
|
433 |
^ (self * aFraction denominator + aFraction numerator) / aFraction denominator
|
|
434 |
!
|
|
435 |
|
|
436 |
differenceFromFraction:aFraction
|
|
437 |
"sent when a fraction does not know how to subtract the recevier, a float"
|
|
438 |
|
|
439 |
^ (self * aFraction denominator - aFraction numerator) / aFraction denominator
|
|
440 |
!
|
|
441 |
|
|
442 |
productFromFraction:aFraction
|
|
443 |
"sent when a fraction does not know how to multiply the recevier, a float"
|
|
444 |
|
|
445 |
^ self * aFraction numerator / aFraction denominator
|
|
446 |
!
|
|
447 |
|
|
448 |
quotientFromFraction:aFraction
|
|
449 |
"sent when a fraction does not know how to divide by the recevier, a float"
|
|
450 |
|
|
451 |
^ aFraction numerator / (self * aFraction denominator)
|
|
452 |
! !
|
|
453 |
|
|
454 |
!Float methodsFor:'truncation and rounding'!
|
|
455 |
|
|
456 |
truncated
|
|
457 |
"return the receiver truncated towards zero as Integer"
|
|
458 |
|
|
459 |
|val|
|
|
460 |
|
|
461 |
%{
|
|
462 |
double floor(), ceil();
|
|
463 |
double dVal;
|
|
464 |
|
|
465 |
dVal = _floatVal(self);
|
|
466 |
if (dVal < 0.0) {
|
|
467 |
dVal = ceil(dVal);
|
|
468 |
} else {
|
|
469 |
dVal = floor(dVal);
|
|
470 |
}
|
|
471 |
|
|
472 |
/*
|
|
473 |
* mhmh it seems that ST-80 is returning integers if possible.
|
|
474 |
* (at least, some pd programs expect it ...)
|
|
475 |
*/
|
|
476 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) {
|
|
477 |
RETURN ( _MKSMALLINT( (INT) dVal ) );
|
|
478 |
}
|
|
479 |
val = _MKFLOAT(dVal COMMA_CON);
|
|
480 |
%}
|
|
481 |
.
|
|
482 |
^ val asInteger
|
|
483 |
!
|
|
484 |
|
|
485 |
rounded
|
|
486 |
"return the receiver rounded to the nearest integer as integer"
|
|
487 |
|
|
488 |
|val|
|
|
489 |
|
|
490 |
%{
|
|
491 |
double floor();
|
|
492 |
double dVal;
|
|
493 |
|
|
494 |
/*
|
|
495 |
* mhmh it seems that ST-80 is returning integers if possible
|
|
496 |
* at least, some pd programs expect it ...
|
|
497 |
*/
|
|
498 |
dVal = floor(_floatVal(self) + 0.5);
|
|
499 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) {
|
|
500 |
RETURN ( _MKSMALLINT( (INT) dVal ) );
|
|
501 |
}
|
|
502 |
val = _MKFLOAT(dVal COMMA_CON);
|
|
503 |
%}
|
|
504 |
.
|
|
505 |
^ val asInteger
|
|
506 |
!
|
|
507 |
|
|
508 |
floor
|
|
509 |
"return the biggest integer-valued float less or equal to the receiver"
|
|
510 |
|
|
511 |
%{ /* NOCONTEXT */
|
|
512 |
|
|
513 |
double floor();
|
|
514 |
|
|
515 |
RETURN ( _MKFLOAT(floor(_floatVal(self)) COMMA_SND) );
|
|
516 |
%}
|
|
517 |
!
|
|
518 |
|
|
519 |
ceiling
|
|
520 |
"return the smallest integer-valued float greater or equal to the receiver"
|
|
521 |
|
|
522 |
%{ /* NOCONTEXT */
|
|
523 |
|
|
524 |
double ceil();
|
|
525 |
|
|
526 |
RETURN ( _MKFLOAT(ceil(_floatVal(self)) COMMA_SND) );
|
|
527 |
%}
|
|
528 |
!
|
|
529 |
|
|
530 |
fractionPart
|
|
531 |
"return a float with value from digits after the decimal point"
|
|
532 |
|
|
533 |
^ self - self truncated asFloat
|
|
534 |
|
|
535 |
"1234.56789 fractionPart"
|
|
536 |
"1.2345e6 fractionPart"
|
|
537 |
! !
|
|
538 |
|
|
539 |
!Float methodsFor:'mathematical functions'!
|
|
540 |
|
|
541 |
ln
|
|
542 |
"return the natural logarithm of myself"
|
|
543 |
|
|
544 |
%{ /* NOCONTEXT */
|
|
545 |
|
|
546 |
double log();
|
|
547 |
double result;
|
|
548 |
extern errno;
|
|
549 |
extern OBJ ErrorNumber;
|
|
550 |
|
|
551 |
errno = 0;
|
|
552 |
result = log(_floatVal(self));
|
|
553 |
if (errno == 0)
|
|
554 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
|
555 |
ErrorNumber = _MKSMALLINT(errno);
|
|
556 |
%}
|
|
557 |
.
|
|
558 |
DomainErrorSignal raise
|
|
559 |
!
|
|
560 |
|
|
561 |
raisedTo:aNumber
|
|
562 |
"return self raised to the power of aNumber"
|
|
563 |
|n|
|
|
564 |
|
|
565 |
n := aNumber asFloat.
|
|
566 |
%{
|
|
567 |
double pow();
|
|
568 |
double result;
|
|
569 |
extern errno;
|
|
570 |
extern OBJ ErrorNumber;
|
|
571 |
|
|
572 |
if (_isFloat(n)) {
|
|
573 |
errno = 0;
|
|
574 |
result = pow(_floatVal(self), _floatVal(n));
|
|
575 |
errno = 0; /* XXXX */
|
|
576 |
if (errno == 0)
|
|
577 |
RETURN ( _MKFLOAT(result COMMA_CON) );
|
|
578 |
ErrorNumber = _MKSMALLINT(errno);
|
|
579 |
}
|
|
580 |
%}
|
|
581 |
.
|
|
582 |
DomainErrorSignal raise
|
|
583 |
!
|
|
584 |
|
|
585 |
exp
|
|
586 |
"return e raised to the power of the receiver"
|
|
587 |
|
|
588 |
%{ /* NOCONTEXT */
|
|
589 |
|
|
590 |
double exp();
|
|
591 |
double result;
|
|
592 |
extern errno;
|
|
593 |
extern OBJ ErrorNumber;
|
|
594 |
|
|
595 |
errno = 0;
|
|
596 |
result = exp(_floatVal(self));
|
|
597 |
if (errno == 0)
|
|
598 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
|
599 |
ErrorNumber = _MKSMALLINT(errno);
|
|
600 |
%}
|
|
601 |
.
|
|
602 |
DomainErrorSignal raise
|
|
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 |
extern OBJ ErrorNumber;
|
|
614 |
|
|
615 |
errno = 0;
|
|
616 |
result = sin(_floatVal(self));
|
|
617 |
if (errno == 0)
|
|
618 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
|
619 |
ErrorNumber = _MKSMALLINT(errno);
|
|
620 |
%}
|
|
621 |
.
|
|
622 |
DomainErrorSignal raise
|
|
623 |
!
|
|
624 |
|
|
625 |
cos
|
|
626 |
"return the cosine of myself interpreted as radians"
|
|
627 |
|
|
628 |
%{ /* NOCONTEXT */
|
|
629 |
|
|
630 |
double cos();
|
|
631 |
double result;
|
|
632 |
extern errno;
|
|
633 |
extern OBJ ErrorNumber;
|
|
634 |
|
|
635 |
errno = 0;
|
|
636 |
result = cos(_floatVal(self));
|
|
637 |
if (errno == 0)
|
|
638 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
|
639 |
ErrorNumber = _MKSMALLINT(errno);
|
|
640 |
%}
|
|
641 |
.
|
|
642 |
DomainErrorSignal raise
|
|
643 |
!
|
|
644 |
|
|
645 |
tan
|
|
646 |
"return the tangent of myself interpreted as radians"
|
|
647 |
|
|
648 |
%{ /* NOCONTEXT */
|
|
649 |
|
|
650 |
double tan();
|
|
651 |
double result;
|
|
652 |
extern errno;
|
|
653 |
extern OBJ ErrorNumber;
|
|
654 |
|
|
655 |
errno = 0;
|
|
656 |
result = tan(_floatVal(self));
|
|
657 |
if (errno == 0)
|
|
658 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
|
659 |
ErrorNumber = _MKSMALLINT(errno);
|
|
660 |
%}
|
|
661 |
.
|
|
662 |
DomainErrorSignal raise
|
|
663 |
!
|
|
664 |
|
|
665 |
arcSin
|
|
666 |
"return the arcsine of myself as radians"
|
|
667 |
|
|
668 |
%{ /* NOCONTEXT */
|
|
669 |
|
|
670 |
double asin();
|
|
671 |
double result;
|
|
672 |
extern errno;
|
|
673 |
extern OBJ ErrorNumber;
|
|
674 |
|
|
675 |
errno = 0;
|
|
676 |
result = asin(_floatVal(self));
|
|
677 |
if (errno == 0)
|
|
678 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
|
679 |
ErrorNumber = _MKSMALLINT(errno);
|
|
680 |
%}
|
|
681 |
.
|
|
682 |
DomainErrorSignal raise
|
|
683 |
!
|
|
684 |
|
|
685 |
arcCos
|
|
686 |
"return the arccosine of myself as radians"
|
|
687 |
|
|
688 |
%{ /* NOCONTEXT */
|
|
689 |
|
|
690 |
double acos();
|
|
691 |
double result;
|
|
692 |
extern errno;
|
|
693 |
extern OBJ ErrorNumber;
|
|
694 |
|
|
695 |
errno = 0;
|
|
696 |
result = acos(_floatVal(self));
|
|
697 |
if (errno == 0)
|
|
698 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
|
699 |
ErrorNumber = _MKSMALLINT(errno);
|
|
700 |
%}
|
|
701 |
.
|
|
702 |
DomainErrorSignal raise
|
|
703 |
!
|
|
704 |
|
|
705 |
arcTan
|
|
706 |
"return the arctangent of myself as radians"
|
|
707 |
|
|
708 |
%{ /* NOCONTEXT */
|
|
709 |
|
|
710 |
double atan();
|
|
711 |
double result;
|
|
712 |
extern errno;
|
|
713 |
extern OBJ ErrorNumber;
|
|
714 |
|
|
715 |
errno = 0;
|
|
716 |
result = atan(_floatVal(self));
|
|
717 |
if (errno == 0)
|
|
718 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
|
719 |
ErrorNumber = _MKSMALLINT(errno);
|
|
720 |
%}
|
|
721 |
.
|
|
722 |
DomainErrorSignal raise
|
|
723 |
!
|
|
724 |
|
|
725 |
sqrt
|
|
726 |
"return the square root of myself"
|
|
727 |
|
|
728 |
%{ /* NOCONTEXT */
|
|
729 |
|
|
730 |
double sqrt();
|
|
731 |
double result;
|
|
732 |
extern errno;
|
|
733 |
extern OBJ ErrorNumber;
|
|
734 |
|
|
735 |
errno = 0;
|
|
736 |
result = sqrt(_floatVal(self));
|
|
737 |
if (errno == 0)
|
|
738 |
RETURN ( _MKFLOAT(result COMMA_SND) );
|
|
739 |
ErrorNumber = _MKSMALLINT(errno);
|
|
740 |
%}
|
|
741 |
.
|
|
742 |
DomainErrorSignal raise
|
|
743 |
! !
|
|
744 |
|
|
745 |
!Float methodsFor:'printing and storing'!
|
|
746 |
|
|
747 |
printString
|
|
748 |
"return a printed representation of the receiver"
|
|
749 |
|
|
750 |
%{ /* NOCONTEXT */
|
|
751 |
|
|
752 |
char buffer[256];
|
|
753 |
REGISTER char *cp;
|
|
754 |
#ifdef THIS_CONTEXT
|
|
755 |
OBJ sav = __thisContext;
|
|
756 |
#endif
|
|
757 |
|
|
758 |
#ifdef SYSV
|
|
759 |
sprintf(buffer, "%.6lg", _floatVal(self));
|
|
760 |
#else
|
|
761 |
sprintf(buffer, "%.6G", _floatVal(self));
|
|
762 |
#endif
|
|
763 |
|
|
764 |
#ifdef THIS_CONTEXT
|
|
765 |
__thisContext = sav;
|
|
766 |
#endif
|
|
767 |
/*
|
|
768 |
* kludge to make integral floats prints as i.0 (not i as printf does)
|
|
769 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not)
|
|
770 |
*/
|
|
771 |
for (cp = buffer; *cp; cp++) {
|
|
772 |
if ((*cp == '.') || (*cp == 'e')) break;
|
|
773 |
}
|
|
774 |
if (! *cp) {
|
|
775 |
*cp++ = '.';
|
|
776 |
*cp++ = '0';
|
|
777 |
*cp = '\0';
|
|
778 |
}
|
|
779 |
|
|
780 |
RETURN ( _MKSTRING(buffer COMMA_SND) );
|
|
781 |
%}
|
|
782 |
"1.0 printString"
|
|
783 |
"1.234 printString"
|
|
784 |
"1e10 printString"
|
|
785 |
"1.2e3 printString"
|
|
786 |
"1.2e30 printString"
|
|
787 |
!
|
|
788 |
|
|
789 |
printfPrintString:formatString
|
|
790 |
"non-portable: return a printed representation of the receiver
|
|
791 |
as specified by formatString, which is defined by printf.
|
|
792 |
If you use this, be aware, that specifying doubles differes on
|
|
793 |
systems; on SYSV machines you have to give something like %lf,
|
|
794 |
while on BSD systems the format string has to be %F."
|
|
795 |
|
|
796 |
%{ /* NOCONTEXT */
|
|
797 |
char buffer[256];
|
|
798 |
|
|
799 |
if (_isString(formatString)) {
|
|
800 |
#ifdef THIS_CONTEXT
|
|
801 |
OBJ sav = __thisContext;
|
|
802 |
#endif
|
|
803 |
sprintf(buffer, _stringVal(formatString), _floatVal(self));
|
|
804 |
#ifdef THIS_CONTEXT
|
|
805 |
__thisContext = sav;
|
|
806 |
#endif
|
|
807 |
RETURN ( _MKSTRING(buffer COMMA_SND) );
|
|
808 |
}
|
|
809 |
%}
|
|
810 |
.
|
|
811 |
self primitiveFailed
|
|
812 |
|
|
813 |
"Float pi printfPrintString:'%%lg -> %lg'"
|
|
814 |
"Float pi printfPrintString:'%%lf -> %lf'"
|
|
815 |
"Float pi printfPrintString:'%%7.5lg -> %7.5lg'"
|
|
816 |
"Float pi printfPrintString:'%%7.5lf -> %7.5lf'"
|
|
817 |
"Float pi printfPrintString:'%%G -> %G'"
|
|
818 |
"Float pi printfPrintString:'%%F -> %F'"
|
|
819 |
"Float pi printfPrintString:'%%7.5G -> %7.5G'"
|
|
820 |
"Float pi printfPrintString:'%%7.5F -> %7.5F'"
|
|
821 |
! !
|