Closed trishume closed 9 years ago
Ok so I fiddled with some things and pushed the BigDecimal out to the very end of the conversion process for maximum accuracy. This means that all conversions now give a result that is accurate to ARBITRARY_CONVERSION_PRECISION
which I arbitrarily set to 20
(hence the name).
This is stellar work. Great solution.
Can you bump the version too?
And I do agree with you. We can look at making Rational
the core type for Measured
and do the conversion to BigDecmal
in the rails adapter.
Bumped version to v0.0.12
. Ready to :ship:?
:sparkling_heart:
I realized that I removed a property from these tests that should be there. I just pushed a commit that asserts exact (no threshold) equality wherever perfect conversion is possible.
asserts exact (no threshold) equality wherever perfect conversion is possible
I was thinking about this exact situation last night! Thanks. When we know we should do exact conversion we should assert as such. So great.
Yeah, I'm good with this. :+1:
Fixes #4 to some extent.
Problem
Measured builds a conversion table in
Measured::ConversionTable
but some values in the table would be wrong, for example the conversion value from feet to yards would not be3
. The reason for this is Measured's tree traversal would go through the base unitft->m->yd
usingBigDecimal
for intermediate values but them->yd
can not be done with perfect precision with decimal numbers.Solution
Use
Rational
s for intermediate values when building the conversion table. This means all intermediate multiplications will be done with perfect precision. Currently this fixes all the previously skipped tests.Todo
round(n)
feature so that CI can passRational
frominverse_conversion_amount
becauseBigDecimal
can't do thisCommentary
Really this issue stems from the fact that
BigDecimal
is used at all. In a perfect world the library would only ever useRational
because it is always perfect and never imprecise :sparkles:. By using BigDecimal we accept that some of our conversions will always be lossy, not to mention we will never be able to represent "1/3 of a meter" accurately.I'm not sure why the library was designed to use BigDecimal at all, but IMO it might be worthwhile to make
Measured
only supportRational
to avoid these issues. If the issue is storage space in the DB then we could always makeMeasured::Rails
do the conversion to/from BigDecimal at the persistence level.@kmcphillips @cyprusad @garethson