[Haskell-cafe] Newbie: Haskell Sine Oddities
Cale Gibbard
cgibbard at gmail.com
Sat Apr 29 16:51:40 EDT 2006
As others have pointed out, floating point representations of numbers
are not exact. You don't even have to use fancy functions like sine to
see all kinds of nice algebraic properties break down.
let x = 1e8; y = 1e-8 in (y + x) - x == y + (x - x)
evaluates to False.
So from this you can see that addition is not even associative,
neither is multiplication. Distributivity also fails in general.
Floating point computations are always approximate and have some level
of error associated with them. If you want proper real numbers, things
like equality testing become impossible in general. If you look
around, I think there are a couple of libraries in Haskell which let
you work with arbitrary precision reals though.
Try: http://www.haskell.org/haskellwiki/Exact_real_arithmetic
- Cale
On 29/04/06, Aditya Siram <aditya_siram at hotmail.com> wrote:
> 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
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
More information about the Haskell-Cafe
mailing list