1753822 Members
9512 Online
108805 Solutions
New Discussion юеВ

IF condition for float

 
Henry Chua
Super Advisor

IF condition for float

Hi guys, I been working with a prgram.. when I use if to compare two float they can seem to be proven even if they are indeed equal

prt of my program is below:
/*the value = value_now cant work even if it is true*/

/*DECLARATION*/
float value; /*VALUE*/
float step_size; /*STEP_SIZE*/
float v_start; /*START SCAN VALUE*/
float v_stop; /*STOP SCAN VALUE*/
float value_now; /*THE CURRENT VALUE FOR ADC*/
float test;
int skip; /*SKIP OF STEPS*/
int steps; /*ADC STEPS*/
int skip_limit; /*SKIP_LIMIT*/
int i,j; /*COUNTERS*/
int a_steps; /*ACTUAL STEPS REQUIRE TO FIND VALUE*/
int stop_flag; /*FLAG TO STOP ADC OPERATION*/

..........

while( stop_flag == 1){
value_now = value_now + step_size;
a_steps++;
printf("counter = %d, value = %f, value now = %f, test = %f\n ", j, value, value_now);

if(value = value_now){ /*PROBLEM*/
stop_flag = 0;
printf("final value = %f\n ", value_now);
break;
}

j++;

} /*END WHILE*/


hope u can help.. many thanks!

regards
Henry
3 REPLIES 3
Stuart Browne
Honored Contributor

Re: IF condition for float

I don't suppose you can show the output of the 'counter=' line at the cross-over point?

Also, you may want to throw some precision on those floats for the output to get a better idea of what's going on..
One long-haired git at your service...
Hein van den Heuvel
Honored Contributor

Re: IF condition for float

>>> if(value = value_now){ /*PROBLEM*/

That's indeed a problem.
This assigns the value_now to value.
In order to compare you need to use ==

if(value == value_now){ /*SMALLER PROBLEM*/

But you still have to be careful!

Not all values can be represented exactly with floating points.

So you really want to deal with a range.
Something along the lines of

if ((value_now - value) < step_size) { ...


You'll have to decided whether to pre-calc the difference and then compare, whether to do an absolute value function first and so on.

In your example is seems (untested) that you can just test for

if (value_now > value) { /* no problem */


Cheersm
Hein.
A. Clay Stephenson
Acclaimed Contributor

Re: IF condition for float

Your actually have 2 problems. One is syntax. The other is in your logic.

if (value = value_now) /* assignment */
should be:
if (value == value_now) /* comparison */

However, there is still a problem. Floating point vales should never be compared for equality because of the lack of precision.

For example, consider 2 values, very nearly equal:
a = 1.999999
b = 2.000001

You most likely would like those to compare equally:

The trick is to make a "nearly_equal" function using the fabs() function.

#include

# define SMALL_DIFF 0.00001

int nearly_equal(double a, double b)
{
int close_enough;

close_enough = (fabs(a - b) <= SMALL_DIFF);
return(close_enough)
}



So now your test should become:

if (nearly_equal((double) value, (double) value_now))
{
stop_flag = 0;
....
....
}


Note that also did not initialize stop_flag to a value so that your initial comparison at the top of the while loop is bogus as well.
If it ain't broke, I can fix that.