[Haskell-cafe] Generic typeclass for converting between types
wren ng thornton
wren at freegeek.org
Sat Jan 24 21:16:47 EST 2009
John Goerzen wrote:
> Magnus Therning wrote:
> > John Goerzen wrote:
> > > It's hard to remember all the functions to use to do these. I often
> > > resort to a chart I made for numeric conversions.
> >
> > Not the type of response you want, but would you publish that chart
> > somewhere, please ;-)
>
> Sure :-)
>
> http://book.realworldhaskell.org/read/using-typeclasses.html#numerictypes.conversion
For those who care about correctness or efficiency, note that the
(toRational . fromRational) path is not good. It's not correct because
of the exceptional values that Float and Double can carry, and it's not
efficient because most hardware supports direct conversions for the
basic numeric types.
See the RealToFrac class in Data.Number.Transfinite in the logfloat[1]
package for a better way. There's a generic instance (Real a,
Transfinite a, Fractional b, Transfinite b) => RealToFrac a b which
Haddock displays as duplicate instances for each of the optimized
GHC-only instances.
[1] http://hackage.haskell.org/cgi-bin/hackage-scripts/package/logfloat
--
Live well,
~wren
More information about the Haskell-Cafe
mailing list