Skip to content

Commit

Permalink
MAINT: fix unit conversion (#174)
Browse files Browse the repository at this point in the history
The original unit conversion done in propagate_data_changes
(scimath.units.quantity) can cause conversion error. This PR adds a fix
and test for that. closes issue #171

---------

Co-authored-by: Chengyu Liu <[email protected]>
  • Loading branch information
homosapien-lcy and Chengyu Liu committed Mar 22, 2023
1 parent e9c2349 commit 49468e0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 1 addition & 2 deletions scimath/units/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ def propagate_data_changes(self):
return

# Replace the predecessor's data with converted data.
new_quantity = self.change_unit_system(predecessor.units)
predecessor.data = new_quantity.data
predecessor.data = units_convert(self.data, self.units, predecessor.units)

# Recursively continue propagating.
predecessor.propagate_data_changes()
Expand Down
9 changes: 9 additions & 0 deletions scimath/units/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ def test_propagation(self):
self.assertAlmostEqual(30., q1.data, 1,
"Propagation test expected data 30, got %s" % str(q1.data))

def test_propagation_to_imperial(self):
""" Tests data propagation for a single converted quantity. """

q1 = Quantity(10.0, units='ft', family_name='depth')
q2 = q1.change_unit_system('METRIC')
q2.data = 2 * q2.data
q2.propagate_data_changes()
self.assertAlmostEqual(q1.data, 20.0)

def test_get_original(self):

q1 = Quantity(10, units='m', family_name='depth')
Expand Down

0 comments on commit 49468e0

Please sign in to comment.