Suppose x and y are of type int.
Are the two expressions:
int(4*x/y)
and:
int(x/(y/4))
always evaluate to the same for all x
and y
of type int
? They should mathematically, but only the second expression is consistent (i.e., producing the expected value) in a program I've written.
In many programming languages, 4*x/y
and x/(y/4)
are different because y/4
, an integer, is the truncated result of the division of y
by 4
. No such truncation exists in 4*x/y
. On obvious difference in when y
is 1
, in which case the second expression divides by zero, whereas the first one computes 4*x
.