5324
|
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.
|
|
24 |
"
|
|
25 |
! !
|
|
26 |
|
|
27 |
!IEEEFloat class methodsFor:'instance creation'!
|
|
28 |
|
5324
|
29 |
fromFloat:aFloat
|
|
30 |
|nB f|
|
|
31 |
|
|
32 |
nB := aFloat byteSize.
|
|
33 |
f := self basicNew:nB.
|
|
34 |
1 to:nB do:[:i |
|
|
35 |
f basicAt:i put:(aFloat byteAt:i)
|
|
36 |
].
|
|
37 |
f exponentSize:(aFloat numBitsInExponent).
|
|
38 |
^ f
|
|
39 |
|
|
40 |
"
|
|
41 |
self fromFloat:1.0
|
|
42 |
self fromFloat:2.0
|
|
43 |
self fromFloat:1.0 asShortFloat
|
|
44 |
self fromFloat:2.0 asShortFloat
|
|
45 |
self fromFloat:1.0 asLongFloat
|
|
46 |
self fromFloat:2.0 asLongFloat
|
|
47 |
"
|
|
48 |
!
|
|
49 |
|
5278
|
50 |
size:numBits exponentSize:exponentSize
|
|
51 |
^ (self basicNew:(numBits // 8)) exponentSize:exponentSize
|
|
52 |
|
|
53 |
"
|
|
54 |
self size:256 exponentSize:19
|
5324
|
55 |
self size:16 exponentSize:5
|
|
56 |
self size:(1.0 basicSize * 8) exponentSize:(1.0 numBitsInExponent)
|
|
57 |
"
|
|
58 |
!
|
|
59 |
|
|
60 |
size:numBits exponentSize:exponentSize fraction:anInteger exponent:exp signBit:signBit
|
|
61 |
^ ((self basicNew:(numBits // 8)) exponentSize:exponentSize)
|
|
62 |
setFraction:anInteger exponent:exp signBit:signBit
|
|
63 |
|
|
64 |
"
|
|
65 |
self size:256 exponentSize:19 fromInteger:1
|
|
66 |
self size:256 exponentSize:19 fromInteger:2
|
5278
|
67 |
"
|
|
68 |
!
|
|
69 |
|
|
70 |
size:numBits exponentSize:exponentSize fromFloat:aFloat
|
|
71 |
^ ((self basicNew:(numBits // 8)) exponentSize:exponentSize) setValueFromFloat:aFloat
|
|
72 |
|
|
73 |
"
|
|
74 |
self size:256 exponentSize:19 fromFloat:1.0
|
|
75 |
self size:256 exponentSize:19 fromFloat:2.0
|
|
76 |
"
|
|
77 |
!
|
|
78 |
|
|
79 |
size:numBits exponentSize:exponentSize fromInteger:anInteger
|
|
80 |
^ ((self basicNew:(numBits // 8)) exponentSize:exponentSize) setValueFromInteger:anInteger
|
|
81 |
|
|
82 |
"
|
|
83 |
self size:256 exponentSize:19 fromInteger:1
|
|
84 |
self size:256 exponentSize:19 fromInteger:2
|
|
85 |
"
|
|
86 |
! !
|
|
87 |
|
5324
|
88 |
!IEEEFloat class methodsFor:'coercing & converting'!
|
|
89 |
|
|
90 |
coerce:aNumber
|
|
91 |
"convert the argument aNumber into an instance of the receiver (class) and return it."
|
|
92 |
|
|
93 |
^ aNumber asIEEEFloat
|
|
94 |
! !
|
5278
|
95 |
|
5324
|
96 |
!IEEEFloat class methodsFor:'constants'!
|
5278
|
97 |
|
5324
|
98 |
unity
|
|
99 |
^ 1.0
|
|
100 |
!
|
|
101 |
|
|
102 |
zero
|
|
103 |
^ 0.0
|
5278
|
104 |
! !
|
|
105 |
|
|
106 |
!IEEEFloat methodsFor:'accessing'!
|
|
107 |
|
5324
|
108 |
exponentBits
|
|
109 |
"return the bits of my exponent.
|
|
110 |
These might be biased.
|
|
111 |
My bytes are stored LSB first; thus we have to fetch the high exponent bytes."
|
|
112 |
|
|
113 |
|nBytes nExpBytes e|
|
|
114 |
|
|
115 |
nBytes := self basicSize.
|
|
116 |
nExpBytes := (exponentSize + 7) // 8.
|
|
117 |
"/ seee eeee eeee mmmm mmmm ....
|
|
118 |
e := (self basicAt:nBytes) bitAnd:16r7F.
|
|
119 |
2 to:nExpBytes do:[:eI |
|
|
120 |
e := (e bitShift:8) bitOr:(self basicAt:nBytes + 1 - eI)
|
|
121 |
].
|
|
122 |
"/ 0eee eeee eeee mmmm
|
|
123 |
e := e rightShift:(nExpBytes * 8) - 1 "sign" - exponentSize.
|
|
124 |
"/ 0000 0eee eeee eeee
|
|
125 |
^ e
|
|
126 |
|
|
127 |
"
|
|
128 |
(self size:32 exponentSize:8) inspect.
|
|
129 |
"
|
|
130 |
"construct a soft-shortFloat
|
|
131 |
|
|
132 |
|f sf|
|
|
133 |
sf := 1.0 asShortFloat.
|
|
134 |
f := self size:(sf basicSize * 8) exponentSize:(sf numBitsInExponent).
|
|
135 |
1 to:sf basicSize do:[:i | f basicAt:i put:(sf basicAt:i)].
|
|
136 |
f inspect
|
|
137 |
"
|
|
138 |
|
|
139 |
"Modified: 12.2.1997 / 16:45:27 / cg"
|
|
140 |
!
|
|
141 |
|
5278
|
142 |
exponentSize
|
|
143 |
^ exponentSize
|
|
144 |
!
|
|
145 |
|
|
146 |
exponentSize:something
|
|
147 |
exponentSize := something.
|
|
148 |
!
|
|
149 |
|
5324
|
150 |
mantissaBits
|
|
151 |
"return the bits of my mantissa (incl. the hidden bit).
|
|
152 |
My bytes are stored LSB first."
|
|
153 |
|
|
154 |
|bits hiddenBit mask c|
|
|
155 |
|
|
156 |
bits := self digitBytes asIntegerMSB:false.
|
|
157 |
bits == 0 ifTrue:[^ 0].
|
|
158 |
|
|
159 |
hiddenBit := (1 bitShift:self numBitsInMantissa).
|
|
160 |
mask := hiddenBit-1.
|
|
161 |
c := bits bitAnd:mask.
|
|
162 |
"/ TODO: care for subnormals (exponent = 0)!!
|
|
163 |
^ c bitOr:hiddenBit.
|
|
164 |
|
|
165 |
"
|
|
166 |
(self size:32 exponentSize:8) inspect.
|
|
167 |
"
|
|
168 |
|
|
169 |
"construct a soft-shortFloat
|
|
170 |
|
|
171 |
|f sf|
|
|
172 |
sf := 1.0 asShortFloat.
|
|
173 |
f := self size:(sf basicSize * 8) exponentSize:(sf numBitsInExponent).
|
|
174 |
1 to:sf basicSize do:[:i | f basicAt:i put:(sf basicAt:i)].
|
|
175 |
f inspect
|
|
176 |
"
|
|
177 |
|
|
178 |
"Modified: 12.2.1997 / 16:45:27 / cg"
|
|
179 |
!
|
|
180 |
|
|
181 |
setFraction:fraction exponent:exponent signBit:signBit
|
|
182 |
|bits|
|
|
183 |
|
|
184 |
bits := (fraction
|
|
185 |
bitOr:(exponent bitShift:self numBitsInMantissa))
|
|
186 |
bitOr:(signBit bitShift:(self basicSize * 8) - 1).
|
|
187 |
1 to:self basicSize do:[:i | self basicAt:i put:(bits digitAt:i)].
|
|
188 |
|
|
189 |
"
|
|
190 |
(self size:64 exponentSize:11) setFraction:0 exponent:1024 signBit:0
|
|
191 |
"
|
|
192 |
!
|
|
193 |
|
5278
|
194 |
setValueFromInteger:intValue
|
|
195 |
"/ how many bits are there, in this int
|
|
196 |
|absValue myNumBits numBitsInNumber shift|
|
|
197 |
|
|
198 |
absValue := intValue abs.
|
|
199 |
numBitsInNumber := absValue highBit.
|
|
200 |
myNumBits := (self basicSize * 8) - 1 "sign" - exponentSize.
|
|
201 |
shift := myNumBits - numBitsInNumber.
|
|
202 |
numBitsInNumber > myNumBits ifTrue:[
|
|
203 |
self halt.
|
|
204 |
] ifFalse:[
|
|
205 |
"/ number:
|
|
206 |
"/ 1xxxxxxx...xxxxx
|
|
207 |
"/ myRep:
|
|
208 |
"/ seee...eeexxxxxxxxxx
|
|
209 |
absValue digitLength to:1 by:-1 do:[:byteIndex |
|
|
210 |
|
|
211 |
].
|
|
212 |
].
|
|
213 |
|
|
214 |
"/ cut off some bits
|
|
215 |
|
|
216 |
"Float numBitsInExponent
|
|
217 |
self size:16 exponentSize:4 fromInteger:1
|
|
218 |
self size:32 exponentSize:11 fromInteger:1
|
|
219 |
|
|
220 |
self size:256 exponentSize:19 fromInteger:1
|
|
221 |
self size:256 exponentSize:19 fromInteger:2
|
|
222 |
"
|
|
223 |
! !
|
|
224 |
|
5324
|
225 |
!IEEEFloat methodsFor:'arithmetic'!
|
|
226 |
|
|
227 |
* aNumber
|
|
228 |
thisContext isRecursive ifTrue:[self halt].
|
|
229 |
^ aNumber productFromIEEEFloat:self
|
|
230 |
!
|
|
231 |
|
|
232 |
+ aNumber
|
|
233 |
thisContext isRecursive ifTrue:[self halt].
|
|
234 |
^ aNumber sumFromIEEEFloat:self
|
|
235 |
|
|
236 |
"
|
|
237 |
(-2.0 asIEEEFloat)
|
|
238 |
(2.0 asIEEEFloat)
|
|
239 |
|
|
240 |
self assert:((4.0 asIEEEFloat) + (2.0 asIEEEFloat)) = (6.0 asIEEEFloat)
|
|
241 |
self assert:((4.0 asIEEEFloat) + (-2.0 asIEEEFloat)) = (2.0 asIEEEFloat)
|
|
242 |
self assert:((-4.0 asIEEEFloat) + (-2.0 asIEEEFloat)) = (-6.0 asIEEEFloat)
|
|
243 |
self assert:((-4.0 asIEEEFloat) + (2.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
244 |
|
|
245 |
self assert:((2.0 asIEEEFloat) + (4.0 asIEEEFloat)) = (6.0 asIEEEFloat)
|
|
246 |
self assert:((2.0 asIEEEFloat) + (-4.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
247 |
self assert:((-2.0 asIEEEFloat) + (-4.0 asIEEEFloat)) = (-6.0 asIEEEFloat)
|
|
248 |
self assert:((-2.0 asIEEEFloat) + (4.0 asIEEEFloat)) = (2.0 asIEEEFloat)
|
|
249 |
|
|
250 |
self assert:((IEEEFloat fromFloat:0.1) + (IEEEFloat fromFloat:-0.1)) = (IEEEFloat fromFloat:0.0)
|
|
251 |
self assert:((IEEEFloat fromFloat:-0.1) + (IEEEFloat fromFloat:0.1)) = (IEEEFloat fromFloat:0.0)
|
|
252 |
"
|
|
253 |
!
|
|
254 |
|
|
255 |
- aNumber
|
|
256 |
thisContext isRecursive ifTrue:[self halt].
|
|
257 |
^ aNumber differenceFromIEEEFloat:self
|
|
258 |
|
|
259 |
"
|
|
260 |
self assert:((4.0 asIEEEFloat) - (2.0 asIEEEFloat)) = (2.0 asIEEEFloat)
|
|
261 |
self assert:((4.0 asIEEEFloat) - (-2.0 asIEEEFloat)) = (6.0 asIEEEFloat)
|
|
262 |
self assert:((-4.0 asIEEEFloat) - (-2.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
263 |
self assert:((-4.0 asIEEEFloat) - (2.0 asIEEEFloat)) = (-6.0 asIEEEFloat)
|
|
264 |
|
|
265 |
self assert:((2.0 asIEEEFloat) - (4.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
266 |
self assert:((2.0 asIEEEFloat) - (-4.0 asIEEEFloat)) = (6.0 asIEEEFloat)
|
|
267 |
self assert:((-2.0 asIEEEFloat) - (-4.0 asIEEEFloat)) = (2.0 asIEEEFloat)
|
|
268 |
self assert:((-2.0 asIEEEFloat) - (4.0 asIEEEFloat)) = (-6.0 asIEEEFloat)
|
|
269 |
|
|
270 |
self assert:((IEEEFloat fromFloat:0.1) - (IEEEFloat fromFloat:0.1)) = (IEEEFloat fromFloat:0.0)
|
|
271 |
self assert:((IEEEFloat fromFloat:0.1) - (IEEEFloat fromFloat:-0.1)) = (IEEEFloat fromFloat:0.2)
|
|
272 |
self assert:((IEEEFloat fromFloat:-0.1) - (IEEEFloat fromFloat:-0.1)) = (IEEEFloat fromFloat:0.0)
|
|
273 |
"
|
|
274 |
!
|
|
275 |
|
|
276 |
/ aNumber
|
|
277 |
thisContext isRecursive ifTrue:[self halt].
|
|
278 |
^ aNumber quotientFromIEEEFloat:self
|
|
279 |
!
|
|
280 |
|
|
281 |
negated
|
|
282 |
|f highByte|
|
|
283 |
|
|
284 |
f := self copy.
|
|
285 |
highByte := self basicAt:(self basicSize).
|
|
286 |
f basicAt:(self basicSize) put:(highByte bitXor:16r80).
|
|
287 |
^ f.
|
|
288 |
|
|
289 |
"
|
|
290 |
self assert:(IEEEFloat fromFloat:2.0) negated = (IEEEFloat fromFloat:-2.0)
|
|
291 |
self assert:(IEEEFloat fromFloat:2.0) negated negated = (IEEEFloat fromFloat:2.0)
|
|
292 |
self assert:(IEEEFloat fromFloat:-2.0) negated = (IEEEFloat fromFloat:2.0)
|
|
293 |
"
|
|
294 |
! !
|
|
295 |
|
|
296 |
!IEEEFloat methodsFor:'coercing & converting'!
|
|
297 |
|
|
298 |
generality
|
|
299 |
^ 97 "/ between OctaFloat and LargeFloat
|
|
300 |
|
|
301 |
"
|
|
302 |
1 asShortFloat generality 70
|
|
303 |
1 asFloat generality 80
|
|
304 |
1 asLongFloat generality 90
|
|
305 |
1 asQuadFloat generality
|
|
306 |
1 asQDouble generality 95
|
|
307 |
1 asLargeFloat generality 100
|
|
308 |
"
|
|
309 |
! !
|
|
310 |
|
|
311 |
!IEEEFloat methodsFor:'comparing'!
|
|
312 |
|
|
313 |
< aNumber
|
|
314 |
self halt.
|
|
315 |
thisContext isRecursive ifTrue:[self halt].
|
|
316 |
|
|
317 |
^ aNumber lessFromIEEEFloat:self
|
|
318 |
!
|
|
319 |
|
|
320 |
= aNumber
|
5331
|
321 |
thisContext isRecursive ifTrue:[self halt].
|
5324
|
322 |
^ aNumber equalFromIEEEFloat:self
|
|
323 |
! !
|
|
324 |
|
|
325 |
!IEEEFloat methodsFor:'double dispatching'!
|
|
326 |
|
|
327 |
differenceFromIEEEFloat:anIEEEFloat
|
|
328 |
"/ anIEEEFloat - self
|
|
329 |
^ self negated sumFromIEEEFloat:anIEEEFloat
|
|
330 |
!
|
|
331 |
|
|
332 |
equalFromIEEEFloat:anIEEEFloat
|
|
333 |
anIEEEFloat basicSize == self basicSize ifTrue:[
|
|
334 |
anIEEEFloat exponentSize == exponentSize ifTrue:[
|
|
335 |
1 to:self basicSize do:[:i |
|
|
336 |
(self basicAt:i) = (anIEEEFloat basicAt:i) ifFalse:[^ false].
|
|
337 |
].
|
|
338 |
^ true.
|
|
339 |
].
|
|
340 |
].
|
|
341 |
"/ more complicated compare
|
|
342 |
self halt.
|
|
343 |
^ false.
|
|
344 |
!
|
|
345 |
|
|
346 |
lessFromIEEEFloat:anIEEEFloat
|
|
347 |
||
|
|
348 |
|
|
349 |
anIEEEFloat basicSize == self basicSize ifTrue:[
|
|
350 |
].
|
|
351 |
self halt.
|
|
352 |
!
|
|
353 |
|
|
354 |
productFromIEEEFloat:anIEEEFloat
|
|
355 |
"/ anIEEEFloat * self
|
|
356 |
|
|
357 |
|m1 m2 e1 e2 m e signBit nB hi shift|
|
|
358 |
|
5331
|
359 |
"/ for now:
|
|
360 |
self assert:(self numBitsInMantissa == anIEEEFloat numBitsInMantissa).
|
|
361 |
|
5324
|
362 |
"/ m1 * m2 / e1 + e2
|
|
363 |
|
|
364 |
m1 := anIEEEFloat mantissaBits.
|
|
365 |
m2 := self mantissaBits.
|
|
366 |
e1 := anIEEEFloat exponentBits.
|
|
367 |
e2 := self exponentBits.
|
|
368 |
e := (e1 - anIEEEFloat eBias) + (e2 - self eBias).
|
|
369 |
|
|
370 |
nB := self numBitsInMantissa. "/ exl. hidden bit
|
|
371 |
|
|
372 |
"/ 1xxxx....xxxx * 1xxxx....xxxx
|
|
373 |
m := m1 * m2.
|
|
374 |
"/ now have nB bits too many
|
|
375 |
e := e - nB.
|
|
376 |
|
|
377 |
signBit := (self negative == anIEEEFloat negative) ifTrue:[0] ifFalse:[1].
|
|
378 |
m == 0 ifTrue:[self halt].
|
|
379 |
|
|
380 |
hi := m highBit.
|
|
381 |
shift := (nB + 1) - hi.
|
|
382 |
m := m bitShift:shift.
|
|
383 |
e := e - shift.
|
|
384 |
m := m bitAnd:(1 bitShift:nB)-1.
|
|
385 |
|
|
386 |
e := e + self eBias.
|
|
387 |
^ self class
|
|
388 |
size:(self basicSize*8) exponentSize:exponentSize
|
|
389 |
fraction:m exponent:e signBit:signBit.
|
|
390 |
|
|
391 |
"
|
|
392 |
self assert:((IEEEFloat fromFloat:1.0) * (IEEEFloat fromFloat:1.0)) = (IEEEFloat fromFloat:1.0)
|
|
393 |
self assert:((IEEEFloat fromFloat:1.0) * (IEEEFloat fromFloat:1.0)) exponent = 1.0 exponent
|
|
394 |
self assert:((IEEEFloat fromFloat:1.0) * (IEEEFloat fromFloat:1.0)) mantissaBits = 1.0 mantissaBits
|
|
395 |
|
|
396 |
self assert:((4.0 asIEEEFloat) * (2.0 asIEEEFloat)) = (8.0 asIEEEFloat)
|
|
397 |
self assert:((-4.0 asIEEEFloat) * (2.0 asIEEEFloat)) = (-8.0 asIEEEFloat)
|
|
398 |
self assert:((4.0 asIEEEFloat) * (-2.0 asIEEEFloat)) = (-8.0 asIEEEFloat)
|
|
399 |
self assert:((-4.0 asIEEEFloat) * (-2.0 asIEEEFloat)) = (8.0 asIEEEFloat)
|
|
400 |
|
|
401 |
self assert:((IEEEFloat fromFloat:100.0) * (IEEEFloat fromFloat:100.0)) = (IEEEFloat fromFloat:10000.0)
|
|
402 |
self assert:((IEEEFloat fromFloat:1e-20) * (IEEEFloat fromFloat:1e20)) = (IEEEFloat fromFloat:(1e-20 * 1e20))
|
|
403 |
"
|
|
404 |
!
|
|
405 |
|
|
406 |
quotientFromIEEEFloat:anIEEEFloat
|
|
407 |
"/ anIEEEFloat / self
|
|
408 |
|
|
409 |
|m1 m2 e1 e2 m e signBit nB hi shift|
|
|
410 |
|
5331
|
411 |
"/ for now:
|
|
412 |
self assert:(self numBitsInMantissa == anIEEEFloat numBitsInMantissa).
|
|
413 |
|
5324
|
414 |
"/ m1 / m2 / e1 - e2
|
|
415 |
|
|
416 |
m1 := anIEEEFloat mantissaBits.
|
|
417 |
m2 := self mantissaBits.
|
|
418 |
e1 := anIEEEFloat exponentBits.
|
|
419 |
e2 := self exponentBits.
|
|
420 |
e := (e1 - anIEEEFloat eBias) - (e2 - self eBias).
|
|
421 |
|
|
422 |
nB := self numBitsInMantissa. "/ exl. hidden bit
|
|
423 |
|
|
424 |
"/ 1xxxx....xxxx / 1xxxx....xxxx
|
|
425 |
m := (m1 bitShift:nB) // m2.
|
|
426 |
"/ rest := m1 \\ m2.
|
|
427 |
|
|
428 |
signBit := (self negative == anIEEEFloat negative) ifTrue:[0] ifFalse:[1].
|
|
429 |
m == 0 ifTrue:[self halt].
|
|
430 |
|
|
431 |
hi := m highBit.
|
|
432 |
shift := (nB + 1) - hi.
|
|
433 |
m := m bitShift:shift.
|
|
434 |
e := e - shift.
|
|
435 |
m := m bitAnd:(1 bitShift:nB)-1.
|
|
436 |
|
|
437 |
e := e + self eBias.
|
|
438 |
^ self class
|
|
439 |
size:(self basicSize*8) exponentSize:exponentSize
|
|
440 |
fraction:m exponent:e signBit:signBit.
|
|
441 |
|
|
442 |
"
|
|
443 |
self assert:((IEEEFloat fromFloat:1.0) / (IEEEFloat fromFloat:1.0)) = (IEEEFloat fromFloat:1.0)
|
|
444 |
self assert:((IEEEFloat fromFloat:1.0) / (IEEEFloat fromFloat:1.0)) exponent = 1.0 exponent
|
|
445 |
self assert:((IEEEFloat fromFloat:1.0) / (IEEEFloat fromFloat:1.0)) mantissaBits = 1.0 mantissaBits
|
|
446 |
|
|
447 |
self assert:((4.0 asIEEEFloat) / (2.0 asIEEEFloat)) = (2.0 asIEEEFloat)
|
|
448 |
self assert:((-4.0 asIEEEFloat) / (2.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
449 |
self assert:((4.0 asIEEEFloat) / (-2.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
450 |
self assert:((-4.0 asIEEEFloat) / (-2.0 asIEEEFloat)) = (2.0 asIEEEFloat)
|
|
451 |
|
|
452 |
self assert:((IEEEFloat fromFloat:100.0) / (IEEEFloat fromFloat:2.0)) = (IEEEFloat fromFloat:50.0)
|
|
453 |
self assert:((IEEEFloat fromFloat:1e-20) / (IEEEFloat fromFloat:1e20)) = (IEEEFloat fromFloat:(1e-20 / 1e20))
|
|
454 |
"
|
|
455 |
!
|
|
456 |
|
|
457 |
sumFromIEEEFloat:anIEEEFloat
|
|
458 |
"/ anIEEEFloat + self
|
|
459 |
|
|
460 |
|m1 m2 e1 e2 m e signBit nB hi shift|
|
|
461 |
|
5331
|
462 |
"/ for now:
|
|
463 |
self assert:(self numBitsInMantissa == anIEEEFloat numBitsInMantissa).
|
|
464 |
|
5324
|
465 |
"/ bring to same exponent, add m1 + m2, normalize
|
|
466 |
|
|
467 |
m1 := anIEEEFloat mantissaBits. m1 == 0 ifTrue:[^ self].
|
|
468 |
m2 := self mantissaBits. m2 == 0 ifTrue:[^ anIEEEFloat].
|
5331
|
469 |
e1 := anIEEEFloat exponentBits - anIEEEFloat eBias.
|
|
470 |
e2 := self exponentBits - self eBias.
|
5324
|
471 |
e := e1.
|
|
472 |
e1 = e2 ifFalse:[
|
|
473 |
e1 > e2 ifTrue:[
|
|
474 |
m2 := m2 rightShift:(e1 - e2).
|
|
475 |
e := e1.
|
|
476 |
] ifFalse:[
|
|
477 |
"/ e2 > e1
|
|
478 |
m1 := m1 rightShift:(e2 - e1).
|
|
479 |
e := e2.
|
|
480 |
]
|
|
481 |
].
|
|
482 |
|
|
483 |
signBit := 0.
|
|
484 |
"/ ok, add the mantissae
|
|
485 |
self negative ifTrue:[
|
|
486 |
anIEEEFloat negative ifTrue:[
|
|
487 |
"/ - (anIEEEFloat + self)
|
|
488 |
m := m1 + m2.
|
|
489 |
signBit := 1.
|
|
490 |
] ifFalse:[
|
|
491 |
"/ anIEEEFloat - self
|
|
492 |
m := m1 - m2
|
|
493 |
].
|
|
494 |
] ifFalse:[
|
|
495 |
anIEEEFloat negative ifTrue:[
|
|
496 |
"/ self - anIEEEFloat
|
|
497 |
m := m2 - m1.
|
|
498 |
] ifFalse:[
|
|
499 |
"/ (anIEEEFloat + self)
|
|
500 |
m := m1 + m2.
|
|
501 |
].
|
|
502 |
].
|
|
503 |
m == 0 ifTrue:[^ self class zero].
|
|
504 |
m < 0 ifTrue:[
|
|
505 |
m := m negated.
|
|
506 |
signBit := 1-signBit
|
|
507 |
].
|
|
508 |
|
|
509 |
nB := self numBitsInMantissa. "/ exl. hidden bit
|
|
510 |
hi := m highBit.
|
|
511 |
shift := (nB + 1) - hi.
|
|
512 |
m := m bitShift:shift.
|
|
513 |
e := e - shift.
|
|
514 |
m := m bitAnd:(1 bitShift:nB)-1.
|
|
515 |
|
|
516 |
^ self class
|
|
517 |
size:(self basicSize*8) exponentSize:exponentSize
|
5331
|
518 |
fraction:m exponent:(e + self eBias) signBit:signBit.
|
5324
|
519 |
|
|
520 |
"
|
|
521 |
seee eeee eeee mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
|
|
522 |
0100 0000 0000 0000 0000 ...... 2.0
|
|
523 |
0011 1111 1111 0000 0000 ...... 1.0
|
|
524 |
0400 0000 0001 0000 0000 ...... 4.0
|
|
525 |
|
|
526 |
self assert:(IEEEFloat fromFloat:1.0) exponent = 1.0 exponent.
|
|
527 |
self assert:(IEEEFloat fromFloat:1.0) mantissaBits = 1.0 mantissaBits.
|
|
528 |
self assert:(IEEEFloat fromFloat:1.0) eBias = 1.0 eBias.
|
|
529 |
|
|
530 |
self assert:(IEEEFloat fromFloat:2.0) exponent = 2.0 exponent.
|
|
531 |
self assert:(IEEEFloat fromFloat:2.0) mantissaBits = 2.0 mantissaBits.
|
|
532 |
self assert:(IEEEFloat fromFloat:2.0) eBias = 2.0 eBias.
|
|
533 |
|
|
534 |
self assert:(IEEEFloat fromFloat:4.0) exponent = 4.0 exponent.
|
|
535 |
self assert:(IEEEFloat fromFloat:4.0) mantissaBits = 4.0 mantissaBits.
|
|
536 |
self assert:(IEEEFloat fromFloat:4.0) eBias = 4.0 eBias.
|
|
537 |
|
|
538 |
self assert:(IEEEFloat fromFloat:3.0) exponent = 3.0 exponent.
|
|
539 |
self assert:(IEEEFloat fromFloat:3.0) mantissaBits = 3.0 mantissaBits.
|
|
540 |
self assert:(IEEEFloat fromFloat:3.0) eBias = 3.0 eBias.
|
|
541 |
|
|
542 |
self assert:(IEEEFloat fromFloat:10.0) exponent = 10.0 exponent.
|
|
543 |
self assert:(IEEEFloat fromFloat:10.0) mantissaBits = 10.0 mantissaBits.
|
|
544 |
self assert:(IEEEFloat fromFloat:10.0) eBias = 10.0 eBias.
|
|
545 |
|
|
546 |
self assert:(IEEEFloat fromFloat:1e-3) exponent = 1e-3 exponent.
|
|
547 |
self assert:(IEEEFloat fromFloat:1e-3) mantissaBits = 1e-3 mantissaBits.
|
|
548 |
self assert:(IEEEFloat fromFloat:1e-3) eBias = 1e-3 eBias.
|
|
549 |
|
|
550 |
(IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:1.0) = (IEEEFloat fromFloat:2.0)
|
|
551 |
((IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:1.0)) exponent = 2.0 exponent
|
|
552 |
((IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:1.0)) mantissaBits = 2.0 mantissaBits
|
|
553 |
|
|
554 |
self assert:((-4.0 asIEEEFloat) + (2.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
555 |
|
|
556 |
(IEEEFloat fromFloat:2.0)
|
|
557 |
self assert:((IEEEFloat fromFloat:2.0) + (IEEEFloat fromFloat:2.0)) = (IEEEFloat fromFloat:4.0)
|
|
558 |
self assert:((IEEEFloat fromFloat:4.0) + (IEEEFloat fromFloat:4.0)) = (IEEEFloat fromFloat:8.0)
|
|
559 |
self assert:((IEEEFloat fromFloat:0.0) + (IEEEFloat fromFloat:1.0)) = (IEEEFloat fromFloat:1.0)
|
|
560 |
self assert:((IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:0.0)) = (IEEEFloat fromFloat:1.0)
|
|
561 |
self assert:((IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:2.0)) = (IEEEFloat fromFloat:3.0)
|
|
562 |
self assert:((IEEEFloat fromFloat:100.0) + (IEEEFloat fromFloat:2.0)) = (IEEEFloat fromFloat:102.0)
|
|
563 |
self assert:((IEEEFloat fromFloat:2.0) + (IEEEFloat fromFloat:100.0)) = (IEEEFloat fromFloat:102.0)
|
|
564 |
|
|
565 |
self assert:((IEEEFloat fromFloat:4.0) + (IEEEFloat fromFloat:-2.0)) = (IEEEFloat fromFloat:2.0)
|
|
566 |
self assert:((IEEEFloat fromFloat:8.0) + (IEEEFloat fromFloat:-2.0)) = (IEEEFloat fromFloat:6.0)
|
|
567 |
self assert:((IEEEFloat fromFloat:100.0) + (IEEEFloat fromFloat:-2.0)) = (IEEEFloat fromFloat:98.0)
|
|
568 |
self assert:((IEEEFloat fromFloat:-2.0) + (IEEEFloat fromFloat:100.0)) = (IEEEFloat fromFloat:98.0)
|
|
569 |
self assert:((IEEEFloat fromFloat:-2.0) + (IEEEFloat fromFloat:-100.0)) = (IEEEFloat fromFloat:-102.0)
|
|
570 |
|
|
571 |
self assert:((IEEEFloat fromFloat:-0.1) + (IEEEFloat fromFloat:0.1)) = (IEEEFloat fromFloat:0.0)
|
|
572 |
"
|
|
573 |
! !
|
|
574 |
|
|
575 |
!IEEEFloat methodsFor:'inspecting'!
|
|
576 |
|
|
577 |
inspectorExtraAttributes
|
|
578 |
^ #()
|
|
579 |
! !
|
|
580 |
|
|
581 |
!IEEEFloat methodsFor:'printing & storing'!
|
|
582 |
|
|
583 |
displayOn:aStream
|
|
584 |
aStream nextPutAll:'IEEEFloat'.
|
|
585 |
^ self.
|
|
586 |
!
|
|
587 |
|
|
588 |
displayString
|
|
589 |
^ 'IEEEFloat'.
|
|
590 |
|
|
591 |
!
|
|
592 |
|
|
593 |
printOn:aStream
|
|
594 |
aStream nextPutAll:'IEEEFloat'.
|
|
595 |
^ self.
|
|
596 |
thisContext isRecursive ifTrue:[
|
|
597 |
aStream nextPutAll:'IEEEFloat (error while printing)'.
|
|
598 |
^ self.
|
|
599 |
].
|
|
600 |
|
|
601 |
PrintfScanf printf:'%g' on:aStream argument:self.
|
|
602 |
!
|
|
603 |
|
|
604 |
printOn:aStream base:b
|
|
605 |
aStream nextPutAll:'IEEEFloat'.
|
|
606 |
!
|
|
607 |
|
|
608 |
printOn:aStream base:b showRadix:showRadix
|
|
609 |
aStream nextPutAll:'IEEEFloat'.
|
|
610 |
!
|
|
611 |
|
|
612 |
printOn:aStream thousandsSeparator:thousandsSeparator
|
|
613 |
aStream nextPutAll:'IEEEFloat'.
|
|
614 |
!
|
|
615 |
|
|
616 |
printString
|
|
617 |
^ 'IEEEFloat'.
|
|
618 |
!
|
|
619 |
|
|
620 |
printStringFormat:formatString
|
|
621 |
^ 'IEEEFloat'.
|
|
622 |
!
|
|
623 |
|
|
624 |
printStringRadix:base
|
|
625 |
^ 'IEEEFloat'.
|
|
626 |
!
|
|
627 |
|
|
628 |
printStringRadix:base showRadix:showRadixBoolean
|
|
629 |
^ 'IEEEFloat'.
|
|
630 |
!
|
|
631 |
|
|
632 |
printStringScientific
|
|
633 |
^ 'IEEEFloat'.
|
|
634 |
!
|
|
635 |
|
|
636 |
printfPrintString:formatString
|
|
637 |
^ 'IEEEFloat'.
|
|
638 |
! !
|
|
639 |
|
|
640 |
!IEEEFloat methodsFor:'queries'!
|
|
641 |
|
|
642 |
eBias
|
|
643 |
"Answer the exponent's bias;
|
|
644 |
that is the offset of the zero exponent when stored"
|
|
645 |
|
|
646 |
^ (1 bitShift:exponentSize-1) - 1
|
|
647 |
|
|
648 |
"
|
|
649 |
1.0 numBitsInExponent 11
|
|
650 |
1.0 eBias 1023
|
|
651 |
1.0 emin -1022
|
|
652 |
1.0 emax 1023
|
|
653 |
1.0 fmin 2.2250738585072E-308
|
|
654 |
1.0 fmax 1.79769313486232E+308
|
|
655 |
"
|
|
656 |
!
|
|
657 |
|
|
658 |
numBitsInExponent
|
|
659 |
^ exponentSize
|
|
660 |
|
|
661 |
"
|
|
662 |
1.0 numBitsInExponent 11
|
|
663 |
1.0 numBitsInMantissa 52
|
|
664 |
1.0 precision 53
|
|
665 |
1.0 decimalPrecision 16
|
|
666 |
1.0 eBias 1023
|
|
667 |
1.0 emin -1022
|
|
668 |
1.0 emax 1023
|
|
669 |
1.0 fmin 2.2250738585072E-308
|
|
670 |
1.0 fmax 1.79769313486232E+308
|
|
671 |
"
|
|
672 |
!
|
|
673 |
|
|
674 |
numBitsInMantissa
|
|
675 |
^ (self basicSize * 8)
|
|
676 |
- 1 "/ sign
|
|
677 |
- exponentSize
|
|
678 |
|
|
679 |
"
|
|
680 |
1.0 numBitsInExponent 11
|
|
681 |
1.0 numBitsInMantissa 52
|
|
682 |
1.0 precision 53
|
|
683 |
1.0 decimalPrecision 16
|
|
684 |
1.0 eBias 1023
|
|
685 |
1.0 emin -1022
|
|
686 |
1.0 emax 1023
|
|
687 |
1.0 fmin 2.2250738585072E-308
|
|
688 |
1.0 fmax 1.79769313486232E+308
|
|
689 |
"
|
|
690 |
! !
|
|
691 |
|
|
692 |
!IEEEFloat methodsFor:'testing'!
|
|
693 |
|
|
694 |
isFinite
|
|
695 |
"return true, if the receiver is a finite float (not NaN or +/-INF)"
|
|
696 |
|
|
697 |
^ self exponentBits ~= ((1 bitShift:self numBitsInExponent)-1)
|
|
698 |
|
|
699 |
"Modified: 12.2.1997 / 16:45:27 / cg"
|
|
700 |
!
|
|
701 |
|
|
702 |
isInfinite
|
|
703 |
"return true, if the receiver is an infinite float (+/-INF)"
|
|
704 |
|
|
705 |
^ (self exponentBits = ((1 bitShift:self numBitsInExponent)-1))
|
|
706 |
and:[ self mantissaBits = 0 ]
|
|
707 |
|
|
708 |
"Modified: 12.2.1997 / 16:45:27 / cg"
|
|
709 |
|
|
710 |
|
|
711 |
!
|
|
712 |
|
|
713 |
isNaN
|
|
714 |
"return true, if the receiver is an invalid float (NaN - not a number)"
|
|
715 |
|
|
716 |
^ (self exponentBits = ((1 bitShift:self numBitsInExponent)-1))
|
|
717 |
and:[ self mantissaBits ~= 0 ]
|
|
718 |
|
|
719 |
"Modified: 12.2.1997 / 16:45:27 / cg"
|
|
720 |
!
|
|
721 |
|
|
722 |
negative
|
|
723 |
"return true if the receiver is less than zero."
|
|
724 |
|
|
725 |
^ (self basicAt:self basicSize) bitTest:16r80
|
|
726 |
!
|
|
727 |
|
|
728 |
positive
|
|
729 |
"return true if the receiver is greater or equal to zero."
|
|
730 |
|
|
731 |
^ ((self basicAt:self basicSize) bitTest:16r80) not
|
|
732 |
! !
|
|
733 |
|
5278
|
734 |
!IEEEFloat class methodsFor:'documentation'!
|
|
735 |
|
|
736 |
version_CVS
|
|
737 |
^ '$Header$'
|
|
738 |
! !
|
|
739 |
|