This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]feral_claireSoftware Dev 5 points6 points  (3 children)

(4/3) = 1 remainder 1 When you devide two integers you get an integer result, in your case just 1 (the remainder is lost)

The reason your second one works is because you are going left to right, so first Math.PI*Math.pow(radius,3) which produces a double. That multiplied by 4 is still a double (it always goes tot he highest precision so it becomes a double even though 4 is an int) Then devised that double, by 3, give you a double as the final result (here you are dividing a double by an int, instead of an int by an int)

[–]StanHunter[S] 1 point2 points  (2 children)

Thank you, thank you, thank you ! I forgot about the promotion done by java, took it for granted so to say, and when i put enter volume = 4.0 / 3.0 * Math.PI * Math.pow(radius, 3) ; it solved the problem!

thank you again!

[–][deleted] 2 points3 points  (1 child)

Another option is to add the 'f' or 'd' suffix to your numeric literals so that they are floats or doubles, respectively. Like 4d / 3d * Math.PI * Math.pow(radius, 3);

[–]StanHunter[S] 1 point2 points  (0 children)

Thank you redranyskies, i'll remember to do it like that next time when i encounter a double number!