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