Int Division: Why Is the Result of 1/3 == 0

Int division: Why is the result of 1/3 == 0?

The two operands (1 and 3) are integers, therefore integer arithmetic (division here) is used. Declaring the result variable as double just causes an implicit conversion to occur after division.

Integer division of course returns the true result of division rounded towards zero. The result of 0.333... is thus rounded down to 0 here. (Note that the processor doesn't actually do any rounding, but you can think of it that way still.)

Also, note that if both operands (numbers) are given as floats; 3.0 and 1.0, or even just the first, then floating-point arithmetic is used, giving you 0.333....

Why does integer division code give the wrong answer?

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;
^

Why does this division result in zero?

What I think you are experiencing is integer arithmetic. You correctly suppose l and b to be 2, but incorrectly assume that k and a will be 3 because it's the same operation. But it's not, it's integer arithmetic (rather than floating-point arithmetic). So when you do i / j (please consider using some whitespace), 2 / 3 = 0.33333... which is cast to an int and thus becomes 0. Then we multiply by 3 again, and 0 * 3 = 0.

If you change i and j to be floats (or pepper your math with (float) casts), this will do what you expect.

Why does 1 / 2 == 0 using double?

It's because of the data type.

When you do 1/2 that is integer division because two operands are integers, hence it resolves to zero (0.5 rounded down to zero).

If you convert any one of them to double, you'll get a double result.

double d = 1d/2;

or

double d = 1/2.0;

Why does the Java compiler allow int/int division if the result must be an int?

The division isn't unsafe; it does exactly as it should do.

Dividing two integers is known as an integer division and simply returns the result of dividing the two numbers without the remainder.

Also, as pointed out in the comments, you're actually casting index to an int, rather than index/PAGESIZE_SEARCH - think about using brackets to be more precise: (int) (index / PAGESIZE_SEARCH).

EDIT: You can read more on this topic here.

Float/Double doesn't seem to be working. The result keeps coming out to be an integer

sphereVolumeConstant will always be 1.0. writing double before variable doesn't make every operation on it double. If You want to make sphereVolumeConstant to be equal to 1.(3) You have to write:

final double sphereVolumeConstant = (4.0/3);

Or:

final double sphereVolumeConstant = ((double) 4 / 3);

Now the operation will be made in double because 4.0 or (double) 4 is double.

Why does the division get rounded to an integer?

You're using Python 2.x, where integer divisions will truncate instead of becoming a floating point number.

>>> 1 / 2
0

You should make one of them a float:

>>> float(10 - 20) / (100 - 10)
-0.1111111111111111

or from __future__ import division, which the forces / to adopt Python 3.x's behavior that always returns a float.

>>> from __future__ import division
>>> (10 - 20) / (100 - 10)
-0.1111111111111111

why is the result always 0 when I try to cast my int to a double in this percentage calculator

Integer division in Java will return the exact number of times one number is divisible by another. 80 is not divisible by 100, even once, so 80/100 = 0 in terms of integer division. You need a double or float to get a decimal result.

To fix it, ensure all the relevant types are double, e.g.:

float score;
float total;

Alternatively, you can cast the calculation, but this is not good practice:

float result = (float) score/total * 100;


Related Topics



Leave a reply



Submit