Advertisement
ulfben

Operator precedence

Sep 4th, 2017
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. //operator precedence exercise (use the debugger)
  2. int main() {
  3.     float x = 3;
  4.     float a = x*x / 4 + 1;  
  5.     float b = (x*x) / 4 + 1; //these three are all the same
  6.     float c = ((x*x) / 4) + 1; //but I think this is clearer
  7.     a = 2.0f;
  8.     b = 1.0f;
  9.     c = 3.0f;
  10.     float result1 = a / b * c; //ambiguous (in mathematics)
  11.     float result2 = (a / b)*c; //this is how C++ evaluates it
  12.     float result3 = a / (b*c); //we can change the order using parenthesis
  13.     return 0;
  14. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement