[Haskell-cafe] double2Float is faster than (fromRational . toRational)

Don Stewart dons at galois.com
Fri May 21 17:10:34 EDT 2010


dvde:
> Dear Haskellers,
>
> I just want to share an observation. I had to convert a Double to a  
> Float value in an inner loop of an application, and I used somethin like  
> this:
>
> xf = (fromRational $ toRational xd) :: Float
>
> The program works on windows but it did not on OSX - it was too slow.  
> Now, after big headaches and much frustration, I replaced the code above  
> with this line (why didn't I come up with this earlier?):
>
> xf = double2Float xd
>
> and now everything works just fine.
>
> I am not really surprised by the speed-up (and no-one should be), but I  
> am still surprised how often such kinds of unobvious problems occur  
> while programming in Haskell. So I write this email just to remind me  
> and you to look out for such pitfalls.

There's no rewrite rule for this optimization. There's a ticket though
with some of the solutions.

-- Don


More information about the Haskell-Cafe mailing list