# tertiary comparison

This is a discussion on tertiary comparison within the C Programming forums, part of the General Programming Boards category; Is there an easy way to compare three variables or more for equality, and inequality? Let me explain. Code: if((x ...

1. ## tertiary comparison

Is there an easy way to compare three variables or more for equality, and inequality? Let me explain.
Code:
```if((x == y) && (y == z) && z != a) {
//code
}```
I am testing to see if x, y, and z are all equal, and none of them equal to a (part in italics).

2. Your way looks pretty good to me. What's wrong?

3. Well, it was expanding to be quite long. x, y, z are all really members of structs and expanding each one took multiple lines. I was just wondering, I can work with no other solution, just curious. Thanks.

4. Well...you could try (for integer types):
Code:
```if((x+y+z)/3 == x && a != x)
{
// code
}```
At least, I think that should work...as long as there's no overflow problem. I guess it isn't much shorter...

5. Originally Posted by itsme86
Well...you could try (for integer types):
Code:
```if((x+y+z)/3 == x && a != x)
{
// code
}```
At least, I think that should work...as long as there's no overflow problem. I guess it isn't much shorter...
It definately would not work:
Code:
```int x, y, z, a;

x = 4;
y = 3;
z = 5;
a = whatever;

if( ( x + y + z ) / 3 == x && a != x; )
{
printf("In this case, the average of x + y + z == x, but they do not equal eachother.\n");
}```
Quzah.

6. The minimum you're going to get is three checks. How you do those is up to you:
Code:
`if( !(z-y) && !(y-x) && (z-a) )`
That should be fun. You again have the possibility of signed undeflow though with the last check.

Quzah.

7. Good point...

8. Try this
Code:
```#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void foo ( int x, int y, int z, int a ) {
if ( x == y && y == z && x != a ) {
printf( "foo yes %d %d %d %d\n", x, y, z, a );
}
}
void bar ( int x, int y, int z, int a ) {
if ( ( x + y + z ) / 3 == x && x != a ) {
printf( "bar yes %d %d %d %d\n", x, y, z, a );
}
}
void baz ( int x, int y, int z, int a ) {
if ( (x & y & z) == x && (x | y | z) == x && x != a ) {
printf( "baz yes %d %d %d %d\n", x, y, z, a );
}
}

int main( void ) {
void (*fn[])(int,int,int,int) = { foo, bar, baz };
int i;
for ( i = 0 ; i < 3 ; i++ ) fn[i]( 2,1,3,4 );
for ( i = 0 ; i < 3 ; i++ ) fn[i]( 1,3,2,4 );
for ( i = 0 ; i < 3 ; i++ ) fn[i]( 1,1,1,2 );
for ( i = 0 ; i < 3 ; i++ ) fn[i]( 1,3,1,2 );
return 0;
}```
You can combine many values together using & and |, and then only perform a pair of tests

9. Then couldn't you just do:
Code:
`if( (x & y & z) == (x | y | z) && x != a )`
But I wouldn't have stumbled across that without your post.

Quzah.

10. <burns>excellent</burns>
Now it looks like it takes a lot more explaining than actually doing it

11. I believe the OP wanted to do the check by writing the variables fewer times, not by actually doing fewer comparisons...

12. Originally Posted by chrismiceli
Well, it was expanding to be quite long. x, y, z are all really members of structs and expanding each one took multiple lines. I was just wondering, I can work with no other solution, just curious. Thanks.
Well... if your structures are _that_ long and you don't like writing, you can put values in temp vars and hope that compiler will optimize them away.
Code:
```{
int tempX = x;
int tempY = y;
int tempZ = z;
if (tempX == tempY && tempY == tempZ && tempZ != a)
{
//code
}
}```

13. Originally Posted by quzah
Then couldn't you just do:
Code:
` if( (x & y & z) == (x | y | z) && x != a )`
But I wouldn't have stumbled across that without your post.

Quzah.
afaik, true is defined as !false and false as 0. Assuming true is always 1 (which it is here, but I don't know if this is guaranteed), this should work:

Code:
`if (((x == y) * z) == z && z != a)`
This could be written as...

Code:
`if ((((x == y) * z) == z) * a != a)`
... but this isn't shorter and only obfuscates stuff.

14. There doesn't seem to be an easy way to achieve this. Another possibility is to create a little function:

Code:
```int xtest(inx x, int y, int z, int a)
{
return (x == y && x == z && x != a) ? TRUE : FALSE;
}```
Then you only need specify each element for test once in your code.

Or even use a macro.

15. won't this work?:

Code:
``` if(a == b == c == d) {
//...
}```

Page 1 of 3 123 Last
Popular pages Recent additions