[Numpy-discussion] what goes wrong with cos(), sin()

Robert Kern robert.kern@gmail....
Wed Feb 21 15:47:51 CST 2007


Christopher Barker wrote:
> Robert Kern wrote:
>> Christopher Barker wrote:
>>> I wonder if there are any C math libs that do a better job than you'd 
>>> expect from standard FP? (short of unlimited precision ones)
>> With respect to π and the zeros of sin() and cos()? Not really.

I'll back off on this a little bit. There are some approaches that will work;
they're not floating point, but they're not really "symbolic" computation either.

  http://keithbriggs.info/xrc.html

>> If
>> numpy.sin(numpy.pi) were to give you 0.0, it would be *wrong*. numpy.sin() is
>> supposed to give you the most accurate result representable in double-precision
>> for the input you gave it.
> 
> But does it?

Not quite, it seems, but 0 is even farther from the correct answer, apparently:

[sage-2.0-intelmac-i386-Darwin]$ ./sage
----------------------------------------------------------------------
| SAGE Version 2.0, Release Date: 2007-01-28                         |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------

sage: npi = RealNumber(3.1415926535897931, min_prec=53)
sage: npi.sin()
0.00000000000000013634246772254977
sage: import numpy
sage: numpy.sin(numpy.pi)
1.22460635382e-16

>> numpy.pi is not π.
> 
> More precisely, it's the best IEEE754 64 bit FP approximation of pi.
> 
> Right. I think that was the trick that HP used -- they somehow stored 
> and worked with pi with more digits. The things you can do if you're 
> making dedicated hardware.
> 
> I do wonder if there would be some way to use the extended precision 
> built in to Intel FP hardware -- i.e. have a pi that you can pass in 
> that has the full 80 bits that can be used internally. I don't know if 
> the trig functions can be done with extended precision though.

Well, you can always use long double if it is implemented on your platform. You
will have to construct a value for π yourself, though. I'm afraid that we don't
really make that easy.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the Numpy-discussion mailing list