Floating-point glitches with the math module. Bug? Or am Imissing something?
Terry Reedy
tjreedy at udel.edu
Wed Sep 22 08:41:33 CEST 2004
"Tim Peters" <tim.peters at gmail.com> wrote in message
news:1f7befae040920235213950f17 at mail.gmail.com...
> This isn't the fault of sloppy trig functions, it's an unavoidable
> consequence of using finite precision arithmetic to evaluate a
> function *near* a local minimum or local maximum. cos() has a local
> maximum at cos(0), and d is near 0. For any reasonable function f
> evaluated at point a such that f(a) is a local min or max, the first
> derivative of f at a is 0. Therefore f(a+h) ~= f(a) + h**2*f''(a)/2
> (the first-order term of the Taylor expansion around a vanishes).
> That in turn roughly means that if you change any of the bits in the
> least significant half of a, it makes no visible difference to the
> computed value of f(a).
To illustrate:
>>> for i in range(50): print `cos(i*1e-9)` # Windows, 2.2
...
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
0.99999999999999989
0.99999999999999989
0.99999999999999989
0.99999999999999989
0.99999999999999989
0.99999999999999989
0.99999999999999989
0.99999999999999989
0.99999999999999978
0.99999999999999978
0.99999999999999978
0.99999999999999978
0.99999999999999978
0.99999999999999967
0.99999999999999967
0.99999999999999967
0.99999999999999967
0.99999999999999956
0.99999999999999956
0.99999999999999956
0.99999999999999956
0.99999999999999944
0.99999999999999944
0.99999999999999944
0.99999999999999933
0.99999999999999933
0.99999999999999933
0.99999999999999922
0.99999999999999922
0.99999999999999922
0.99999999999999911
0.99999999999999911
0.99999999999999911
0.999999999999999
0.999999999999999
0.99999999999999889
0.99999999999999889
0.99999999999999889
0.99999999999999878
The mathematically smooth cos(x) is effectively a stairstep function for
|x|<5e-8. Stairsteps are hard to invert ;-).
Terry J. Reedy
More information about the Python-list
mailing list