Closed nodrog closed 11 years ago
Unfortunately the way alchemist works is to convert every unit into a base unit (in the case of length meters) and then convert it to the unit you wanted. So here's the math that's occuring
1 foot in meters is 0.3048 1 inch in meters is 0.0254
so to convert 1 foot to inches the following equation is done
1.0 feet * 0.3048 meters/feet = 0.3048 meters 0.3048 meters / (0.0254 meters/inches) = 12.000000000000002 inches (according to ruby)
irb(main):026:0> x = 0.3048
=> 0.3048
irb(main):027:0> x / 0.0254
=> 12.000000000000002
So adding these values together produces the 24.000000000000004.
I have considered changing the way conversion work to act more like a tree so inches would not have to know what feet in meters were in order to perform the conversion which would eliminate floating point issues.
Another option would be to utilize stdlib's BigDecimal class since it seems to be more accurate (although I don't see it claiming that anywhere...)
irb(main):028:0> require 'bigdecimal'
=> true
irb(main):029:0> BigDecimal.new("0.3048") / BigDecimal.new("0.0254")
=> #<BigDecimal:7fab84bec3f0,'0.12E2',9(27)>
irb(main):030:0> _.to_f
=> 12.0
Either way I plan to have alchemist become more precise in the future.
Master branch now uses BigDecimal, I may make this a configuration option in the future, but this will make it more precise and not have odd rounding errors.
Love the look of this gem...
But when I do the following:
i get this result:
is this as expected? units confuse the *\ out of me
thanks