
Total NOOB question
Hello all, I'm taking an intro to CS class based on Java. A question in the book gives the following declarations:
init iResult, num1=25, num2=40
double fResult, val1=17.0, val2=12.78
and then asks: what result is stored in this assignment statement?
fResult = (int) ((double) num1/num2);
To me, this means that num1 is cast to a double and num2 will have to be a double as well to perform the division (25.0/40.0), answer .63. This is then cast to an integer, making it 1, and then turned into a double again since we're looking for fResult, giving a final answer of 1.0....is this correct?...if not, please point out my error.
Thanks!
C

Re: Total NOOB question
Did you type it in to a Java program and try running it? That's what I would do.

Re: Total NOOB question
So, I put it into a Java program and ran it and got 0.0, I guess all I was looking for is the "math rules" for Java. This answer tells me that it doesn't round decimals up (unless the program is set up to do so..?), so my original .63 would then be 0, then put to 0.0 since fResult is a double....is this correct?
Thanks,
C

Re: Total NOOB question
Absolutely right  Java, like a lot of programming languages *truncates* floating point values when it converts them to integer