Ruby floating point calculations

helo eveyone,

for a rails app i’m working on i need to do some calculations with some
floating point data. the calculations aren’t on currency so i can’t do
the
trick where you do the math with integers and have the units in pennies.
i
would like the results to be as accurate as possible with the least
amount
of work (haha). any suggestions? is there a ruby gem for more accurate
float
calculations?

thanks in advance,

stuart

Define accuracy. What are you doing here?

calculating ratios mostly, but some other stuff too. for my purposes
accuracy would be calculations being accurate to .001 … i guess i
probably just could premultiply the values i’m going to use with 1000
to account for the precision i’m trying to get and divide by 1000
after… to tell you the truth i’m not sure how bad the floating
inacuracy is generally… but i remember seeing some calculations that
were pretty far off

Ah, sounds like you want to use Rational:
http://www.ruby-doc.org/stdlib/libdoc/rational/rdoc/index.html

You can also use BigDecimal (require ‘bigdecimal’).

When using Float you get whatever your machine’s native double type
gives you, which these days is often around 16 significant decimal
digits.

Fred