Order of operations question

This is a discussion on Order of operations question within the C++ Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> void main() { float a = 4, b = 4, c = 8; a = (a + ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    3

    Order of operations question



    Code:
    #include <stdio.h>
    void main()
    {
    	float a = 4, b = 4, c = 8;
    	a = (a + c) / ++c;
    	printf("\n a = %f", a);
    
    }
    Does the () take precedence over the pre increment or not?
    In some text books the () appears higher up in the table of precedence.

    I ran this program in VS 2003 and I got 1.444, which is 13 / 9.

    Why don't I get (12) / 9 if the parens come first?

    Last edited by nicomp; 09-20-2006 at 09:40 AM.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    It does take higher precedence.

    But you have to consider the precedence of all operators involved along with their associativity rules. You have 4 operators in effect on that formula. (), +, / and ++.

    So... let's order them by precedence and associativity.

    1st ()
    2nd +
    3rd ++
    4th /

    The addition operator is moved up because the grouping operator forces evaluation of the expression contained inside. So, the first thing that happens is the sum of a with c.

    The pre-increment has higher precedence over the division. As such, it is evaluated first. c is incremented.

    Only then is the division made.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    > I ran this program in VS 2003 and I got 1.444, which is 13 / 9.

    I didn't see this, sorry.

    That may come from the fact a, b and c are floats and some manner of rounding is happening. Remember that floats have a very low precision.

    Change b and c to int and a to double. the result will have a much higher precision which I believe will be acceptable for your problem
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    Quote Originally Posted by Mario F.
    So, the first thing that happens is the sum of a with c..
    I don't understand. If the first thing that happens is the sum of a with c then a+c should be 12. Instead, a+c is giving 13 which implies that c has been incremented already.

    What am I missing?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    I suspect the answer is that it is undefined, though I am not entirely sure.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    If you are using the ++ operator before the variable name, the first thing that happens is the variable is incremented. Which is the exact opposite of using ++ after the variable name, it will increment it after the other operations are preformed.
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I've always used ++ after the variable and I have never had any problems with it.
    Now when I start thinking using ++ before the variable seems to be very useful in some cases...

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    > Does the () take precedence over the pre increment or not?
    Do not confuse the fact that you have used () with "This is always done first".

    > In some text books the () appears higher up in the table of precedence.
    Sounds like some other books are wrong then.

    > I ran this program in VS 2003 and I got 1.444, which is 13 / 9.
    Good for you.
    Your expression is undefined, so any answer (or indeed no answer) is entirely plausable.
    http://c-faq.com/expr/index.html

    NEVER EVER use ++ on a variable, and attempt to use that same variable in another part of the expression. It's undefined - no if's, but's or maybe's.

    Also read the part of that FAQ which talks about "sequence points".

    Given
    f() + g() / h();
    No amount of () or operator precedence will tell the compiler which function to call first. Precedence and () only describe how sub-expressions are combined, not evaluated.
    It is perfectly valid for a compiler to call f() first.

    Oh yeah, the void main is wrong as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Imagine it as a series of function calls like this:
    Code:
    a = divide(add(a, c), increment(c));
    C++ doesn't specify which function argument will be evaluated first, so for the divide function, you don't know whether the add(a,c) will run first or the increment(c). The result is undefined.

    All the parentheses do is stop it from being this:
    Code:
    a = add(a, divide(c, increment(c)));
    Last edited by Daved; 09-20-2006 at 10:22 AM.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    Quote Originally Posted by Salem
    > Oh yeah, the void main is wrong as well.
    5275: Told ya!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Embedded SQL Order By
    By cjohnman in forum C Programming
    Replies: 12
    Last Post: 04-15-2008, 03:45 PM
  2. Numerical order with ifs question?
    By JDrake in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2006, 12:29 PM
  3. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM
  5. Question involving using math operations...
    By Screwz Luse in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 04:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21