[Haskell-cafe] Newbie: Haskell Sine Oddities
p.turner at computer.org
Sat Apr 29 15:59:25 EDT 2006
Aditya Siram wrote:
> Prelude> sin pi
> 1.22460635382238e-16 --WRONG!
Neil Mitchell wrote:
> Floating point numbers are not exact, the value of pi is not exact
> either, and I guess that between them they are giving you errors.
Yes. Actually, this particular inexactness is entirely due to the value of pi. The calculation of sin pi is being performed using the Double data type, which cannot represent pi exactly. Since Double uses binary fractions, doing
shows a decimal approximation to the binary approximation. To investigate the representation of pi, subtract from it a number which _can_ be represented easily and exactly as a binary fraction, as follows:
This shows that pi is represented using an approximation that is close to
This value, the computer's pi, differs from true pi by
so the sin function is working perfectly.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe