Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Testing bedmas.

  1. Sep 21, 2009 #1
    In the following sources -

    Code (Text):
    #include<stdio.h>
    main()
    {
        char a = 2^2/2*2+5-1;
        printf("%d \n", a);
        //Expected - ((((2^2)/2)*2)+5)-1
        //Expected result -- 8
        //Actual result -- 4
        a = 1 - 5+2*2/2^2;
        printf("%d \n", a);
        //Expected - ((((2^2)/2)*2)+5)-1
        //Expected result -- 8
        //Actual result -- -4
    }
    I expect a result 8 after computation of 2^2/2*2+5-1 or 1 - 5+2*2/2^2 since 1 - 5+2*2/2^2 will mean -
    ((((2^2)/2)*2)+5)-1 and 2^2/2*2+5-1 will mean -
    ((((2^2)/2)*2)+5)-1

    Which yields 8.
     
  2. jcsd
  3. Sep 22, 2009 #2
    Sooo...is this a bug in C?
     
  4. Sep 22, 2009 #3
    In C, ^ means bitwise xor, not exponentiation.
     
  5. Sep 22, 2009 #4
    So how do we write exponential here?

    And what's the actual order then?
     
  6. Sep 24, 2009 #5
    aaaaa...no answers?
     
  7. Sep 28, 2009 #6

    Mark44

    Staff: Mentor

    There is no exponent operator in C. If you want to raise a number to a power, use the standard library function pow(). For example, to calculate 22, do something like this:
    Code (Text):

    #include <math.h>
    .
    .
    .
    double x;
    x = pow(2.0, 2.0);
     
     
  8. Oct 4, 2009 #7
    oh...I remember that function though.

    So what does exponential do?
     
  9. Oct 4, 2009 #8
    I mean if I've written '^' what will it mean in C?
     
  10. Oct 4, 2009 #9

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Bitwise exclusive 'or' as mXSCNT said:

    So take two numbers and the result number has a one bit where one and only one of the bits is on in the two numbers.

    25 = 11001
    12 = 01100
    ^. = 10101 = 21
     
  11. Oct 5, 2009 #10
    humm...thanks
     
  12. Oct 5, 2009 #11
    Ok...new issues; in this code -

    Code (Text):
    #include<stdio.h>
    main()
    {
        char a = 2/2*2+5-1;
        printf("%d \n", a);
        //Expected - (((2/2)*2)+5)-1
        //Expected result -- 6
        //Actual result -- 6
        a = 1 - 5+2*2/2;
        printf("%d \n", a);
        //Expected -(((2/2)*2)+5)+1
        //Expected result -- 6
        //Actual result -- -2
    }
    So why does it come - 2 in the second print?
     
  13. Oct 5, 2009 #12

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Order of operations:
    a = 1 - 5+2*2/2;
    a = 1 - 5+2*1
    a = 1 - 5 + 2
    a = -4 + 2 = -2
     
  14. Oct 5, 2009 #13
    Code (Text):

        char a = 2 / 2 * 2 [B][COLOR="Red"]+[/COLOR][/B] 5 [B][COLOR="#ff0000"]-[/COLOR][/B] 1;

        a = 1 [B][COLOR="Red"]-[/COLOR][/B] 5 + 2 * 2 / 2;

        //Expected -(((2/2)*2)[B][COLOR="Red"]+[/COLOR][/B]5)[B][COLOR="Red"]+[/COLOR][/B]1

     
    typo?
    i dont get it. :(
     
  15. Oct 5, 2009 #14
    Yes, I got it, thanks.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Testing bedmas.
  1. Prime number test (Replies: 4)

  2. G95 coarray test code (Replies: 1)

  3. C++ prime testing (Replies: 13)

Loading...