My rails app is not doing math correctly. I think this has something to do with the variable types (int vs float) but not sure what's wrong.
The root problem is this method in my Stat model:
def lean_mass
self.weight * 0.01 * (100 - self.body_fat)
end
Where
Stat.weight = 140
Stat.body_fat = 15
it returns 119.00000000000001
instead of 119
.
However, where
Stat.weight = 210
Stat.body_fat = 15
it returns 178.5
, the correct number.
Anyone know why it's throwing in that small decimal?
The datatype for weight is integer and body_fat is decimal if that helps.
Floating-point numbers cannot precisely represent all real numbers. And furthermore floating-point operations cannot precisely represent every arithmetic operation. This leads to many surprising situations.
A simple example that shows this behavior:
0.1 + 0.2
#=> 0.30000000000000004
I advise to read: https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
You can avoid most of this problems by using BigDecimal
instead of floats:
require 'bigdecimal'
BigDecimal.new('0.01') * 140 * (100 - 15)
#=> 119.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With