6 messages
Open this post in threaded view
|

 The Sine function in the prelude is not behaving as I expected. In the following Hugs session I tested sin on 0, 90,180 & 360 degrees. Prelude> sin 0 0.0                                 --correct Prelude> sin (pi/2) 1.0                                 --correct Prelude> sin pi 1.22460635382238e-16  --WRONG! Prelude> sin (2*pi) -2.44921270764475e-16 --WRONG! Is this normal behaviour? Or am I using the trig functions in an unexpected way? Thanks... Deech _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

## Re: Newbie: Haskell Sine Oddities

 > 1.22460635382238e-16  --WRONG! Thats just another way of writing: 0.000000000000000122460635382238 Which you can consider as 0 +- error 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. Thanks Neil _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

## Re: Newbie: Haskell Sine Oddities

 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. _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|