-
June 14th, 2007, 07:48 AM
#1
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).
-
June 14th, 2007, 08:34 AM
#2
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
-
June 14th, 2007, 08:46 AM
#3
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!
-
June 14th, 2007, 09:13 AM
#4
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
-
June 14th, 2007, 09:23 AM
#5
Re: comparsion function
simply can't use an "if". conditions at the machine level are certainly okay
-
June 14th, 2007, 09:49 AM
#6
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
-
June 14th, 2007, 02:04 PM
#7
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.
-
June 14th, 2007, 02:08 PM
#8
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;
-
June 14th, 2007, 05:23 PM
#9
Re: comparsion function
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
-
June 15th, 2007, 01:26 AM
#10
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.
-
June 15th, 2007, 01:53 AM
#11
Re: comparsion function
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 :-)
-
June 15th, 2007, 10:46 AM
#12
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.
-
June 15th, 2007, 11:15 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|