author | Stefan Vogel <sv@exept.de> |
Thu, 14 Dec 1995 23:42:02 +0100 | |
changeset 757 | 93d5f6b86e98 |
parent 701 | a309e3ef7faf |
child 809 | 5eef87c2907b |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
175 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
54 | 13 |
LimitedPrecisionReal variableByteSubclass:#Float |
701 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'LastErrorNumber' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
1 | 18 |
! |
19 |
||
701 | 20 |
!Float primitiveDefinitions! |
21 |
%{ |
|
22 |
#include <errno.h> |
|
23 |
||
24 |
/* |
|
25 |
* on some systems errno is a macro ... check for it here |
|
26 |
*/ |
|
27 |
#ifndef errno |
|
28 |
extern errno; |
|
29 |
#endif |
|
30 |
||
31 |
%} |
|
32 |
! ! |
|
33 |
||
68 | 34 |
!Float class methodsFor:'documentation'! |
35 |
||
88 | 36 |
copyright |
37 |
" |
|
38 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
175 | 39 |
All Rights Reserved |
88 | 40 |
|
41 |
This software is furnished under a license and may be used |
|
42 |
only in accordance with the terms of that license and with the |
|
43 |
inclusion of the above copyright notice. This software may not |
|
44 |
be provided or otherwise made available to, or used by, any |
|
45 |
other person. No title to or ownership of the software is |
|
46 |
hereby transferred. |
|
47 |
" |
|
48 |
! |
|
49 |
||
68 | 50 |
documentation |
51 |
" |
|
52 |
Floats represent rational numbers with limited precision. In ST/X, Float uses |
|
53 |
the underlying C-compilers double implementation, therefore instances of Float |
|
54 |
are usually represented by the 8-byte IEE double precision float format. |
|
55 |
(but there is no guaranty). |
|
1 | 56 |
|
68 | 57 |
Notice, that Floats are defined as Byte-array to prevent the garbage collector |
58 |
from going into the value ... otherwise I needed a special case in many places. |
|
59 |
||
60 |
Also notice, that ST/X Floats are what Doubles are in ST-80 - this may change |
|
61 |
in one of the next versions (at least on machines, which provide different float |
|
62 |
and double types in their C-compiler. |
|
63 |
||
64 |
WARNING: |
|
65 |
The layout of float instances is known by the runtime system and the compiler; |
|
66 |
you may not add instance variables here. |
|
67 |
Also, subclassing is complicated by the fact, that the VM creates floats, and does |
|
68 |
its float-checks by an identity compare with the Float-class. (i.e. your subclasses |
|
69 |
instances will not be recognized as float-like objects, thus mixed mode arithmetic |
|
369 | 70 |
will always coerce them, effectively slowing things down). |
71 |
||
68 | 72 |
This may be changed, to use a flag bit in the class. |
85 | 73 |
|
74 |
Class Variables: |
|
75 |
||
175 | 76 |
LastErrorNumber <Integer> value of errno (after a trig- or other math err) |
68 | 77 |
" |
78 |
! ! |
|
1 | 79 |
|
80 |
!Float class methodsFor:'instance creation'! |
|
81 |
||
82 |
basicNew |
|
83 |
"return a new float - here we return 0.0 |
|
84 |
- floats are usually NOT created this way ... |
|
85 |
Its implemented here to allow things like binary store & load |
|
86 |
of floats. (but even this support will go away eventually, its not |
|
87 |
a good idea to store the bits of a float - the reader might have a |
|
13 | 88 |
totally different representation - so floats will eventually be |
89 |
binary stored in a device independent format." |
|
1 | 90 |
|
91 |
%{ /* NOCONTEXT */ |
|
92 |
RETURN ( _MKFLOAT((0.0) COMMA_SND) ); |
|
93 |
%} |
|
94 |
! ! |
|
95 |
||
701 | 96 |
!Float class methodsFor:'binary storage'! |
97 |
||
98 |
binaryDefinitionFrom: stream manager: manager |
|
99 |
|f| |
|
100 |
||
101 |
f := self basicNew. |
|
102 |
self readBinaryIEEEDoubleFrom:stream into:f. |
|
103 |
^ f |
|
104 |
! |
|
105 |
||
106 |
readBinaryIEEEDoubleFrom:aStream into:aFloat |
|
107 |
"read the receivers value from the binary stream, aStream, |
|
108 |
interpreting the next bytes as an IEEE formatted 8-byte float" |
|
109 |
||
110 |
" |
|
111 |
this implementation is wrong: does not work on non-IEEE machines |
|
112 |
(to date all machines where ST/X is running on use |
|
113 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
114 |
" |
|
115 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
116 |
"swap the bytes" |
|
117 |
8 to:1 by:-1 do:[:i | |
|
118 |
aFloat basicAt:i put:(aStream next) |
|
119 |
]. |
|
120 |
^ self |
|
121 |
]. |
|
122 |
1 to:8 do:[:i | |
|
123 |
aFloat basicAt:i put:aStream next |
|
124 |
] |
|
125 |
! |
|
126 |
||
127 |
readBinaryIEEESingleFrom:aStream into:aFloat |
|
128 |
"read a float value from the binary stream, aStream, |
|
129 |
interpreting the next bytes as an IEEE formatted 4-byte float" |
|
130 |
||
131 |
" |
|
132 |
this implementation is wrong: does not work on non-IEEE machines |
|
133 |
(to date all machines where ST/X is running on use |
|
134 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
135 |
" |
|
136 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
137 |
"swap the bytes" |
|
138 |
8 to:4 by:-1 do:[:i | |
|
139 |
aFloat basicAt:i put:(aStream next) |
|
140 |
]. |
|
141 |
^ self |
|
142 |
]. |
|
143 |
1 to:4 do:[:i | |
|
144 |
aFloat basicAt:i put:aStream next |
|
145 |
] |
|
146 |
! |
|
147 |
||
148 |
storeBinaryIEEEDouble:aFloat on:aStream |
|
149 |
"store aFloat as an IEEE formatted 8-byte float |
|
150 |
onto the binary stream, aStream" |
|
151 |
||
152 |
" |
|
153 |
this implementation is wrong: does not work on non-IEEE machines |
|
154 |
(to date all machines where ST/X is running on use |
|
155 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
156 |
" |
|
157 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
158 |
"swap the bytes" |
|
159 |
8 to:1 by:-1 do:[:i | |
|
160 |
aStream nextPut:(aFloat basicAt:i). |
|
161 |
]. |
|
162 |
^ self |
|
163 |
]. |
|
164 |
1 to:8 do:[:i | |
|
165 |
aStream nextPut:(aFloat basicAt:i). |
|
166 |
]. |
|
167 |
! |
|
168 |
||
169 |
storeBinaryIEEESingle:aFloat on:aStream |
|
170 |
"store aFloat as an IEEE formatted 4-byte float |
|
171 |
onto the binary stream, aStream" |
|
172 |
||
173 |
" |
|
174 |
this implementation is wrong: does not work on non-IEEE machines |
|
175 |
(to date all machines where ST/X is running on use |
|
176 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
177 |
" |
|
178 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
179 |
"swap the bytes" |
|
180 |
8 to:4 by:-1 do:[:i | |
|
181 |
aStream nextPut:(aFloat basicAt:i). |
|
182 |
]. |
|
183 |
^ self |
|
184 |
]. |
|
185 |
1 to:4 do:[:i | |
|
186 |
aStream nextPut:(aFloat basicAt:i). |
|
187 |
] |
|
188 |
! ! |
|
189 |
||
1 | 190 |
!Float class methodsFor:'constants'! |
191 |
||
701 | 192 |
pi |
193 |
"return the constant pi" |
|
1 | 194 |
|
701 | 195 |
^ 3.1415926535897932384626434 |
1 | 196 |
! |
197 |
||
198 |
unity |
|
199 |
"return the neutral element for multiplication" |
|
200 |
||
201 |
^ 1.0 |
|
202 |
! |
|
203 |
||
701 | 204 |
zero |
205 |
"return the neutral element for addition" |
|
1 | 206 |
|
701 | 207 |
^ 0.0 |
1 | 208 |
! ! |
209 |
||
3 | 210 |
!Float class methodsFor:'queries'! |
211 |
||
212 |
isBuiltInClass |
|
213 |
"this class is known by the run-time-system" |
|
214 |
||
215 |
^ self == Float |
|
85 | 216 |
! |
217 |
||
218 |
lastErrorNumber |
|
219 |
"return the value of errno after an error" |
|
220 |
||
221 |
^ LastErrorNumber |
|
222 |
||
223 |
" |
|
224 |
2 arcSin. |
|
225 |
OperatingSystem errorTextForNumber:(Float lastErrorNumber) |
|
226 |
" |
|
3 | 227 |
! ! |
228 |
||
1 | 229 |
!Float methodsFor:'arithmetic'! |
230 |
||
701 | 231 |
* aNumber |
232 |
"return the product of the receiver and the argument, aNumber" |
|
233 |
||
234 |
%{ /* NOCONTEXT */ |
|
235 |
||
236 |
OBJ newFloat; |
|
237 |
double result; |
|
238 |
||
239 |
if (__isSmallInteger(aNumber)) { |
|
240 |
result = _floatVal(self) * (double)(_intVal(aNumber)); |
|
241 |
retResult: |
|
242 |
_qMKFLOAT(newFloat, result, SENDER); |
|
243 |
RETURN ( newFloat ); |
|
244 |
} |
|
245 |
if (__isFloatLike(aNumber)) { |
|
246 |
result = _floatVal(self) * _floatVal(aNumber); |
|
247 |
goto retResult; |
|
248 |
} |
|
249 |
%} |
|
250 |
. |
|
251 |
^ aNumber productFromFloat:self |
|
252 |
! |
|
253 |
||
1 | 254 |
+ aNumber |
255 |
"return the sum of the receiver and the argument, aNumber" |
|
256 |
||
257 |
%{ /* NOCONTEXT */ |
|
13 | 258 |
|
1 | 259 |
OBJ newFloat; |
260 |
double result; |
|
261 |
||
250 | 262 |
if (__isSmallInteger(aNumber)) { |
175 | 263 |
result = _floatVal(self) + (double)(_intVal(aNumber)); |
1 | 264 |
retResult: |
175 | 265 |
_qMKFLOAT(newFloat, result, SENDER); |
266 |
RETURN ( newFloat ); |
|
1 | 267 |
} |
283 | 268 |
if (__isFloatLike(aNumber)) { |
175 | 269 |
result = _floatVal(self) + _floatVal(aNumber); |
270 |
goto retResult; |
|
1 | 271 |
} |
272 |
%} |
|
273 |
. |
|
274 |
^ aNumber sumFromFloat:self |
|
275 |
! |
|
276 |
||
277 |
- aNumber |
|
278 |
"return the difference of the receiver and the argument, aNumber" |
|
279 |
||
280 |
%{ /* NOCONTEXT */ |
|
13 | 281 |
|
1 | 282 |
OBJ newFloat; |
283 |
double result; |
|
284 |
||
250 | 285 |
if (__isSmallInteger(aNumber)) { |
175 | 286 |
result = _floatVal(self) - (double)(_intVal(aNumber)); |
1 | 287 |
retResult: |
175 | 288 |
_qMKFLOAT(newFloat, result, SENDER); |
289 |
RETURN ( newFloat ); |
|
1 | 290 |
} |
283 | 291 |
if (__isFloatLike(aNumber)) { |
175 | 292 |
result = _floatVal(self) - _floatVal(aNumber); |
293 |
goto retResult; |
|
1 | 294 |
} |
295 |
%} |
|
296 |
. |
|
297 |
^ aNumber differenceFromFloat:self |
|
298 |
! |
|
299 |
||
300 |
/ aNumber |
|
301 |
"return the quotient of the receiver and the argument, aNumber" |
|
302 |
||
303 |
%{ /* NOCONTEXT */ |
|
304 |
||
305 |
OBJ newFloat; |
|
306 |
double result, val; |
|
307 |
||
250 | 308 |
if (__isSmallInteger(aNumber)) { |
175 | 309 |
if (aNumber != _MKSMALLINT(0)) { |
310 |
result = _floatVal(self) / ( (double)_intVal(aNumber)) ; |
|
1 | 311 |
retResult: |
175 | 312 |
_qMKFLOAT(newFloat, result, SENDER); |
313 |
RETURN ( newFloat ); |
|
314 |
} |
|
1 | 315 |
} else { |
305 | 316 |
if (__isFloatLike(aNumber)) { |
175 | 317 |
val = _floatVal(aNumber); |
318 |
if (val != 0.0) { |
|
319 |
result = _floatVal(self) / val; |
|
320 |
goto retResult; |
|
321 |
} |
|
322 |
} |
|
1 | 323 |
} |
324 |
%} |
|
325 |
. |
|
326 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
142 | 327 |
" |
328 |
No, you shalt not divide by zero |
|
329 |
" |
|
175 | 330 |
^ DivisionByZeroSignal raise. |
1 | 331 |
]. |
332 |
^ aNumber quotientFromFloat:self |
|
333 |
! |
|
334 |
||
335 |
negated |
|
336 |
"return myself negated" |
|
337 |
||
338 |
%{ /* NOCONTEXT */ |
|
13 | 339 |
|
3 | 340 |
OBJ newFloat; |
533
c75af3681d0f
attention qMKFLOAT macro may perform a GC - dont use floatVal
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
341 |
double rslt = - _floatVal(self); |
3 | 342 |
|
533
c75af3681d0f
attention qMKFLOAT macro may perform a GC - dont use floatVal
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
343 |
_qMKFLOAT(newFloat, rslt, SENDER); |
3 | 344 |
RETURN ( newFloat ); |
1 | 345 |
%} |
346 |
! ! |
|
347 |
||
701 | 348 |
!Float methodsFor:'binary storage'! |
349 |
||
350 |
storeBinaryDefinitionOn:stream manager:manager |
|
351 |
manager putIdOfClass:(self class) on:stream. |
|
352 |
Float storeBinaryIEEEDouble:self on:stream. |
|
353 |
! ! |
|
354 |
||
355 |
!Float methodsFor:'coercion and converting'! |
|
1 | 356 |
|
701 | 357 |
asFloat |
358 |
"return a float with same value - thats me" |
|
359 |
||
360 |
^ self |
|
361 |
! |
|
362 |
||
363 |
asInteger |
|
364 |
"return an integer with same value - might truncate" |
|
365 |
||
366 |
|l v sign| |
|
1 | 367 |
|
368 |
%{ /* NOCONTEXT */ |
|
13 | 369 |
|
701 | 370 |
if ((_floatVal(self) >= (double)_MIN_INT) |
371 |
&& (_floatVal(self) <= (double)_MAX_INT)) { |
|
372 |
RETURN ( _MKSMALLINT( (INT)_floatVal(self)) ); |
|
373 |
} |
|
1 | 374 |
%} |
701 | 375 |
. |
376 |
^ super asInteger |
|
377 |
||
378 |
"12345.0 asInteger" |
|
379 |
"1e15 asInteger" |
|
1 | 380 |
! |
381 |
||
701 | 382 |
coerce:aNumber |
383 |
"return aNumber converted into receivers type" |
|
1 | 384 |
|
701 | 385 |
^ aNumber asFloat |
386 |
! |
|
13 | 387 |
|
701 | 388 |
generality |
389 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
390 |
||
391 |
^ 80 |
|
1 | 392 |
! ! |
393 |
||
394 |
!Float methodsFor:'comparing'! |
|
395 |
||
396 |
< aNumber |
|
397 |
"return true, if the argument is greater" |
|
398 |
||
399 |
%{ /* NOCONTEXT */ |
|
13 | 400 |
|
250 | 401 |
if (__isSmallInteger(aNumber)) { |
175 | 402 |
RETURN ( (_floatVal(self) < (double)(_intVal(aNumber))) ? true : false ); |
1 | 403 |
} |
283 | 404 |
if (__isFloatLike(aNumber)) { |
175 | 405 |
RETURN ( (_floatVal(self) < _floatVal(aNumber)) ? true : false ); |
1 | 406 |
} |
407 |
%} |
|
408 |
. |
|
409 |
^ aNumber lessFromFloat:self |
|
410 |
! |
|
411 |
||
412 |
<= aNumber |
|
413 |
"return true, if the argument is greater or equal" |
|
414 |
||
415 |
%{ /* NOCONTEXT */ |
|
13 | 416 |
|
250 | 417 |
if (__isSmallInteger(aNumber)) { |
175 | 418 |
RETURN ( (_floatVal(self) <= (double)(_intVal(aNumber))) ? true : false ); |
1 | 419 |
} |
283 | 420 |
if (__isFloatLike(aNumber)) { |
175 | 421 |
RETURN ( (_floatVal(self) <= _floatVal(aNumber)) ? true : false ); |
1 | 422 |
} |
423 |
%} |
|
424 |
. |
|
425 |
^ self retry:#<= coercing:aNumber |
|
426 |
! |
|
427 |
||
428 |
= aNumber |
|
175 | 429 |
"return true, if the arguments value are equal by value" |
1 | 430 |
|
431 |
%{ /* NOCONTEXT */ |
|
13 | 432 |
|
250 | 433 |
if (__isSmallInteger(aNumber)) { |
175 | 434 |
RETURN ( (_floatVal(self) == (double)(_intVal(aNumber))) ? true : false ); |
1 | 435 |
} |
283 | 436 |
if (__isFloatLike(aNumber)) { |
175 | 437 |
RETURN ( (_floatVal(self) == _floatVal(aNumber)) ? true : false ); |
1 | 438 |
} |
439 |
%} |
|
440 |
. |
|
441 |
^ self retry:#= coercing:aNumber |
|
442 |
! |
|
443 |
||
701 | 444 |
> aNumber |
445 |
"return true, if the argument is less" |
|
1 | 446 |
|
447 |
%{ /* NOCONTEXT */ |
|
13 | 448 |
|
250 | 449 |
if (__isSmallInteger(aNumber)) { |
701 | 450 |
RETURN ( (_floatVal(self) > (double)(_intVal(aNumber))) ? true : false ); |
1 | 451 |
} |
283 | 452 |
if (__isFloatLike(aNumber)) { |
701 | 453 |
RETURN ( (_floatVal(self) > _floatVal(aNumber)) ? true : false ); |
1 | 454 |
} |
455 |
%} |
|
456 |
. |
|
701 | 457 |
^ self retry:#> coercing:aNumber |
458 |
! |
|
459 |
||
460 |
>= aNumber |
|
461 |
"return true, if the argument is less or equal" |
|
462 |
||
463 |
%{ /* NOCONTEXT */ |
|
464 |
||
465 |
if (__isSmallInteger(aNumber)) { |
|
466 |
RETURN ( (_floatVal(self) >= (double)(_intVal(aNumber))) ? true : false ); |
|
467 |
} |
|
468 |
if (__isFloatLike(aNumber)) { |
|
469 |
RETURN ( (_floatVal(self) >= _floatVal(aNumber)) ? true : false ); |
|
470 |
} |
|
471 |
%} |
|
472 |
. |
|
473 |
^ self retry:#>= coercing:aNumber |
|
92 | 474 |
! |
475 |
||
476 |
hash |
|
477 |
"return a number for hashing; redefined, since floats compare |
|
478 |
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same |
|
479 |
as 3 hash." |
|
480 |
||
305 | 481 |
|i| |
92 | 482 |
|
483 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
175 | 484 |
i := self asInteger. |
485 |
self = i ifTrue:[ |
|
486 |
^ i hash |
|
487 |
]. |
|
92 | 488 |
]. |
489 |
||
490 |
" |
|
305 | 491 |
mhmh take some of my value-bits to hash on |
92 | 492 |
" |
305 | 493 |
^ (((self basicAt:8) bitAnd:16r3F) bitShift:24) + |
494 |
((self basicAt:7) bitShift:16) + |
|
495 |
((self basicAt:6) bitShift:8) + |
|
496 |
(self basicAt:5) |
|
92 | 497 |
|
498 |
" |
|
499 |
3 hash |
|
500 |
3.0 hash |
|
501 |
3.1 hash |
|
502 |
3.14159 hash |
|
503 |
31.4159 hash |
|
504 |
3.141591 hash |
|
505 |
1.234567890123456 hash |
|
506 |
1.234567890123457 hash |
|
507 |
Set withAll:#(3 3.0 99 99.0 3.1415) |
|
508 |
" |
|
1 | 509 |
! |
510 |
||
701 | 511 |
~= aNumber |
512 |
"return true, if the arguments value are not equal" |
|
1 | 513 |
|
514 |
%{ /* NOCONTEXT */ |
|
515 |
||
701 | 516 |
if (__isSmallInteger(aNumber)) { |
517 |
RETURN ( (_floatVal(self) != (double)(_intVal(aNumber))) ? true : false ); |
|
518 |
} |
|
519 |
if (__isFloatLike(aNumber)) { |
|
520 |
RETURN ( (_floatVal(self) != _floatVal(aNumber)) ? true : false ); |
|
1 | 521 |
} |
522 |
%} |
|
523 |
. |
|
701 | 524 |
^ self retry:#~= coercing:aNumber |
1 | 525 |
! ! |
526 |
||
527 |
!Float methodsFor:'mathematical functions'! |
|
528 |
||
701 | 529 |
arcCos |
530 |
"return the arccosine of myself as radians" |
|
531 |
||
532 |
%{ /* NOCONTEXT */ |
|
533 |
||
534 |
double acos(); |
|
535 |
double result; |
|
536 |
||
537 |
errno = 0; |
|
538 |
result = acos(_floatVal(self)); |
|
539 |
if (errno == 0) |
|
540 |
RETURN ( _MKFLOAT(result COMMA_SND) ); |
|
541 |
Float_LastErrorNumber = _MKSMALLINT(errno); |
|
542 |
%} |
|
543 |
. |
|
544 |
^ DomainErrorSignal raise |
|
545 |
! |
|
546 |
||
547 |
arcSin |
|
548 |
"return the arcsine of myself as radians" |
|
549 |
||
550 |
%{ /* NOCONTEXT */ |
|
551 |
||
552 |
double asin(); |
|
553 |
double result; |
|
554 |
||
555 |
errno = 0; |
|
556 |
result = asin(_floatVal(self)); |
|
557 |
if (errno == 0) |
|
558 |
RETURN ( _MKFLOAT(result COMMA_SND) ); |
|
559 |
Float_LastErrorNumber = _MKSMALLINT(errno); |
|
560 |
%} |
|
561 |
. |
|
562 |
^ DomainErrorSignal raise |
|
563 |
! |
|
564 |
||
565 |
arcTan |
|
566 |
"return the arctangent of myself as radians" |
|
567 |
||
568 |
%{ /* NOCONTEXT */ |
|
569 |
||
570 |
double atan(); |
|
571 |
double result; |
|
572 |
||
573 |
errno = 0; |
|
574 |
result = atan(_floatVal(self)); |
|
575 |
if (errno == 0) |
|
576 |
RETURN ( _MKFLOAT(result COMMA_SND) ); |
|
577 |
Float_LastErrorNumber = _MKSMALLINT(errno); |
|
578 |
%} |
|
579 |
. |
|
580 |
^ DomainErrorSignal raise |
|
581 |
! |
|
582 |
||
583 |
cos |
|
584 |
"return the cosine of myself interpreted as radians" |
|
585 |
||
586 |
%{ /* NOCONTEXT */ |
|
587 |
||
588 |
double cos(); |
|
589 |
double result; |
|
590 |
||
591 |
errno = 0; |
|
592 |
result = cos(_floatVal(self)); |
|
593 |
if (errno == 0) |
|
594 |
RETURN ( _MKFLOAT(result COMMA_SND) ); |
|
595 |
Float_LastErrorNumber = _MKSMALLINT(errno); |
|
596 |
%} |
|
597 |
. |
|
598 |
^ DomainErrorSignal raise |
|
599 |
! |
|
600 |
||
601 |
exp |
|
602 |
"return e raised to the power of the receiver" |
|
603 |
||
604 |
%{ /* NOCONTEXT */ |
|
605 |
||
606 |
double exp(); |
|
607 |
double result; |
|
608 |
||
609 |
errno = 0; |
|
610 |
result = exp(_floatVal(self)); |
|
611 |
if (errno == 0) |
|
612 |
RETURN ( _MKFLOAT(result COMMA_SND) ); |
|
613 |
Float_LastErrorNumber = _MKSMALLINT(errno); |
|
614 |
%} |
|
615 |
. |
|
616 |
^ DomainErrorSignal raise |
|
617 |
! |
|
618 |
||
1 | 619 |
ln |
620 |
"return the natural logarithm of myself" |
|
621 |
||
622 |
%{ /* NOCONTEXT */ |
|
623 |
||
624 |
double log(); |
|
625 |
double result; |
|
626 |
||
627 |
errno = 0; |
|
628 |
result = log(_floatVal(self)); |
|
629 |
if (errno == 0) |
|
175 | 630 |
RETURN ( _MKFLOAT(result COMMA_SND) ); |
85 | 631 |
Float_LastErrorNumber = _MKSMALLINT(errno); |
1 | 632 |
%} |
633 |
. |
|
142 | 634 |
" |
635 |
an invalid value for logarithm |
|
636 |
" |
|
131 | 637 |
^ DomainErrorSignal raise |
1 | 638 |
! |
639 |
||
640 |
raisedTo:aNumber |
|
641 |
"return self raised to the power of aNumber" |
|
642 |
|n| |
|
643 |
||
644 |
n := aNumber asFloat. |
|
645 |
%{ |
|
646 |
double pow(); |
|
647 |
double result; |
|
648 |
||
283 | 649 |
if (__isFloatLike(n)) { |
175 | 650 |
errno = 0; |
651 |
result = pow(_floatVal(self), _floatVal(n)); |
|
652 |
errno = 0; /* XXXX */ |
|
653 |
if (errno == 0) |
|
654 |
RETURN ( _MKFLOAT(result COMMA_CON) ); |
|
655 |
Float_LastErrorNumber = _MKSMALLINT(errno); |
|
1 | 656 |
} |
657 |
%} |
|
658 |
. |
|
142 | 659 |
" |
660 |
an invalid argument (not convertable to float ?) |
|
661 |
" |
|
131 | 662 |
^ DomainErrorSignal raise |
1 | 663 |
! |
664 |
||
665 |
sin |
|
666 |
"return the sine of myself interpreted as radians" |
|
667 |
||
668 |
%{ /* NOCONTEXT */ |
|
669 |
||
670 |
double sin(); |
|
671 |
double result; |
|
672 |
||
673 |
errno = 0; |
|
674 |
result = sin(_floatVal(self)); |
|
675 |
if (errno == 0) |
|
175 | 676 |
RETURN ( _MKFLOAT(result COMMA_SND) ); |
85 | 677 |
Float_LastErrorNumber = _MKSMALLINT(errno); |
1 | 678 |
%} |
679 |
. |
|
131 | 680 |
^ DomainErrorSignal raise |
1 | 681 |
! |
682 |
||
683 |
sqrt |
|
684 |
"return the square root of myself" |
|
685 |
||
686 |
%{ /* NOCONTEXT */ |
|
687 |
||
688 |
double sqrt(); |
|
689 |
double result; |
|
690 |
||
691 |
errno = 0; |
|
692 |
result = sqrt(_floatVal(self)); |
|
693 |
if (errno == 0) |
|
175 | 694 |
RETURN ( _MKFLOAT(result COMMA_SND) ); |
85 | 695 |
Float_LastErrorNumber = _MKSMALLINT(errno); |
1 | 696 |
%} |
697 |
. |
|
131 | 698 |
^ DomainErrorSignal raise |
701 | 699 |
! |
258 | 700 |
|
701 | 701 |
tan |
702 |
"return the tangent of myself interpreted as radians" |
|
258 | 703 |
|
704 |
%{ /* NOCONTEXT */ |
|
705 |
||
701 | 706 |
double tan(); |
707 |
double result; |
|
44 | 708 |
|
701 | 709 |
errno = 0; |
710 |
result = tan(_floatVal(self)); |
|
711 |
if (errno == 0) |
|
712 |
RETURN ( _MKFLOAT(result COMMA_SND) ); |
|
713 |
Float_LastErrorNumber = _MKSMALLINT(errno); |
|
714 |
%} |
|
715 |
. |
|
716 |
^ DomainErrorSignal raise |
|
13 | 717 |
! ! |
718 |
||
92 | 719 |
!Float methodsFor:'printing & storing'! |
1 | 720 |
|
721 |
printString |
|
722 |
"return a printed representation of the receiver" |
|
723 |
||
724 |
%{ /* NOCONTEXT */ |
|
725 |
||
13 | 726 |
char buffer[64]; |
1 | 727 |
REGISTER char *cp; |
314 | 728 |
OBJ s; |
729 |
||
54 | 730 |
#ifdef THISCONTEXT_IN_REGISTER |
31 | 731 |
/* |
732 |
* actually only needed on sparc: since thisContext is |
|
733 |
* in a global register, which gets destroyed by printf, |
|
734 |
* manually save it here - very stupid ... |
|
735 |
*/ |
|
369 | 736 |
extern OBJ __thisContext__; |
737 |
__thisContext__ = __thisContext; |
|
54 | 738 |
#endif |
1 | 739 |
|
740 |
#ifdef SYSV |
|
741 |
sprintf(buffer, "%.6lg", _floatVal(self)); |
|
742 |
#else |
|
743 |
sprintf(buffer, "%.6G", _floatVal(self)); |
|
744 |
#endif |
|
745 |
||
54 | 746 |
#ifdef THISCONTEXT_IN_REGISTER |
369 | 747 |
__thisContext = __thisContext__; |
748 |
__thisContext__ = nil; |
|
54 | 749 |
#endif |
1 | 750 |
/* |
13 | 751 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
1 | 752 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
753 |
*/ |
|
754 |
for (cp = buffer; *cp; cp++) { |
|
175 | 755 |
if ((*cp == '.') || (*cp == 'e')) break; |
1 | 756 |
} |
757 |
if (! *cp) { |
|
175 | 758 |
*cp++ = '.'; |
759 |
*cp++ = '0'; |
|
760 |
*cp = '\0'; |
|
1 | 761 |
} |
762 |
||
314 | 763 |
s = _MKSTRING(buffer COMMA_SND); |
764 |
if (s != nil) { |
|
343 | 765 |
RETURN (s); |
314 | 766 |
} |
767 |
%}. |
|
768 |
" |
|
769 |
memory allocation (for the new string) failed. |
|
770 |
When we arrive here, there was no memory, even after a garbage collect. |
|
771 |
This means, that the VM wanted to get some more memory from the |
|
772 |
OS, which was not kind enough to give it. |
|
773 |
Bad luck - you should increase the swap space on your machine. |
|
774 |
" |
|
775 |
^ ObjectMemory allocationFailureSignal raise. |
|
776 |
||
1 | 777 |
"1.0 printString" |
778 |
"1.234 printString" |
|
779 |
"1e10 printString" |
|
780 |
"1.2e3 printString" |
|
781 |
"1.2e30 printString" |
|
782 |
! |
|
783 |
||
784 |
printfPrintString:formatString |
|
785 |
"non-portable: return a printed representation of the receiver |
|
786 |
as specified by formatString, which is defined by printf. |
|
31 | 787 |
If you use this, be aware, that specifying doubles differs on |
1 | 788 |
systems; on SYSV machines you have to give something like %lf, |
13 | 789 |
while on BSD systems the format string has to be %F. |
31 | 790 |
Also, the resulting string may not be longer than 255 bytes - |
54 | 791 |
since thats the (static) size of the buffer. |
792 |
This method is NONSTANDARD and may be removed without notice." |
|
1 | 793 |
|
13 | 794 |
%{ /* STACK: 400 */ |
1 | 795 |
char buffer[256]; |
314 | 796 |
OBJ s; |
1 | 797 |
|
54 | 798 |
if (__isString(formatString)) { |
369 | 799 |
#ifdef THISCONTEXT_IN_REGISTER |
175 | 800 |
/* |
801 |
* actually only needed on sparc: since thisContext is |
|
802 |
* in a global register, which gets destroyed by printf, |
|
803 |
* manually save it here - very stupid ... |
|
804 |
*/ |
|
379 | 805 |
extern OBJ __thisContext__; |
806 |
__thisContext__ = __thisContext; |
|
369 | 807 |
#endif |
31 | 808 |
|
175 | 809 |
sprintf(buffer, _stringVal(formatString), _floatVal(self)); |
369 | 810 |
|
811 |
#ifdef THISCONTEXT_IN_REGISTER |
|
812 |
__thisContext = __thisContext; |
|
813 |
__thisContext__ = nil; |
|
814 |
#endif |
|
815 |
||
343 | 816 |
s = _MKSTRING(buffer COMMA_SND); |
817 |
if (s != nil) { |
|
818 |
RETURN (s); |
|
819 |
} |
|
1 | 820 |
} |
314 | 821 |
%}. |
1 | 822 |
self primitiveFailed |
823 |
||
824 |
"Float pi printfPrintString:'%%lg -> %lg'" |
|
825 |
"Float pi printfPrintString:'%%lf -> %lf'" |
|
826 |
"Float pi printfPrintString:'%%7.5lg -> %7.5lg'" |
|
827 |
"Float pi printfPrintString:'%%7.5lf -> %7.5lf'" |
|
828 |
"Float pi printfPrintString:'%%G -> %G'" |
|
829 |
"Float pi printfPrintString:'%%F -> %F'" |
|
830 |
"Float pi printfPrintString:'%%7.5G -> %7.5G'" |
|
831 |
"Float pi printfPrintString:'%%7.5F -> %7.5F'" |
|
832 |
! ! |
|
701 | 833 |
|
834 |
!Float methodsFor:'private accessing'! |
|
835 |
||
836 |
basicAt:index |
|
837 |
"return an internal byte of the float. |
|
838 |
The value returned here depends on byte order, float representation etc. |
|
839 |
Therefore, this method should be used strictly private. |
|
840 |
||
841 |
Notice: |
|
842 |
the need to redefine this method here is due to the |
|
843 |
inability of many machines to store floats in non-double aligned memory. |
|
844 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
|
845 |
and the actual float is stored at index 5 .. 12. |
|
846 |
To hide this at one place, this method knows about that, and returns |
|
847 |
values as if this filler wasnt present." |
|
848 |
||
849 |
%{ /* NOCONTEXT */ |
|
850 |
||
851 |
register int indx; |
|
852 |
unsigned char *cp; |
|
853 |
||
854 |
/* |
|
855 |
* notice the missing test for self being a nonNilObject - |
|
856 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
857 |
* and SmallInteger |
|
858 |
*/ |
|
859 |
if (__isSmallInteger(index)) { |
|
860 |
indx = _intVal(index) - 1; |
|
861 |
if ((indx >= 0) && (indx < sizeof(double))) { |
|
862 |
cp = (unsigned char *)(& (_FloatInstPtr(self)->f_floatvalue)); |
|
863 |
RETURN ( _MKSMALLINT(cp[indx] & 0xFF) ); |
|
864 |
} |
|
865 |
} |
|
866 |
%}. |
|
867 |
index isInteger ifFalse:[ |
|
868 |
^ self indexNotInteger |
|
869 |
]. |
|
870 |
^ self subscriptBoundsError:index |
|
871 |
! |
|
872 |
||
873 |
basicAt:index put:value |
|
874 |
"set an internal byte of the float. |
|
875 |
The value to be stored here depends on byte order, float representation etc. |
|
876 |
Therefore, this method should be used strictly private. |
|
877 |
||
878 |
Notice: |
|
879 |
the need to redefine this method here is due to the |
|
880 |
inability of many machines to store floats in non-double aligned memory. |
|
881 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
|
882 |
and the actual float is stored at index 5 .. 12. |
|
883 |
To hide this at one place, this method knows about that, and returns |
|
884 |
values as if this filler wasnt present." |
|
885 |
||
886 |
%{ /* NOCONTEXT */ |
|
887 |
register int indx, val; |
|
888 |
unsigned char *cp; |
|
889 |
||
890 |
/* |
|
891 |
* notice the missing test for self being a nonNilObject - |
|
892 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
893 |
* and SmallInteger |
|
894 |
*/ |
|
895 |
if (__bothSmallInteger(index, value)) { |
|
896 |
val = _intVal(value); |
|
897 |
if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) { |
|
898 |
indx = _intVal(index) - 1; |
|
899 |
if ((indx >= 0) && (indx < sizeof(double))) { |
|
900 |
cp = (unsigned char *)(& (_FloatInstPtr(self)->f_floatvalue)); |
|
901 |
cp[indx] = val; |
|
902 |
RETURN ( value ); |
|
903 |
} |
|
904 |
} |
|
905 |
} |
|
906 |
%}. |
|
907 |
index isInteger ifFalse:[ |
|
908 |
^ self indexNotInteger |
|
909 |
]. |
|
910 |
value isInteger ifFalse:[ |
|
911 |
" |
|
912 |
the object to store should be an integer number |
|
913 |
" |
|
914 |
^ self elementNotInteger |
|
915 |
]. |
|
916 |
(value between:0 and:255) ifFalse:[ |
|
917 |
" |
|
918 |
the object to store must be a bytes value |
|
919 |
" |
|
920 |
^ self elementBoundsError |
|
921 |
]. |
|
922 |
^ self subscriptBoundsError:index |
|
923 |
! ! |
|
924 |
||
925 |
!Float methodsFor:'testing'! |
|
926 |
||
927 |
negative |
|
928 |
"return true if the receiver is less than zero" |
|
929 |
||
930 |
%{ /* NOCONTEXT */ |
|
931 |
||
932 |
RETURN ( (_floatVal(self) < 0.0) ? true : false ); |
|
933 |
%} |
|
934 |
! |
|
935 |
||
936 |
positive |
|
937 |
"return true if the receiver is greater or equal to zero" |
|
938 |
||
939 |
%{ /* NOCONTEXT */ |
|
940 |
||
941 |
RETURN ( (_floatVal(self) >= 0.0) ? true : false ); |
|
942 |
%} |
|
943 |
! ! |
|
944 |
||
945 |
!Float methodsFor:'truncation and rounding'! |
|
946 |
||
947 |
ceiling |
|
948 |
"return the smallest integer-valued float greater or equal to the receiver" |
|
949 |
||
950 |
|val| |
|
951 |
||
952 |
%{ |
|
953 |
double ceil(); |
|
954 |
double dVal; |
|
955 |
||
956 |
/* |
|
957 |
* ST-80 (and X3J20) returns integer. |
|
958 |
*/ |
|
959 |
dVal = ceil(_floatVal(self)); |
|
960 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
|
961 |
RETURN ( _MKSMALLINT( (INT) dVal ) ); |
|
962 |
} |
|
963 |
val = _MKFLOAT(dVal COMMA_CON); |
|
964 |
%}. |
|
965 |
^ val asInteger |
|
966 |
! |
|
967 |
||
968 |
floor |
|
969 |
"return the integer nearest the receiver towards negative infinity." |
|
970 |
||
971 |
|val| |
|
972 |
||
973 |
%{ |
|
974 |
double floor(); |
|
975 |
double dVal; |
|
976 |
||
977 |
/* |
|
978 |
* ST-80 (and X3J20) returns integer. |
|
979 |
*/ |
|
980 |
dVal = floor(_floatVal(self)); |
|
981 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
|
982 |
RETURN ( _MKSMALLINT( (INT) dVal ) ); |
|
983 |
} |
|
984 |
val = _MKFLOAT(dVal COMMA_CON); |
|
985 |
%}. |
|
986 |
^ val asInteger |
|
987 |
! |
|
988 |
||
989 |
rounded |
|
990 |
"return the receiver rounded to the nearest integer as an integer" |
|
991 |
||
992 |
|val| |
|
993 |
||
994 |
%{ |
|
995 |
double floor(); |
|
996 |
double dVal; |
|
997 |
||
998 |
/* |
|
999 |
* ST-80 (and X3J20) returns integer. |
|
1000 |
*/ |
|
1001 |
dVal = floor(_floatVal(self) + 0.5); |
|
1002 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
|
1003 |
RETURN ( _MKSMALLINT( (INT) dVal ) ); |
|
1004 |
} |
|
1005 |
val = _MKFLOAT(dVal COMMA_CON); |
|
1006 |
%}. |
|
1007 |
^ val asInteger |
|
1008 |
! |
|
1009 |
||
1010 |
truncated |
|
1011 |
"return the receiver truncated towards zero as an integer" |
|
1012 |
||
1013 |
|val| |
|
1014 |
||
1015 |
%{ |
|
1016 |
double floor(), ceil(); |
|
1017 |
double dVal; |
|
1018 |
||
1019 |
dVal = _floatVal(self); |
|
1020 |
if (dVal < 0.0) { |
|
1021 |
dVal = ceil(dVal); |
|
1022 |
} else { |
|
1023 |
dVal = floor(dVal); |
|
1024 |
} |
|
1025 |
||
1026 |
/* |
|
1027 |
* ST-80 (and X3J20) returns integer. |
|
1028 |
*/ |
|
1029 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
|
1030 |
RETURN ( _MKSMALLINT( (INT) dVal ) ); |
|
1031 |
} |
|
1032 |
val = _MKFLOAT(dVal COMMA_CON); |
|
1033 |
%}. |
|
1034 |
^ val asInteger |
|
1035 |
! ! |
|
1036 |
||
1037 |
!Float class methodsFor:'documentation'! |
|
1038 |
||
1039 |
version |
|
1040 |
^ '$Header: /cvs/stx/stx/libbasic/Float.st,v 1.32 1995-12-07 21:34:29 cg Exp $' |
|
1041 |
! ! |