[Haskell-cafe] Error with Float

Cale Gibbard cgibbard at gmail.com
Tue Jul 19 11:00:34 EDT 2005


Perhaps you mean:
f x
    | x < 1     = 0 : f (2*x)
    | otherwise = 1 : f (2*(x-1))

Note that in the second case, the 1 is subtracted before multiplication by 2.

If you were referring to the problem that this eventually gives
constantly 0 for values like 0.6, try importing the Ratio module and
applying it to 6%10, which is the exact rational value rather than a
floating point representation.

 - Cale

On 19/07/05, Dinh Tien Tuan Anh <tuananhbirm at hotmail.com> wrote:
> 
> This is my function to convert a fraction (0<x<1) to binary :
> 
>    f x
>       ¦t<1        = 0::f t
>       ¦otherwise = 1::f (t-1)
>       where t = 2*x
> 
> 
> I guess there's nothing wrong with that, but when traced, it has something
> like 0.6*2 - 1 = 0.600001
> This error got accumulated and made my f function wrong (will eventually
> evaluate an infinite 0, no matter what value of x)
> 
> Please tell me there's some ways to deal with that.
> 
> Thanks a lot
> 
> _________________________________________________________________
> Want to block unwanted pop-ups? Download the free MSN Toolbar now!
> http://toolbar.msn.co.uk/
> 
> _______________________________________________
> 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