(2) double answer = 13 / (double) 5; will give him an answer of 2.6 because we are naming 5 a ddt. When dividing an int by a double, you get a ddt answer.
(3) double answer = 13.0 / 5; will give him an answer of 2.6 because you again are dividing a double by an int, resulting in a double answer. When only 13.0 is stated, the compiler knows that this is intended to be a ddt.
(4) double answer = 13 / 5.0; will give him an answer of 2.6 because you are again dividing an int by a double, resulting in a ddt. When only 5.0 is stated, the compiler knows that this is intended to be a ddt.
(5) double answer = (double) (13/5); will NOT give him an answer of 2.6 because you are taking the quantity (13/5) and then taking the result and making it a double, with a result of 2.0
Hi Ryan, just one item of follow-up, when we have something like:
ReplyDeletedouble answer = 13 / (double) 5
Are we actually changing 5 to a double permanently?
We are not permanently making it a double, only acting as one to cause the output to become a double
ReplyDeleteVery good, thank you for the clarification here. 25 XPs awarded for this completion.
ReplyDelete