-
Notifications
You must be signed in to change notification settings - Fork 113
Description
There appear to be two situations where values (or lack thereof) in
@@cached_units seem to adversely affect the scalar values of units that are
created or converted.
The issues that have been observed are as follows:
Firstly, when creating two identical Units one after the other, the first time,
(when the unit type in question is not in the cache), the float value that is
passed in is converted to an integer/fixnum scalar value.
For example:
(byebug) RubyUnits::Unit.clear_cache
true
(byebug) test = RubyUnits::Unit.new(81.0, 'lbs/acre').scalar
81
(byebug) test.class
FixnumHowever, if we then immediately create another identical Unit, the float value
is converted to a Float scalar value.
(byebug) test2 = RubyUnits::Unit.new(81.0, 'lbs/acre').scalar
81.0
(byebug) test2.class
FloatSecondly, this situation also affects the result if the units are converted to
the same units after they are created:
(byebug) RubyUnits::Unit.clear_cache
true
(byebug) test = RubyUnits::Unit.new(81.0, 'lbs/acre').convert_to('lbs/acre')
81 lbs/acre
(byebug) test.scalar.class
Rational
(byebug) test2 = RubyUnits::Unit.new(81.0, 'lbs/acre').convert_to('lbs/acre')
81 lbs/acre
(byebug) test2.scalar.class
FloatFurthermore, the scalar above, ending up as a float, loses precision (it is
originally passed in as 81.0):
(byebug) test2.scalar
80.99999999999999I have created some tests which illustrate the issue, which you can see here:
master...agworld:float-cache-issue