author | Claus Gittinger <cg@exept.de> |
Sat, 21 Sep 1996 00:47:48 +0200 | |
changeset 1670 | 55cde0b3ee8e |
parent 1556 | 134d96466f5a |
child 1688 | 8a42db1eea60 |
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 |
||
1556 | 34 |
!Float class methodsFor:'documentation'! |
68 | 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 |
Number |
|
1556 | 79 |
ShortFloat Fraction Integer |
1295 | 80 |
FloatArray DoubleArray |
81 |
||
82 |
[author:] |
|
83 |
Claus Gittinger |
|
68 | 84 |
" |
85 |
! ! |
|
1 | 86 |
|
1556 | 87 |
!Float class methodsFor:'instance creation'! |
1 | 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 |
||
1556 | 107 |
!Float class methodsFor:'binary storage'! |
701 | 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 |
||
1556 | 229 |
!Float class methodsFor:'constants'! |
1 | 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 |
||
1556 | 255 |
!Float class methodsFor:'queries'! |
3 | 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 */ |
1670 | 420 |
double dVal; |
13 | 421 |
|
1670 | 422 |
dVal = __floatVal(self); |
423 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
|
424 |
RETURN ( __MKSMALLINT( (INT)dVal) ); |
|
701 | 425 |
} |
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
426 |
%}. |
701 | 427 |
^ super asInteger |
428 |
||
429 |
"12345.0 asInteger" |
|
430 |
"1e15 asInteger" |
|
1 | 431 |
! |
432 |
||
1198
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
433 |
asShortFloat |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
434 |
"return a shortFloat with same value as receiver" |
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 |
%{ /* NOCONTEXT */ |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
437 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
438 |
OBJ dummy = @global(ShortFloat); |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
439 |
OBJ newFloat; |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
440 |
float fVal = (float)__floatVal(self); |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
441 |
|
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
442 |
__qMKSFLOAT(newFloat, fVal, SENDER); |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
443 |
RETURN ( newFloat ); |
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 |
! |
284eeba6b719
docu & limited shortFloat support
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
446 |
|
701 | 447 |
coerce:aNumber |
448 |
"return aNumber converted into receivers type" |
|
1 | 449 |
|
701 | 450 |
^ aNumber asFloat |
451 |
! |
|
13 | 452 |
|
701 | 453 |
generality |
454 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
455 |
||
456 |
^ 80 |
|
1 | 457 |
! ! |
458 |
||
459 |
!Float methodsFor:'comparing'! |
|
460 |
||
461 |
< aNumber |
|
462 |
"return true, if the argument is greater" |
|
463 |
||
464 |
%{ /* NOCONTEXT */ |
|
13 | 465 |
|
250 | 466 |
if (__isSmallInteger(aNumber)) { |
1133 | 467 |
RETURN ( (__floatVal(self) < (double)(__intVal(aNumber))) ? true : false ); |
1 | 468 |
} |
283 | 469 |
if (__isFloatLike(aNumber)) { |
1133 | 470 |
RETURN ( (__floatVal(self) < __floatVal(aNumber)) ? true : false ); |
1 | 471 |
} |
472 |
%} |
|
473 |
. |
|
474 |
^ aNumber lessFromFloat:self |
|
475 |
! |
|
476 |
||
477 |
<= aNumber |
|
478 |
"return true, if the argument is greater or equal" |
|
479 |
||
480 |
%{ /* NOCONTEXT */ |
|
13 | 481 |
|
250 | 482 |
if (__isSmallInteger(aNumber)) { |
1133 | 483 |
RETURN ( (__floatVal(self) <= (double)(__intVal(aNumber))) ? true : false ); |
1 | 484 |
} |
283 | 485 |
if (__isFloatLike(aNumber)) { |
1133 | 486 |
RETURN ( (__floatVal(self) <= __floatVal(aNumber)) ? true : false ); |
1 | 487 |
} |
488 |
%} |
|
489 |
. |
|
490 |
^ self retry:#<= coercing:aNumber |
|
491 |
! |
|
492 |
||
493 |
= aNumber |
|
175 | 494 |
"return true, if the arguments value are equal by value" |
1 | 495 |
|
496 |
%{ /* NOCONTEXT */ |
|
13 | 497 |
|
250 | 498 |
if (__isSmallInteger(aNumber)) { |
1133 | 499 |
RETURN ( (__floatVal(self) == (double)(__intVal(aNumber))) ? true : false ); |
1 | 500 |
} |
283 | 501 |
if (__isFloatLike(aNumber)) { |
1133 | 502 |
RETURN ( (__floatVal(self) == __floatVal(aNumber)) ? true : false ); |
1 | 503 |
} |
504 |
%} |
|
505 |
. |
|
506 |
^ self retry:#= coercing:aNumber |
|
507 |
! |
|
508 |
||
701 | 509 |
> aNumber |
510 |
"return true, if the argument is less" |
|
1 | 511 |
|
512 |
%{ /* NOCONTEXT */ |
|
13 | 513 |
|
250 | 514 |
if (__isSmallInteger(aNumber)) { |
1133 | 515 |
RETURN ( (__floatVal(self) > (double)(__intVal(aNumber))) ? true : false ); |
1 | 516 |
} |
283 | 517 |
if (__isFloatLike(aNumber)) { |
1133 | 518 |
RETURN ( (__floatVal(self) > __floatVal(aNumber)) ? true : false ); |
1 | 519 |
} |
520 |
%} |
|
521 |
. |
|
701 | 522 |
^ self retry:#> coercing:aNumber |
523 |
! |
|
524 |
||
525 |
>= aNumber |
|
526 |
"return true, if the argument is less or equal" |
|
527 |
||
528 |
%{ /* NOCONTEXT */ |
|
529 |
||
530 |
if (__isSmallInteger(aNumber)) { |
|
1133 | 531 |
RETURN ( (__floatVal(self) >= (double)(__intVal(aNumber))) ? true : false ); |
701 | 532 |
} |
533 |
if (__isFloatLike(aNumber)) { |
|
1133 | 534 |
RETURN ( (__floatVal(self) >= __floatVal(aNumber)) ? true : false ); |
701 | 535 |
} |
536 |
%} |
|
537 |
. |
|
538 |
^ self retry:#>= coercing:aNumber |
|
92 | 539 |
! |
540 |
||
541 |
hash |
|
542 |
"return a number for hashing; redefined, since floats compare |
|
543 |
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same |
|
544 |
as 3 hash." |
|
545 |
||
305 | 546 |
|i| |
92 | 547 |
|
548 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
175 | 549 |
i := self asInteger. |
550 |
self = i ifTrue:[ |
|
551 |
^ i hash |
|
552 |
]. |
|
92 | 553 |
]. |
554 |
||
555 |
" |
|
305 | 556 |
mhmh take some of my value-bits to hash on |
92 | 557 |
" |
305 | 558 |
^ (((self basicAt:8) bitAnd:16r3F) bitShift:24) + |
559 |
((self basicAt:7) bitShift:16) + |
|
560 |
((self basicAt:6) bitShift:8) + |
|
561 |
(self basicAt:5) |
|
92 | 562 |
|
563 |
" |
|
564 |
3 hash |
|
565 |
3.0 hash |
|
566 |
3.1 hash |
|
567 |
3.14159 hash |
|
568 |
31.4159 hash |
|
569 |
3.141591 hash |
|
570 |
1.234567890123456 hash |
|
571 |
1.234567890123457 hash |
|
572 |
Set withAll:#(3 3.0 99 99.0 3.1415) |
|
573 |
" |
|
1 | 574 |
! |
575 |
||
701 | 576 |
~= aNumber |
577 |
"return true, if the arguments value are not equal" |
|
1 | 578 |
|
579 |
%{ /* NOCONTEXT */ |
|
580 |
||
701 | 581 |
if (__isSmallInteger(aNumber)) { |
1133 | 582 |
RETURN ( (__floatVal(self) != (double)(__intVal(aNumber))) ? true : false ); |
701 | 583 |
} |
584 |
if (__isFloatLike(aNumber)) { |
|
1133 | 585 |
RETURN ( (__floatVal(self) != __floatVal(aNumber)) ? true : false ); |
1 | 586 |
} |
587 |
%} |
|
588 |
. |
|
701 | 589 |
^ self retry:#~= coercing:aNumber |
1 | 590 |
! ! |
591 |
||
592 |
!Float methodsFor:'mathematical functions'! |
|
593 |
||
701 | 594 |
arcCos |
595 |
"return the arccosine of myself as radians" |
|
596 |
||
597 |
%{ /* NOCONTEXT */ |
|
598 |
||
599 |
double acos(); |
|
600 |
double result; |
|
601 |
||
602 |
errno = 0; |
|
1133 | 603 |
result = acos(__floatVal(self)); |
701 | 604 |
if (errno == 0) |
1133 | 605 |
RETURN ( __MKFLOAT(result COMMA_SND) ); |
606 |
Float_LastErrorNumber = __MKSMALLINT(errno); |
|
701 | 607 |
%} |
608 |
. |
|
609 |
^ DomainErrorSignal raise |
|
610 |
! |
|
611 |
||
612 |
arcSin |
|
613 |
"return the arcsine of myself as radians" |
|
614 |
||
615 |
%{ /* NOCONTEXT */ |
|
616 |
||
617 |
double asin(); |
|
618 |
double result; |
|
619 |
||
620 |
errno = 0; |
|
1133 | 621 |
result = asin(__floatVal(self)); |
701 | 622 |
if (errno == 0) |
1133 | 623 |
RETURN ( __MKFLOAT(result COMMA_SND) ); |
624 |
Float_LastErrorNumber = __MKSMALLINT(errno); |
|
701 | 625 |
%} |
626 |
. |
|
627 |
^ DomainErrorSignal raise |
|
628 |
! |
|
629 |
||
630 |
arcTan |
|
631 |
"return the arctangent of myself as radians" |
|
632 |
||
633 |
%{ /* NOCONTEXT */ |
|
634 |
||
635 |
double atan(); |
|
636 |
double result; |
|
637 |
||
638 |
errno = 0; |
|
1133 | 639 |
result = atan(__floatVal(self)); |
701 | 640 |
if (errno == 0) |
1133 | 641 |
RETURN ( __MKFLOAT(result COMMA_SND) ); |
642 |
Float_LastErrorNumber = __MKSMALLINT(errno); |
|
701 | 643 |
%} |
644 |
. |
|
645 |
^ DomainErrorSignal raise |
|
646 |
! |
|
647 |
||
648 |
cos |
|
649 |
"return the cosine of myself interpreted as radians" |
|
650 |
||
651 |
%{ /* NOCONTEXT */ |
|
652 |
||
653 |
double cos(); |
|
654 |
double result; |
|
655 |
||
656 |
errno = 0; |
|
1133 | 657 |
result = cos(__floatVal(self)); |
701 | 658 |
if (errno == 0) |
1133 | 659 |
RETURN ( __MKFLOAT(result COMMA_SND) ); |
660 |
Float_LastErrorNumber = __MKSMALLINT(errno); |
|
701 | 661 |
%} |
662 |
. |
|
663 |
^ DomainErrorSignal raise |
|
664 |
! |
|
665 |
||
666 |
exp |
|
667 |
"return e raised to the power of the receiver" |
|
668 |
||
669 |
%{ /* NOCONTEXT */ |
|
670 |
||
671 |
double exp(); |
|
672 |
double result; |
|
673 |
||
674 |
errno = 0; |
|
1133 | 675 |
result = exp(__floatVal(self)); |
701 | 676 |
if (errno == 0) |
1133 | 677 |
RETURN ( __MKFLOAT(result COMMA_SND) ); |
678 |
Float_LastErrorNumber = __MKSMALLINT(errno); |
|
701 | 679 |
%} |
680 |
. |
|
681 |
^ DomainErrorSignal raise |
|
682 |
! |
|
683 |
||
1 | 684 |
ln |
685 |
"return the natural logarithm of myself" |
|
686 |
||
687 |
%{ /* NOCONTEXT */ |
|
688 |
||
689 |
double log(); |
|
690 |
double result; |
|
691 |
||
692 |
errno = 0; |
|
1133 | 693 |
result = log(__floatVal(self)); |
1 | 694 |
if (errno == 0) |
1133 | 695 |
RETURN ( __MKFLOAT(result COMMA_SND) ); |
696 |
Float_LastErrorNumber = __MKSMALLINT(errno); |
|
1 | 697 |
%} |
698 |
. |
|
142 | 699 |
" |
700 |
an invalid value for logarithm |
|
701 |
" |
|
131 | 702 |
^ DomainErrorSignal raise |
1 | 703 |
! |
704 |
||
705 |
raisedTo:aNumber |
|
706 |
"return self raised to the power of aNumber" |
|
707 |
|n| |
|
708 |
||
709 |
n := aNumber asFloat. |
|
710 |
%{ |
|
711 |
double pow(); |
|
712 |
double result; |
|
713 |
||
283 | 714 |
if (__isFloatLike(n)) { |
175 | 715 |
errno = 0; |
1133 | 716 |
result = pow(__floatVal(self), __floatVal(n)); |
175 | 717 |
errno = 0; /* XXXX */ |
718 |
if (errno == 0) |
|
1133 | 719 |
RETURN ( __MKFLOAT(result COMMA_CON) ); |
720 |
Float_LastErrorNumber = __MKSMALLINT(errno); |
|
1 | 721 |
} |
722 |
%} |
|
723 |
. |
|
142 | 724 |
" |
725 |
an invalid argument (not convertable to float ?) |
|
726 |
" |
|
131 | 727 |
^ DomainErrorSignal raise |
1 | 728 |
! |
729 |
||
730 |
sin |
|
731 |
"return the sine of myself interpreted as radians" |
|
732 |
||
733 |
%{ /* NOCONTEXT */ |
|
734 |
||
735 |
double sin(); |
|
736 |
double result; |
|
737 |
||
738 |
errno = 0; |
|
1133 | 739 |
result = sin(__floatVal(self)); |
1 | 740 |
if (errno == 0) |
1133 | 741 |
RETURN ( __MKFLOAT(result COMMA_SND) ); |
742 |
Float_LastErrorNumber = __MKSMALLINT(errno); |
|
1 | 743 |
%} |
744 |
. |
|
131 | 745 |
^ DomainErrorSignal raise |
1 | 746 |
! |
747 |
||
748 |
sqrt |
|
749 |
"return the square root of myself" |
|
750 |
||
751 |
%{ /* NOCONTEXT */ |
|
752 |
||
753 |
double sqrt(); |
|
754 |
double result; |
|
755 |
||
756 |
errno = 0; |
|
1133 | 757 |
result = sqrt(__floatVal(self)); |
1 | 758 |
if (errno == 0) |
1133 | 759 |
RETURN ( __MKFLOAT(result COMMA_SND) ); |
760 |
Float_LastErrorNumber = __MKSMALLINT(errno); |
|
1 | 761 |
%} |
762 |
. |
|
131 | 763 |
^ DomainErrorSignal raise |
701 | 764 |
! |
258 | 765 |
|
701 | 766 |
tan |
767 |
"return the tangent of myself interpreted as radians" |
|
258 | 768 |
|
769 |
%{ /* NOCONTEXT */ |
|
770 |
||
701 | 771 |
double tan(); |
772 |
double result; |
|
44 | 773 |
|
701 | 774 |
errno = 0; |
1133 | 775 |
result = tan(__floatVal(self)); |
701 | 776 |
if (errno == 0) |
1133 | 777 |
RETURN ( __MKFLOAT(result COMMA_SND) ); |
778 |
Float_LastErrorNumber = __MKSMALLINT(errno); |
|
701 | 779 |
%} |
780 |
. |
|
781 |
^ DomainErrorSignal raise |
|
13 | 782 |
! ! |
783 |
||
92 | 784 |
!Float methodsFor:'printing & storing'! |
1 | 785 |
|
786 |
printString |
|
787 |
"return a printed representation of the receiver" |
|
788 |
||
789 |
%{ /* NOCONTEXT */ |
|
790 |
||
13 | 791 |
char buffer[64]; |
1 | 792 |
REGISTER char *cp; |
314 | 793 |
OBJ s; |
794 |
||
31 | 795 |
/* |
796 |
* actually only needed on sparc: since thisContext is |
|
797 |
* in a global register, which gets destroyed by printf, |
|
798 |
* manually save it here - very stupid ... |
|
799 |
*/ |
|
809
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
800 |
__BEGIN_PROTECT_REGISTERS__ |
1 | 801 |
|
802 |
#ifdef SYSV |
|
1133 | 803 |
sprintf(buffer, "%.6lg", __floatVal(self)); |
1 | 804 |
#else |
1133 | 805 |
sprintf(buffer, "%.6G", __floatVal(self)); |
1 | 806 |
#endif |
807 |
||
809
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
808 |
__END_PROTECT_REGISTERS__ |
1133 | 809 |
|
1 | 810 |
/* |
13 | 811 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
1 | 812 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
813 |
*/ |
|
814 |
for (cp = buffer; *cp; cp++) { |
|
175 | 815 |
if ((*cp == '.') || (*cp == 'e')) break; |
1 | 816 |
} |
817 |
if (! *cp) { |
|
175 | 818 |
*cp++ = '.'; |
819 |
*cp++ = '0'; |
|
820 |
*cp = '\0'; |
|
1 | 821 |
} |
822 |
||
1133 | 823 |
s = __MKSTRING(buffer COMMA_SND); |
314 | 824 |
if (s != nil) { |
343 | 825 |
RETURN (s); |
314 | 826 |
} |
827 |
%}. |
|
828 |
" |
|
829 |
memory allocation (for the new string) failed. |
|
830 |
When we arrive here, there was no memory, even after a garbage collect. |
|
831 |
This means, that the VM wanted to get some more memory from the |
|
832 |
OS, which was not kind enough to give it. |
|
833 |
Bad luck - you should increase the swap space on your machine. |
|
834 |
" |
|
835 |
^ ObjectMemory allocationFailureSignal raise. |
|
836 |
||
1 | 837 |
"1.0 printString" |
838 |
"1.234 printString" |
|
839 |
"1e10 printString" |
|
840 |
"1.2e3 printString" |
|
841 |
"1.2e30 printString" |
|
842 |
! |
|
843 |
||
844 |
printfPrintString:formatString |
|
845 |
"non-portable: return a printed representation of the receiver |
|
846 |
as specified by formatString, which is defined by printf. |
|
31 | 847 |
If you use this, be aware, that specifying doubles differs on |
1 | 848 |
systems; on SYSV machines you have to give something like %lf, |
13 | 849 |
while on BSD systems the format string has to be %F. |
31 | 850 |
Also, the resulting string may not be longer than 255 bytes - |
54 | 851 |
since thats the (static) size of the buffer. |
852 |
This method is NONSTANDARD and may be removed without notice." |
|
1 | 853 |
|
13 | 854 |
%{ /* STACK: 400 */ |
1 | 855 |
char buffer[256]; |
314 | 856 |
OBJ s; |
1 | 857 |
|
54 | 858 |
if (__isString(formatString)) { |
175 | 859 |
/* |
860 |
* actually only needed on sparc: since thisContext is |
|
861 |
* in a global register, which gets destroyed by printf, |
|
862 |
* manually save it here - very stupid ... |
|
863 |
*/ |
|
809
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
864 |
__BEGIN_PROTECT_REGISTERS__ |
31 | 865 |
|
1133 | 866 |
sprintf(buffer, __stringVal(formatString), __floatVal(self)); |
369 | 867 |
|
809
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
868 |
__END_PROTECT_REGISTERS__ |
369 | 869 |
|
1133 | 870 |
s = __MKSTRING(buffer COMMA_SND); |
343 | 871 |
if (s != nil) { |
872 |
RETURN (s); |
|
873 |
} |
|
1 | 874 |
} |
314 | 875 |
%}. |
1 | 876 |
self primitiveFailed |
877 |
||
878 |
"Float pi printfPrintString:'%%lg -> %lg'" |
|
879 |
"Float pi printfPrintString:'%%lf -> %lf'" |
|
880 |
"Float pi printfPrintString:'%%7.5lg -> %7.5lg'" |
|
881 |
"Float pi printfPrintString:'%%7.5lf -> %7.5lf'" |
|
882 |
"Float pi printfPrintString:'%%G -> %G'" |
|
883 |
"Float pi printfPrintString:'%%F -> %F'" |
|
884 |
"Float pi printfPrintString:'%%7.5G -> %7.5G'" |
|
885 |
"Float pi printfPrintString:'%%7.5F -> %7.5F'" |
|
886 |
! ! |
|
701 | 887 |
|
888 |
!Float methodsFor:'private accessing'! |
|
889 |
||
890 |
basicAt:index |
|
891 |
"return an internal byte of the float. |
|
892 |
The value returned here depends on byte order, float representation etc. |
|
893 |
Therefore, this method should be used strictly private. |
|
894 |
||
895 |
Notice: |
|
896 |
the need to redefine this method here is due to the |
|
897 |
inability of many machines to store floats in non-double aligned memory. |
|
898 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
|
899 |
and the actual float is stored at index 5 .. 12. |
|
900 |
To hide this at one place, this method knows about that, and returns |
|
901 |
values as if this filler wasnt present." |
|
902 |
||
903 |
%{ /* NOCONTEXT */ |
|
904 |
||
905 |
register int indx; |
|
906 |
unsigned char *cp; |
|
907 |
||
908 |
/* |
|
909 |
* notice the missing test for self being a nonNilObject - |
|
910 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
911 |
* and SmallInteger |
|
912 |
*/ |
|
913 |
if (__isSmallInteger(index)) { |
|
1133 | 914 |
indx = __intVal(index) - 1; |
701 | 915 |
if ((indx >= 0) && (indx < sizeof(double))) { |
1133 | 916 |
cp = (unsigned char *)(& (__FloatInstPtr(self)->f_floatvalue)); |
917 |
RETURN ( __MKSMALLINT(cp[indx] & 0xFF) ); |
|
701 | 918 |
} |
919 |
} |
|
920 |
%}. |
|
921 |
index isInteger ifFalse:[ |
|
922 |
^ self indexNotInteger |
|
923 |
]. |
|
924 |
^ self subscriptBoundsError:index |
|
925 |
! |
|
926 |
||
927 |
basicAt:index put:value |
|
928 |
"set an internal byte of the float. |
|
929 |
The value to be stored here depends on byte order, float representation etc. |
|
930 |
Therefore, this method should be used strictly private. |
|
931 |
||
932 |
Notice: |
|
933 |
the need to redefine this method here is due to the |
|
934 |
inability of many machines to store floats in non-double aligned memory. |
|
935 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
|
936 |
and the actual float is stored at index 5 .. 12. |
|
937 |
To hide this at one place, this method knows about that, and returns |
|
938 |
values as if this filler wasnt present." |
|
939 |
||
940 |
%{ /* NOCONTEXT */ |
|
941 |
register int indx, val; |
|
942 |
unsigned char *cp; |
|
943 |
||
944 |
/* |
|
945 |
* notice the missing test for self being a nonNilObject - |
|
946 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
947 |
* and SmallInteger |
|
948 |
*/ |
|
949 |
if (__bothSmallInteger(index, value)) { |
|
1133 | 950 |
val = __intVal(value); |
701 | 951 |
if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) { |
1133 | 952 |
indx = __intVal(index) - 1; |
701 | 953 |
if ((indx >= 0) && (indx < sizeof(double))) { |
1133 | 954 |
cp = (unsigned char *)(& (__FloatInstPtr(self)->f_floatvalue)); |
701 | 955 |
cp[indx] = val; |
956 |
RETURN ( value ); |
|
957 |
} |
|
958 |
} |
|
959 |
} |
|
960 |
%}. |
|
961 |
index isInteger ifFalse:[ |
|
962 |
^ self indexNotInteger |
|
963 |
]. |
|
964 |
value isInteger ifFalse:[ |
|
965 |
" |
|
966 |
the object to store should be an integer number |
|
967 |
" |
|
968 |
^ self elementNotInteger |
|
969 |
]. |
|
970 |
(value between:0 and:255) ifFalse:[ |
|
971 |
" |
|
972 |
the object to store must be a bytes value |
|
973 |
" |
|
974 |
^ self elementBoundsError |
|
975 |
]. |
|
976 |
^ self subscriptBoundsError:index |
|
977 |
! ! |
|
978 |
||
979 |
!Float methodsFor:'testing'! |
|
980 |
||
981 |
negative |
|
982 |
"return true if the receiver is less than zero" |
|
983 |
||
984 |
%{ /* NOCONTEXT */ |
|
985 |
||
1133 | 986 |
RETURN ( (__floatVal(self) < 0.0) ? true : false ); |
701 | 987 |
%} |
988 |
! |
|
989 |
||
990 |
positive |
|
991 |
"return true if the receiver is greater or equal to zero" |
|
992 |
||
993 |
%{ /* NOCONTEXT */ |
|
994 |
||
1133 | 995 |
RETURN ( (__floatVal(self) >= 0.0) ? true : false ); |
701 | 996 |
%} |
997 |
! ! |
|
998 |
||
999 |
!Float methodsFor:'truncation and rounding'! |
|
1000 |
||
1001 |
ceiling |
|
1002 |
"return the smallest integer-valued float greater or equal to the receiver" |
|
1003 |
||
1004 |
|val| |
|
1005 |
||
1006 |
%{ |
|
1007 |
double ceil(); |
|
1008 |
double dVal; |
|
1009 |
||
1010 |
/* |
|
1011 |
* ST-80 (and X3J20) returns integer. |
|
1012 |
*/ |
|
1133 | 1013 |
dVal = ceil(__floatVal(self)); |
701 | 1014 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
1133 | 1015 |
RETURN ( __MKSMALLINT( (INT) dVal ) ); |
701 | 1016 |
} |
1133 | 1017 |
val = __MKFLOAT(dVal COMMA_CON); |
701 | 1018 |
%}. |
1019 |
^ val asInteger |
|
1020 |
! |
|
1021 |
||
1022 |
floor |
|
1023 |
"return the integer nearest the receiver towards negative infinity." |
|
1024 |
||
1025 |
|val| |
|
1026 |
||
1027 |
%{ |
|
1028 |
double floor(); |
|
1029 |
double dVal; |
|
1030 |
||
1031 |
/* |
|
1032 |
* ST-80 (and X3J20) returns integer. |
|
1033 |
*/ |
|
1133 | 1034 |
dVal = floor(__floatVal(self)); |
701 | 1035 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
1133 | 1036 |
RETURN ( __MKSMALLINT( (INT) dVal ) ); |
701 | 1037 |
} |
1133 | 1038 |
val = __MKFLOAT(dVal COMMA_CON); |
701 | 1039 |
%}. |
1040 |
^ val asInteger |
|
1041 |
! |
|
1042 |
||
1043 |
rounded |
|
1044 |
"return the receiver rounded to the nearest integer as an integer" |
|
1045 |
||
1046 |
|val| |
|
1047 |
||
1048 |
%{ |
|
1049 |
double floor(); |
|
1050 |
double dVal; |
|
1051 |
||
1052 |
/* |
|
1053 |
* ST-80 (and X3J20) returns integer. |
|
1054 |
*/ |
|
1133 | 1055 |
dVal = floor(__floatVal(self) + 0.5); |
701 | 1056 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
1133 | 1057 |
RETURN ( __MKSMALLINT( (INT) dVal ) ); |
701 | 1058 |
} |
1133 | 1059 |
val = __MKFLOAT(dVal COMMA_CON); |
701 | 1060 |
%}. |
1061 |
^ val asInteger |
|
1062 |
! |
|
1063 |
||
1064 |
truncated |
|
1065 |
"return the receiver truncated towards zero as an integer" |
|
1066 |
||
1067 |
|val| |
|
1068 |
||
1069 |
%{ |
|
1070 |
double floor(), ceil(); |
|
1071 |
double dVal; |
|
1072 |
||
1133 | 1073 |
dVal = __floatVal(self); |
701 | 1074 |
if (dVal < 0.0) { |
1075 |
dVal = ceil(dVal); |
|
1076 |
} else { |
|
1077 |
dVal = floor(dVal); |
|
1078 |
} |
|
1079 |
||
1080 |
/* |
|
1081 |
* ST-80 (and X3J20) returns integer. |
|
1082 |
*/ |
|
1083 |
if ((dVal >= (double)_MIN_INT) && (dVal <= (double)_MAX_INT)) { |
|
1133 | 1084 |
RETURN ( __MKSMALLINT( (INT) dVal ) ); |
701 | 1085 |
} |
1133 | 1086 |
val = __MKFLOAT(dVal COMMA_CON); |
701 | 1087 |
%}. |
1088 |
^ val asInteger |
|
1089 |
! ! |
|
1090 |
||
1556 | 1091 |
!Float class methodsFor:'documentation'! |
701 | 1092 |
|
1093 |
version |
|
1670 | 1094 |
^ '$Header: /cvs/stx/stx/libbasic/Float.st,v 1.41 1996-09-20 22:47:43 cg Exp $' |
701 | 1095 |
! ! |