Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected result in long/int division

I have values like this:

long millis = 11400000;
int consta = 86400000;
double res = millis/consta;

The question is: why res equals 0.0 (instead of ca. 0.131944)? It's stored in double so there should be no rounding right?

like image 700
alex Avatar asked Sep 07 '12 08:09

alex


People also ask

Can long be divided by int?

This is because long: The long data type is a 64-bit signed two's complement integer. int: The int data type is a 32-bit signed two's complement integer. See Primitive type It means both are integer. Since we divide long by integer, result is long 0.

What happens when you have an int divided by an int Java?

When dividing an integer by an integer, the answer will be an integer (not rounded). When an operation involves two types (as the mixed division shown above), the smaller type is converted to the larger type. In the case above, the integer 5 was converted to a double type before the division occurred.

What happens if you add a long to an int?

Yes, you can add a long and an int just fine, and you'll end up with a long . The int undergoes a widening primitive conversion, as described in the Java Language Specification, specifically JLS8, §5.1. 2 .

What happens when you divide two ints?

When dividing two numbers of the same type (integers, doubles, etc.) the result will always be of the same type (so 'int/int' will always result in int). In this case you have double var = integer result which casts the integer result to a double after the calculation in which case the fractional data is already lost.


5 Answers

When you are using a binary operator, both arguments should be of a same type and the result will be in their type too. When you want to divide (int)/(long) it turns into (long)/(long) and the result is (long). you shouldmake it (double)/(long) or (int)/(double) to get a double result. Since double is greater that int and long, int and long will be turned into double in (double)/(long) and (int)/(double)

like image 184
Kamran Amini Avatar answered Oct 02 '22 07:10

Kamran Amini


Because you are dividing a long by an int you get an long results. What you are effectively doing is

double res = (double) (millis/consta);

as millis/consta is 0, when cast to double is 0.0

Try the following to divide a double by an int and get a double result.

double res = (double) millis/consta;

which is the same as

double res = ((double) millis)/((double) consta));
like image 20
Peter Lawrey Avatar answered Oct 02 '22 07:10

Peter Lawrey


You are doing longdivision (int gets cast to long) so you get long values, which are integers (so, 0)

You should do

  double res = (double) millis / consta;

Once one of the values is casted to double, the other is too casted so the operation uses the same type in both operators.

like image 41
SJuan76 Avatar answered Oct 02 '22 06:10

SJuan76


millis/consta is an integer division, which results in 0. the casting in the line:

double res = millis/consta;

is done on the result:

double res = (double)(millis/consta);

What you need to do is to cast one of the operands:

double res = (double)millis/consta;
like image 21
MByD Avatar answered Oct 02 '22 07:10

MByD


The resulting type of a long and int devision will be a long, which can't hold decimals.

you want to cast it to a double before you assign it

like image 40
Minion91 Avatar answered Oct 02 '22 06:10

Minion91