Skip to content

Commit 0056466

Browse files
authored
Merge pull request python-quantities#116 from bjodah/fix-115
Fix negative UncertainQuantity
2 parents 90aa54b + dcbb872 commit 0056466

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎quantities/tests/test_uncertainty.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ def test_uncertainquantity_multiply(self):
5151
self.assertQuantityEqual(a*2, [2, 4]*pq.m)
5252
self.assertQuantityEqual((a*2).uncertainty, [0.2,0.4]*pq.m)
5353

54+
def test_uncertainquantity_negative(self):
55+
a = UncertainQuantity([1, 2], 'm', [.1, .2])
56+
self.assertQuantityEqual(-a, [-1., -2.]*pq.m)
57+
self.assertQuantityEqual((-a).uncertainty, [0.1, 0.2]*pq.m)
58+
self.assertQuantityEqual(-a, a*-1)
59+
self.assertQuantityEqual((-a).uncertainty, (a*-1).uncertainty)
60+
5461
def test_uncertainquantity_divide(self):
5562
a = UncertainQuantity([1, 2], 'm', [.1, .2])
5663
self.assertQuantityEqual(a/a, [1., 1.])

‎quantities/uncertainquantity.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ def __mul__(self, other):
132132
def __rmul__(self, other):
133133
return self.__mul__(other)
134134

135+
def __neg__(self):
136+
return self*-1
137+
135138
@with_doc(Quantity.__truediv__, use_header=False)
136139
def __truediv__(self, other):
137140
res = super(UncertainQuantity, self).__truediv__(other)

0 commit comments

Comments
 (0)