Division of integers in Java [duplicate]
Asked Answered
B

7

111

This is a basic question but I can't find an answer. I've looked into floating point arithmetic and a few other topics but nothing has seemed to address this. I'm sure I just have the wrong terminology.

Basically, I want to take two quantities - completed, and total - and divide them to come up with a percentage (of how much has been completed). The quantities are longs. Here's the setup:

long completed = 25000;
long total = 50000;

System.out.println(completed/total);  // Prints 0

I've tried reassigning the result to a double - it prints 0.0. Where am I going wrong?

Incidentally, the next step is to multiply this result by 100, which I assume should be easy once this small hurdle is stepped over.

BTW not homework here just plain old numskull-ness (and maybe too much coding today).

Bruyn answered 28/8, 2011 at 11:34 Comment(1)
Did you try (double)completed / (double) total ... and then assigning result to a double?Sitsang
P
165

Converting the output is too late; the calculation has already taken place in integer arithmetic. You need to convert the inputs to double:

System.out.println((double)completed/(double)total);

Note that you don't actually need to convert both of the inputs. So long as one of them is double, the other will be implicitly converted. But I prefer to do both, for symmetry.

Petua answered 28/8, 2011 at 11:35 Comment(2)
Is there any keyword in java to divide two numbers e.g. 'Math.floorDiv(num1,num2)'Applicable
@Applicable - I'm not sure what you're asking. If num1 and num2 are positive integers, then num1 / num2 gives you what you want. Otherwise, it's just Math.floor(num1 / num2).Petua
S
14

You don't even need doubles for this. Just multiply by 100 first and then divide. Otherwise the result would be less than 1 and get truncated to zero, as you saw.

edit: or if overflow is likely, if it would overflow (ie the dividend is bigger than 922337203685477581), divide the divisor by 100 first.

Stokehold answered 28/8, 2011 at 12:8 Comment(2)
Just to be clear: will the result be rounded or floored to zero? Big difference.Caseation
Using a double prints something like 20.0, using above approach prints plain 20. The best and easiest solution in my case.Urena
S
10
In Java
Integer/Integer = Integer
Integer/Double = Double//Either of numerator or denominator must be floating point number
1/10 = 0
1.0/10 = 0.1
1/10.0 = 0.1

Just type cast either of them.

Stagger answered 14/7, 2016 at 18:2 Comment(0)
T
3

Convert both completed and total to double or at least cast them to double when doing the devision. I.e. cast the varaibles to double not just the result.

Fair warning, there is a floating point precision problem when working with float and double.

Telemark answered 28/8, 2011 at 11:36 Comment(0)
M
1

If you don't explicitly cast one of the two values to a float before doing the division then an integer division will be used (so that's why you get 0). You just need one of the two operands to be a floating point value, so that the normal division is used (and other integer value is automatically turned into a float).

Just try with

float completed = 50000.0f;

and it will be fine.

Myosotis answered 28/8, 2011 at 11:36 Comment(0)
D
1

As explain by the JLS, integer operation are quite simple.

If an integer operator other than a shift operator has at least one operand of type long, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. If the other operand is not long, it is first widened (§5.1.5) to type long by numeric promotion (§5.6).

Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of type int. If either operand is not an int, it is first widened to type int by numeric promotion.

So to make it short, an operation would always result in a int at the only exception that there is a long value in it.

int = int + int
long = int + long
int = short + short

Note that the priority of the operator is important, so if you have

long = int * int + long

the int * int operation would result in an int, it would be promote into a long during the operation int + long

Demagogy answered 9/5, 2017 at 8:46 Comment(0)
T
-2

As your output results a double you should cast either completed variable or total variable or both to double while dividing.

So, the correct implmentation will be:

System.out.println((double)completed/total);
Threewheeler answered 20/8, 2017 at 4:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.