-1

I took a programming test, and my teacher took points off for diving by 3 instead of 3.0 , even though it appears I get the same answer.

volu = PI*(BASE*BASE)*(HEIGHT/3);

Does the .0 even matter in a c++ program, if so why? Thank you!

EDIT: I used doubles for PI and BASE

burrowfestor
  • 37
  • 1
  • 4

2 Answers2

2

(For what it's worth I find the way you have it to be rather prudent.)

If HEIGHT was an integral type then it would have been absolutely essential that you wrote 3.0 rather than 3. (3.0 is a literal of type double, cf. 3 which is an int.)

Otherwise the division would have taken place in integer arithmetic, causing the result to go off.

Personally I'd remove all the redundant parentheses and write it as

PI * BASE * BASE * HEIGHT / 3;
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Yes, two integers will be divided using euclidean division. Adding the .0 changes it into a normal division, because you're no longer dividing two integers but an integer and a double.

Adam Feor
  • 513
  • 3
  • 10