Rounding error in Ruby math
I was surprised at the answer ruby gave me when I did some basic math, it turns out to be some rounding errors.
my_age = 0.28 my_age *= 100 ==> 28.000000000000004
This isn’t the answer a human would produce, and its not a bug in Code Academy, but just how Ruby’s floating point arithmetic is computed.
…and in fact this is not specific to Ruby either. It’s a common problem with virtually all programming languages on virtually any existing hardware. You can read about it on Wikipedia or at this site.
To avoid nasty rounding errors with decimals, you can use Ruby’s standard library package bigdecimal, which includes the BigDecimal class. You can either do
BigDecimal('0.29') to convert a string representation to a BigDecimal, or use the
to_d converter method, defined in
bigdecimal/util. Then just convert a number to a BigDecimal, do math with it, convert it back to whatever you need (Float in the example below).
0.29 * 100 #=> 28.999999999999996 require 'bigdecimal' require 'bigdecimal/util' # loads the to_d method (0.29.to_d * 100).to_f #=> 29.0