Thread: Ternary Operator and Saturation Arithmetic

  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,059
    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
    28,413
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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 10: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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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,815
    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 or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Tags for this Thread