Skip to content

Values in @@cached_units can adversely affect Unit scalar creation/conversion #172

@ethancrawford

Description

@ethancrawford

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
Fixnum

However, 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
Float

Secondly, 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
Float

Furthermore, the scalar above, ending up as a float, loses precision (it is
originally passed in as 81.0):

(byebug) test2.scalar
80.99999999999999

I have created some tests which illustrate the issue, which you can see here:
master...agworld:float-cache-issue

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions