Modifying the value of a float-like object

smichr smichr at
Fri May 1 02:13:54 EDT 2009

On Apr 18, 4:21 pm, Eric.Le.Bi... at wrote:
> On Apr 15, 5:33 pm, Arnaud Delobelle <arno... at> wrote:
> I adjusted your code in a few ways, and put the result at due credit):
> 1) There was a strange behavior, which is fixed (by performing only
> dynamic calculations of UExpr, with an optimization through a new
> "constant" class):

Although normal arithmetic operations are correlated, the correlation
is lost (AFAICT) as soon as you use a function:

>>> a=Number_with_uncert(3,1);y=a+cos(a);y.error
>>> #check it manually
>>> def f(a):
... 	return a+cos(a)
>>> h=1e-5;print (f(3+h)-f(3))/h
>>> #check it analytically
>>> 1-sin(3)

It's effectively creating a new variable, tmp=cos(a) so y looks like a
+tmp and the uncertainty in this is (a.error**2 + tmp.error**2)**.5:
>>> a
<class '__main__.Semi_formal_var'> object with result 3.0 +- 1
>>> cos(a)
<class '__main__.Semi_formal_var'> object with result -0.9899924966 +-
>>> (1+0.14112000806**2)**.5

That result we got when requesting the error in y
We can also see that the a inside the cos() is invisible by taking the
derivative wrt a

>>> y.derivative_value(a)

Also, this approach is limited in that only variables can be arguments
to functions, not node expressions. If x and d are variables, x/d
becomes an expression and you cannot compute sin(x/d). Or am I missing


More information about the Python-list mailing list