I have a function which, taken two ints as paramters, should return the result of their division.
I am, however, unsure about a few things.
Firstly, what the return type would be? Ie, 4/2
would return 2
, therefore I presume an Int
, but 5/3
...? Also what is the best operator to do so?
div
is integer division in haskell. It will round down like C would do. So
4 `div` 2 = 2
5 `div` 3 = 1
To do regular division you have to convert to a Fractional
type with fromIntegral
.
(/) :: Fractional a => a -> a -> a
So the (/)
operator takes two Fractional
types and returns the same fractional type. Examples would be Rational
, Double
, and Float
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.