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