CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Oct 2005
    Location
    Valka, Riga
    Posts
    36

    Exclamation comparsion function

    okay, this might seem dumb, but I'll ask anyways:

    how can I check what's the releation of two numbers, ie whether a>b or b>a or a==b without using comparsion operators and any branching constructions (namely if).

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: comparsion function

    You basically can't without resorting to some really obtuse code. So the question is WHY??????
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Oct 2005
    Location
    Valka, Riga
    Posts
    36

    Post Re: comparsion function

    I had this exercise in a test and couldn't get past it. I did everything else, but just couldn't figure this one out. all I did was end up with more and more complex calculations and tricks which ended up in having the need for at least 1 < or > or if... or <>

    and everyone I asked didn't do it either.

    the code can be as obtuse as needed, it should just do the trick. no beautifullnes required here!

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: comparsion function

    OK
    any branching constructions (namely if).
    If this means you simply cant use an "if" there are ways. If you mean no conditional branching at the machine level, it is nearly impossible.

    So which is it?
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Oct 2005
    Location
    Valka, Riga
    Posts
    36

    Re: comparsion function

    simply can't use an "if". conditions at the machine level are certainly okay

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: comparsion function

    trivial then:

    Code:
    (a==b) ? { //code for == } : ((a-b)/abs(a-b)) ? { code for a<b } : { code for a> b}
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Oct 2005
    Location
    Valka, Riga
    Posts
    36

    Arrow Re: comparsion function

    okay, thanks! but could you translate that into a c++ function?

    like

    Code:
    int compare(int a, int b){
    
    ...code...
    
    return result.
    }
    Last edited by edijs.vee; June 14th, 2007 at 02:18 PM.

  8. #8
    Join Date
    Jun 2007
    Posts
    105

    Re: comparsion function

    yep its the conditional operator its the same as an if statement

    n = x > y ? x : y;

    is the same as...

    if (x > y)
    n = x;
    else
    n = y;

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: comparsion function

    Quote Originally Posted by ne0n82
    yep its the conditional operator its the same as an if statement

    n = x > y ? x : y;

    is the same as...

    if (x > y)
    n = x;
    else
    n = y;
    But you used a relational operator which is illegal.

    To get the OP answer...

    Code:
    return (a==b) ? 0 : ((a-b)/abs(a-b)) ? -1  : 1;
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Join Date
    Feb 2004
    Location
    Helsinki, Finland
    Posts
    31

    Re: comparison function

    The ternary conditional operator is technically a branching construct provided the compiler can't/isn't allowed to use any branch replacement instructions. By using these, the compiler can completely eliminate simple branching constructs. For x86, the most useful instruction is CMOV, which requires Pentium Pro, Pentium II, AMD Athlon or later to run. For backwards compatibility, this isn't usually enabled by default.

    As for the question:

    a > b
    Evaluate (b - a). If the result is negative, the comparison is true.

    a == b
    Evaluate (a - b) | (b - a). If the result is negative, the comparison is false.

    This function returns parameter 'p' for nonnegative compare and 'n' for negative.
    Code:
    #include <climits>
    const unsigned int INT_BITS = CHAR_BIT * sizeof(int);
    
    int conditOper(const int compare, const int p, const int n) {
    	return p + ((n - p) & (compare >> (INT_BITS - 1)));
    }
    This is a practical, fast solution based on number signedness.

  11. #11
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: comparsion function

    Quote Originally Posted by TheCPUWizard
    Code:
    return (a==b) ? 0 : ((a-b)/abs(a-b)) ? -1  : 1;
    Sorry but I don't understand your answer. This expression will never return 1!! Only can return -1 or 0.
    To return 1, (a-b)/abs(a-b) must be 0. That's impossible!!! The previous condition would have been true. So, (a-b)/abs(a-b) must be 1 รณ -1, and both are true!!!

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  12. #12
    Join Date
    Jan 2001
    Posts
    253

    Re: comparsion function

    The method posted by Synthetic Being is the closest to what was asked for by the original poster - it doesn't use comparison operators.

    This method works ok as long as the 2 numbers being compared are within INT_MAX of each other. It will give incorrect results when the subtraction overflows.

    If the overflow detection is important, the following will work:

    Code:
    #include <climits>
    
    const unsigned int INT_BITS = CHAR_BIT * sizeof(int);
    
    int SignBit( const int value )
    {
       return static_cast<unsigned int>(value) >> (INT_BITS - 1);
    }
    
    int CompareWithOverflow( int first, int second )
    {
       return (SignBit(second) - SignBit(first)) +
              (SignBit(first) ^ SignBit(second) ^ 1) * (SignBit(second-first) - SignBit(first-second));
    }
    Note: I am basing this on the original code by Synthetic Being (checking the sign bits), and then extending it to check for sign differences between the original numbers.

    The routine CompareWithOverflow will return -1 if first is less than second, 0 if they are equal, and 1 if first is greater than second. No comparison operators are used.

  13. #13
    Join Date
    Dec 2005
    Posts
    642

    Re: comparsion function

    The most significant bit acts as a sign bit in 2's complement and sign-magnitude representation. C++ also allows 1's complement, in which the sign test is different.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured