For the computation involving multiple types of variables, such as integer and double, is the following approach the correct way in terms of not causing any hidden error or information lost?

int a = 2;
double b = 3.0;
double c;
c = (double)(a+b);
c = (double)(a/b);

Any computation involving a double returns a double - basically, numbers are promoted to the highest type in the expression before the expression is evaluated. See the Java Language Spec for the gory details - worth reading, honestly.

So you don't actually need the cast in this case, because the value returned by (integer/double) will be a double.


So yes, your expressions are correct in that they get what I think you mean them to get, but you might still want to read up a bit more on conversions and promotions.

Now suppose a and b were both ints - in that case, your line 5:

c = (double)(a/b);

would end up with the (correct but probably unintended) value of 0.0: (2/3) is integer division, so the answer is 0, not 0.666..., then the zero is cast to a double and stored in c. Again, the cast doesn't do anything, since the integer 0 would be promoted to 0.0d when being stored to a double variable.


However, if you wanted to make this come out to a floating-point value (still speaking of integer/integer division), you could use a case inside the parens: ((double)a/b) would evaluate to the desired 0.6666...

too late :) just read up the previous post

Edited 5 Years Ago by stultuske: just too late

Comments
Plus one anyway... I'm sure your answer would have been just as good

Can you let me know how to handle the case of

c = (double)(a/b);

where both a and b are integer, and c is defined as double. We want correct value of 0.666666666666 to be assigned to c instead of 0.

Thanks.

Any computation involving a double returns a double - basically, numbers are promoted to the highest type in the expression before the expression is evaluated. See the Java Language Spec for the gory details - worth reading, honestly.

So you don't actually need the cast in this case, because the value returned by (integer/double) will be a double.


So yes, your expressions are correct in that they get what I think you mean them to get, but you might still want to read up a bit more on conversions and promotions.

Now suppose a and b were both ints - in that case, your line 5:

c = (double)(a/b);

would end up with the (correct but probably unintended) value of 0.0: (2/3) is integer division, so the answer is 0, not 0.666..., then the zero is cast to a double and stored in c. Again, the cast doesn't do anything, since the integer 0 would be promoted to 0.0d when being stored to a double variable.


However, if you wanted to make this come out to a floating-point value (still speaking of integer/integer division), you could use a case inside the parens: ((double)a/b) would evaluate to the desired 0.6666...

Edited 5 Years Ago by winecoding: n/a

c = (double)(a/b);

In this case, you are casting (a/b) after they have already performed the arithmetic, which of course sets c to 0.0. You could simply cast a or b to a double and you should get the result desired.

c = ((double) a) / b

Some people like doing this:

c = (0.0 + a) / b

but that is just messy to me, works though.

Edited 5 Years Ago by ztini: n/a

If either side of the division is a float or a double, floating-point division will be executed, and a real (floating-point) number will be the result. How can you make sure that one or the other of a or b is represented as a floating-point (float or double) when the division takes place?

This article has been dead for over six months. Start a new discussion instead.