I notice that I have two different result when I compile this operation in java, and python.
10 / 3 + 2 * 4 / 3 - 3
result in java = 2.0
in python = 3.0
I also execute this operation in many calculators and the result is 3.0 can anyone explain how java deal with this?
double var = 10/3+2*4/3-3;
System.out.println(var);
The expression components will be evaluated as integers, i.e. intermediate values are truncated (10/3
evaluates to 3
, 2*4/3
evaluates to 2
and so on).
Changing to the below will result in 3.0
:
double var = 10.0 / 3.0 + 2.0 * 4.0 / 3.0 - 3.0;