EatsKittens | 12 Dec 20:19 2013
Picon

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?
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Brandon Allbery | 12 Dec 20:28 2013
Picon

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

On Thu, Dec 12, 2013 at 2:19 PM, EatsKittens <temporalabstraction <at> 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?

What does it mean for (Complex Double)?
What does it mean for (a -> a) (NumInstances)?

--
brandon s allbery kf8nh                               sine nomine associates
allbery.b <at> gmail.com                                  ballbery <at> sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Niklas Haas | 12 Dec 20:29 2013
Picon

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

On Thu, 12 Dec 2013 20:19:35 +0100, EatsKittens <temporalabstraction <at> 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 <at> 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.
Ben Foppa | 12 Dec 20:34 2013
Picon

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

> 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 <haskell <at> nand.wakku.to> wrote:
On Thu, 12 Dec 2013 20:19:35 +0100, EatsKittens <temporalabstraction <at> 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 <at> 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 <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Ben Gamari | 12 Dec 20:39 2013
Picon

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

EatsKittens <temporalabstraction <at> gmail.com> 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 <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
EatsKittens | 12 Dec 20:55 2013
Picon

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

Ah, I had no ideal the real typeclass existed, that completely solves my issue.


On 12 December 2013 20:39, Ben Gamari <bgamari.foss <at> gmail.com> wrote:
EatsKittens <temporalabstraction <at> gmail.com> 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 <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Evan Laforge | 12 Dec 21:32 2013
Picon

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

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
<temporalabstraction <at> gmail.com> wrote:
> Ah, I had no ideal the real typeclass existed, that completely solves my
> issue.
>
>
> On 12 December 2013 20:39, Ben Gamari <bgamari.foss <at> gmail.com> wrote:
>>
>> EatsKittens <temporalabstraction <at> gmail.com> 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 <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

Gmane