<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: IF condition for float in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/if-condition-for-float/m-p/3707431#M103418</link>
    <description>I don't suppose you can show the output of the 'counter=' line at the cross-over point?&lt;BR /&gt;&lt;BR /&gt;Also, you may want to throw some precision on those floats for the output to get a better idea of what's going on..</description>
    <pubDate>Wed, 11 Jan 2006 22:44:00 GMT</pubDate>
    <dc:creator>Stuart Browne</dc:creator>
    <dc:date>2006-01-11T22:44:00Z</dc:date>
    <item>
      <title>IF condition for float</title>
      <link>https://community.hpe.com/t5/operating-system-linux/if-condition-for-float/m-p/3707430#M103417</link>
      <description>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&lt;BR /&gt;&lt;BR /&gt;prt of my program is below:&lt;BR /&gt;/*the value = value_now cant work even if it is true*/&lt;BR /&gt;&lt;BR /&gt;/*DECLARATION*/&lt;BR /&gt;float value;            /*VALUE*/&lt;BR /&gt;float step_size;        /*STEP_SIZE*/&lt;BR /&gt;float v_start;          /*START SCAN VALUE*/&lt;BR /&gt;float v_stop;   /*STOP SCAN VALUE*/&lt;BR /&gt;float value_now;        /*THE CURRENT VALUE FOR ADC*/&lt;BR /&gt;float test;&lt;BR /&gt;int skip;       /*SKIP OF STEPS*/&lt;BR /&gt;int steps;      /*ADC STEPS*/&lt;BR /&gt;int skip_limit; /*SKIP_LIMIT*/&lt;BR /&gt;int i,j;        /*COUNTERS*/&lt;BR /&gt;int a_steps;    /*ACTUAL STEPS REQUIRE TO FIND VALUE*/&lt;BR /&gt;int stop_flag;  /*FLAG TO STOP ADC OPERATION*/&lt;BR /&gt;&lt;BR /&gt;..........&lt;BR /&gt;&lt;BR /&gt;while( stop_flag == 1){&lt;BR /&gt;        value_now = value_now + step_size;&lt;BR /&gt;        a_steps++;&lt;BR /&gt;        printf("counter = %d, value = %f, value now = %f, test = %f\n ", j, value, value_now);&lt;BR /&gt;&lt;BR /&gt;        if(value = value_now){ /*PROBLEM*/&lt;BR /&gt;                                        stop_flag = 0;&lt;BR /&gt;                                        printf("final value = %f\n ", value_now);&lt;BR /&gt;                                        break;&lt;BR /&gt;                                }&lt;BR /&gt;&lt;BR /&gt;                                j++;&lt;BR /&gt;&lt;BR /&gt;                        }       /*END WHILE*/&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;hope u can help.. many thanks!&lt;BR /&gt;&lt;BR /&gt;regards&lt;BR /&gt;Henry</description>
      <pubDate>Wed, 11 Jan 2006 21:34:30 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/if-condition-for-float/m-p/3707430#M103417</guid>
      <dc:creator>Henry Chua</dc:creator>
      <dc:date>2006-01-11T21:34:30Z</dc:date>
    </item>
    <item>
      <title>Re: IF condition for float</title>
      <link>https://community.hpe.com/t5/operating-system-linux/if-condition-for-float/m-p/3707431#M103418</link>
      <description>I don't suppose you can show the output of the 'counter=' line at the cross-over point?&lt;BR /&gt;&lt;BR /&gt;Also, you may want to throw some precision on those floats for the output to get a better idea of what's going on..</description>
      <pubDate>Wed, 11 Jan 2006 22:44:00 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/if-condition-for-float/m-p/3707431#M103418</guid>
      <dc:creator>Stuart Browne</dc:creator>
      <dc:date>2006-01-11T22:44:00Z</dc:date>
    </item>
    <item>
      <title>Re: IF condition for float</title>
      <link>https://community.hpe.com/t5/operating-system-linux/if-condition-for-float/m-p/3707432#M103419</link>
      <description>&amp;gt;&amp;gt;&amp;gt; if(value = value_now){ /*PROBLEM*/&lt;BR /&gt;&lt;BR /&gt;That's indeed a problem.&lt;BR /&gt;This assigns the value_now to value.&lt;BR /&gt;In order to compare you need to use ==&lt;BR /&gt;&lt;BR /&gt;if(value == value_now){ /*SMALLER PROBLEM*/&lt;BR /&gt;&lt;BR /&gt;But you still have to be careful!&lt;BR /&gt; &lt;BR /&gt;Not all values can be represented exactly with floating points.&lt;BR /&gt;&lt;BR /&gt;So you really want to deal with a range.&lt;BR /&gt;Something along the lines of&lt;BR /&gt;&lt;BR /&gt;if ((value_now - value) &amp;lt; step_size) { ...&lt;BR /&gt;&lt;BR /&gt; &lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;In your example is seems (untested) that you can just test for &lt;BR /&gt;&lt;BR /&gt;if (value_now &amp;gt; value) { /* no problem */&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Cheersm&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Wed, 11 Jan 2006 22:48:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/if-condition-for-float/m-p/3707432#M103419</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2006-01-11T22:48:03Z</dc:date>
    </item>
    <item>
      <title>Re: IF condition for float</title>
      <link>https://community.hpe.com/t5/operating-system-linux/if-condition-for-float/m-p/3707433#M103420</link>
      <description>Your actually have 2 problems. One is syntax. The other is in your logic.&lt;BR /&gt;&lt;BR /&gt;if (value = value_now) /* assignment */&lt;BR /&gt;should be:&lt;BR /&gt;if (value == value_now) /* comparison */&lt;BR /&gt;&lt;BR /&gt;However, there is still a problem. Floating point vales should never be compared for equality because of the lack of precision. &lt;BR /&gt;&lt;BR /&gt;For example, consider 2 values, very nearly equal:&lt;BR /&gt;a = 1.999999&lt;BR /&gt;b = 2.000001&lt;BR /&gt;&lt;BR /&gt;You most likely would like those to compare equally:&lt;BR /&gt;&lt;BR /&gt;The trick is to make a "nearly_equal" function using the fabs() function.&lt;BR /&gt;&lt;BR /&gt;#include &lt;MATH.H&gt;&lt;BR /&gt;&lt;BR /&gt;# define SMALL_DIFF 0.00001&lt;BR /&gt;&lt;BR /&gt;int nearly_equal(double a, double b)&lt;BR /&gt;{&lt;BR /&gt;  int close_enough;&lt;BR /&gt;&lt;BR /&gt;  close_enough = (fabs(a - b) &amp;lt;= SMALL_DIFF);&lt;BR /&gt;  return(close_enough)&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;So now your test should become:&lt;BR /&gt;&lt;BR /&gt;if (nearly_equal((double) value, (double) value_now))&lt;BR /&gt;  {&lt;BR /&gt;    stop_flag = 0;&lt;BR /&gt;    ....&lt;BR /&gt;    ....&lt;BR /&gt;  }&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;/MATH.H&gt;</description>
      <pubDate>Wed, 11 Jan 2006 22:49:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/if-condition-for-float/m-p/3707433#M103420</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-01-11T22:49:18Z</dc:date>
    </item>
  </channel>
</rss>

