[squeak-dev] Float storage as Double?

Levente Uzonyi leves at elte.hu
Tue Apr 27 07:15:30 UTC 2010


On Mon, 26 Apr 2010, Chris Muller wrote:

>> That's not true, every Float stores 64 bits.
>
> Hi Levente, it does?  Does that mean that (Float fromIEEE32Bit:
> myFloat asIEEE32BitWord) loses information?  What would be a more
> precise method of serialization then?

From Float's class comment:
"My instances represent IEEE-754 floating-point double-precision numbers."
So using the 32-bit format loses 32 bits:

myFloat := 1.234567891234567. "===>  1.234567891234567"
newFloat := Float fromIEEE32Bit: myFloat asIEEE32BitWord. "===> 1.234567880630493"
newFloat = myFloat. "===>  false"

A Float is an "array" of two words (32-bit). You can serialize the two 
words and create a new Float object from them later:

myFloat := 1.234567891234567. "===>  1.234567891234567"
word1 := myFloat basicAt: 1. "===> 1072939210"
word2 := myFloat basicAt: 2. "===> 1121498327".
newFloat := (Float basicNew: 2)
 	basicAt: 1 put: word1;
 	basicAt: 2 put: word2;
 	yourself. "===> 1.234567891234567"
newFloat = myFloat. "===> true"


Levente

>
>
>> Just because some digits are
>> not shown in the printString, it doesn't mean they aren't there:
>>
>> 1.01 printString. "==> '1.01'"
>> 1.01 printShowingDecimalPlaces: 60. "==>
>> '1.010000000000000008881784197001252323389053344726562500000000'"
>>
>>>
>>> I can work up a "fixed" point Complex number simply by writing over the
>>> internal ScaledDecimal representation using using  z real: (z real) asString
>>> , but this is rather a lot of overhead and for precision less than 15ish
>>> decimal places, it would be nice to use hardware.
>>>
>>>
>>> Any relatively easy way to do this (store native precision floating
>>> point)?
>>
>> It's done, see above.
>>
>>>
>>> is there a faster way to truncate a ScaledDecimal than the above?
>>
>> Sure, don't convert it to strings. :) I think you shouldn't use
>> ScaledDecimal at all, since it's just a combination of a fraction and scale.
>> You only need the fraction. Use #truncateTo: to truncate a number.
>>
>>
>> Levente
>>
>>>
>>>
>>> Lawson
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>
>


More information about the Squeak-dev mailing list