magnitude icon indicating copy to clipboard operation
magnitude copied to clipboard

trying to call Magnitude.ounit with the wrong units still changes the units of the Magnitude object

Open Joedang opened this issue 7 years ago • 0 comments

This is on version 0.9.4.
Example:

In [2]: import magnitude as mag

In [2]: x= mag.mg(1,'m')

In [3]: print(x)
1.0000 m

In [4]: print(x.ounit('s'))
---------------------------------------------------------------------------
MagnitudeError                            Traceback (most recent call last)
<ipython-input-4-a7c9d9ab1404> in <module>()
----> 1 print(x.ounit('s'))

/usr/local/lib/python3.4/dist-packages/magnitude.py in ounit(self, unit)
   602         if self.out_factor.unit != self.unit:
   603             raise MagnitudeError("Inconsistent Magnitude units: %s, %s" %
--> 604                                  (self.out_factor.unit, self.unit))
   605         return self
   606

MagnitudeError: Inconsistent Magnitude units: [0, 1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0]

In [5]: print(x)
1.0000 s

In [6]: print(x*mag.mg(1,'m'))
1.0000 m2

As you can see, the signature of the Magnitude's dimensions doesn't change, but it displays the wrong units after the messed up ounit call. It really seems like it ought to not change the displayed units, if it's not a valid string for that dimensional signature. As it is now, this could (and did) cause some serious confusion, if someone (me) were to accidentally put the wrong units into ounit, especially if it's done inside of try:.

Update:
print(mg(5,'m').toval('N')) also does not throw an error. It seems to just ignore the exponent of any units that don't match.

Joedang avatar Sep 21 '17 21:09 Joedang