ansys / pyansys-units

Pythonic interface for units, unit systems, and unit conversions.
http://units.docs.pyansys.com/
MIT License
6 stars 2 forks source link

Temperature Conversion is Inexact #285

Open dnwillia-work opened 3 days ago

dnwillia-work commented 3 days ago

πŸ” Before submitting the issue

🐞 Description of the bug

Here's what happens when converting 5 Celsius to Fahrenheit:

(venv) D:\ANSYSDev\pyansys\pyansys-units>python
Python 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ansys.units import Quantity, UnitRegistry
>>> ureg = UnitRegistry()
>>> five_c = Quantity(value=5.0, units=ureg.C)
>>> five_c.to(ureg.F)
Quantity (41.0000000050066, "F")

with pint:

(venv) D:\ANSYSDev\pyansys\pyansys-units>python
Python 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> five_c = ureg.Quantity(5.0, ureg.degC)
>>> five_c.to('degF')
<Quantity(41.0, 'degree_Fahrenheit')>
>>> quit()

In principle this should be exact as the conversion is:

\frac{9}{5} [F C^{-1}] * 5 [C] + 32 [F] = 41 [F]

Perhaps it's a bit pedantic, but pint seems to get it right. Pint does seem to store exact conversion factors:

https://github.com/hgrecco/pint/blob/7035daf5f1f9516b578a6cd9588a181a7ea349ba/pint/default_en.txt#L210

whereas the pyansys-units library is storing approximate ones:

https://github.com/ansys/pyansys-units/blob/9ecd864bf2d5570c7226bc952d3119fb1a4e9b5d/src/ansys/units/cfg.yaml#L171

πŸ“ Steps to reproduce

See above.

πŸ’» Which operating system are you using?

Windows

πŸ“€ Which ANSYS version are you using?

N/A

🐍 Which Python version are you using?

3.9

πŸ“¦ Installed packages

(venv) D:\ANSYSDev\pyansys\pyansys-units>python -m pip freeze
-e git+https://github.com/ansys/pyansys-units.git@9ecd864bf2d5570c7226bc952d3119fb1a4e9b5d#egg=ansys_units
appdirs==1.4.4
flexcache==0.3
flexparser==0.3.1
importlib_metadata==8.0.0
Pint==0.24.1
PyYAML==6.0.1
typing_extensions==4.12.2
zipp==3.19.2
dnwillia-work commented 3 days ago

A pragmatic change would be to update the conversion factors to 0.5555555555555555 so that there is a bit more precision. If I do that locally I get:

(venv) D:\ANSYSDev\pyansys\pyansys-units>python
Python 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ansys.units import Quantity, UnitRegistry
>>> ureg = UnitRegistry()
>>> five_c = Quantity(value=5.0, units=ureg.C)
>>> five_c.to(ureg.F)
Quantity (41.0, "F")
>>>