Doing some very basic decimal maths in Ruby (x = 1/b * c)
.
a = BigDecimal.new("1");
b = BigDecimal.new("0.8903");
c = BigDecimal.new("0.8903");
x = a / b * c;
puts x
Output: 0.9999999998379
To rule out problems I've simplified the test case to just one decimal (x = 1/b * b)
a = BigDecimal.new("1");
b = BigDecimal.new("0.8903");
x = a / b * b;
puts x
Output: 0.9999999998379
However rearranging the original formula to x = c / b
gives the correct answer.
a = BigDecimal.new("1");
b = BigDecimal.new("0.8903");
c = BigDecimal.new("0.8903");
x = c / b;
puts x
Output: 1.0
Any ideas what's causing this or whether this is a BigDecimal bug?
Thanks for the comment telling me what's wrong with floating point maths, that's why I'm using BigDecimal not floats, unless BigDecimal also has the same issue?