Is it possible to lose precision when only using multiplication and division of integers? How about using doubles?

In java, if we have an operation like below that only uses integers and multiplication and division, is it possible that result would not be equal to int3 due to lose of precision?

Int int1, int2, int3 = some values
Double result = ((((int1 / int2) * int3)/ int3)* int2)

Edit: What if we have Double int1, int2, int3 = some values?

Thanks.

Jon Skeet
people
quotationmark

You're performing division using integer operands, so yes, that's very likely to lose information. It's quite the same as losing precision in the same way that normal floating point arithmetic loses precision, but it's still information loss. It does every time the result isn't an exact integer.

For example:

int int1 = 1;
int int2 = 2;
int int3 = 3;

double result = (((int1 / int2) * int3) / int3) * int2;

... gives zero, because int1 / int2 is zero.

Beyond that, there's then the possibility of overflow on the multiplication side of things.

people

See more on this question at Stackoverflow