Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does integer division code give the wrong answer?

I have a very simple division in Java (it's a product quantity / production per hour), however whenever I make this division I get strange errors:

float res = quantity / standard; 

I have tried the above division with several values and I always get errors, however the one that I've tried everywhere else and gotten right was this:

Everywhere in the world:

13.6 = 6800 / 500; 

Java:

13.0 = 6800 / 500; 

I've researched BigDecimal and BigInteger, however I haven't found a way to create this division with them, is there any other way to do this division in Java without having precision errors??

Any help will be greatly appreciated.

like image 691
Julian C. Avatar asked Sep 02 '11 16:09

Julian C.


People also ask

What is unusual about the result of integer division?

Integer division determines how many times one integer goes into another. The remainder after integer division is simply dropped, no matter how big it is. because 4 goes into 7 just once. The result is not rounded up to 2.

Why does integer division round down?

If the divisor and dividend have the same sign then the result is zero or positive. If the divisor and dividend have opposite signs then the result is zero or negative. If the division is inexact then the quotient is rounded towards zero. That is, up if it is negative, and down if it is positive.

What happens when you divide an integer by an integer?

When an integer is divided by another integer, then it satisfies the division algorithm which says 'dividend = divisor × quotient + remainder'. When an integer is divided by 1, the result is always the integer itself. For example, -5 ÷ 1 = -5.


1 Answers

You're dividing integers, which means that you're using integer division.

In integer division the fractional part of the result is thrown away.

Try the following:

float res = (float) quantity / standard;             ^^^^^^^ 

The above forces the numerator to be treated as a float which in turn promotes the denominator to float as well, and a float-division is performed instead of an int-division.

Note that if you're dealing with literals, you can change

float f = 6800 / 500; 

to include the f suffix to make the denominator a float:

float f = 6800f / 500;               ^ 
like image 93
aioobe Avatar answered Sep 24 '22 13:09

aioobe