Make Quantity
comparisons less error prone
#12
Labels
type:enhancement
New feature or enhancement visitble to users
Milestone
What's needed?
We need to make it as hard as possible for users to shot themselves in the foot.
Allowing
==
comparisons withQuantity
s is generally unsafe, because it uses afloat
underneath.Proposed solution
Disallow using
==
withQuantity
s.Ideally it should be rejected by the type system (
mypy
), but it should also raise an exception (or returnNotImplemented
if type checking is not used). If possible users should be redirected to usingQuantity.isclose()
instead.Ideally
isclose()
should probably handle 0 (or values close to 0) specially, as documented inmath.isclose
.Users can always escape this by accessing the raw
float
and using==
with that in weird cases where==
might be necessary, but that should be considered a low-level operation.Use cases
This came up when trying to see if a battery pool is full or empty. Naturally one would do
soc == Percentage(100)
, but this could have issues if the soc calculation has some precision issue and it is something like 99.999999 instead. But this problem is much more general than that, users should always try to think in terms ofisclose()
instead of strict equality withfloat
s.The text was updated successfully, but these errors were encountered: