Skip to content

Commit 46c2efd

Browse files
committed
subtraction of same element handled correctly
1 parent 3206885 commit 46c2efd

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

quantities/tests/test_uncertainty.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,10 @@ def test_uncertainquantity_divide(self):
6060
self.assertQuantityEqual((a/2).uncertainty, [0.05, 0.1 ]*pq.m)
6161
self.assertQuantityEqual(1/a, [1., 0.5]/pq.m)
6262
self.assertQuantityEqual((1/a).uncertainty, [0.1, 0.05]/pq.m)
63+
64+
def test_uncertainquantity_subtract(self):
65+
import numpy as np
66+
a = UncertainQuantity(1, 'm', 1)
67+
b = a.copy() # different object
68+
self.assertQuantityEqual(a-a, UncertainQuantity(0, 'm', 0))
69+
self.assertQuantityEqual(a-b, UncertainQuantity(0, 'm', np.sqrt(2)))

quantities/uncertainquantity.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ def __radd__(self, other):
102102
@scale_other_units
103103
def __sub__(self, other):
104104
res = super(UncertainQuantity, self).__sub__(other)
105-
u = (self.uncertainty**2+other.uncertainty**2)**0.5
105+
if self is not other:
106+
u = (self.uncertainty**2+other.uncertainty**2)**0.5
107+
else:
108+
u = self.uncertainty*0
106109
return UncertainQuantity(res, uncertainty=u, copy=False)
107110

108111
@with_doc(Quantity.__rsub__, use_header=False)

0 commit comments

Comments
 (0)