Click to See Complete Forum and Search --> : Can comparisons be done without If statements in C/C++


nripun
December 11th, 2006, 07:32 AM
Can i perform a comparison operation without using IF statement.

For example:

if( x< 0.5 || x > 1.0 )
return 0;
else
return 1;

Here the values 0.5 and 1.0 are fixed ie. the condition always check for 0.5 and 1.0 and no other values.
Can i replace the above If-else statement?

TheCPUWizard
December 11th, 2006, 07:41 AM
return ( x< 0.5 && x > 1.0 ) ? 0 : 1;


Internally this still generates conditional branches, so I am curious as to *why* you don't want to use an if... :confused:

Brad Jones
December 11th, 2006, 08:09 AM
what would this do:


while( x< 0.5 && x > 1.0 )
{
return 0;
}
return 1;


(of course, the example being used would always be false since a number can't be both less than .5 and greater than 1. As such, your statement could simply be changed to:

return 1;

TheCPUWizard
December 11th, 2006, 08:10 AM
Brad,

That code is just plain devious :sick:

Brad Jones
December 11th, 2006, 08:13 AM
Brad,

That code is just plain devious :sick:

Devious is a much nicer word than I'd use. As you stated, the 'if' statement should be used as that is its function. My solution was poor coding (in my opinion). Then again, I don't like stacking a bunch of stuff in an return statement either :).

Brad!

Krishnaa
December 11th, 2006, 08:21 AM
Why? I am curious, if is one of the mostly used C/C++ keyword. what reason makes you not use it?
BTW Is using ASM an option?

Krishnaa
December 11th, 2006, 08:23 AM
return ( x< 0.5 && x > 1.0 ) ? 0 : 1;


Internally this still generates conditional branches, so I am curious as to *why* you don't want to use an if... :confused:

For compiler using IF or "?:" is same...so it's necessory to know your objective behind not using IF...

nripun
December 11th, 2006, 08:38 AM
Please check the attachment Graph1.jpg.

In the graph you can 4 classes. For a particular value of S there can be more than one value of μS[.b] since the classes/sets overlap in the X axis. My aim is to find the [b]μS for each value of S.

The following are the Equation of the lines.


Equation Condition
----------- -------------

1) Class I y = 1.0 x<= 0.18
2) Class I y = (0.35 - x)/0.17 0.18<x<0.35
3) Class II y = (x-0.2)/0.2 0.20<x<0.4
4) Class II y = (1.015 - x) / 0.615 0.4<x<1.015
5) Class III y = 1.0 0.683 < = x < 1.21
6) Class III y = (2.12 - x) / 0.91 1.21<x<2.12
7) Class IV y = (x - 2.01) / 0.07 2.01x<2.68
8) Class IV y = (x-3.75)/ 1.17 2.68<x<3.85


One way of solving my problem is to use an If condition statement to determine the set in which x lies and then use the appropriate equation. But if the number of sets increase in number, naturally the number of if statements also increase.

Is there a mathematical equation or way to solve the if condition?

Krishnaa
December 11th, 2006, 08:48 AM
Have you thought of using switch() case ???

nripun
December 11th, 2006, 08:52 AM
how does switch() solve inequality?

Krishnaa
December 11th, 2006, 09:04 AM
Okay, so you want to identify the class from given value, right ? without using too many conditions?

You can use static array of values, which holds all possible values of classes, there you can simply multiply you float value by 10/100 to get the integer index and then get the class value store at that index.

For example, I have 3 possible classes,

Class 1 has range of 0.1 to 3.5,
Class 2 has range of 3.6 to 8.5,
Class 1 has range of 8.6 to 9.9.

Now I take an array of 100 elements, and store class numbers in it as,

array[0] = NULL; // to show not applicatable value, or there is no class for this value
array[1] to array[35] has value 1,
array[36] to array[85] has 2,
array[86] to array[99] has 3...thats it...now if I get value to classify, such as in following function,

int array[100] ={.......as per above logic...};
int ClassifyMe(float fValue)
{
int index = fValue*10 ; // we have only 2 digit possible values..
if(index > 0 && index <100)
{
return array[index] ;///thats it..
}
}


This way you use a little more memory, but save too posible 100 comparisons happening everytime.

You can certainly sacrifice few kbs to save thousands of if-else....

nripun
December 12th, 2006, 03:28 AM
Yes ASM is an option. How is the High Level IF statement converted into a low level machine code. I assume every statement is converted into digital logic. Pardon me if my assumption is wrong.

TheCPUWizard
December 12th, 2006, 07:59 AM
It translates to a conditional branch instruction (as I alluded to much earlier...)

Krishnaa
December 12th, 2006, 08:50 AM
Yes ASM is an option. How is the High Level IF statement converted into a low level machine code. I assume every statement is converted into digital logic. Pardon me if my assumption is wrong.

Yep,

If your possible values range is not too big then why don't you try the solution I posted in my earlier post, that will reduce each and evey comparison requirement.

TheCPUWizard
December 12th, 2006, 08:54 AM
Krishnaa,


that will reduce each and evey comparison requirement.




Actually it will reduce the coding required for every comparision requirement. But will not reduce the number of comparisions actually required..... :)

SuperKoko
December 12th, 2006, 09:19 AM
Actually it will reduce the coding required for every comparision requirement. But will not reduce the number of comparisions actually required..... :)
Yes.

If, for any irrationnal reason, one really wants to avoid conditional jumps, he can use indirect calls.

int x,y;
/* ... */
if (x<y)
do_something();
else
do_something_different();

Can be written:

void do_something();
void do_something_different();
/* ... */
int x,y;
/* ... */
void (*doit[2])()={do_something, do_something_different};
doit[((x-y)>>(sizeof(int)*CHAR_BIT-1)) + 1](); /* warning, not portable, but should work on many two's complement machine */

Nothing says that the compiler will not generate any conditional jump, but he is more likely to generate none then with a classical if/else conditional statement.
:D

Krishnaa
December 13th, 2006, 07:52 AM
Krishnaa,


But will not reduce the number of comparisions actually required..... :)

how? as I understand, each class has certain range, if they dont cross each other then why do we need any comparison, except on array bounds check ?

nvidia
December 15th, 2006, 05:37 AM
Can i perform a comparison operation without using IF statement.

For example:

if( x< 0.5 || x > 1.0 )
return 0;
else
return 1;

Here the values 0.5 and 1.0 are fixed ie. the condition always check for 0.5 and 1.0 and no other values.
Can i replace the above If-else statement?
how does switch() solve inequality?


i dunno why you wanna do it without if else but here it is anyway :)

bool a = (x< 0.5 || x > 1.0);

switch(a) //or directly: switch(x< 0.5 || x > 1.0)
{
case false:
return 0;
break;

case true:
return 1;
break;
}

spoulson
December 26th, 2006, 09:46 AM
Yes ASM is an option. How is the High Level IF statement converted into a low level machine code. I assume every statement is converted into digital logic. Pardon me if my assumption is wrong.

If compiling to native binary (i.e. not a .NET assembly), then the compiler can output an assembly listing, where it shows exactly what op-codes are generated for each line of C/C++ code. You will also be able to see how the compiler optimizes between debug and release mode and other compiler flags. (optimize for speed, optimize for size, etc.)

Still, I don't see why we're avoiding the most basic programming logic element, the if statement.