r-quantities / units

Measurement units for R
https://r-quantities.github.io/units
173 stars 27 forks source link

`set_units(1, "s^-1") + set_units(1, "s")` should throw error #312

Closed ttrikalin closed 2 years ago

ttrikalin commented 2 years ago

There appears to be a bug when dealing with a unit and its inverse.

# 1\ These evals should be FALSE but is TRUE
units::ud_are_convertible("s", "s^-1")
units::ud_are_convertible("s^2", "s^-2")
units::ud_are_convertible("m/s", "s/m")

# 2\ `+` should throw error, but returns `units` object 
# with the units of the first summand. 
# Example
units::set_units(1, "s^-1") + units::set_units(1, "s")
units::set_units(1, "s") + units::set_units(1, "s^-1")

# 3\ `*` correctly simplifies product to be dimensionless
units::set_units(1, "s") * units::set_units(1, "s^-1")
Enchufa2 commented 2 years ago

This is no bug, but just reflects the design decisions made in udunits2. As a general conversion framework, it allows you to apply any kind of (valid) conversion. For example, you could define a conversion of grams of coal into energy released when it burns. There definitely is a conversion factor (which is not the same as dimensional analysis) to convert from units to their reciprocals, and udunits2 chose to support that.

We rely on udunits2, and so we are not willing to change this philosophy. If you are not happy with this behaviour, I suggest to bring this upstream.

ttrikalin commented 2 years ago

Thank your replying.
I now see in the udunits2 code that they define a reciprocal converter and understand its behavior. I was using the library to implement a rudimentary dimensional analysis check. I will work around this behavior.