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