I'm creating a structure of Rationals (int * int) and one of my functions is:
fun diff ((n, d), (n', d')) =
let
val (top, bot) = sum ((n, d), (~n', d'))
in
(top / gcd(top, bot), bot / gcd(top, bot))
end
gcd
gives me the greatest common denominator, so I don't end up with 2/8, but rather 1/4 as it should be. gcd
uses mod
to find the gcd, so it returns an int
. But I can't get the expression with division to be typed as an int. When I tried adding : int * int
to the end of the diff
declaration, it gives me a type error that the expressions real * real
and int * int
don't match.
How can I force integer division, or cast the expression to an integer? If both are possible, which one is better?
Yeah you are using the wrong operator. /
is the floating-point division operator. div
(as mentioned by D.Shawley) is the integer division operator. div
is the right thing to use in this case, because you are dividing two integers and want to get an integer.