Way to implement toFloat :: (Num a) => a -> Float

Is there a way to add a method to a typeclass like num to implement this concept? A function that converts any number to floats?

On Thu, Dec 12, 2013 at 2:19 PM, EatsKittens
Is there a way to add a method to a typeclass like num to implement this concept? A function that converts any number to floats?
What does it mean for (Complex Double)? What does it mean for (a -> a) (NumInstances)? -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On Thu, 12 Dec 2013 20:19:35 +0100, EatsKittens
Is there a way to add a method to a typeclass like num to implement this concept? A function that converts any number to floats? Non-text part: text/html _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
The typeclass Real has this in a slightly different form: toRational :: Real a => a -> Rational You can use fromRational :: Rational -> Float together with this to implement your function.

toRational :: Real a => a -> Rational
This, combined with
fromRational :: Fractional a => Rational -> a
forms realToFrac :: (Real a, Fractional b) => a -> b
On Thu, Dec 12, 2013 at 2:29 PM, Niklas Haas
On Thu, 12 Dec 2013 20:19:35 +0100, EatsKittens < temporalabstraction@gmail.com> wrote:
Is there a way to add a method to a typeclass like num to implement this concept? A function that converts any number to floats? Non-text part: text/html _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
The typeclass Real has this in a slightly different form:
toRational :: Real a => a -> Rational
You can use fromRational :: Rational -> Float together with this to implement your function. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

EatsKittens
Is there a way to add a method to a typeclass like num to implement this concept? A function that converts any number to floats?
Does the following do what you expect? realToFrac :: (Fractional b, Real a) => a -> b realToFrac' :: (Real a) => a -> Float realToFrac' = realToFrac :: (Real a) => a -> Float Cheers, - Ben

Ah, I had no ideal the real typeclass existed, that completely solves my
issue.
On 12 December 2013 20:39, Ben Gamari
EatsKittens
writes: Is there a way to add a method to a typeclass like num to implement this concept? A function that converts any number to floats?
Does the following do what you expect?
realToFrac :: (Fractional b, Real a) => a -> b
realToFrac' :: (Real a) => a -> Float realToFrac' = realToFrac :: (Real a) => a -> Float
Cheers,
- Ben

Just be careful, realToFrac goes through rational first, which can't
represent special values like NaN and Infinity, so those will turn
into random numbers. Also it can be very inefficient.
Still, it's acceptable for a generic "any to Float" conversion. For
conversion between Doubles and Floats (or newtypes thereof), however,
I use things like
d2f :: Double -> Float
d2f (Types.D# d) = Types.F# (Prim.double2Float# d)
On Thu, Dec 12, 2013 at 11:55 AM, EatsKittens
Ah, I had no ideal the real typeclass existed, that completely solves my issue.
On 12 December 2013 20:39, Ben Gamari
wrote: EatsKittens
writes: Is there a way to add a method to a typeclass like num to implement this concept? A function that converts any number to floats?
Does the following do what you expect?
realToFrac :: (Fractional b, Real a) => a -> b
realToFrac' :: (Real a) => a -> Float realToFrac' = realToFrac :: (Real a) => a -> Float
Cheers,
- Ben
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (6)
-
Ben Foppa
-
Ben Gamari
-
Brandon Allbery
-
EatsKittens
-
Evan Laforge
-
Niklas Haas