yunruse / Noether

Just another units package
MIT License
9 stars 0 forks source link

__round__ is not defined; __int__ and __float__ have ambiguity #84

Closed yunruse closed 2 months ago

yunruse commented 2 months ago

General info

round() doesn't work on Measure (or any of its subclasses).

Python 3.12.1 noether==1.1

Steps

>>> round(month(2) / year, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type Measure doesn't define __round__ method

Expected behaviour

Measure @ unit OR Measure / unit should produce the same result under round(). In other words:

Measure: round the value if and only if dimensionless - it is ambiguous otherwise! MeasureRelative: always round based on the unit. That is:

>>> round(month(2) @ year, 3)
0.167

Indeed we might want to tighten __int__ and __float__ to work in the exact same way.

yunruse commented 2 months ago

Done. Measure now works with the functions __int__, __float__, __round__, .real, .imag, .numerator, .denominator, .conjugate, .as_integer_ratio.

MeasureRelative works based on the unit provided. It also 'rounds into' the unit, so:

>>> round(month(2) @ year, 3)
60 d + 23 hr + 55 min + 19.2 s  # time
>>> round(month(2) / year, 3)
0.167

but otherwise @ and / are identical for the remaining functions. It feels subtly inconsistent, however, which might be a tripping hazard.


In development I noticed the .value property is used a concerning amount. It's just the underlying representative in base units (i.e. SI) but a bit… hackily in places. Lower priority, perhaps..?