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*”)
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*”)
Read IEEE 754 - Wikipedia about how floating point
numbers
are stored. 32-bit float doesn’t have enough precision to store 0.1’s
bitpattern. That is why you should never compare floating point numbers
directly.
Jan
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.