Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to_reduced_units() fails with g*t/kg #1427

Closed
brwe opened this issue Nov 29, 2021 · 2 comments · Fixed by #1449
Closed

to_reduced_units() fails with g*t/kg #1427

brwe opened this issue Nov 29, 2021 · 2 comments · Fixed by #1449
Labels

Comments

@brwe
Copy link

brwe commented Nov 29, 2021

In am trying to run the following example:

import pint

ureg = pint.UnitRegistry()
 
ridiculous_factor = 0.5 * ureg.g * ureg.t / ureg.kg
ridiculous_factor.to_reduced_units()

But this fails with:

---------------------------------------------------------------------------
DimensionalityError                       Traceback (most recent call last)
<ipython-input-211-e3a1dda5eff7> in <module>
      4 
      5 ridiculous_factor = 0.5 * ureg.g * ureg.t / ureg.kg
----> 6 ridiculous_factor.to_reduced_units()

~/.local/lib/python3.9/site-packages/pint/quantity.py in to_reduced_units(self)
    800         # can we make this more efficient?
    801         newq = copy.copy(self)
--> 802         newq.ito_reduced_units()
    803         return newq
    804 

~/.local/lib/python3.9/site-packages/pint/quantity.py in ito_reduced_units(self)
    790                         break
    791 
--> 792         return self.ito(newunits)
    793 
    794     def to_reduced_units(self) -> Quantity[_MagnitudeType]:

~/.local/lib/python3.9/site-packages/pint/quantity.py in ito(self, other, *contexts, **ctx_kwargs)
    699         other = to_units_container(other, self._REGISTRY)
    700 
--> 701         self._magnitude = self._convert_magnitude(other, *contexts, **ctx_kwargs)
    702         self._units = other
    703 

~/.local/lib/python3.9/site-packages/pint/quantity.py in _convert_magnitude(self, other, *contexts, **ctx_kwargs)
    678                 return self._REGISTRY.convert(self._magnitude, self._units, other)
    679 
--> 680         return self._REGISTRY.convert(
    681             self._magnitude,
    682             self._units,

~/.local/lib/python3.9/site-packages/pint/registry.py in convert(self, value, src, dst, inplace)
   1001             return value
   1002 
-> 1003         return self._convert(value, src, dst, inplace)
   1004 
   1005     def _convert(self, value, src, dst, inplace=False, check_dimensionality=True):

~/.local/lib/python3.9/site-packages/pint/registry.py in _convert(self, value, src, dst, inplace)
   1915                 value, src = src._magnitude, src._units
   1916 
-> 1917         return super()._convert(value, src, dst, inplace)
   1918 
   1919     def _get_compatible_units(self, input_units, group_or_system):

~/.local/lib/python3.9/site-packages/pint/registry.py in _convert(self, value, src, dst, inplace)
   1516 
   1517         if not (src_offset_unit or dst_offset_unit):
-> 1518             return super()._convert(value, src, dst, inplace)
   1519 
   1520         src_dim = self._get_dimensionality(src)

~/.local/lib/python3.9/site-packages/pint/registry.py in _convert(self, value, src, dst, inplace, check_dimensionality)
   1034             # then the conversion cannot be performed.
   1035             if src_dim != dst_dim:
-> 1036                 raise DimensionalityError(src, dst, src_dim, dst_dim)
   1037 
   1038         # Here src and dst have only multiplicative units left. Thus we can

DimensionalityError: Cannot convert from 'gram * metric_ton / kilogram' ([mass]) to 'dimensionless' (dimensionless)

If I replace the g with kg all works as expected.
Version is v0.18

Any pointer appreciated.

@jules-ch jules-ch added the bug label Nov 29, 2021
@jules-ch
Copy link
Collaborator

It is a bug related to to_reduced_units it does not handle well 3 units with the same dimensionality it seems.

@jules-ch
Copy link
Collaborator

Duplicate #774

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants