2 / 4 = 0.0 in java? [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does the division of two integers return 0.0 in Java?
I have some very confusing problem..
I want to calculate some stuff and after some debugging I saw that java calculates this arithmetic: 2 / 4 = 0.0 But it should be 0.5
2 & 4 are stored in integer variables
the result is stored in a double-type.
Did I miss something clearly?

It is because of integer division (Integer division rounds toward 0). Cast one of the operand to double type.
Example:
double temp = (double)2/4
will give you correct results.

Use
myDouble = (double) integerWhoseValueIs2 / integerWhoseValueIs4
The fact you store it in a double doesn't change the fact that the division of two integers makes an integer. When you store it, it's too late.
From the Java Language Specification :
Integer division rounds toward 0. That is, the quotient produced for
operands n and d that are integers after binary numeric promotion
(§5.6.2) is an integer value q whose magnitude is as large as possible
while satisfying |d · q| ≤ |n|.

If you do not write a cast to double, the result is always an int.
double a = (double) 2/4;

Related

Why does division be negative zero returns positive infinity in Java? [duplicate]

This question already has answers here:
Java division by zero doesnt throw an ArithmeticException - why?
(8 answers)
Closed 1 year ago.
When I divide a positive double by negative zero, the result returns positive infinity.
double d = 10.0 / -0;
But when I divide a negative double by positive zero, the result returns negative infinity.
double d = -10.0 / 0;
Can someone explain this behavior ? Shouldn't the both operations return negative infinity ?
As far as I am aware, there isn't actually a negative integer zero in the Java specification due to the two's complement representation used, which is clarified in answer to this question. Therefore -0 as an integer is just an unsigned 0.
If you run this test with floating point numbers, it performs as you expected:
double d = 10.0 / -0.0; // -Infinity
So the issue isn't with your logic, just your test.

Java not doing math to decimal places [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
How to make the division of 2 ints produce a float instead of another int?
(9 answers)
Division of integers in Java [duplicate]
(7 answers)
Closed 5 years ago.
So I'm trying to do math on ints stored in an array of ints.
float day1Hours = (day1[3]-day1[2]) / 2;
for this specific problem, day1[3] = 19 and day1[2] is 10. So, it should be doing 19-10 = 9, and then dividing that by 2 to make 4.5. But, the output that I am getting is 4. I've also tried storing day1Hours as a double but that made no difference. How would I make this be able to do the math correctly and get those decimal values that I need?
The problem is that you are doing integer division and then converting to a float. Try
float day1Hours = (day1[3]-day1[2]) / 2.0f;
Using a float literal in the denominator will cause the division to be done in floating point, and you won't get integer truncation. (As an alternative to using a float literal, you could cast the numerator or denominator to a float, but that seems somewhat baroque to me. It would be more suitable if both the numerator and denominator were int variables.)
The reason that just changing the type of day1Hours doesn't affect the problem is that the entire right side is evaluated first using the declared data type of day1 and then converted to whatever type is on the left of the assignment.
float day1Hours = (float)(20-9) / 2; //5.5
Problem is that on the right side of equation the numbers are integers and when dividing 2 integers the decimal places are truncated (integer division rounds towards zero) 4.5 -> 4.0.
Try changing 2 -> 2f so the 2 would be considered a float instead of an integer.

Java: Variable assignment issue

just a quick question and I'm probably gonna feel stupid for asking but still would like to know why it is so...!
Anyways, quick example:
x is a double.
double conversion = (x-32)*5/9;
This does the maths just fine.
double conversion = (x-32)*(5/9);
This isn't fine because the (5/9) is being treated as an int, thus result is overall 0.
double conversion = (x-32)*(5f/9f);
This does the maths just fine, as it explicitly makes the 5/9 values a float.
So my question is: Why does the first equation work perfectly fine? ( double conversion = (x-32)*5/9; )
Why isn't the 5/9 being made a 0 if it were an int supposedly? What makes the 5/9 different from (5/9) ?
The difference is between whether you do the multiplication first or the division first - and what the types of those operations are.
This:
(x - 32) * 5 / 9
is equivalent to:
((x - 32) * 5) / 9
So if the type of x is double, then the type of x - 32 is double, so the 5 is promoted to double, the multiplication is done in double arithmetic, giving a double result, and then the division is also done in double arithmetic.
Even if x is an integer type, you're doing the multiplication first, which will presumably give you a value bigger than 9 (in your test case), leaving you with a non-zero result. For example, if x is 45, then x-32 is 13, (x - 32) * 5 is 65, and the overall result is 7, then converted to 7.0 on assignment. That's not the same result you'll get if x is a double with the value 45.0, but it's still better than multiplying by 0...
You basically answered your own question. Evaluation order makes all the difference.
basic left to right evaluation results in different type casting than your explicit evaluation order in your second example
Assuming x is a double then your first equation divides a double by an integer due to order of operations.
(x-32) = y-> y*5 = z -> z/9
at each stage a double is being operated on, overriding integer arithmetic.
It is the order it's done in.
If you multiply by 5, you get a large number. If you then divide by 9, you still get an int,
But the remainder is discarded.
The reason is the order of the operations.
(x-32)*5/9 makes first (x-32)*5 and then divides the result by 9
(x-32)*(5/9) makes first (x-32) and (5/9). After both results are multiplied.
Your value of x might be double and it is making entire equation in double because brackets execute first.
It's a matter of when the conversion takes place. 5/9 consists of one 5 and one 9 (both ints) being divided with integer division. If either is a float (5f/9 or 5/9f) they will divide as floats.

Java: Why is this double variable coming out 0? [duplicate]

This question already has answers here:
Why does the division of two integers return 0.0 in Java? [duplicate]
(6 answers)
Why does this Java division print out zero? [duplicate]
(5 answers)
Closed 8 years ago.
I am using Java 1.6
final double check = 3 / 4;
System.out.println(check);
Console is showing: 0.0
Why is this happening? Shouldn't it come out 0.75?
Make that:
double check = 3.0 / 4;
and it'll work. You got 0 because 3 / 4 is an integer division, whose value is 0.
Because both are integer hence result will also be integer.
Cast any one into double like this:
double check = (double)3 / 4;
By doing:
3 / 4
you are performing an integer division, since 3 and 4 are int constants, not doubles. The result is therefore an int. But since you are assigning it to a double, the result of the division will then be promoted to a double. But at this point it is too late, since the integer division will have produced 0!
You need to do:
3.0 / 4
to achieve your desired result, since in this case, 4 will automatically be promoted to a double, and the result of the division will also be a double.
To be perfectly sure of what happens and if you like symmetry, you can also write:
3.0 / 4.0
You are dividing integers and assigning the result to double.In java division of two int values always yields an int.
So change the statement double check = 3 / 4; to double check = 3.0 / 4;

Division is incorect in java [duplicate]

This question already has answers here:
Floating point arithmetic not producing exact results [duplicate]
(7 answers)
Closed 9 years ago.
I'am confused. I'm trying to get an int value:
Integer ord = new Double(33 / (-2 * 1.1)).intValue();
Expectation: -15
Output: -14
What's wrong?
When I try:
Double d = 33 / (-2 * 1.1);
Output: -14.999999999999998
Any ideas? Thanks in advance!
.intValue() will trunc the frarctinal part so you can use Math.ceil(), Math.floor() or you can use Math.round() to approximate it to the nearest value
Integer result = (int) Math.round(new Double(33/(-2*1.1))); //-15
Integer result = (int) Math.floor(new Double(33/(-2*1.1))); //-15
Integer result = (int) Math.ceil(new Double(33/(-2*1.1))); //-14
You can see that Math.ceil() give us 14 because this is a negative number -14>-15 so the ceil of -14.9999 is -14 and the inverse apply on Math.floor()
intValue() doesn't do round but truncate.
The double output of -14.999999999999998 has it's origin in the precision of the double type. A floating point number is always a sum of 2^n numbers. The result is that not all numbers can be represented precisely, even with double.
Your integer example returns -14 because the integer value of -14.999999999999998 is -14. There is no rounding when getting the integer value. It is just cut of at the decimal point.
For rounding use either Math.ceil() for rounding up, Math.floor() for rounding down or Math.round() for general rounding.
When getting the int value of a double, java isn't doing any round up or down for you. It just omits the decimals.
What you want to do is to use the Math.round(double) to get the value you are expecting.
I believe the java doc for Math.round() says it will return a long value, but if you are sure that your result never will be larger than the maximum int value, then you can cast it to an int.
int result = (int) Math.round(new Double(33/(-2*1.1)));
Warning: Using the authority argument :P
Josh Bloch has an entire item in his Effective Java book against using double or float when aiming for accuracy. In my life, working with currency, for example, I have had the best results working with BigDecimal
You are using 32 bit float so losing a lot of precision. Try Double d = 33 / (-2 * 1.1d);
And, as everybody say, better round than truncate.

Categories