On 1 Dec 2003 17:37:41 GMT, in comp.lang.c , Joona I Palaste

<pa*****@cc.helsinki.fi> wrote:

vishnu mahendra <vi*************@yahoo.com> scribbled the following: 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.

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.

--

Mark McIntyre

CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----

http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups

---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---