PainterQubits / Unitful.jl

Physical quantities with arbitrary units
Other
602 stars 110 forks source link

Kelvin and °C should not be comparable? #521

Open cstjean opened 2 years ago

cstjean commented 2 years ago

Currently,

julia> 5K - 5°C
ERROR: AffineError: an invalid operation was attempted with affine quantities: 5 K - 5 °C

which is great because it's ambiguous, but

julia> 10°C - 5°C > 2°C
false

This is clearly wrong for anyone who doesn't know about Unitful quirks, and it just bit one of my colleagues. In this case, 10°C - 5°C becomes 5K, which is considered less than 2°C because 2°C == 275 K.

Thus, Kelvin and °C comparisons should be an AffineError.

lukebemish commented 2 years ago

One possible issue here is that there are times you would want to compare values in Kelvin and °C - for instance, if you've got a series of inputs to something in a mix of both, or simply if you've got values in both. It might actually be worth rethinking how affine units are thought of in general, actually - I would say that a difference between two affine values, say 200 K - 150 K, should probably not be comparable to an affine value on its own, but affine values in differing units, but the same dimension, should still be comparable, though I'm not sure if there's any good way to implement this.

cstjean commented 3 months ago

I would say that a difference between two affine values, say 200 K - 150 K, should probably not be comparable to an affine value on its own,

I'm sympathetic to that, it's similar to the 4D matrix affine transformation in 3D rendering pipelines. https://github.com/PainterQubits/Unitful.jl/pull/176 was my attempt as this, but it was superseded by Andrew's PR which didn't implement that distinction directly (but which is admittedly a lot more elegant than my PR)

See also discussion in https://github.com/PainterQubits/Unitful.jl/issues/137