Not Yet Answered # The computation involving multiple types of variables.

jon.kiparsky 326 stultuske 1,049 Discussion Starter winecoding ztini 8 jon.kiparsky 326 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

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...

1

too late :) just read up the previous post

*Edited 5 Years Ago by stultuske*: just too late

0

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

0

`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

0

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.

Recommended Articles

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...

the function that I created to find the ...