Dividing by UncertainQuantity returns wrong uncertainty value

Bug #402830 reported by Tony S Yu
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
python-quantities
Fix Released
High
Tony S Yu

Bug Description

Dividing by an UncertainQuantity doesn't give consistent behavior. For example,
when both the numerator and denomenator are UncertainQuantities, the uncertainty is correct
>>> import quantities as pq
>>> t = pq.UncertainQuantity(1, pq.s, 0.1)
>>> print pq.UncertainQuantity(1) / t
1.0 1/s
±0.1 1/s (1σ)

When only the denomenator is an UncertainQuantity, the uncertainty blows up:
>>> t = pq.UncertainQuantity(1, pq.s, 0.1)
>>> print pq.Quantity(1) / t
1.0 1/s
±10.0 1/s (1σ)

A simple patch is attached below. An alternative patch would convert `other` to an UncertainQuantity and reuse `__truediv__`.

=== modified file 'quantities/tests/test_60_uncertainty.py'
--- quantities/tests/test_60_uncertainty.py 2009-06-18 21:11:08 +0000
+++ quantities/tests/test_60_uncertainty.py 2009-07-22 01:35:49 +0000
@@ -81,3 +81,7 @@
         str(a/2),
         '[ 0.5 1. ] m\n±[ 0.05 0.1 ] m (1σ)'
     )
+ assert_equal(
+ str(1/a),
+ '[ 1. 0.5] 1/m\n±[ 0.1 0.05] 1/m (1σ)'
+ )

=== modified file 'quantities/uncertainquantity.py'
--- quantities/uncertainquantity.py 2009-06-18 20:54:16 +0000
+++ quantities/uncertainquantity.py 2009-07-22 02:24:12 +0000
@@ -148,7 +148,7 @@
     def __rtruediv__(self, other):
         temp = UncertainQuantity(
             1/self.magnitude, self.dimensionality**-1,
- 1/self.uncertainty.magnitude, copy=False
+ self.relative_uncertainty/self.magnitude, copy=False
         )
         return other * temp

Darren Dale (dsdale24)
Changed in python-quantities:
assignee: nobody → Tony S Yu (tonyyu)
importance: Undecided → High
milestone: none → 0.5
status: New → Fix Committed
Darren Dale (dsdale24)
Changed in python-quantities:
milestone: none → 0.5.0
Darren Dale (dsdale24)
Changed in python-quantities:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.