pint icon indicating copy to clipboard operation
pint copied to clipboard

Unexpected output for siunitx format when using Decimal nonint_type

Open tovrstra opened this issue 1 year ago • 2 comments

Minimal example: (tested with pint-0.21)

import decimal
from pint import UnitRegistry
ureg = UnitRegistry(non_int_type=decimal.Decimal)
q = ureg("8.989e9 newton * meter^2 / coulomb^2")
print(f"{q:Lx}")

Output:

\SI[]{8.989E+9}{\meter\tothe{2.000}\newton\per\coulomb\tothe{2.000}}

Expected output:

\SI[]{8.989E+9}{\meter\squared\newton\per\coulomb\squared}

For reference, the output when running the example without the non_int_type=decimal.Decimal argument:

\SI[]{8989000000.0}{\meter\squared\newton\per\coulomb\squared}

tovrstra avatar May 07 '23 07:05 tovrstra

This can be fixed by improving the siunitx_format_unit, which can be found here:

https://github.com/hgrecco/pint/blob/0b69ad47b2069e29ba48eb64b30093d53da163d6/pint/formatting.py#L444

I'm happy to create PR after the ongoing refactoring.

Some ideas for how to handle this:

  • Test if a Decimal instances is integer: d.as_integer_ratio()[1] == 1
  • It would be good to fix the analogous bug with the Fraction class while we're at it.

tovrstra avatar May 07 '23 07:05 tovrstra

This issue probably also belongs to #1757

tovrstra avatar Sep 24 '23 12:09 tovrstra