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