python-quantities-developers team mailing list archive
-
python-quantities-developers team
-
Mailing list archive
-
Message #00109
[Bug 402830] Re: Dividing by UncertainQuantity returns wrong uncertainty value
** Changed in: python-quantities
Status: Fix Committed => Fix Released
--
Dividing by UncertainQuantity returns wrong uncertainty value
https://bugs.launchpad.net/bugs/402830
You received this bug notification because you are a member of Python
Quantities Development Team, which is subscribed to python-quantities.
Status in Physical Quantities for Python: Fix Released
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