Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+3 votes

For example:

is average = (double) value / count the same as average = value / (double) count?

asked in CSC211_Winter2018 by (1 point)

1 Answer

+1 vote

In this particular instance, no. It wouldn't matter which variable you would cast in this instance because they would return the same double value regardless of whether the numerator or the denominator gets casted to a double. However, if you were taking the value from a function that had a fraction such as 1/3 times another variable, and you casted the variable to double instead of the fraction, then it would perform an integer operation on the fraction and compute your result as 0.

tl;dr It doesn't matter in this instance, but be mindful of which variable you cast in more complex functions.

answered by (1 point)
edited by
...