Robert Dober
8/9/2007 7:40:00 PM
On 8/9/07, Luis Parravicini <lparravi@gmail.com> wrote:
> Hello
>
> I'm reading Java class files using ruby and found some problems when
> handling float values. The numbers are stored using IEEE 754 format.
> After several hours trying and thinking I might be using unpack the
> wrong way, I've wrote the following code which packs a Float and then
> immediately unpacks it. And the result is different from the original
> value.
>
> ------------
> a = 0.1234567
> b = [a].pack('g').unpack('g')[0]
Hmm I am not sure that this is exactly related but I would rather find
this behavior normal, given my old memories of floating number
representation, look at this e.g.
irb(main):004:0> 0.1234567 + 1000 - 1000
=> 0.12345670000002
If you use enough bits you are fine:
b = [a].pack('G').unpack('G')
HTH
Robert
--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein