Ternary Operator and Saturation Arithmetic

This is a discussion on Ternary Operator and Saturation Arithmetic within the C Programming forums, part of the General Programming Boards category; Can someone explain to me how come these two operations yield different return values, using x=200, y=100? Code: // this ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    20

    Ternary Operator and Saturation Arithmetic

    Can someone explain to me how come these two operations yield different return values, using x=200, y=100?

    Code:
    // this returns 200
    ((x + y) > 255) ? (255) : (x + y);
        
    // this returns 255
    if((x + y) > 255) return 255;
        return x + y;

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It doesn't. It works fine. Your problem is elsewhere.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    For starters, the ternary operator can't return 200. It'll return either 255 or 300.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    The output of this program is 255, so it appears that you are mistaken:
    Code:
    #include <stdio.h>
    
    int foo(int x, int y)
    {
        return ((x + y) > 255) ? (255) : (x + y);
    }
    
    int main(void)
    {
        printf("%d\n", foo(200, 100));
        return 0;
    }
    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

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by enkwiringmindz View Post
    Can someone explain to me how come these two operations yield different return values, using x=200, y=100?

    Code:
    // this returns 200
    ((x + y) > 255) ? (255) : (x + y);
    Well, given that the above line of code doesn't actually include the word "return" in it, I wouldn't be surprised by ANY particular value it happened to return. In fact, I'm particularly unsurprised that it returns 200, because the value is probably loaded into a register during the (pointless) comparisons and additions, then just happens to be sitting there when you return from the function without actually returning anything. I bet if you turn on optimization the value will change.

    EDIT: Also, you guys are assuming the types are int, and there's no reason to assume that. If they were char, the datatype which actually makes the most sense here, the code would return 44. This is not the correct way to do a saturation!

    EDIT EDIT: The correct way is this:

    Code:
    return ( 255 - x < y ) ? 255 : ( x + y );
    Last edited by brewbuck; 04-16-2010 at 11:30 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    20
    I should have mentioned the return value is actually a char not int, it should still return 255 though right?

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    20
    OK I see, thanks a lot for your help.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    Quote Originally Posted by brewbuck
    Also, you guys are assuming the types are int, and there's no reason to assume that. If they were char, the datatype which actually makes the most sense here
    I did not assume int: I merely provided an example that uses int. Without knowing the context of "Saturation Arithmetic", I think int makes more sense than char, though unsigned char might make nearly as much sense as int.

    Quote Originally Posted by brewbuck
    the code would return 44.
    Or have an implementation defined result, if char is signed and CHAR_MAX is less than 300.
    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

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by laserlight View Post
    I did not assume int: I merely provided an example that uses int. Without knowing the context of "Saturation Arithmetic", I think int makes more sense than char, though unsigned char might make nearly as much sense as int.
    Normally, what is meant is clamping to the minimum/maximum values of the data type instead of allowing it to overflow.

    Or have an implementation defined result, if char is signed and CHAR_MAX is less than 300.
    True, but in any case the result isn't right.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    Quote Originally Posted by enkwiringmindz View Post
    I should have mentioned the return value is actually a char not int, it should still return 255 though right?
    It may not.

    char is not necessarily unsigned (it can also be signed - that is implementation defined). A signed char cannot necessarily hold the value 255. The results of adding two signed char's, if overflow occurs, is implementation defined.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


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