3

I am trying to understand the behavior of the code below which I wrote to experiment on calculation overflow.

public static void main(String[] args) {

    System.out.println(getSomeValue());
    System.out.println(getFixedSomeValue());
}

private static double getSomeValue() {
    return (2500000 - 0) * 250000 * (200 + 310);
}

private static double getFixedSomeValue() {
    return (double) (2500000 - 0) * 250000 * (200 + 310);
}

output:

-9.9787264E8
3.1875E14

What I have understood is: It can be because of Integer overflow as:

Double.MAX_VALUE = 1.7976931348623157E308
Integer.MAX_VALUE = 2147483647

I don't understand why the values differ? When the return type of method is double, shouldn't it automatically cast it to double?

3

3 Answers 3

8

(2500000 - 0) * 250000 * (200 + 310) is an expression comprised of int literal and numeric operators, so it is evaluated using integer operators and the result is an int, and therefore overflows (since it is limited to Integer.MAX_VALUE). The overflown result is converted to double before the method returns, but that's too late.

When you add (double) at the beginning, you cast (2500000 - 0) to double and avoid the numeric overflow, since now all the operators are floating point operators that result in double value. This is similar to writing

return (2500000.0 - 0) * 250000 * (200 + 310)
               ^ decimal point = double literal rather than int
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Eran! Just one more doubt, Am I safe doing a double cast as in getFixedSomeValue()? Or there is a better way to do that?
@impossible what do you mean by "safe"? If you mean to ask whether that code is safe to avoid numeric overflow, it is safe as long as the expression you case to double - (2500000 - 0) - doesn't overflow. I prefer to work with double literals, such as 2500000.0, instead of casting.
I am sorry to skip details. There are no literals but 'int' variables. But result I expect is in double. I just want to know, which one is safe: (double) ((intA - intB) * (intC * intD * intE) * (intX + intY)); (double) ((double)(intA - intB) * (double)(intC * intD * intE) * (double)(intX + intY));
@impossible the safest would be to change all the int variables to double. If that's not possible, cast the first operand in each expression to double. For example, if intC * intD * intE may result in int overflow, change it to (double)intC * intD * intE. Or, for the full expression - ((double)intA - intB) * ((double)intC * intD * intE) * ((double)intX + intY)
I got my answer Eran! Thank you!
0

Yes and no. Upon return it casts it to double but the calculations are done in integer format which upon reaching the max value it starts from the min value again. So if you do INT_MAX + 1 you will get INT_MIN and that is why you are getting a negative result in double.

Comments

0

When the return type of method is double, shouldn't it automatically cast it to double?

It does, just not perhaps where you mean.

private static double getSomeValue() {
    return (2500000 - 0) * 250000 * (200 + 310);
}

is the same as:

private static double getSomeValue() {
    int e = (2500000 - 0) * 250000 * (200 + 310);
    return (double) e;
}

i.e. the expression is evaluated using int, and then cast to double at the very end.

3 Comments

Thanks Andy, I got my answer for that question. Here is another. Are both of these same? 1. (double) ((intA - intB) * (intC * intD * intE) * (intX + intY)); 2. (double) ((double)(intA - intB) * (double)(intC * intD * intE) * (double)(intX + intY)); Can no 1 overflow on Integer?
"Here is another" If you have a separate question, please ask a separate question.
Sorry, My bad. I should have not written like that. Actually both the questions are related. I got the 2nd question after I read your answer and got more understanding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.