3

Using Python Pint, I'm getting a strange result when multiplying units of the same quantity. You would expect them to merge, but they don't. For example:

from pint import UnitRegistry
units = UnitRegistry()

Then:

(3 * units.m) * ( 5 * units.m)  

... results in:

<Quantity(15, 'meter ** 2')>

... which is correct. But if I convert one of the factors to millimeters, like so:

(3 * units.m) * ( 5000 * units.mm)

... it gives a nonsense answer:

<Quantity(15000, 'meter * millimeter')>

The same thing happens with division, and with other dimensions, like mass, time, etc.

However, addition does work:

(3 * units.m) + ( 5000 * units.mm)
<Quantity(8.0, 'meter')>

Anyone know anything about this?

  • 1
    The tutorial mentions `to_base_units` and `to_reduced_units` methods, maybe one of them does the convertion you need? https://pint.readthedocs.io/en/latest/tutorial.html – Santiago Bruno Oct 15 '18 at 23:03
  • @SantiagoBruno Sure, but shouldn't this work without doing that? Notice how it does already work correctly when doing addition. – Ethan Herdrick Oct 15 '18 at 23:20
  • 1
    Not sure. Actually I don't use this library, just was curious about it so read some docs. It says that you can instantiate `UnitRegistry` with parameter `auto_reduce_dimensions=True`. It is disabled by default because it may be an expensive operation – Santiago Bruno Oct 15 '18 at 23:28
  • @SantiagoBruno that's the answer! Thanks! Can you turn that comment into an answer so I can accept it? – Ethan Herdrick Oct 15 '18 at 23:47

1 Answers1

5

The tutorial mentions to_base_units and to_reduced_units methods, maybe one of them does the conversion you need https://pint.readthedocs.io/en/latest/tutorial.html

It also says that you can instantiate UnitRegistry with parameter auto_reduce_dimensions=True to have this done automatically. It is disabled by default because it may be an expensive operation

Santiago Bruno
  • 461
  • 2
  • 9