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

Daniel van den Eijkel dvde at gmx.net
Fri May 21 15:55:35 EDT 2010


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.

Best,
Daniel


More information about the Haskell-Cafe mailing list