Number conversions, like floats to doubles
Quick and probably stupid question: If I want to convert a Float to a Double, should I use fromRational . toRational ? It seems to work, but isn't this a bit weird? It took a while for me to figure this out. I suppose they are rationals because of the finite precision of Floats and Doubles? And similarly, (fromInteger . toInteger) is the right way to convert the integral types? Why not just have a function like: convertIntegral :: (Integral a, Integral b) => a -> b -- Ben Escoto
In NumExts, there's floatToDouble and doubleToFloat. On Sat, 8 Nov 2003, Ben Escoto wrote:
Quick and probably stupid question:
If I want to convert a Float to a Double, should I use
fromRational . toRational
? It seems to work, but isn't this a bit weird? It took a while for me to figure this out. I suppose they are rationals because of the finite precision of Floats and Doubles? And similarly, (fromInteger . toInteger) is the right way to convert the integral types?
Why not just have a function like:
convertIntegral :: (Integral a, Integral b) => a -> b
-- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume
W liście z sob, 08-11-2003, godz. 22:59, Hal Daume III pisze:
In NumExts, there's floatToDouble and doubleToFloat.
It's a GHC extension, while realToFrac is Haskell 98. -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/
W liście z sob, 08-11-2003, godz. 22:41, Ben Escoto pisze:
If I want to convert a Float to a Double, should I use
fromRational . toRational
realToFrac :: (Fractional b, Real a) => a -> b It is actually defined as fromRational . toRational but GHC knows to generate specialized code for particular types.
And similarly, (fromInteger . toInteger) is the right way to convert the integral types?
fromIntegral :: (Num b, Integral a) => a -> b -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/
On Sun, 09 Nov 2003 01:11:09 +0100 "Marcin 'Qrczak' Kowalczyk" <qrczak@knm.org.pl> wrote:
realToFrac :: (Fractional b, Real a) => a -> b
It is actually defined as fromRational . toRational but GHC knows to generate specialized code for particular types.
Oops, I was looking through the prelude but somehow missed these functions. Thank you. -- Ben Escoto
participants (3)
-
Ben Escoto -
Hal Daume III -
Marcin 'Qrczak' Kowalczyk