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

Bengt Richter bokr at oz.net
Tue Sep 21 16:18:06 EDT 2004


On Mon, 20 Sep 2004 18:13:06 -0400, Chris Metzler <cmetzler at speakeasy.snip-me.net> wrote:

>
>I'm getting some extremely odd results using the trig functions
>in the math module.  I don't know whether there's a bug here, or
>(more likely) I'm just missing something.  They kinda look like
>problems I'd see with truncation/roundoff errors, but they're
>occurring for larger values of the floating point exponent than
>I'd expect this to happen.
>
>The script below illustrates the kind of thing I'm seeing.  Do
>other people see the same thing?  Am I missing something here?
>
>Thanks,
>
>-c
>
>
>
>import math
>
>x = 0.90455926850657064620
>y = 0.90463240818925083619
>z = -2.00200807043415807129e-08
>
>
>cos_gamma1 = math.cos(x - y) - math.sin(x)*math.sin(y)*(1.0 - math.cos(z))
>gamma1 = math.acos(cos_gamma1)
>
As Tim has pointed out, you are doing an inverse in a very sensitive part of the
inverse function. Not only that, if you want the gamma angle, acos is ambiguous re sign,
since cos(x)=cos(-x). So IWT your best option would be to reformulate. Probably
in terms of cartesian components, which you resolve in such a way that you can
get the angles you want using atan2 as the inverse function. What do x, y, and z
represent in your real problem?

Regards,
Bengt Richter



More information about the Python-list mailing list