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.
|
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.
|
|
343 |
fraction := aNumber bitShift:(nM - h + 1).
|
|
344 |
fraction := fraction bitAnd:((1 bitShift:nM)-1).
|
|
345 |
e := h - 1.
|
|
346 |
|
|
347 |
^ (self class basicNew:self basicSize) exponentSize:exponentSize;
|
|
348 |
setFraction:fraction exponent:(e + self eBias)
|
5342
|
349 |
signBit:(aNumber negative ifTrue:[1] ifFalse:[0]);
|
|
350 |
yourself
|
|
351 |
].
|
|
352 |
^ self class coerce:aNumber
|
|
353 |
|
|
354 |
"Modified: / 15-06-2017 / 10:27:03 / cg"
|
|
355 |
!
|
|
356 |
|
5324
|
357 |
generality
|
|
358 |
^ 97 "/ between OctaFloat and LargeFloat
|
|
359 |
|
|
360 |
"
|
|
361 |
1 asShortFloat generality 70
|
|
362 |
1 asFloat generality 80
|
|
363 |
1 asLongFloat generality 90
|
|
364 |
1 asQuadFloat generality
|
|
365 |
1 asQDouble generality 95
|
|
366 |
1 asLargeFloat generality 100
|
|
367 |
"
|
|
368 |
! !
|
|
369 |
|
|
370 |
!IEEEFloat methodsFor:'comparing'!
|
|
371 |
|
|
372 |
< aNumber
|
5334
|
373 |
"/ thisContext isRecursive ifTrue:[self halt].
|
5324
|
374 |
^ aNumber lessFromIEEEFloat:self
|
|
375 |
!
|
|
376 |
|
|
377 |
= aNumber
|
|
378 |
^ aNumber equalFromIEEEFloat:self
|
|
379 |
! !
|
|
380 |
|
|
381 |
!IEEEFloat methodsFor:'double dispatching'!
|
|
382 |
|
|
383 |
differenceFromIEEEFloat:anIEEEFloat
|
|
384 |
"/ anIEEEFloat - self
|
|
385 |
^ self negated sumFromIEEEFloat:anIEEEFloat
|
|
386 |
!
|
|
387 |
|
|
388 |
equalFromIEEEFloat:anIEEEFloat
|
5334
|
389 |
|m1 m2 nM1 nM2 e1 e2|
|
|
390 |
|
5324
|
391 |
anIEEEFloat basicSize == self basicSize ifTrue:[
|
|
392 |
anIEEEFloat exponentSize == exponentSize ifTrue:[
|
|
393 |
1 to:self basicSize do:[:i |
|
|
394 |
(self basicAt:i) = (anIEEEFloat basicAt:i) ifFalse:[^ false].
|
|
395 |
].
|
|
396 |
^ true.
|
|
397 |
].
|
|
398 |
].
|
5334
|
399 |
|
5324
|
400 |
"/ more complicated compare
|
5334
|
401 |
|
|
402 |
self negative = anIEEEFloat negative ifFalse:[^ false].
|
|
403 |
|
|
404 |
"/ bring to same exponent, add m1 + m2, normalize
|
|
405 |
|
|
406 |
m1 := anIEEEFloat mantissaBits.
|
|
407 |
m2 := self mantissaBits.
|
|
408 |
|
|
409 |
nM1 := anIEEEFloat numBitsInMantissa.
|
|
410 |
nM2 := self numBitsInMantissa.
|
|
411 |
nM1 ~= nM2 ifTrue:[
|
|
412 |
nM1 > nM2 ifTrue:[
|
|
413 |
m2 := m2 bitShift:(nM1-nM2).
|
|
414 |
] ifFalse:[
|
|
415 |
m1 := m1 bitShift:(nM2-nM1).
|
|
416 |
].
|
|
417 |
].
|
|
418 |
m1 = m2 ifFalse:[^ false].
|
|
419 |
|
|
420 |
e1 := anIEEEFloat exponentBits - anIEEEFloat eBias.
|
|
421 |
e2 := self exponentBits - self eBias.
|
|
422 |
e1 = e2 ifFalse:[^ false].
|
|
423 |
|
|
424 |
^ true.
|
5324
|
425 |
!
|
|
426 |
|
|
427 |
lessFromIEEEFloat:anIEEEFloat
|
5342
|
428 |
|m1 m2 nM1 nM2 e1 e2|
|
|
429 |
|
|
430 |
self negative = anIEEEFloat negative ifFalse:[
|
|
431 |
^ anIEEEFloat negative
|
|
432 |
].
|
5324
|
433 |
|
|
434 |
anIEEEFloat basicSize == self basicSize ifTrue:[
|
5342
|
435 |
anIEEEFloat exponentSize == exponentSize ifTrue:[
|
|
436 |
self basicSize to:1 by:-1 do:[:i |
|
|
437 |
(anIEEEFloat basicAt:i) < (self basicAt:i) ifTrue:[^ true].
|
|
438 |
(anIEEEFloat basicAt:i) > (self basicAt:i) ifTrue:[^ false].
|
|
439 |
].
|
|
440 |
^ false.
|
|
441 |
].
|
5324
|
442 |
].
|
5342
|
443 |
|
|
444 |
"/ more complicated compare
|
|
445 |
|
|
446 |
"/ bring to same exponent, add m1 + m2, normalize
|
|
447 |
|
|
448 |
m1 := anIEEEFloat mantissaBits.
|
|
449 |
m2 := self mantissaBits.
|
|
450 |
|
|
451 |
nM1 := anIEEEFloat numBitsInMantissa.
|
|
452 |
nM2 := self numBitsInMantissa.
|
|
453 |
nM1 ~= nM2 ifTrue:[
|
|
454 |
nM1 > nM2 ifTrue:[
|
|
455 |
m2 := m2 bitShift:(nM1-nM2).
|
|
456 |
] ifFalse:[
|
|
457 |
m1 := m1 bitShift:(nM2-nM1).
|
|
458 |
].
|
|
459 |
].
|
|
460 |
|
|
461 |
e1 := anIEEEFloat exponent.
|
|
462 |
e2 := self exponent.
|
|
463 |
e1 = e2 ifTrue:[^ m1 < m2].
|
|
464 |
^ e1 < e2.
|
5324
|
465 |
!
|
|
466 |
|
|
467 |
productFromIEEEFloat:anIEEEFloat
|
|
468 |
"/ anIEEEFloat * self
|
|
469 |
|
5334
|
470 |
|m1 m2 e1 e2 m e signBit hi shift nM1 nM2 nM|
|
5331
|
471 |
|
5324
|
472 |
"/ m1 * m2 / e1 + e2
|
|
473 |
|
|
474 |
m1 := anIEEEFloat mantissaBits.
|
|
475 |
m2 := self mantissaBits.
|
5334
|
476 |
|
|
477 |
nM1 := anIEEEFloat numBitsInMantissa.
|
|
478 |
nM2 := nM := self numBitsInMantissa.
|
|
479 |
nM1 ~= nM2 ifTrue:[
|
|
480 |
nM1 > nM2 ifTrue:[
|
|
481 |
m2 := m2 bitShift:(nM1-nM2).
|
|
482 |
nM := nM1
|
|
483 |
] ifFalse:[
|
|
484 |
m1 := m1 bitShift:(nM2-nM1).
|
|
485 |
nM := nM2
|
|
486 |
].
|
|
487 |
].
|
|
488 |
|
5324
|
489 |
e1 := anIEEEFloat exponentBits.
|
|
490 |
e2 := self exponentBits.
|
|
491 |
e := (e1 - anIEEEFloat eBias) + (e2 - self eBias).
|
|
492 |
|
|
493 |
"/ 1xxxx....xxxx * 1xxxx....xxxx
|
|
494 |
m := m1 * m2.
|
|
495 |
"/ now have nB bits too many
|
5334
|
496 |
e := e - nM.
|
5324
|
497 |
|
|
498 |
signBit := (self negative == anIEEEFloat negative) ifTrue:[0] ifFalse:[1].
|
|
499 |
m == 0 ifTrue:[self halt].
|
|
500 |
|
|
501 |
hi := m highBit.
|
5334
|
502 |
shift := (nM + 1) - hi.
|
5324
|
503 |
m := m bitShift:shift.
|
|
504 |
e := e - shift.
|
5334
|
505 |
m := m bitAnd:(1 bitShift:nM)-1.
|
5324
|
506 |
|
|
507 |
e := e + self eBias.
|
|
508 |
^ self class
|
|
509 |
size:(self basicSize*8) exponentSize:exponentSize
|
|
510 |
fraction:m exponent:e signBit:signBit.
|
|
511 |
|
|
512 |
"
|
|
513 |
self assert:((IEEEFloat fromFloat:1.0) * (IEEEFloat fromFloat:1.0)) = (IEEEFloat fromFloat:1.0)
|
|
514 |
self assert:((IEEEFloat fromFloat:1.0) * (IEEEFloat fromFloat:1.0)) exponent = 1.0 exponent
|
|
515 |
self assert:((IEEEFloat fromFloat:1.0) * (IEEEFloat fromFloat:1.0)) mantissaBits = 1.0 mantissaBits
|
|
516 |
|
|
517 |
self assert:((4.0 asIEEEFloat) * (2.0 asIEEEFloat)) = (8.0 asIEEEFloat)
|
|
518 |
self assert:((-4.0 asIEEEFloat) * (2.0 asIEEEFloat)) = (-8.0 asIEEEFloat)
|
|
519 |
self assert:((4.0 asIEEEFloat) * (-2.0 asIEEEFloat)) = (-8.0 asIEEEFloat)
|
|
520 |
self assert:((-4.0 asIEEEFloat) * (-2.0 asIEEEFloat)) = (8.0 asIEEEFloat)
|
|
521 |
|
|
522 |
self assert:((IEEEFloat fromFloat:100.0) * (IEEEFloat fromFloat:100.0)) = (IEEEFloat fromFloat:10000.0)
|
|
523 |
self assert:((IEEEFloat fromFloat:1e-20) * (IEEEFloat fromFloat:1e20)) = (IEEEFloat fromFloat:(1e-20 * 1e20))
|
|
524 |
"
|
|
525 |
!
|
|
526 |
|
|
527 |
quotientFromIEEEFloat:anIEEEFloat
|
|
528 |
"/ anIEEEFloat / self
|
|
529 |
|
5334
|
530 |
|m1 m2 e1 e2 m e signBit nM1 nM2 nM hi shift|
|
5331
|
531 |
|
5324
|
532 |
"/ m1 / m2 / e1 - e2
|
|
533 |
|
5346
|
534 |
m1 := anIEEEFloat mantissaBits.
|
|
535 |
m1 == 0 ifTrue:[^ anIEEEFloat ].
|
5324
|
536 |
m2 := self mantissaBits.
|
5334
|
537 |
|
|
538 |
nM1 := anIEEEFloat numBitsInMantissa.
|
|
539 |
nM2 := nM := self numBitsInMantissa.
|
|
540 |
nM1 ~= nM2 ifTrue:[
|
|
541 |
nM1 > nM2 ifTrue:[
|
|
542 |
m2 := m2 bitShift:(nM1-nM2).
|
|
543 |
nM := nM1
|
|
544 |
] ifFalse:[
|
|
545 |
m1 := m1 bitShift:(nM2-nM1).
|
|
546 |
nM := nM2
|
|
547 |
].
|
|
548 |
].
|
|
549 |
|
5324
|
550 |
e1 := anIEEEFloat exponentBits.
|
|
551 |
e2 := self exponentBits.
|
|
552 |
e := (e1 - anIEEEFloat eBias) - (e2 - self eBias).
|
|
553 |
|
|
554 |
"/ 1xxxx....xxxx / 1xxxx....xxxx
|
5334
|
555 |
m := (m1 bitShift:nM) // m2.
|
5324
|
556 |
"/ rest := m1 \\ m2.
|
|
557 |
|
|
558 |
signBit := (self negative == anIEEEFloat negative) ifTrue:[0] ifFalse:[1].
|
|
559 |
m == 0 ifTrue:[self halt].
|
|
560 |
|
|
561 |
hi := m highBit.
|
5334
|
562 |
shift := (nM + 1) - hi.
|
5324
|
563 |
m := m bitShift:shift.
|
|
564 |
e := e - shift.
|
5334
|
565 |
m := m bitAnd:(1 bitShift:nM)-1.
|
5324
|
566 |
|
|
567 |
e := e + self eBias.
|
|
568 |
^ self class
|
|
569 |
size:(self basicSize*8) exponentSize:exponentSize
|
|
570 |
fraction:m exponent:e signBit:signBit.
|
|
571 |
|
|
572 |
"
|
|
573 |
self assert:((IEEEFloat fromFloat:1.0) / (IEEEFloat fromFloat:1.0)) = (IEEEFloat fromFloat:1.0)
|
|
574 |
self assert:((IEEEFloat fromFloat:1.0) / (IEEEFloat fromFloat:1.0)) exponent = 1.0 exponent
|
|
575 |
self assert:((IEEEFloat fromFloat:1.0) / (IEEEFloat fromFloat:1.0)) mantissaBits = 1.0 mantissaBits
|
|
576 |
|
|
577 |
self assert:((4.0 asIEEEFloat) / (2.0 asIEEEFloat)) = (2.0 asIEEEFloat)
|
|
578 |
self assert:((-4.0 asIEEEFloat) / (2.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
579 |
self assert:((4.0 asIEEEFloat) / (-2.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
580 |
self assert:((-4.0 asIEEEFloat) / (-2.0 asIEEEFloat)) = (2.0 asIEEEFloat)
|
|
581 |
|
|
582 |
self assert:((IEEEFloat fromFloat:100.0) / (IEEEFloat fromFloat:2.0)) = (IEEEFloat fromFloat:50.0)
|
|
583 |
self assert:((IEEEFloat fromFloat:1e-20) / (IEEEFloat fromFloat:1e20)) = (IEEEFloat fromFloat:(1e-20 / 1e20))
|
|
584 |
"
|
|
585 |
!
|
|
586 |
|
|
587 |
sumFromIEEEFloat:anIEEEFloat
|
|
588 |
"/ anIEEEFloat + self
|
|
589 |
|
5334
|
590 |
|m1 m2 e1 e2 m e signBit hi shift nM1 nM2 nM|
|
5331
|
591 |
|
5324
|
592 |
"/ bring to same exponent, add m1 + m2, normalize
|
|
593 |
|
|
594 |
m1 := anIEEEFloat mantissaBits. m1 == 0 ifTrue:[^ self].
|
|
595 |
m2 := self mantissaBits. m2 == 0 ifTrue:[^ anIEEEFloat].
|
5334
|
596 |
|
|
597 |
nM1 := anIEEEFloat numBitsInMantissa.
|
|
598 |
nM2 := nM := self numBitsInMantissa.
|
|
599 |
nM1 ~= nM2 ifTrue:[
|
|
600 |
nM1 > nM2 ifTrue:[
|
|
601 |
m2 := m2 bitShift:(nM1-nM2).
|
|
602 |
nM := nM1
|
|
603 |
] ifFalse:[
|
|
604 |
m1 := m1 bitShift:(nM2-nM1).
|
|
605 |
nM := nM2
|
|
606 |
].
|
|
607 |
].
|
|
608 |
|
5331
|
609 |
e1 := anIEEEFloat exponentBits - anIEEEFloat eBias.
|
|
610 |
e2 := self exponentBits - self eBias.
|
5324
|
611 |
e := e1.
|
|
612 |
e1 = e2 ifFalse:[
|
|
613 |
e1 > e2 ifTrue:[
|
|
614 |
m2 := m2 rightShift:(e1 - e2).
|
|
615 |
e := e1.
|
|
616 |
] ifFalse:[
|
|
617 |
"/ e2 > e1
|
|
618 |
m1 := m1 rightShift:(e2 - e1).
|
|
619 |
e := e2.
|
|
620 |
]
|
|
621 |
].
|
|
622 |
|
|
623 |
signBit := 0.
|
|
624 |
"/ ok, add the mantissae
|
|
625 |
self negative ifTrue:[
|
|
626 |
anIEEEFloat negative ifTrue:[
|
|
627 |
"/ - (anIEEEFloat + self)
|
|
628 |
m := m1 + m2.
|
|
629 |
signBit := 1.
|
|
630 |
] ifFalse:[
|
|
631 |
"/ anIEEEFloat - self
|
|
632 |
m := m1 - m2
|
|
633 |
].
|
|
634 |
] ifFalse:[
|
|
635 |
anIEEEFloat negative ifTrue:[
|
|
636 |
"/ self - anIEEEFloat
|
|
637 |
m := m2 - m1.
|
|
638 |
] ifFalse:[
|
|
639 |
"/ (anIEEEFloat + self)
|
|
640 |
m := m1 + m2.
|
|
641 |
].
|
|
642 |
].
|
|
643 |
m == 0 ifTrue:[^ self class zero].
|
|
644 |
m < 0 ifTrue:[
|
|
645 |
m := m negated.
|
|
646 |
signBit := 1-signBit
|
|
647 |
].
|
|
648 |
|
|
649 |
hi := m highBit.
|
5334
|
650 |
shift := (nM + 1) - hi.
|
5324
|
651 |
m := m bitShift:shift.
|
|
652 |
e := e - shift.
|
5334
|
653 |
m := m bitAnd:(1 bitShift:nM)-1.
|
5324
|
654 |
|
|
655 |
^ self class
|
|
656 |
size:(self basicSize*8) exponentSize:exponentSize
|
5334
|
657 |
fraction:m exponent:(e + self eBias) signBit:signBit.
|
5324
|
658 |
|
|
659 |
"
|
|
660 |
seee eeee eeee mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
|
|
661 |
0100 0000 0000 0000 0000 ...... 2.0
|
|
662 |
0011 1111 1111 0000 0000 ...... 1.0
|
|
663 |
0400 0000 0001 0000 0000 ...... 4.0
|
|
664 |
|
|
665 |
self assert:(IEEEFloat fromFloat:1.0) exponent = 1.0 exponent.
|
|
666 |
self assert:(IEEEFloat fromFloat:1.0) mantissaBits = 1.0 mantissaBits.
|
|
667 |
self assert:(IEEEFloat fromFloat:1.0) eBias = 1.0 eBias.
|
|
668 |
|
|
669 |
self assert:(IEEEFloat fromFloat:2.0) exponent = 2.0 exponent.
|
|
670 |
self assert:(IEEEFloat fromFloat:2.0) mantissaBits = 2.0 mantissaBits.
|
|
671 |
self assert:(IEEEFloat fromFloat:2.0) eBias = 2.0 eBias.
|
|
672 |
|
|
673 |
self assert:(IEEEFloat fromFloat:4.0) exponent = 4.0 exponent.
|
|
674 |
self assert:(IEEEFloat fromFloat:4.0) mantissaBits = 4.0 mantissaBits.
|
|
675 |
self assert:(IEEEFloat fromFloat:4.0) eBias = 4.0 eBias.
|
|
676 |
|
|
677 |
self assert:(IEEEFloat fromFloat:3.0) exponent = 3.0 exponent.
|
|
678 |
self assert:(IEEEFloat fromFloat:3.0) mantissaBits = 3.0 mantissaBits.
|
|
679 |
self assert:(IEEEFloat fromFloat:3.0) eBias = 3.0 eBias.
|
|
680 |
|
|
681 |
self assert:(IEEEFloat fromFloat:10.0) exponent = 10.0 exponent.
|
|
682 |
self assert:(IEEEFloat fromFloat:10.0) mantissaBits = 10.0 mantissaBits.
|
|
683 |
self assert:(IEEEFloat fromFloat:10.0) eBias = 10.0 eBias.
|
|
684 |
|
|
685 |
self assert:(IEEEFloat fromFloat:1e-3) exponent = 1e-3 exponent.
|
|
686 |
self assert:(IEEEFloat fromFloat:1e-3) mantissaBits = 1e-3 mantissaBits.
|
|
687 |
self assert:(IEEEFloat fromFloat:1e-3) eBias = 1e-3 eBias.
|
|
688 |
|
|
689 |
(IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:1.0) = (IEEEFloat fromFloat:2.0)
|
|
690 |
((IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:1.0)) exponent = 2.0 exponent
|
|
691 |
((IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:1.0)) mantissaBits = 2.0 mantissaBits
|
|
692 |
|
|
693 |
self assert:((-4.0 asIEEEFloat) + (2.0 asIEEEFloat)) = (-2.0 asIEEEFloat)
|
|
694 |
|
|
695 |
(IEEEFloat fromFloat:2.0)
|
|
696 |
self assert:((IEEEFloat fromFloat:2.0) + (IEEEFloat fromFloat:2.0)) = (IEEEFloat fromFloat:4.0)
|
|
697 |
self assert:((IEEEFloat fromFloat:4.0) + (IEEEFloat fromFloat:4.0)) = (IEEEFloat fromFloat:8.0)
|
|
698 |
self assert:((IEEEFloat fromFloat:0.0) + (IEEEFloat fromFloat:1.0)) = (IEEEFloat fromFloat:1.0)
|
|
699 |
self assert:((IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:0.0)) = (IEEEFloat fromFloat:1.0)
|
|
700 |
self assert:((IEEEFloat fromFloat:1.0) + (IEEEFloat fromFloat:2.0)) = (IEEEFloat fromFloat:3.0)
|
|
701 |
self assert:((IEEEFloat fromFloat:100.0) + (IEEEFloat fromFloat:2.0)) = (IEEEFloat fromFloat:102.0)
|
|
702 |
self assert:((IEEEFloat fromFloat:2.0) + (IEEEFloat fromFloat:100.0)) = (IEEEFloat fromFloat:102.0)
|
|
703 |
|
|
704 |
self assert:((IEEEFloat fromFloat:4.0) + (IEEEFloat fromFloat:-2.0)) = (IEEEFloat fromFloat:2.0)
|
|
705 |
self assert:((IEEEFloat fromFloat:8.0) + (IEEEFloat fromFloat:-2.0)) = (IEEEFloat fromFloat:6.0)
|
|
706 |
self assert:((IEEEFloat fromFloat:100.0) + (IEEEFloat fromFloat:-2.0)) = (IEEEFloat fromFloat:98.0)
|
|
707 |
self assert:((IEEEFloat fromFloat:-2.0) + (IEEEFloat fromFloat:100.0)) = (IEEEFloat fromFloat:98.0)
|
|
708 |
self assert:((IEEEFloat fromFloat:-2.0) + (IEEEFloat fromFloat:-100.0)) = (IEEEFloat fromFloat:-102.0)
|
|
709 |
|
|
710 |
self assert:((IEEEFloat fromFloat:-0.1) + (IEEEFloat fromFloat:0.1)) = (IEEEFloat fromFloat:0.0)
|
|
711 |
"
|
|
712 |
! !
|
|
713 |
|
|
714 |
!IEEEFloat methodsFor:'inspecting'!
|
|
715 |
|
|
716 |
inspectorExtraAttributes
|
5345
|
717 |
^ super inspectorExtraAttributes "/ #()
|
|
718 |
! !
|
|
719 |
|
|
720 |
!IEEEFloat methodsFor:'mathematical functions'!
|
|
721 |
|
|
722 |
log10
|
|
723 |
"return log base-10 of the receiver.
|
|
724 |
Raises an exception, if the receiver is less or equal to zero.
|
|
725 |
Here, fallback to the general logarithm code."
|
|
726 |
|
|
727 |
^ self ln / (self coerce:Float ln10)
|
5324
|
728 |
! !
|
|
729 |
|
|
730 |
!IEEEFloat methodsFor:'printing & storing'!
|
|
731 |
|
|
732 |
printOn:aStream
|
|
733 |
thisContext isRecursive ifTrue:[
|
5345
|
734 |
aStream nextPutAll:'IEEEFloat (recursion error while printing)'.
|
5324
|
735 |
^ self.
|
|
736 |
].
|
5342
|
737 |
"/ super printOn:aStream.
|
5324
|
738 |
PrintfScanf printf:'%g' on:aStream argument:self.
|
|
739 |
! !
|
|
740 |
|
|
741 |
!IEEEFloat methodsFor:'queries'!
|
|
742 |
|
|
743 |
eBias
|
|
744 |
"Answer the exponent's bias;
|
|
745 |
that is the offset of the zero exponent when stored"
|
|
746 |
|
|
747 |
^ (1 bitShift:exponentSize-1) - 1
|
|
748 |
|
|
749 |
"
|
|
750 |
1.0 numBitsInExponent 11
|
|
751 |
1.0 eBias 1023
|
|
752 |
1.0 emin -1022
|
|
753 |
1.0 emax 1023
|
|
754 |
1.0 fmin 2.2250738585072E-308
|
|
755 |
1.0 fmax 1.79769313486232E+308
|
|
756 |
"
|
|
757 |
!
|
|
758 |
|
5345
|
759 |
epsilon
|
|
760 |
^ self radix asFloat raisedToInteger:(1 - self precision)
|
|
761 |
!
|
|
762 |
|
5324
|
763 |
numBitsInExponent
|
|
764 |
^ exponentSize
|
|
765 |
|
|
766 |
"
|
|
767 |
1.0 numBitsInExponent 11
|
|
768 |
1.0 numBitsInMantissa 52
|
|
769 |
1.0 precision 53
|
|
770 |
1.0 decimalPrecision 16
|
|
771 |
1.0 eBias 1023
|
|
772 |
1.0 emin -1022
|
|
773 |
1.0 emax 1023
|
|
774 |
1.0 fmin 2.2250738585072E-308
|
|
775 |
1.0 fmax 1.79769313486232E+308
|
|
776 |
"
|
|
777 |
!
|
|
778 |
|
|
779 |
numBitsInMantissa
|
5334
|
780 |
"answer the number of bits in the mantissa (the significant).
|
|
781 |
This is an IEEE float,
|
|
782 |
where n*8 - 1 - exponentSize (n=nr of bytes)
|
|
783 |
bits are available in the mantissa (the hidden bit is not counted here):
|
|
784 |
s ee...ee mmm...mmm
|
5324
|
785 |
"
|
5334
|
786 |
|
|
787 |
^ (self basicSize * 8) - exponentSize - 1 "/ sign
|
5342
|
788 |
!
|
|
789 |
|
5345
|
790 |
precision
|
|
791 |
^ self numBitsInMantissa + 1 - self numBitsInIntegerPart
|
|
792 |
!
|
|
793 |
|
5342
|
794 |
radix
|
|
795 |
"Answer the exponent's radix"
|
|
796 |
|
|
797 |
^ 2
|
5324
|
798 |
! !
|
|
799 |
|
|
800 |
!IEEEFloat methodsFor:'testing'!
|
|
801 |
|
|
802 |
isFinite
|
|
803 |
"return true, if the receiver is a finite float (not NaN or +/-INF)"
|
|
804 |
|
|
805 |
^ self exponentBits ~= ((1 bitShift:self numBitsInExponent)-1)
|
|
806 |
|
|
807 |
"Modified: 12.2.1997 / 16:45:27 / cg"
|
|
808 |
!
|
|
809 |
|
|
810 |
isInfinite
|
|
811 |
"return true, if the receiver is an infinite float (+/-INF)"
|
|
812 |
|
|
813 |
^ (self exponentBits = ((1 bitShift:self numBitsInExponent)-1))
|
|
814 |
and:[ self mantissaBits = 0 ]
|
|
815 |
|
|
816 |
"Modified: 12.2.1997 / 16:45:27 / cg"
|
|
817 |
|
|
818 |
|
|
819 |
!
|
|
820 |
|
|
821 |
isNaN
|
|
822 |
"return true, if the receiver is an invalid float (NaN - not a number)"
|
|
823 |
|
|
824 |
^ (self exponentBits = ((1 bitShift:self numBitsInExponent)-1))
|
|
825 |
and:[ self mantissaBits ~= 0 ]
|
|
826 |
|
|
827 |
"Modified: 12.2.1997 / 16:45:27 / cg"
|
|
828 |
!
|
|
829 |
|
5333
|
830 |
isZero
|
|
831 |
"return true, if the receiver is zero"
|
|
832 |
|
|
833 |
1 to:self size do:[:i |
|
|
834 |
(self basicAt:i) ~~ 0 ifTrue:[^ false].
|
|
835 |
].
|
|
836 |
^ true
|
|
837 |
|
|
838 |
!
|
|
839 |
|
5324
|
840 |
negative
|
|
841 |
"return true if the receiver is less than zero."
|
|
842 |
|
|
843 |
^ (self basicAt:self basicSize) bitTest:16r80
|
|
844 |
!
|
|
845 |
|
|
846 |
positive
|
|
847 |
"return true if the receiver is greater or equal to zero."
|
|
848 |
|
|
849 |
^ ((self basicAt:self basicSize) bitTest:16r80) not
|
|
850 |
! !
|
|
851 |
|
5345
|
852 |
!IEEEFloat methodsFor:'truncation & rounding'!
|
|
853 |
|
|
854 |
ceilingAsFloat
|
|
855 |
"return the smallest integer-valued float greater or equal to the receiver.
|
|
856 |
This is much like #ceiling, but avoids a (possibly expensive) conversion
|
|
857 |
of the result to an integer.
|
|
858 |
It may be useful, if the result is to be further used in another float-operation."
|
|
859 |
|
|
860 |
self halt.
|
|
861 |
|
|
862 |
"
|
|
863 |
1.5 asIEEEFloat ceilingAsFloat
|
|
864 |
"
|
5346
|
865 |
!
|
|
866 |
|
|
867 |
fractionPart
|
|
868 |
"extract the after-decimal fraction part.
|
|
869 |
such that (self truncated + self fractionPart) = self"
|
|
870 |
|
|
871 |
|e eB n m nM|
|
|
872 |
|
|
873 |
e := self exponentBits.
|
|
874 |
eB := self eBias.
|
|
875 |
e < eB ifTrue:[
|
|
876 |
"/ integer part is zero
|
|
877 |
^ self
|
|
878 |
].
|
|
879 |
m := self mantissaBits.
|
|
880 |
n := e - eB + 1.
|
|
881 |
e := eB - 1.
|
|
882 |
nM := self numBitsInMantissa.
|
|
883 |
m := (m bitShift:n) bitAnd:(1 bitShift:nM)-1.
|
|
884 |
"/ normalize
|
|
885 |
[m highBit == nM] whileTrue:[
|
|
886 |
m := m bitShift:1.
|
|
887 |
e := e - 1.
|
|
888 |
].
|
|
889 |
^ ((self class basicNew:self basicSize) exponentSize:exponentSize)
|
|
890 |
setFraction:m exponent:e signBit:(self negative ifTrue:[1] ifFalse:[0]).
|
|
891 |
|
|
892 |
"
|
|
893 |
1.5 asIEEEFloat ceilingAsFloat
|
|
894 |
"
|
|
895 |
!
|
|
896 |
|
|
897 |
truncated
|
|
898 |
"return the receiver truncated towards zero as an integer"
|
|
899 |
|
|
900 |
^ super truncated.
|
|
901 |
|
|
902 |
"
|
|
903 |
1.5 asIEEEFloat truncated
|
|
904 |
"
|
|
905 |
!
|
|
906 |
|
|
907 |
truncatedAsFloat
|
|
908 |
"return the receiver truncated towards zero as a float.
|
|
909 |
This is much like #truncated, but avoids a (possibly expensive) conversion
|
|
910 |
of the result to an integer.
|
|
911 |
It may be useful, if the result is to be further used in another
|
|
912 |
float-operation."
|
|
913 |
|
|
914 |
^ self - self fractionPart.
|
|
915 |
|
|
916 |
"
|
|
917 |
1.5 truncatedAsFloat
|
|
918 |
1.0 truncatedAsFloat
|
|
919 |
1.5 asIEEEFloat truncatedAsFloat
|
|
920 |
1.0 asIEEEFloat truncatedAsFloat
|
|
921 |
"
|
5345
|
922 |
! !
|
|
923 |
|
5278
|
924 |
!IEEEFloat class methodsFor:'documentation'!
|
|
925 |
|
|
926 |
version_CVS
|
|
927 |
^ '$Header$'
|
|
928 |
! !
|
|
929 |
|