Improve precision in pressure conversion (#79362)

* Improve precision in pressure conversion

* Use _STANDARD_GRAVITY

* Add again pytest.approx
This commit is contained in:
epenet 2022-10-06 12:48:31 +02:00 committed by GitHub
parent 47d0598e75
commit aa0bb9c3d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 18 deletions

View File

@ -71,6 +71,10 @@ _DAYS_TO_SECS = 24 * _HRS_TO_SECS # 1 day = 24 hours = 86400 seconds
_POUND_TO_G = 453.59237
_OUNCE_TO_G = _POUND_TO_G / 16
# Pressure conversion constants
_STANDARD_GRAVITY = 9.80665
_MERCURY_DENSITY = 13.5951
# Volume conversion constants
_L_TO_CUBIC_METER = 0.001 # 1 L = 0.001 m³
_ML_TO_CUBIC_METER = 0.001 * _L_TO_CUBIC_METER # 1 mL = 0.001 L
@ -211,9 +215,9 @@ class PressureConverter(BaseUnitConverter):
PRESSURE_BAR: 1 / 100000,
PRESSURE_CBAR: 1 / 1000,
PRESSURE_MBAR: 1 / 100,
PRESSURE_INHG: 1 / 3386.389,
PRESSURE_INHG: 1 / (_IN_TO_M * 1000 * _STANDARD_GRAVITY * _MERCURY_DENSITY),
PRESSURE_PSI: 1 / 6894.757,
PRESSURE_MMHG: 1 / 133.322,
PRESSURE_MMHG: 1 / (_MM_TO_M * 1000 * _STANDARD_GRAVITY * _MERCURY_DENSITY),
}
VALID_UNITS = {
PRESSURE_PA,

View File

@ -118,7 +118,7 @@ def test_convert_from_inhg():
101.59167
)
assert pressure_util.convert(inhg, PRESSURE_INHG, PRESSURE_MMHG) == pytest.approx(
762.002
762
)
@ -126,23 +126,23 @@ def test_convert_from_mmhg():
"""Test conversion from mmHg to other units."""
inhg = 30
assert pressure_util.convert(inhg, PRESSURE_MMHG, PRESSURE_PSI) == pytest.approx(
0.580102
0.580103
)
assert pressure_util.convert(inhg, PRESSURE_MMHG, PRESSURE_KPA) == pytest.approx(
3.99966
3.99967
)
assert pressure_util.convert(inhg, PRESSURE_MMHG, PRESSURE_HPA) == pytest.approx(
39.9966
39.9967
)
assert pressure_util.convert(inhg, PRESSURE_MMHG, PRESSURE_PA) == pytest.approx(
3999.66
3999.67
)
assert pressure_util.convert(inhg, PRESSURE_MMHG, PRESSURE_MBAR) == pytest.approx(
39.9966
39.9967
)
assert pressure_util.convert(inhg, PRESSURE_MMHG, PRESSURE_CBAR) == pytest.approx(
3.99966
3.99967
)
assert pressure_util.convert(inhg, PRESSURE_MMHG, PRESSURE_INHG) == pytest.approx(
1.181099
1.181102
)

View File

@ -361,14 +361,14 @@ def test_power_convert(
(30, PRESSURE_INHG, pytest.approx(101591.67), PRESSURE_PA),
(30, PRESSURE_INHG, pytest.approx(1015.9167), PRESSURE_MBAR),
(30, PRESSURE_INHG, pytest.approx(101.59167), PRESSURE_CBAR),
(30, PRESSURE_INHG, pytest.approx(762.002), PRESSURE_MMHG),
(30, PRESSURE_MMHG, pytest.approx(0.580102), PRESSURE_PSI),
(30, PRESSURE_MMHG, pytest.approx(3.99966), PRESSURE_KPA),
(30, PRESSURE_MMHG, pytest.approx(39.9966), PRESSURE_HPA),
(30, PRESSURE_MMHG, pytest.approx(3999.66), PRESSURE_PA),
(30, PRESSURE_MMHG, pytest.approx(39.9966), PRESSURE_MBAR),
(30, PRESSURE_MMHG, pytest.approx(3.99966), PRESSURE_CBAR),
(30, PRESSURE_MMHG, pytest.approx(1.181099), PRESSURE_INHG),
(30, PRESSURE_INHG, pytest.approx(762), PRESSURE_MMHG),
(30, PRESSURE_MMHG, pytest.approx(0.580103), PRESSURE_PSI),
(30, PRESSURE_MMHG, pytest.approx(3.99967), PRESSURE_KPA),
(30, PRESSURE_MMHG, pytest.approx(39.9967), PRESSURE_HPA),
(30, PRESSURE_MMHG, pytest.approx(3999.67), PRESSURE_PA),
(30, PRESSURE_MMHG, pytest.approx(39.9967), PRESSURE_MBAR),
(30, PRESSURE_MMHG, pytest.approx(3.99967), PRESSURE_CBAR),
(30, PRESSURE_MMHG, pytest.approx(1.181102), PRESSURE_INHG),
],
)
def test_pressure_convert(