SHARE
TWEET

Operator precedence

ulfben Sep 4th, 2017 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top