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

wren ng thornton wren at freegeek.org
Sun May 23 07:12:16 EDT 2010


Daniel Fischer wrote:
> There are more rules elsewhere. If you compile with optimisations, GHC 
> turns your realToFrac into double2Float# nicely, so it's okay to use 
> realToFrac.
> However, without optimisations, no rules fire, so you'll get 
> (fromRational . toRational).

That must be new, because it didn't used to be the case. Also, rewrite 
rules can be fragile. Not to mention that the (fromRational . 
toRational) definition is incorrect for converting between Float and 
Double because Rational cannot properly encode the transfinite values in 
Float/Double.

The robust solution is to use the RealToFrac class from the logfloat 
package:

http://hackage.haskell.org/packages/archive/logfloat/0.12.1/doc/html/Data-Number-RealToFrac.html

-- 
Live well,
~wren


More information about the Haskell-Cafe mailing list