Possible bug in rb_float_new() ???

T

Tim Hunter

Francisco said:
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 Hulan 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)"
 
S

Sebastian Hungerecker

Am Sonntag 26 Juli 2009 18:11:48 schrieb Francisco Tufr=F3:
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=20
converting back from float to double, you of course won't get that precissi=
on=20
back, so the number that comes out is not the same one that came in.

HTH,
Sebastian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,924
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top