[Haskell-cafe] Newbie: Haskell Sine Oddities

Scott Turner 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
   Hugs.Base> pi
   3.14159265358979
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:
   Hugs.Base> pi-3.140625
   0.000967653589793116
This shows that pi is represented using an approximation that is close to
   3.141592653589793116
This value, the computer's pi, differs from true pi by
   0.000000000000000122...
so the sin function is working perfectly.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org//pipermail/haskell-cafe/attachments/20060429/235acda1/attachment.htm


More information about the Haskell-Cafe mailing list