ulfben

Operator precedence

Sep 4th, 2017
117
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×