Floating-point glitches with the math module. Bug? Or am I missing something?

Chris Metzler cmetzler at speakeasy.snip-me.net
Mon Sep 20 19:57:55 EDT 2004


On Mon, 20 Sep 2004 18:13:06 -0400, Chris Metzler wrote:

> 
> I'm getting some extremely odd results using the trig functions
> in the math module.

Upon further investigation, it looks like the series used to
compute trig functions in the math module are truncated at a
surprisingly small number of terms.

>>> print math.acos(math.cos(2e-2))
0.02
>>> print math.acos(math.cos(2e-3))
0.00200000000002
>>> print math.acos(math.cos(2e-4))
0.000199999999726
>>> print math.acos(math.cos(2e-5))
2.00000008274e-05
>>> print math.acos(math.cos(2e-6))
1.99997787816e-06
>>> print math.acos(math.cos(2e-7))
1.99920056239e-07
>>> print math.acos(math.cos(2e-8))
2.10734242554e-08
>>> print math.acos(math.cos(2e-9))
0.0

5% error at 2e-8 is awful; and anything below 1.1e-8 is 0,
pretty much.  I can't find a module in the Library Reference
that provides higher-precision functions.  Is there one
around?

Thanks,

-c





More information about the Python-list mailing list