# Array#pack and String#unpack query

**URL:** <https://www.ruby-forum.com/t/array-pack-and-string-unpack-query/148334>\
**Category:** Ruby\
**Created:** [September 11, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/array-pack-and-string-unpack-query/148334 "2008-09-11T19:58:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrillo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@mrillo](https://www.ruby-forum.com/u/mrillo)\
**Post date:** [September 11, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/array-pack-and-string-unpack-query/148334/1 "2008-09-11T19:58:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mrillo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@mrillo](https://www.ruby-forum.com/u/mrillo)\
**Post date:** [September 11, 2008, 8:06pm UTC](https://www.ruby-forum.com/t/array-pack-and-string-unpack-query/148334/2 "2008-09-11T20:06:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mrillo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@mrillo](https://www.ruby-forum.com/u/mrillo)\
**Post date:** [September 12, 2008, 12:27am UTC](https://www.ruby-forum.com/t/array-pack-and-string-unpack-query/148334/3 "2008-09-12T00:27:35Z")

</div>

On Thursday 11 September 2008 19:59:39 Anton HÃ¶rnquist 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 [IEEE 754 - Wikipedia](http://en.wikipedia.org/wiki/IEEE-754) 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
