The old round off problem?

David Treadwell i.failed.turing.test at gmail.com
Sat Mar 4 17:14:29 EST 2006


On Mar 4, 2006, at 4:33 PM, Paul Rubin wrote:

> "sam" <samschul at pacbell.net> writes:
>> Hello all, I am taking a class in scientific programming at the local
>> college. My problem is that the following difference produces  
>> round off
>> errors as the value of x increases. For x >= 19 the diference goes to
>> zero.I understand the problem, but am curious as to whether their
>> exists a solution.   [f(x)  = cosh^2(x) - sinh^2(x)  = 1]
>
> The usual way is to notice that the difference goes to zero because
> the lowest order terms of the Taylor series for cosh^2 and sinh^2 are
> equal and cancel each other out.  The solution is to write down the
> series for (cosh^2(x) - sinh^2(x)) and add up a few non-cancelled  
> terms.
> All these series should converge very fast.

Here's my analysis:

First, keep in mind that the range of values for a Taylor expansion  
of sinh() and cosh() is limited. Then...

Write down the series for (cosh^2(x) - sinh^2(x)), as Paul suggested.

c2 = taylor(cosh(x)^2,x,8)

s2 = taylor(sinh(x)^2,x,8)

The first method gives the expected answer:

eval(c2-s2) ==> 1

Write down the series for cosh(x), square that; write the series for  
sinh(x), square that and subtract.

Compare the two (I just did this in Eigenmath). Higher-order terms  
remain for the second method.. I don't know how many terms my cosh()  
and sinh() functions use, but if I evaluate (again, in Eigenmath)

The second method, which is more akin to what Python is doing (unless  
there is an explicit 'sinh**2(x)' function) is different.

c2 = (taylor(cosh(x),x,8))^2, which gives terms up to and including  
(x**8)**2 = x**16

s2 = (taylor(sinh(x),x,9))^2, which gives terms up to and including  
x**18, then subtract the two, the result is

eval(c2 - s2) ==> 1 - 1/1814400 * x**10 - ... higher terms.

This begs the question, "What is the algorithm used by my Python?"

And, the ever-important question, "Does it matter in my final  
result?" This is important, because I do not, in general, trust the  
result when subtracting two large numbers.

:--David T.





More information about the Python-list mailing list