[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Array#pack and String#unpack query

Anton Hörnquist

9/11/2008 5:51:00 PM

Hi,

In order to learn a bit of ruby I'm trying to make a module for reading
and writing audio files in WAV format. First and foremost I'd like to
read and write files with 32-bit float bitdepth.

I'm using Array#pack and String#unpack but run into trouble with
precision.

The following of code..

arr = [ 1.0, 0.75, 0.5, 0.1, 0.025, 0.01 ]
p arr.pack("e*").unpack("e*")
p arr.pack("E*").unpack("E*")

...will output this:

[1.0, 0.75, 0.5, 0.100000001490116, 0.025000000372529,
0.00999999977648258]
[1.0, 0.75, 0.5, 0.1, 0.025, 0.01]

How come?

/Anton
--
Posted via http://www.ruby-....

2 Answers

Anton Hörnquist

9/11/2008 6:00:00 PM

0

I forgot a thing: I expected the output to be the same for "e*" and
"E*":

[1.0, 0.75, 0.5, 0.1, 0.025, 0.01]
[1.0, 0.75, 0.5, 0.1, 0.025, 0.01]

Am I wrong?

/Anton


Anton Hörnquist wrote:
> Hi,
>
> In order to learn a bit of ruby I'm trying to make a module for reading
> and writing audio files in WAV format. First and foremost I'd like to
> read and write files with 32-bit float bitdepth.
>
> I'm using Array#pack and String#unpack but run into trouble with
> precision.
>
> The following of code..
>
> arr = [ 1.0, 0.75, 0.5, 0.1, 0.025, 0.01 ]
> p arr.pack("e*").unpack("e*")
> p arr.pack("E*").unpack("E*")
>
> ...will output this:
>
> [1.0, 0.75, 0.5, 0.100000001490116, 0.025000000372529,
> 0.00999999977648258]
> [1.0, 0.75, 0.5, 0.1, 0.025, 0.01]
>
> How come?
>
> /Anton

--
Posted via http://www.ruby-....

Jan Dvorak

9/11/2008 10:20:00 PM

0

On Thursday 11 September 2008 19:59:39 Anton H=C3=B6rnquist wrote:
> > [1.0, 0.75, 0.5, 0.100000001490116, 0.025000000372529,
> > 0.00999999977648258]
> > [1.0, 0.75, 0.5, 0.1, 0.025, 0.01]
> >
> > How come?

Read http://en.wikipedia.org/wik... about how floating point numbers=
=20
are stored. 32-bit float doesn't have enough precision to store 0.1's=20
bitpattern. That is why you should never compare floating point numbers=20
directly.

Jan