# Possible bug in rb\_float\_new()?

**URL:** <https://www.ruby-forum.com/t/possible-bug-in-rb-float-new/172714>\
**Category:** Ruby\
**Created:** [July 26, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/possible-bug-in-rb-float-new/172714 "2009-07-26T18:12:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francisco\_TufrSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d9b06d/32.png) [@Francisco\_TufrSSS\_S](https://www.ruby-forum.com/u/Francisco_TufrSSS_S)\
**Post date:** [July 26, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/possible-bug-in-rb-float-new/172714/1 "2009-07-26T18:12:08Z")

</div>

Hi, when writing the ruby bindings for libsndfile i had a problem with  
float numbers.  
I have abstracted the important code and i put it right here, is  
something i’m doind not right or a bug?  
You can check the code at: [http://pastebin.com/m6c5f3ed1](http://pastebin.com/m6c5f3ed1)

---

<div class="post-metadata">

**Author:** ![Francisco\_TufrSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d9b06d/32.png) [@Francisco\_TufrSSS\_S](https://www.ruby-forum.com/u/Francisco_TufrSSS_S)\
**Post date:** [July 26, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/possible-bug-in-rb-float-new/172714/2 "2009-07-26T18:27:56Z")

</div>

Francisco Tufró wrote:

> Hi, when writing the ruby bindings for libsndfile i had a problem with  
> float numbers.  
> I have abstracted the important code and i put it right here, is  
> something i’m doind not right or a bug?  
> You can check the code at: [http://pastebin.com/m6c5f3ed1](http://pastebin.com/m6c5f3ed1)

This has been discussed quite a bit lately. Just as there are fractions  
for which no exact decimal representation exists (1/3, for example),  
there are decimal floating-point numbers for which no exact binary  
representation exists.

In an earlier thread on this topic, Chris H. said “Have a look at  
“What Every Computer Scientist Should Know About Floating-Point  
Arithmetic” ([http://docs.sun.com/source/806-3568/ncg\_goldberg.html](http://docs.sun.com/source/806-3568/ncg_goldberg.html))”

---

<div class="post-metadata">

**Author:** ![Francisco\_TufrSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d9b06d/32.png) [@Francisco\_TufrSSS\_S](https://www.ruby-forum.com/u/Francisco_TufrSSS_S)\
**Post date:** [July 26, 2009, 7:00pm UTC](https://www.ruby-forum.com/t/possible-bug-in-rb-float-new/172714/3 "2009-07-26T19:00:07Z")

</div>

Am Sonntag 26 Juli 2009 18:11:48 schrieb Francisco  
Tufró:\> Hi, when writing the ruby bindings for libsndfile i had a problem with

> float numbers.  
> I have abstracted the important code and i put it right here, is  
> something i’m doind not right or a bug?  
> You can check the code at: [http://pastebin.com/m6c5f3ed1](http://pastebin.com/m6c5f3ed1)

The problem has nothing to do with rb\_float\_new.  
As I said on IRC, you’re storing the double you get from ruby into an  
array of floats. By doing that you implicitly cast the double to float  
and  
since floats are less accurate than doubles, you lose precission. When  
converting back from float to double, you of course won’t get that  
precission  
back, so the number that comes out is not the same one that came in.

HTH,  
Sebastian
