His problem was composing four functions, each with a small error. The first
two applications work well enough, but there is a about a percent error in
the third composition. The big source of error is f(x+h)-f(x). Subtracting
two floating point numbers that are nearly equal is a known source
On Mon, Apr 20, 2009 at 7:53 PM, Matt wrote:
> I'm trying to calculate the derivative of a function in Python like so:
>
> def D5(func,h=1e-5):
> ""' Return derivative of function func'''
> def df(x): return (func(x+h)-func(x))/h
> df.__name__ = func.__name__ + '_dx'
> return df
You should look up "numerical methods" or similar. There are ways of
rearranging your calculations to minimize the loss of precision. You could
also try a numerical/scientific library like GMP (for integers or rational
numbers). I don't know of any (Python) library for extended precision
fl