java - what is better: cast to double or adding double zero? -


lets have 4 int variables: a, b, c, d

and need calculate (a * b) / (c * d)

there 2 ways it:

1) double result = ((double)(a * b)) / (c * d)

2) double result = (a * b) / (c * d + 0d)

what proc , cons every of ways?

if don't mind saying so, ways bad indeed.

(a * b) still computed in integral arithmetic suffer wrap-around. (this merely confounds belief excess parentheses scaled down version of devil.) in c , c++ it's far worse, overflow behaviour undefined , compilers reserve right eat cat.

worse that, c * d evaluated in integer arithmetic in both cases! result of expression converted double.

i believe 1.0 * * b / c / d clearest way of writing a, b, c, , d all promoted double types prior evaluation. way specifies intention get-go.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -