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
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
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)”
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
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