replacement for relational operations

  • Thread starter vishnu mahendra
  • Start date
V

vishnu mahendra

hello to all,

Any one please tell me how to compare two variables or constants without using
relational operations.

thank you in advance,
vishnu.
 
J

Joona I Palaste

vishnu mahendra said:
hello to all,
Any one please tell me how to compare two variables or constants without using
relational operations.

Well, if they're both numbers, substract them from each other and use
the result as an if expression. The if will be false if they're equal
or true if they're not. If you want to know which is bigger, ask a
higher C guru. I don't really know all that much about bit-twiddling
arithmetic.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am not very happy acting pleased whenever prominent scientists overmagnify
intellectual enlightenment."
- Anon
 
M

Mark McIntyre

Well, if they're both numbers, substract them from each other and use
the result as an if expression. The if will be false if they're equal
or true if they're not. If you want to know which is bigger, ask a
higher C guru. I don't really know all that much about bit-twiddling
arithmetic.

how about this, for unsigned types only

if(!(a-b)) puts("the same");
else
{
while(a-- && b--) a--, b--;
if(a && !b) puts ("a was larger");
if(!a && b) puts ("b was larger");
}

you can probably trivially extend this to cover negative numbers by
operating on combinations of a and b together.

You could also to do something like this:
if(b-a)
{
errno = 0;
log(b-a);
if(errno) puts("b was smaller than a");
errno = 0;
log(a-b);
if(errno) puts("a was smaller than b");
}
else
puts ("a equals b")


although that assumes neither a=b nor b-a is outside the range of a
double, that you don't get an underflow in the log, and that
math_errhandling ORs with MATH_ERRNO. I'm sure some cleverness could
work round all this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,303
Messages
2,571,557
Members
48,359
Latest member
Raqib_121635
Top