5357
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
5278
|
3 |
"{ Package: 'stx:libbasic2' }"
|
|
4 |
|
|
5 |
"{ NameSpace: Smalltalk }"
|
|
6 |
|
|
7 |
LimitedPrecisionReal variableByteSubclass:#IEEEFloat
|
|
8 |
instanceVariableNames:'exponentSize'
|
|
9 |
classVariableNames:''
|
|
10 |
poolDictionaries:''
|
|
11 |
category:'Magnitude-Numbers'
|
|
12 |
!
|
|
13 |
|
|
14 |
!IEEEFloat class methodsFor:'documentation'!
|
|
15 |
|
|
16 |
documentation
|
|
17 |
"
|
|
18 |
Unfinished, ongoing work
|
|
19 |
|
|
20 |
soft float emulation for arbitrary IEEE float formats.
|
|
21 |
This is very very slow and should only be used when importing
|
|
22 |
funny sized floating point numbers (such as float24 or float8) from
|
|
23 |
external sources, or to simulate computations on otherwise unsupported floating pnt numbers.
|
5341
|
24 |
|
|
25 |
Create one by passing the overall number of bits and the number of exponent-bits:
|
|
26 |
IEEEFloat size:64 exponentSize:(Float numBitsInExponent)
|
5342
|
27 |
or:
|
|
28 |
1.0 asIEEEFloat
|
5341
|
29 |
will give you a (slow) variant of a regular float.
|
|
30 |
|
|
31 |
And:
|
|
32 |
IEEEFloat size:256 exponentSize:19
|
|
33 |
will give you a 256 bit ieee float.
|
|
34 |
|
5278
|
35 |
"
|
|
36 |
! !
|
|
37 |
|
|
38 |
!IEEEFloat class methodsFor:'instance creation'!
|
|
39 |
|
5324
|
40 |
fromFloat:aFloat
|
|
41 |
|nB f|
|
|
42 |
|
|
43 |
nB := aFloat byteSize.
|
|
44 |
f := self basicNew:nB.
|
|
45 |
1 to:nB do:[:i |
|
|
46 |
f basicAt:i put:(aFloat byteAt:i)
|
|
47 |
].
|
|
48 |
f exponentSize:(aFloat numBitsInExponent).
|
|
49 |
^ f
|
|
50 |
|
|
51 |
"
|
|
52 |
self fromFloat:1.0
|
|
53 |
self fromFloat:2.0
|
|
54 |
self fromFloat:1.0 asShortFloat
|
|
55 |
self fromFloat:2.0 asShortFloat
|
|
56 |
self fromFloat:1.0 asLongFloat
|
|
57 |
self fromFloat:2.0 asLongFloat
|
|
58 |
"
|
|
59 |
!
|
|
60 |
|
5346
|
61 |
fromInteger:anInteger
|
|
62 |
^ self fromFloat:anInteger asFloat
|
|
63 |
|
|
64 |
"
|
|
65 |
self fromInteger:1
|
|
66 |
self fromInteger:2
|
|
67 |
self fromInteger:1e20 asInteger
|
|
68 |
self fromInteger:1e100 asInteger
|
|
69 |
"
|
|
70 |
!
|
|
71 |
|
5278
|
72 |
size:numBits exponentSize:exponentSize
|
|
73 |
^ (self basicNew:(numBits // 8)) exponentSize:exponentSize
|
|
74 |
|
|
75 |
"
|
|
76 |
self size:256 exponentSize:19
|
5324
|
77 |
self size:16 exponentSize:5
|
|
78 |
self size:(1.0 basicSize * 8) exponentSize:(1.0 numBitsInExponent)
|
|
79 |
"
|
|
80 |
!
|
|
81 |
|
|
82 |
size:numBits exponentSize:exponentSize fraction:anInteger exponent:exp signBit:signBit
|
|
83 |
^ ((self basicNew:(numBits // 8)) exponentSize:exponentSize)
|
|
84 |
setFraction:anInteger exponent:exp signBit:signBit
|
|
85 |
|
|
86 |
"
|
|
87 |
self size:256 exponentSize:19 fromInteger:1
|
|
88 |
self size:256 exponentSize:19 fromInteger:2
|
5278
|
89 |
"
|
|
90 |
!
|
|
91 |
|
|
92 |
size:numBits exponentSize:exponentSize fromFloat:aFloat
|
|
93 |
^ ((self basicNew:(numBits // 8)) exponentSize:exponentSize) setValueFromFloat:aFloat
|
|
94 |
|
|
95 |
"
|
|
96 |
self size:256 exponentSize:19 fromFloat:1.0
|
|
97 |
self size:256 exponentSize:19 fromFloat:2.0
|
|
98 |
"
|
|
99 |
!
|
|
100 |
|
|
101 |
size:numBits exponentSize:exponentSize fromInteger:anInteger
|
|
102 |
^ ((self basicNew:(numBits // 8)) exponentSize:exponentSize) setValueFromInteger:anInteger
|
|
103 |
|
|
104 |
"
|
|
105 |
self size:256 exponentSize:19 fromInteger:1
|
|
106 |
self size:256 exponentSize:19 fromInteger:2
|
|
107 |
"
|
|
108 |
! !
|
|
109 |
|
5324
|
110 |
!IEEEFloat class methodsFor:'coercing & converting'!
|
|
111 |
|
|
112 |
coerce:aNumber
|
|
113 |
"convert the argument aNumber into an instance of the receiver (class) and return it."
|
|
114 |
|
|
115 |
^ aNumber asIEEEFloat
|
|
116 |
! !
|
5278
|
117 |
|
5324
|
118 |
!IEEEFloat class methodsFor:'constants'!
|
5278
|
119 |
|
5324
|
120 |
unity
|
|
121 |
^ 1.0
|
|
122 |
!
|
|
123 |
|
|
124 |
zero
|
|
125 |
^ 0.0
|
5278
|
126 |
! !
|
|
127 |
|
|
128 |
!IEEEFloat methodsFor:'accessing'!
|
|
129 |
|
5324
|
130 |
exponentBits
|
|
131 |
"return the bits of my exponent.
|
|
132 |
These might be biased.
|
|
133 |
My bytes are stored LSB first; thus we have to fetch the high exponent bytes."
|
|
134 |
|
5334
|
135 |
|nBytes nExpBytes e|
|
|
136 |
|
|
137 |
nBytes := self basicSize.
|
|
138 |
nExpBytes := (exponentSize + 1 + 7) // 8.
|
|
139 |
"/ seee eeee eeee mmmm mmmm ....
|
|
140 |
"/ BAD: assumes LSB
|
|
141 |
e := (self basicAt:nBytes) bitAnd:16r7F.
|
|
142 |
2 to:nExpBytes do:[:eI |
|
|
143 |
e := (e bitShift:8) bitOr:(self basicAt:nBytes + 1 - eI)
|
|
144 |
].
|
|
145 |
"/ 0eee eeee eeee mmmm
|
|
146 |
e := e rightShift:(nExpBytes * 8) - 1 "sign" - exponentSize.
|
|
147 |
"/ 0000 0eee eeee eeee
|
|
148 |
^ e
|
5324
|
149 |
|
5334
|
150 |
"
|
|
151 |
1.0 asShortFloat asIEEEFloat
|
|
152 |
(1 to:4) conform:[:i | (1.0 asShortFloat basicAt:i) = (1.0 asShortFloat asIEEEFloat basicAt:i)]
|
|
153 |
1.0 asShortFloat exponentBits
|
|
154 |
1.0 asShortFloat asIEEEFloat exponentBits
|
|
155 |
-1.0 asShortFloat exponentBits
|
|
156 |
-1.0 asShortFloat asIEEEFloat exponentBits
|
5324
|
157 |
|
5334
|
158 |
(self size:32 exponentSize:8) inspect.
|
|
159 |
(self size:64 exponentSize:11) inspect.
|
|
160 |
(self size:128 exponentSize:16) inspect.
|
|
161 |
(self size:256 exponentSize:16) inspect.
|
|
162 |
"
|
5324
|
163 |
|
5334
|
164 |
"construct a soft-shortFloat
|
|
165 |
|
|
166 |
|f sf|
|
|
167 |
sf := 1.0 asShortFloat.
|
|
168 |
f := self size:(sf basicSize * 8) exponentSize:(sf numBitsInExponent).
|
|
169 |
1 to:sf basicSize do:[:i | f basicAt:i put:(sf basicAt:i)].
|
|
170 |
f inspect
|
|
171 |
"
|
5324
|
172 |
|
|
173 |
"Modified: 12.2.1997 / 16:45:27 / cg"
|
|
174 |
!
|
|
175 |
|
5278
|
176 |
exponentSize
|
|
177 |
^ exponentSize
|
|
178 |
!
|
|
179 |
|
|
180 |
exponentSize:something
|
|
181 |
exponentSize := something.
|
|
182 |
!
|
|
183 |
|
5324
|
184 |
mantissaBits
|
|
185 |
"return the bits of my mantissa (incl. the hidden bit).
|
|
186 |
My bytes are stored LSB first."
|
|
187 |
|
|
188 |
|bits hiddenBit mask c|
|
|
189 |
|
|
190 |
bits := self digitBytes asIntegerMSB:false.
|
|
191 |
bits == 0 ifTrue:[^ 0].
|
|
192 |
|
|
193 |
hiddenBit := (1 bitShift:self numBitsInMantissa).
|
|
194 |
mask := hiddenBit-1.
|
|
195 |
c := bits bitAnd:mask.
|
|
196 |
"/ TODO: care for subnormals (exponent = 0)!!
|
|
197 |
^ c bitOr:hiddenBit.
|
|
198 |
|
|
199 |
"
|
|
200 |
(self size:32 exponentSize:8) inspect.
|
|
201 |
"
|
|
202 |
|
|
203 |
"construct a soft-shortFloat
|
|
204 |
|
|
205 |
|f sf|
|
|
206 |
sf := 1.0 asShortFloat.
|
|
207 |
f := self size:(sf basicSize * 8) exponentSize:(sf numBitsInExponent).
|
|
208 |
1 to:sf basicSize do:[:i | f basicAt:i put:(sf basicAt:i)].
|
|
209 |
f inspect
|
|
210 |
"
|
|
211 |
|
|
212 |
"Modified: 12.2.1997 / 16:45:27 / cg"
|
|
213 |
!
|
|
214 |
|
5346
|
215 |
setFraction:normalizedFraction exponent:biasedExponent signBit:signBit
|
5324
|
216 |
|bits|
|
|
217 |
|
5346
|
218 |
bits := (normalizedFraction
|
|
219 |
bitOr:(biasedExponent bitShift:self numBitsInMantissa))
|
5324
|
220 |
bitOr:(signBit bitShift:(self basicSize * 8) - 1).
|
|
221 |
1 to:self basicSize do:[:i | self basicAt:i put:(bits digitAt:i)].
|
|
222 |
|
|
223 |
"
|
|
224 |
(self size:64 exponentSize:11) setFraction:0 exponent:1024 signBit:0
|
|
225 |
"
|
|
226 |
!
|
|
227 |
|
5278
|
228 |
setValueFromInteger:intValue
|
|
229 |
"/ how many bits are there, in this int
|
|
230 |
|absValue myNumBits numBitsInNumber shift|
|
|
231 |
|
|
232 |
absValue := intValue abs.
|
|
233 |
numBitsInNumber := absValue highBit.
|
|
234 |
myNumBits := (self basicSize * 8) - 1 "sign" - exponentSize.
|
|
235 |
shift := myNumBits - numBitsInNumber.
|
|
236 |
numBitsInNumber > myNumBits ifTrue:[
|
|
237 |
self halt.
|
|
238 |
] ifFalse:[
|
|
239 |
"/ number:
|
|
240 |
"/ 1xxxxxxx...xxxxx
|
|
241 |
"/ myRep:
|
|
242 |
"/ seee...eeexxxxxxxxxx
|
|
243 |
absValue digitLength to:1 by:-1 do:[:byteIndex |
|
|
244 |
|
|
245 |
].
|
|
246 |
].
|
|
247 |
|
|
248 |
"/ cut off some bits
|
|
249 |
|
|
250 |
"Float numBitsInExponent
|
|
251 |
self size:16 exponentSize:4 fromInteger:1
|
|
252 |
self size:32 exponentSize:11 fromInteger:1
|
|
253 |
|
|
254 |
self size:256 exponentSize:19 fromInteger:1
|
|
255 |
self size:256 exponentSize:19 fromInteger:2
|
|
256 |
"
|
|
257 |
! !
|
|
258 |
|
5324
|
259 |
!IEEEFloat methodsFor:'arithmetic'!
|
|
260 |
|
|
261 |
* aNumber
|
5334
|
262 |
"/ thisContext isRecursive ifTrue:[self halt].
|
5324
|
263 |
^ aNumber productFromIEEEFloat:self
|
|
264 |
!
|
|
265 |
|
|
266 |
+ aNumber
|
5334
|
267 |
"/ thisContext isRecursive ifTrue:[self halt].
|
5324
|
268 |
^ aNumber sumFromIEEEFloat:self
|
|
269 |
|
|
270 |
"
|
|
271 |
(-2.0 asIEEEFloat)
|
|
272 |
(2.0 asIEEEFloat)
|
|
273 |
|
|
274 |
self assert:((4.0 asIEEEFloat) + (2.0 asIEEEFloat)) = (6.0 asIEEEFloat)
|
|
275 |
self assert:((4.0 asIEEEFloat) + (-2.0 asIEEEFloat)) = (2.0 asIEEEFloat)
|
|
276 |
self assert:((-4.0 asIEEEFloat) + (-2.0 asIEEEFloat)) = (-6.0 asIEEEFloat)
|
|
277 |
self assert:((-4.0 asIEEEFloat) + (2.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
278 |
|
|
279 |
self assert:((2.0 asIEEEFloat) + (4.0 asIEEEFloat)) = (6.0 asIEEEFloat)
|
|
280 |
self assert:((2.0 asIEEEFloat) + (-4.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
281 |
self assert:((-2.0 asIEEEFloat) + (-4.0 asIEEEFloat)) = (-6.0 asIEEEFloat)
|
|
282 |
self assert:((-2.0 asIEEEFloat) + (4.0 asIEEEFloat)) = (2.0 asIEEEFloat)
|
|
283 |
|
|
284 |
self assert:((IEEEFloat fromFloat:0.1) + (IEEEFloat fromFloat:-0.1)) = (IEEEFloat fromFloat:0.0)
|
|
285 |
self assert:((IEEEFloat fromFloat:-0.1) + (IEEEFloat fromFloat:0.1)) = (IEEEFloat fromFloat:0.0)
|
|
286 |
"
|
|
287 |
!
|
|
288 |
|
|
289 |
- aNumber
|
5334
|
290 |
"/ thisContext isRecursive ifTrue:[self halt].
|
5324
|
291 |
^ aNumber differenceFromIEEEFloat:self
|
|
292 |
|
|
293 |
"
|
|
294 |
self assert:((4.0 asIEEEFloat) - (2.0 asIEEEFloat)) = (2.0 asIEEEFloat)
|
|
295 |
self assert:((4.0 asIEEEFloat) - (-2.0 asIEEEFloat)) = (6.0 asIEEEFloat)
|
|
296 |
self assert:((-4.0 asIEEEFloat) - (-2.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
297 |
self assert:((-4.0 asIEEEFloat) - (2.0 asIEEEFloat)) = (-6.0 asIEEEFloat)
|
|
298 |
|
|
299 |
self assert:((2.0 asIEEEFloat) - (4.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
300 |
self assert:((2.0 asIEEEFloat) - (-4.0 asIEEEFloat)) = (6.0 asIEEEFloat)
|
|
301 |
self assert:((-2.0 asIEEEFloat) - (-4.0 asIEEEFloat)) = (2.0 asIEEEFloat)
|
|
302 |
self assert:((-2.0 asIEEEFloat) - (4.0 asIEEEFloat)) = (-6.0 asIEEEFloat)
|
|
303 |
|
|
304 |
self assert:((IEEEFloat fromFloat:0.1) - (IEEEFloat fromFloat:0.1)) = (IEEEFloat fromFloat:0.0)
|
|
305 |
self assert:((IEEEFloat fromFloat:0.1) - (IEEEFloat fromFloat:-0.1)) = (IEEEFloat fromFloat:0.2)
|
|
306 |
self assert:((IEEEFloat fromFloat:-0.1) - (IEEEFloat fromFloat:-0.1)) = (IEEEFloat fromFloat:0.0)
|
|
307 |
"
|
|
308 |
!
|
|
309 |
|
|
310 |
/ aNumber
|
5334
|
311 |
"/ thisContext isRecursive ifTrue:[self halt].
|
5324
|
312 |
^ aNumber quotientFromIEEEFloat:self
|
|
313 |
!
|
|
314 |
|
|
315 |
negated
|
|
316 |
|f highByte|
|
|
317 |
|
|
318 |
f := self copy.
|
|
319 |
highByte := self basicAt:(self basicSize).
|
|
320 |
f basicAt:(self basicSize) put:(highByte bitXor:16r80).
|
|
321 |
^ f.
|
|
322 |
|
|
323 |
"
|
|
324 |
self assert:(IEEEFloat fromFloat:2.0) negated = (IEEEFloat fromFloat:-2.0)
|
|
325 |
self assert:(IEEEFloat fromFloat:2.0) negated negated = (IEEEFloat fromFloat:2.0)
|
|
326 |
self assert:(IEEEFloat fromFloat:-2.0) negated = (IEEEFloat fromFloat:2.0)
|
|
327 |
"
|
|
328 |
! !
|
|
329 |
|
|
330 |
!IEEEFloat methodsFor:'coercing & converting'!
|
|
331 |
|
5342
|
332 |
coerce:aNumber
|
|
333 |
"convert the argument aNumber into an instance of the receiver's class and return it.
|
|
334 |
Redefined to preserve the size and exponentSize"
|
|
335 |
|
5346
|
336 |
|fraction e h nM|
|
|
337 |
|
5342
|
338 |
aNumber isInteger ifTrue:[
|
5346
|
339 |
nM := self numBitsInMantissa.
|
|
340 |
"/ cut off precision by shifting right (if h > nM)
|
|
341 |
"/ or add zeros by shifting to the left
|
|
342 |
h := aNumber highBit.
|
5355
|
343 |
h == 0 ifTrue:[
|
|
344 |
fraction := e := 0.
|
|
345 |
] ifFalse:[
|
|
346 |
fraction := aNumber bitShift:(nM - h + 1).
|
|
347 |
fraction := fraction bitAnd:((1 bitShift:nM)-1).
|
|
348 |
e := self eBias + h - 1.
|
|
349 |
].
|
5346
|
350 |
^ (self class basicNew:self basicSize) exponentSize:exponentSize;
|
5355
|
351 |
setFraction:fraction exponent:e
|
5342
|
352 |
signBit:(aNumber negative ifTrue:[1] ifFalse:[0]);
|
|
353 |
yourself
|
|
354 |
].
|
|
355 |
^ self class coerce:aNumber
|
|
356 |
|
|
357 |
"Modified: / 15-06-2017 / 10:27:03 / cg"
|
|
358 |
!
|
|
359 |
|
5324
|
360 |
generality
|
|
361 |
^ 97 "/ between OctaFloat and LargeFloat
|
|
362 |
|
|
363 |
"
|
|
364 |
1 asShortFloat generality 70
|
|
365 |
1 asFloat generality 80
|
|
366 |
1 asLongFloat generality 90
|
|
367 |
1 asQuadFloat generality
|
|
368 |
1 asQDouble generality 95
|
|
369 |
1 asLargeFloat generality 100
|
|
370 |
"
|
|
371 |
! !
|
|
372 |
|
|
373 |
!IEEEFloat methodsFor:'comparing'!
|
|
374 |
|
|
375 |
< aNumber
|
5334
|
376 |
"/ thisContext isRecursive ifTrue:[self halt].
|
5324
|
377 |
^ aNumber lessFromIEEEFloat:self
|
|
378 |
!
|
|
379 |
|
|
380 |
= aNumber
|
|
381 |
^ aNumber equalFromIEEEFloat:self
|
|
382 |
! !
|
|
383 |
|
|
384 |
!IEEEFloat methodsFor:'double dispatching'!
|
|
385 |
|
|
386 |
differenceFromIEEEFloat:anIEEEFloat
|
|
387 |
"/ anIEEEFloat - self
|
5355
|
388 |
self isZero ifTrue:[^ anIEEEFloat]. "/ otherwise, we might get a negative zero
|
5324
|
389 |
^ self negated sumFromIEEEFloat:anIEEEFloat
|
5355
|
390 |
|
|
391 |
"
|
|
392 |
0.0 + (0.0 negated)
|
|
393 |
(0.0 negated) + 0.0
|
|
394 |
0.0 asIEEEFloat + (0.0 negated)
|
|
395 |
(0.0 asIEEEFloat negated) + 0.0
|
|
396 |
0.0 asIEEEFloat + (0.0 asIEEEFloat negated)
|
|
397 |
(0.0 asIEEEFloat negated) + 0.0 asIEEEFloat
|
|
398 |
"
|
5324
|
399 |
!
|
|
400 |
|
|
401 |
equalFromIEEEFloat:anIEEEFloat
|
5355
|
402 |
|nBytes m1 m2 nM1 nM2 e1 e2|
|
5334
|
403 |
|
5355
|
404 |
nBytes := self basicSize.
|
|
405 |
anIEEEFloat basicSize == nBytes ifTrue:[
|
5324
|
406 |
anIEEEFloat exponentSize == exponentSize ifTrue:[
|
5355
|
407 |
1 to:nBytes-1 do:[:i |
|
5324
|
408 |
(self basicAt:i) = (anIEEEFloat basicAt:i) ifFalse:[^ false].
|
|
409 |
].
|
5357
|
410 |
"/ care for negative zero
|
5355
|
411 |
(self basicAt:nBytes) = (anIEEEFloat basicAt:nBytes) ifFalse:[
|
|
412 |
((self basicAt:nBytes) bitAnd:16r7F) == 0 ifTrue:[
|
|
413 |
((anIEEEFloat basicAt:nBytes) bitAnd:16r7F) == 0 ifTrue:[
|
|
414 |
^ true.
|
|
415 |
].
|
|
416 |
].
|
|
417 |
^ false
|
|
418 |
].
|
5324
|
419 |
^ true.
|
|
420 |
].
|
|
421 |
].
|
5334
|
422 |
|
5324
|
423 |
"/ more complicated compare
|
5334
|
424 |
|
5355
|
425 |
self negative = anIEEEFloat negative ifFalse:[
|
|
426 |
^ self isZero and:[anIEEEFloat isZero]
|
|
427 |
].
|
5334
|
428 |
|
|
429 |
"/ bring to same exponent, add m1 + m2, normalize
|
|
430 |
|
|
431 |
m1 := anIEEEFloat mantissaBits.
|
|
432 |
m2 := self mantissaBits.
|
|
433 |
|
|
434 |
nM1 := anIEEEFloat numBitsInMantissa.
|
|
435 |
nM2 := self numBitsInMantissa.
|
|
436 |
nM1 ~= nM2 ifTrue:[
|
|
437 |
nM1 > nM2 ifTrue:[
|
|
438 |
m2 := m2 bitShift:(nM1-nM2).
|
|
439 |
] ifFalse:[
|
|
440 |
m1 := m1 bitShift:(nM2-nM1).
|
|
441 |
].
|
|
442 |
].
|
|
443 |
m1 = m2 ifFalse:[^ false].
|
|
444 |
|
|
445 |
e1 := anIEEEFloat exponentBits - anIEEEFloat eBias.
|
|
446 |
e2 := self exponentBits - self eBias.
|
|
447 |
e1 = e2 ifFalse:[^ false].
|
|
448 |
|
|
449 |
^ true.
|
5324
|
450 |
!
|
|
451 |
|
|
452 |
lessFromIEEEFloat:anIEEEFloat
|
5357
|
453 |
|m1 m2 nM1 nM2 e1 e2 meNegative mySize myByte otherByte isLess|
|
5342
|
454 |
|
5357
|
455 |
meNegative := self negative.
|
|
456 |
meNegative = anIEEEFloat negative ifFalse:[
|
5342
|
457 |
^ anIEEEFloat negative
|
|
458 |
].
|
5324
|
459 |
|
5357
|
460 |
"/ same sized floats can be compared easily
|
|
461 |
"/ (but care for the negative 0)
|
|
462 |
mySize := self basicSize.
|
|
463 |
mySize == anIEEEFloat basicSize ifTrue:[
|
5342
|
464 |
anIEEEFloat exponentSize == exponentSize ifTrue:[
|
5357
|
465 |
"/ ignore the sign bit
|
|
466 |
myByte := (self basicAt:mySize) bitAnd:16r7F.
|
|
467 |
otherByte := (anIEEEFloat basicAt:mySize) bitAnd:16r7F.
|
|
468 |
otherByte < myByte ifTrue:[
|
|
469 |
^ meNegative not
|
|
470 |
].
|
|
471 |
otherByte > myByte ifTrue:[
|
|
472 |
^ meNegative
|
5342
|
473 |
].
|
5357
|
474 |
mySize-1 to:1 by:-1 do:[:i |
|
|
475 |
myByte := self basicAt:i.
|
|
476 |
otherByte := anIEEEFloat basicAt:i.
|
|
477 |
otherByte < myByte ifTrue:[
|
|
478 |
^ meNegative not
|
|
479 |
].
|
|
480 |
otherByte > myByte ifTrue:[
|
|
481 |
^ meNegative
|
|
482 |
].
|
|
483 |
].
|
|
484 |
"/ same
|
5342
|
485 |
^ false.
|
|
486 |
].
|
5324
|
487 |
].
|
5342
|
488 |
|
|
489 |
"/ more complicated compare
|
|
490 |
|
|
491 |
"/ bring to same exponent, add m1 + m2, normalize
|
|
492 |
|
|
493 |
m1 := anIEEEFloat mantissaBits.
|
|
494 |
m2 := self mantissaBits.
|
|
495 |
|
|
496 |
nM1 := anIEEEFloat numBitsInMantissa.
|
|
497 |
nM2 := self numBitsInMantissa.
|
|
498 |
nM1 ~= nM2 ifTrue:[
|
|
499 |
nM1 > nM2 ifTrue:[
|
|
500 |
m2 := m2 bitShift:(nM1-nM2).
|
|
501 |
] ifFalse:[
|
|
502 |
m1 := m1 bitShift:(nM2-nM1).
|
|
503 |
].
|
|
504 |
].
|
|
505 |
|
|
506 |
e1 := anIEEEFloat exponent.
|
|
507 |
e2 := self exponent.
|
5357
|
508 |
isLess := e1 = e2 ifTrue:[m1 < m2] ifFalse:[e1 < e2].
|
|
509 |
meNegative ifTrue:[^ isLess not].
|
|
510 |
^ isLess.
|
|
511 |
|
|
512 |
"
|
|
513 |
self assert:(1.0 asIEEEFloat < 2.0 asIEEEFloat).
|
|
514 |
self assert:(1.0 asIEEEFloat < 1.0 asIEEEFloat) not.
|
|
515 |
self assert:(1.0 asIEEEFloat < 0.0 asIEEEFloat) not.
|
|
516 |
self assert:(1.0 asIEEEFloat < -0.0 asIEEEFloat) not.
|
|
517 |
|
|
518 |
self assert:(-1.0 asIEEEFloat < 2.0 asIEEEFloat).
|
|
519 |
self assert:(-1.0 asIEEEFloat < 1.0 asIEEEFloat).
|
|
520 |
self assert:(-1.0 asIEEEFloat < 0.0 asIEEEFloat).
|
|
521 |
self assert:(-1.0 asIEEEFloat < -0.0 asIEEEFloat).
|
|
522 |
|
|
523 |
self assert:(1.0 asIEEEFloat < -2.0 asIEEEFloat) not.
|
|
524 |
self assert:(1.0 asIEEEFloat < -1.0 asIEEEFloat) not.
|
|
525 |
self assert:(1.0 asIEEEFloat < 0.0 asIEEEFloat) not.
|
|
526 |
self assert:(1.0 asIEEEFloat < -0.0 asIEEEFloat) not.
|
|
527 |
|
|
528 |
self assert:(-1.0 asIEEEFloat < -2.0 asIEEEFloat) not.
|
|
529 |
self assert:(-1.0 asIEEEFloat < -1.0 asIEEEFloat) not.
|
|
530 |
self assert:(-1.0 asIEEEFloat < 0.0 asIEEEFloat).
|
|
531 |
self assert:(-1.0 asIEEEFloat < -0.0 asIEEEFloat).
|
|
532 |
"
|
5324
|
533 |
!
|
|
534 |
|
|
535 |
productFromIEEEFloat:anIEEEFloat
|
|
536 |
"/ anIEEEFloat * self
|
|
537 |
|
5334
|
538 |
|m1 m2 e1 e2 m e signBit hi shift nM1 nM2 nM|
|
5331
|
539 |
|
5324
|
540 |
"/ m1 * m2 / e1 + e2
|
|
541 |
|
|
542 |
m1 := anIEEEFloat mantissaBits.
|
|
543 |
m2 := self mantissaBits.
|
5334
|
544 |
|
|
545 |
nM1 := anIEEEFloat numBitsInMantissa.
|
|
546 |
nM2 := nM := self numBitsInMantissa.
|
|
547 |
nM1 ~= nM2 ifTrue:[
|
|
548 |
nM1 > nM2 ifTrue:[
|
|
549 |
m2 := m2 bitShift:(nM1-nM2).
|
|
550 |
nM := nM1
|
|
551 |
] ifFalse:[
|
|
552 |
m1 := m1 bitShift:(nM2-nM1).
|
|
553 |
nM := nM2
|
|
554 |
].
|
|
555 |
].
|
|
556 |
|
5324
|
557 |
e1 := anIEEEFloat exponentBits.
|
|
558 |
e2 := self exponentBits.
|
|
559 |
e := (e1 - anIEEEFloat eBias) + (e2 - self eBias).
|
|
560 |
|
|
561 |
"/ 1xxxx....xxxx * 1xxxx....xxxx
|
|
562 |
m := m1 * m2.
|
|
563 |
"/ now have nB bits too many
|
5334
|
564 |
e := e - nM.
|
5324
|
565 |
|
|
566 |
signBit := (self negative == anIEEEFloat negative) ifTrue:[0] ifFalse:[1].
|
|
567 |
m == 0 ifTrue:[self halt].
|
|
568 |
|
|
569 |
hi := m highBit.
|
5334
|
570 |
shift := (nM + 1) - hi.
|
5324
|
571 |
m := m bitShift:shift.
|
|
572 |
e := e - shift.
|
5334
|
573 |
m := m bitAnd:(1 bitShift:nM)-1.
|
5324
|
574 |
|
|
575 |
e := e + self eBias.
|
|
576 |
^ self class
|
|
577 |
size:(self basicSize*8) exponentSize:exponentSize
|
|
578 |
fraction:m exponent:e signBit:signBit.
|
|
579 |
|
|
580 |
"
|
|
581 |
self assert:((IEEEFloat fromFloat:1.0) * (IEEEFloat fromFloat:1.0)) = (IEEEFloat fromFloat:1.0)
|
|
582 |
self assert:((IEEEFloat fromFloat:1.0) * (IEEEFloat fromFloat:1.0)) exponent = 1.0 exponent
|
|
583 |
self assert:((IEEEFloat fromFloat:1.0) * (IEEEFloat fromFloat:1.0)) mantissaBits = 1.0 mantissaBits
|
|
584 |
|
|
585 |
self assert:((4.0 asIEEEFloat) * (2.0 asIEEEFloat)) = (8.0 asIEEEFloat)
|
|
586 |
self assert:((-4.0 asIEEEFloat) * (2.0 asIEEEFloat)) = (-8.0 asIEEEFloat)
|
|
587 |
self assert:((4.0 asIEEEFloat) * (-2.0 asIEEEFloat)) = (-8.0 asIEEEFloat)
|
|
588 |
self assert:((-4.0 asIEEEFloat) * (-2.0 asIEEEFloat)) = (8.0 asIEEEFloat)
|
|
589 |
|
|
590 |
self assert:((IEEEFloat fromFloat:100.0) * (IEEEFloat fromFloat:100.0)) = (IEEEFloat fromFloat:10000.0)
|
|
591 |
self assert:((IEEEFloat fromFloat:1e-20) * (IEEEFloat fromFloat:1e20)) = (IEEEFloat fromFloat:(1e-20 * 1e20))
|
|
592 |
"
|
|
593 |
!
|
|
594 |
|
|
595 |
quotientFromIEEEFloat:anIEEEFloat
|
|
596 |
"/ anIEEEFloat / self
|
|
597 |
|
5334
|
598 |
|m1 m2 e1 e2 m e signBit nM1 nM2 nM hi shift|
|
5331
|
599 |
|
5324
|
600 |
"/ m1 / m2 / e1 - e2
|
|
601 |
|
5346
|
602 |
m1 := anIEEEFloat mantissaBits.
|
|
603 |
m1 == 0 ifTrue:[^ anIEEEFloat ].
|
5324
|
604 |
m2 := self mantissaBits.
|
5334
|
605 |
|
|
606 |
nM1 := anIEEEFloat numBitsInMantissa.
|
|
607 |
nM2 := nM := self numBitsInMantissa.
|
|
608 |
nM1 ~= nM2 ifTrue:[
|
|
609 |
nM1 > nM2 ifTrue:[
|
|
610 |
m2 := m2 bitShift:(nM1-nM2).
|
|
611 |
nM := nM1
|
|
612 |
] ifFalse:[
|
|
613 |
m1 := m1 bitShift:(nM2-nM1).
|
|
614 |
nM := nM2
|
|
615 |
].
|
|
616 |
].
|
|
617 |
|
5324
|
618 |
e1 := anIEEEFloat exponentBits.
|
|
619 |
e2 := self exponentBits.
|
|
620 |
e := (e1 - anIEEEFloat eBias) - (e2 - self eBias).
|
|
621 |
|
|
622 |
"/ 1xxxx....xxxx / 1xxxx....xxxx
|
5334
|
623 |
m := (m1 bitShift:nM) // m2.
|
5324
|
624 |
"/ rest := m1 \\ m2.
|
|
625 |
|
|
626 |
signBit := (self negative == anIEEEFloat negative) ifTrue:[0] ifFalse:[1].
|
|
627 |
m == 0 ifTrue:[self halt].
|
|
628 |
|
|
629 |
hi := m highBit.
|
5334
|
630 |
shift := (nM + 1) - hi.
|
5324
|
631 |
m := m bitShift:shift.
|
|
632 |
e := e - shift.
|
5334
|
633 |
m := m bitAnd:(1 bitShift:nM)-1.
|
5324
|
634 |
|
|
635 |
e := e + self eBias.
|
|
636 |
^ self class
|
|
637 |
size:(self basicSize*8) exponentSize:exponentSize
|
|
638 |
fraction:m exponent:e signBit:signBit.
|
|
639 |
|
|
640 |
"
|
|
641 |
self assert:((IEEEFloat fromFloat:1.0) / (IEEEFloat fromFloat:1.0)) = (IEEEFloat fromFloat:1.0)
|
|
642 |
self assert:((IEEEFloat fromFloat:1.0) / (IEEEFloat fromFloat:1.0)) exponent = 1.0 exponent
|
|
643 |
self assert:((IEEEFloat fromFloat:1.0) / (IEEEFloat fromFloat:1.0)) mantissaBits = 1.0 mantissaBits
|
|
644 |
|
|
645 |
self assert:((4.0 asIEEEFloat) / (2.0 asIEEEFloat)) = (2.0 asIEEEFloat)
|
|
646 |
self assert:((-4.0 asIEEEFloat) / (2.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
647 |
self assert:((4.0 asIEEEFloat) / (-2.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
648 |
self assert:((-4.0 asIEEEFloat) / (-2.0 asIEEEFloat)) = (2.0 asIEEEFloat)
|
|
649 |
|
|
650 |
self assert:((IEEEFloat fromFloat:100.0) / (IEEEFloat fromFloat:2.0)) = (IEEEFloat fromFloat:50.0)
|
|
651 |
self assert:((IEEEFloat fromFloat:1e-20) / (IEEEFloat fromFloat:1e20)) = (IEEEFloat fromFloat:(1e-20 / 1e20))
|
|
652 |
"
|
|
653 |
!
|
|
654 |
|
|
655 |
sumFromIEEEFloat:anIEEEFloat
|
|
656 |
"/ anIEEEFloat + self
|
|
657 |
|
5334
|
658 |
|m1 m2 e1 e2 m e signBit hi shift nM1 nM2 nM|
|
5331
|
659 |
|
5324
|
660 |
"/ bring to same exponent, add m1 + m2, normalize
|
|
661 |
|
5355
|
662 |
m1 := anIEEEFloat mantissaBits.
|
|
663 |
m1 == 0 ifTrue:[
|
|
664 |
"/ always produce a positive zero
|
|
665 |
self isZero ifTrue:[
|
|
666 |
self negative ifTrue:[ ^ self copy basicAt:(self basicSize) put:0; yourself ].
|
|
667 |
].
|
|
668 |
^ self
|
|
669 |
].
|
|
670 |
m2 := self mantissaBits.
|
|
671 |
m2 == 0 ifTrue:[
|
|
672 |
anIEEEFloat isZero ifTrue:[
|
|
673 |
anIEEEFloat negative ifTrue:[ ^ anIEEEFloat copy basicAt:(anIEEEFloat basicSize) put:0; yourself].
|
|
674 |
].
|
|
675 |
^ anIEEEFloat
|
|
676 |
].
|
5334
|
677 |
|
|
678 |
nM1 := anIEEEFloat numBitsInMantissa.
|
|
679 |
nM2 := nM := self numBitsInMantissa.
|
|
680 |
nM1 ~= nM2 ifTrue:[
|
|
681 |
nM1 > nM2 ifTrue:[
|
|
682 |
m2 := m2 bitShift:(nM1-nM2).
|
|
683 |
nM := nM1
|
|
684 |
] ifFalse:[
|
|
685 |
m1 := m1 bitShift:(nM2-nM1).
|
|
686 |
nM := nM2
|
|
687 |
].
|
|
688 |
].
|
|
689 |
|
5331
|
690 |
e1 := anIEEEFloat exponentBits - anIEEEFloat eBias.
|
|
691 |
e2 := self exponentBits - self eBias.
|
5324
|
692 |
e := e1.
|
|
693 |
e1 = e2 ifFalse:[
|
|
694 |
e1 > e2 ifTrue:[
|
|
695 |
m2 := m2 rightShift:(e1 - e2).
|
|
696 |
e := e1.
|
|
697 |
] ifFalse:[
|
|
698 |
"/ e2 > e1
|
|
699 |
m1 := m1 rightShift:(e2 - e1).
|
|
700 |
e := e2.
|
|
701 |
]
|
|
702 |
].
|
|
703 |
|
|
704 |
signBit := 0.
|
|
705 |
"/ ok, add the mantissae
|
|
706 |
self negative ifTrue:[
|
|
707 |
anIEEEFloat negative ifTrue:[
|
|
708 |
"/ - (anIEEEFloat + self)
|
|
709 |
m := m1 + m2.
|
|
710 |
signBit := 1.
|
|
711 |
] ifFalse:[
|
|
712 |
"/ anIEEEFloat - self
|
|
713 |
m := m1 - m2
|
|
714 |
].
|
|
715 |
] ifFalse:[
|
|
716 |
anIEEEFloat negative ifTrue:[
|
|
717 |
"/ self - anIEEEFloat
|
|
718 |
m := m2 - m1.
|
|
719 |
] ifFalse:[
|
|
720 |
"/ (anIEEEFloat + self)
|
|
721 |
m := m1 + m2.
|
|
722 |
].
|
|
723 |
].
|
|
724 |
m == 0 ifTrue:[^ self class zero].
|
|
725 |
m < 0 ifTrue:[
|
|
726 |
m := m negated.
|
|
727 |
signBit := 1-signBit
|
|
728 |
].
|
|
729 |
|
|
730 |
hi := m highBit.
|
5334
|
731 |
shift := (nM + 1) - hi.
|
5324
|
732 |
m := m bitShift:shift.
|
|
733 |
e := e - shift.
|
5334
|
734 |
m := m bitAnd:(1 bitShift:nM)-1.
|
5324
|
735 |
|
|
736 |
^ self class
|
|
737 |
size:(self basicSize*8) exponentSize:exponentSize
|
5334
|
738 |
fraction:m exponent:(e + self eBias) signBit:signBit.
|
5324
|
739 |
|
|
740 |
"
|
|
741 |
seee eeee eeee mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
|
|
742 |
0100 0000 0000 0000 0000 ...... 2.0
|
|
743 |
0011 1111 1111 0000 0000 ...... 1.0
|
|
744 |
0400 0000 0001 0000 0000 ...... 4.0
|
|
745 |
|
|
746 |
self assert:(IEEEFloat fromFloat:1.0) exponent = 1.0 exponent.
|
|
747 |
self assert:(IEEEFloat fromFloat:1.0) mantissaBits = 1.0 mantissaBits.
|
|
748 |
self assert:(IEEEFloat fromFloat:1.0) eBias = 1.0 eBias.
|
|
749 |
|
|
750 |
self assert:(IEEEFloat fromFloat:2.0) exponent = 2.0 exponent.
|
|
751 |
self assert:(IEEEFloat fromFloat:2.0) mantissaBits = 2.0 mantissaBits.
|
|
752 |
self assert:(IEEEFloat fromFloat:2.0) eBias = 2.0 eBias.
|
|
753 |
|
|
754 |
self assert:(IEEEFloat fromFloat:4.0) exponent = 4.0 exponent.
|
|
755 |
self assert:(IEEEFloat fromFloat:4.0) mantissaBits = 4.0 mantissaBits.
|
|
756 |
self assert:(IEEEFloat fromFloat:4.0) eBias = 4.0 eBias.
|
|
757 |
|
|
758 |
self assert:(IEEEFloat fromFloat:3.0) exponent = 3.0 exponent.
|
|
759 |
self assert:(IEEEFloat fromFloat:3.0) mantissaBits = 3.0 mantissaBits.
|
|
760 |
self assert:(IEEEFloat fromFloat:3.0) eBias = 3.0 eBias.
|
|
761 |
|
|
762 |
self assert:(IEEEFloat fromFloat:10.0) exponent = 10.0 exponent.
|
|
763 |
self assert:(IEEEFloat fromFloat:10.0) mantissaBits = 10.0 mantissaBits.
|
|
764 |
self assert:(IEEEFloat fromFloat:10.0) eBias = 10.0 eBias.
|
|
765 |
|
|
766 |
self assert:(IEEEFloat fromFloat:1e-3) exponent = 1e-3 exponent.
|
|
767 |
self assert:(IEEEFloat fromFloat:1e-3) mantissaBits = 1e-3 mantissaBits.
|
|
768 |
self assert:(IEEEFloat fromFloat:1e-3) eBias = 1e-3 eBias.
|
|
769 |
|
|
770 |
(IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:1.0) = (IEEEFloat fromFloat:2.0)
|
|
771 |
((IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:1.0)) exponent = 2.0 exponent
|
|
772 |
((IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:1.0)) mantissaBits = 2.0 mantissaBits
|
|
773 |
|
|
774 |
self assert:((-4.0 asIEEEFloat) + (2.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
775 |
|
|
776 |
(IEEEFloat fromFloat:2.0)
|
|
777 |
self assert:((IEEEFloat fromFloat:2.0) + (IEEEFloat fromFloat:2.0)) = (IEEEFloat fromFloat:4.0)
|
|
778 |
self assert:((IEEEFloat fromFloat:4.0) + (IEEEFloat fromFloat:4.0)) = (IEEEFloat fromFloat:8.0)
|
|
779 |
self assert:((IEEEFloat fromFloat:0.0) + (IEEEFloat fromFloat:1.0)) = (IEEEFloat fromFloat:1.0)
|
|
780 |
self assert:((IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:0.0)) = (IEEEFloat fromFloat:1.0)
|
|
781 |
self assert:((IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:2.0)) = (IEEEFloat fromFloat:3.0)
|
|
782 |
self assert:((IEEEFloat fromFloat:100.0) + (IEEEFloat fromFloat:2.0)) = (IEEEFloat fromFloat:102.0)
|
|
783 |
self assert:((IEEEFloat fromFloat:2.0) + (IEEEFloat fromFloat:100.0)) = (IEEEFloat fromFloat:102.0)
|
|
784 |
|
|
785 |
self assert:((IEEEFloat fromFloat:4.0) + (IEEEFloat fromFloat:-2.0)) = (IEEEFloat fromFloat:2.0)
|
|
786 |
self assert:((IEEEFloat fromFloat:8.0) + (IEEEFloat fromFloat:-2.0)) = (IEEEFloat fromFloat:6.0)
|
|
787 |
self assert:((IEEEFloat fromFloat:100.0) + (IEEEFloat fromFloat:-2.0)) = (IEEEFloat fromFloat:98.0)
|
|
788 |
self assert:((IEEEFloat fromFloat:-2.0) + (IEEEFloat fromFloat:100.0)) = (IEEEFloat fromFloat:98.0)
|
|
789 |
self assert:((IEEEFloat fromFloat:-2.0) + (IEEEFloat fromFloat:-100.0)) = (IEEEFloat fromFloat:-102.0)
|
|
790 |
|
|
791 |
self assert:((IEEEFloat fromFloat:-0.1) + (IEEEFloat fromFloat:0.1)) = (IEEEFloat fromFloat:0.0)
|
|
792 |
"
|
|
793 |
! !
|
|
794 |
|
|
795 |
!IEEEFloat methodsFor:'inspecting'!
|
|
796 |
|
|
797 |
inspectorExtraAttributes
|
5345
|
798 |
^ super inspectorExtraAttributes "/ #()
|
|
799 |
! !
|
|
800 |
|
|
801 |
!IEEEFloat methodsFor:'mathematical functions'!
|
|
802 |
|
|
803 |
log10
|
|
804 |
"return log base-10 of the receiver.
|
|
805 |
Raises an exception, if the receiver is less or equal to zero.
|
|
806 |
Here, fallback to the general logarithm code."
|
|
807 |
|
|
808 |
^ self ln / (self coerce:Float ln10)
|
5324
|
809 |
! !
|
|
810 |
|
|
811 |
!IEEEFloat methodsFor:'printing & storing'!
|
|
812 |
|
|
813 |
printOn:aStream
|
|
814 |
thisContext isRecursive ifTrue:[
|
5345
|
815 |
aStream nextPutAll:'IEEEFloat (recursion error while printing)'.
|
5324
|
816 |
^ self.
|
|
817 |
].
|
5342
|
818 |
"/ super printOn:aStream.
|
5324
|
819 |
PrintfScanf printf:'%g' on:aStream argument:self.
|
|
820 |
! !
|
|
821 |
|
|
822 |
!IEEEFloat methodsFor:'queries'!
|
|
823 |
|
|
824 |
eBias
|
|
825 |
"Answer the exponent's bias;
|
|
826 |
that is the offset of the zero exponent when stored"
|
|
827 |
|
|
828 |
^ (1 bitShift:exponentSize-1) - 1
|
|
829 |
|
|
830 |
"
|
|
831 |
1.0 numBitsInExponent 11
|
|
832 |
1.0 eBias 1023
|
|
833 |
1.0 emin -1022
|
|
834 |
1.0 emax 1023
|
|
835 |
1.0 fmin 2.2250738585072E-308
|
|
836 |
1.0 fmax 1.79769313486232E+308
|
|
837 |
"
|
|
838 |
!
|
|
839 |
|
5345
|
840 |
epsilon
|
|
841 |
^ self radix asFloat raisedToInteger:(1 - self precision)
|
|
842 |
!
|
|
843 |
|
5324
|
844 |
numBitsInExponent
|
|
845 |
^ exponentSize
|
|
846 |
|
|
847 |
"
|
|
848 |
1.0 numBitsInExponent 11
|
|
849 |
1.0 numBitsInMantissa 52
|
|
850 |
1.0 precision 53
|
|
851 |
1.0 decimalPrecision 16
|
|
852 |
1.0 eBias 1023
|
|
853 |
1.0 emin -1022
|
|
854 |
1.0 emax 1023
|
|
855 |
1.0 fmin 2.2250738585072E-308
|
|
856 |
1.0 fmax 1.79769313486232E+308
|
|
857 |
"
|
|
858 |
!
|
|
859 |
|
|
860 |
numBitsInMantissa
|
5334
|
861 |
"answer the number of bits in the mantissa (the significant).
|
|
862 |
This is an IEEE float,
|
|
863 |
where n*8 - 1 - exponentSize (n=nr of bytes)
|
|
864 |
bits are available in the mantissa (the hidden bit is not counted here):
|
|
865 |
s ee...ee mmm...mmm
|
5324
|
866 |
"
|
5334
|
867 |
|
|
868 |
^ (self basicSize * 8) - exponentSize - 1 "/ sign
|
5342
|
869 |
!
|
|
870 |
|
5345
|
871 |
precision
|
|
872 |
^ self numBitsInMantissa + 1 - self numBitsInIntegerPart
|
|
873 |
!
|
|
874 |
|
5342
|
875 |
radix
|
|
876 |
"Answer the exponent's radix"
|
|
877 |
|
|
878 |
^ 2
|
5324
|
879 |
! !
|
|
880 |
|
|
881 |
!IEEEFloat methodsFor:'testing'!
|
|
882 |
|
|
883 |
isFinite
|
|
884 |
"return true, if the receiver is a finite float (not NaN or +/-INF)"
|
|
885 |
|
|
886 |
^ self exponentBits ~= ((1 bitShift:self numBitsInExponent)-1)
|
|
887 |
|
|
888 |
"Modified: 12.2.1997 / 16:45:27 / cg"
|
|
889 |
!
|
|
890 |
|
|
891 |
isInfinite
|
|
892 |
"return true, if the receiver is an infinite float (+/-INF)"
|
|
893 |
|
|
894 |
^ (self exponentBits = ((1 bitShift:self numBitsInExponent)-1))
|
|
895 |
and:[ self mantissaBits = 0 ]
|
|
896 |
|
|
897 |
"Modified: 12.2.1997 / 16:45:27 / cg"
|
|
898 |
|
|
899 |
|
|
900 |
!
|
|
901 |
|
|
902 |
isNaN
|
|
903 |
"return true, if the receiver is an invalid float (NaN - not a number)"
|
|
904 |
|
|
905 |
^ (self exponentBits = ((1 bitShift:self numBitsInExponent)-1))
|
|
906 |
and:[ self mantissaBits ~= 0 ]
|
|
907 |
|
|
908 |
"Modified: 12.2.1997 / 16:45:27 / cg"
|
|
909 |
!
|
|
910 |
|
5333
|
911 |
isZero
|
|
912 |
"return true, if the receiver is zero"
|
|
913 |
|
5355
|
914 |
1 to:self basicSize-1 do:[:i |
|
5333
|
915 |
(self basicAt:i) ~~ 0 ifTrue:[^ false].
|
|
916 |
].
|
5355
|
917 |
((self basicAt:self basicSize) bitAnd:16r7F) ~~ 0 ifTrue:[^ false].
|
5333
|
918 |
^ true
|
|
919 |
|
5355
|
920 |
"
|
|
921 |
0.0 negated = 0.0
|
|
922 |
0.0 negated isZero
|
|
923 |
0.0 asIEEEFloat isZero
|
|
924 |
0.0 asIEEEFloat negated isZero
|
|
925 |
0.0 asIEEEFloat = 0.0
|
|
926 |
0.0 asIEEEFloat negated = 0.0
|
|
927 |
"
|
5333
|
928 |
!
|
|
929 |
|
5324
|
930 |
negative
|
|
931 |
"return true if the receiver is less than zero."
|
|
932 |
|
5356
|
933 |
|sz hi|
|
|
934 |
|
|
935 |
sz := self basicSize.
|
|
936 |
((hi := self basicAt:sz) bitTest:16r80) ifTrue:[
|
|
937 |
"/ possibly negative; but could still be a negative zero
|
|
938 |
hi == 16r80 ifFalse:[^ true].
|
|
939 |
1 to:sz-1 do:[:i | (self basicAt:i) == 0 ifFalse:[^ true]].
|
|
940 |
"/ yes - it is -0.0
|
|
941 |
].
|
|
942 |
^ false
|
|
943 |
|
|
944 |
"
|
|
945 |
-1.0 asIEEEFloat negative
|
|
946 |
0.0 asIEEEFloat negative
|
|
947 |
-0.0 asIEEEFloat negative
|
|
948 |
"
|
5324
|
949 |
!
|
|
950 |
|
|
951 |
positive
|
|
952 |
"return true if the receiver is greater or equal to zero."
|
|
953 |
|
|
954 |
^ ((self basicAt:self basicSize) bitTest:16r80) not
|
|
955 |
! !
|
|
956 |
|
5345
|
957 |
!IEEEFloat methodsFor:'truncation & rounding'!
|
|
958 |
|
|
959 |
ceilingAsFloat
|
|
960 |
"return the smallest integer-valued float greater or equal to the receiver.
|
|
961 |
This is much like #ceiling, but avoids a (possibly expensive) conversion
|
|
962 |
of the result to an integer.
|
|
963 |
It may be useful, if the result is to be further used in another float-operation."
|
|
964 |
|
|
965 |
self halt.
|
|
966 |
|
|
967 |
"
|
|
968 |
1.5 asIEEEFloat ceilingAsFloat
|
|
969 |
"
|
5346
|
970 |
!
|
|
971 |
|
|
972 |
fractionPart
|
|
973 |
"extract the after-decimal fraction part.
|
|
974 |
such that (self truncated + self fractionPart) = self"
|
|
975 |
|
|
976 |
|e eB n m nM|
|
|
977 |
|
|
978 |
e := self exponentBits.
|
|
979 |
eB := self eBias.
|
|
980 |
e < eB ifTrue:[
|
|
981 |
"/ integer part is zero
|
|
982 |
^ self
|
|
983 |
].
|
|
984 |
m := self mantissaBits.
|
|
985 |
n := e - eB + 1.
|
|
986 |
e := eB - 1.
|
|
987 |
nM := self numBitsInMantissa.
|
|
988 |
m := (m bitShift:n) bitAnd:(1 bitShift:nM)-1.
|
|
989 |
"/ normalize
|
|
990 |
[m highBit == nM] whileTrue:[
|
|
991 |
m := m bitShift:1.
|
|
992 |
e := e - 1.
|
|
993 |
].
|
|
994 |
^ ((self class basicNew:self basicSize) exponentSize:exponentSize)
|
|
995 |
setFraction:m exponent:e signBit:(self negative ifTrue:[1] ifFalse:[0]).
|
|
996 |
|
|
997 |
"
|
|
998 |
1.5 asIEEEFloat ceilingAsFloat
|
|
999 |
"
|
|
1000 |
!
|
|
1001 |
|
|
1002 |
truncated
|
|
1003 |
"return the receiver truncated towards zero as an integer"
|
|
1004 |
|
|
1005 |
^ super truncated.
|
|
1006 |
|
|
1007 |
"
|
|
1008 |
1.5 asIEEEFloat truncated
|
|
1009 |
"
|
|
1010 |
!
|
|
1011 |
|
|
1012 |
truncatedAsFloat
|
|
1013 |
"return the receiver truncated towards zero as a float.
|
|
1014 |
This is much like #truncated, but avoids a (possibly expensive) conversion
|
|
1015 |
of the result to an integer.
|
|
1016 |
It may be useful, if the result is to be further used in another
|
|
1017 |
float-operation."
|
|
1018 |
|
|
1019 |
^ self - self fractionPart.
|
|
1020 |
|
|
1021 |
"
|
|
1022 |
1.5 truncatedAsFloat
|
|
1023 |
1.0 truncatedAsFloat
|
|
1024 |
1.5 asIEEEFloat truncatedAsFloat
|
|
1025 |
1.0 asIEEEFloat truncatedAsFloat
|
|
1026 |
"
|
5345
|
1027 |
! !
|
|
1028 |
|
5278
|
1029 |
!IEEEFloat class methodsFor:'documentation'!
|
|
1030 |
|
|
1031 |
version_CVS
|
|
1032 |
^ '$Header$'
|
|
1033 |
! !
|
|
1034 |
|