Discussion:
Dividing Floats
Daniel Russell
2001-05-31 16:25:46 UTC
Permalink
Can anyone please help me.
How can you divide two floats? (and return a float, even if they divide
equally)
Ie (something like...) div 2.4 1.2 ---> 2.0
The above doesn't work since div can only be applied to integral numbers:

div :: Integral a => a -> a -> a


What you need is the operator (/) which can only be applied to fractional
numbers:

(/) :: Fractional a => a -> a -> a

For example:
2.4 / 1.2 ---> 2.0
or (/) 2.4 1.2 ---> 2.0


Dan Russell
Kingston University
Daniel Russell
2001-05-31 16:25:46 UTC
Permalink
Can anyone please help me.
How can you divide two floats? (and return a float, even if they divide
equally)
Ie (something like...) div 2.4 1.2 ---> 2.0
The above doesn't work since div can only be applied to integral numbers:

div :: Integral a => a -> a -> a


What you need is the operator (/) which can only be applied to fractional
numbers:

(/) :: Fractional a => a -> a -> a

For example:
2.4 / 1.2 ---> 2.0
or (/) 2.4 1.2 ---> 2.0


Dan Russell
Kingston University
Daniel Russell
2001-05-31 16:25:46 UTC
Permalink
Can anyone please help me.
How can you divide two floats? (and return a float, even if they divide
equally)
Ie (something like...) div 2.4 1.2 ---> 2.0
The above doesn't work since div can only be applied to integral numbers:

div :: Integral a => a -> a -> a


What you need is the operator (/) which can only be applied to fractional
numbers:

(/) :: Fractional a => a -> a -> a

For example:
2.4 / 1.2 ---> 2.0
or (/) 2.4 1.2 ---> 2.0


Dan Russell
Kingston University

Continue reading on narkive:
Loading...