Unidata / UDUNITS-2

API and utility for arithmetic manipulation of units of physical quantities
http://www.unidata.ucar.edu/software/udunits
Other
59 stars 36 forks source link

Should gallileanRaise really be dropping the offset? #82

Open pelson opened 5 years ago

pelson commented 5 years ago

In https://github.com/Unidata/UDUNITS-2/blob/v2.2.27.6/lib/unitcore.c#L1784-L1822 offsets are dropped when raising a unit to the given power. This is demonstrated succinctly with:

$ udunits -H "K" -W "degC"
    1 K = -272.15 degC
    x/degC = (x/K) - 273.15

Clearly we can subtract 273.15 from our Kelvins to reach Celsius. But,

$ udunits2 -H "K^2" -W "degC^2"
    1 K^2 = 1 degC^2
    x/degC^2 = (x/K^2)

Suggests that if we have Kelvins² and want Celcius² we don't need to do anything to our numbers, which seems quite incorrect.

This behaviour is adopted by ut_convert, and can -in my opinion- lead to some unexpected results.

I confess I don't have the background to really have a strong physical sense of what the operation should do, but a better default behaviour would surely to honour the offset...

x/K² = x/degC² + 2 · 273.15 (x/degC)⁻² + 273.15²

And

(x/degC²) = (x/K²) - 2 · 273.15 (x/K)⁻² + 273.15²

And to raise in ut_convert if x is a negative quantity (to keep the results in ℝ+).

Have I overlooked some reason why offset units behave in the way they do, or is it just something that hasn't yet been needed/implemented?