Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ethancrawford opened this issue Sep 19, 2018 · 0 comments
Assignees
Labels

Comments

@ethancrawford
Copy link

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

@olbrich olbrich self-assigned this Sep 19, 2018
@olbrich olbrich added the bug label Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants