<?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 Comparing Real Numbers in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841348#M59317</link>
    <description>&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;the following two variables are declared real: CavNum and MinSig0.&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;However, whenthe following if() statement is executed it writes&lt;/DIV&gt;
&lt;DIV&gt;the values to afile as:&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;CavNum = 0.5001566410&lt;/DIV&gt;
&lt;DIV&gt;MinSig0 = 0.5001566410&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;and the numbers appear identical.&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;if(CavNum .LT. MinSig0) then&lt;/DIV&gt;
&lt;DIV&gt; .... code for write CavNum &amp;amp; MinSig0 to file using F16.10 fmt &lt;/DIV&gt;
&lt;DIV&gt;end if&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;Two questions:&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;1) Why does the If() statement above consider the two numbers not equal?&lt;/DIV&gt;
&lt;DIV&gt;2) Ifmemory represents the numbers with 32 bits, why are the numberswriten to file with 9 significant digits instead of 7?&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;Thanks for any comments.&lt;/DIV&gt;
&lt;P&gt;Message Edited by halcyong@fcc.net on &lt;SPAN class="date_text"&gt;09-15-2005&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;01:33 PM&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Message Edited by halcyong@fcc.net on &lt;SPAN class="date_text"&gt;09-15-2005&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;01:35 PM&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 16 Sep 2005 03:31:27 GMT</pubDate>
    <dc:creator>ahasan</dc:creator>
    <dc:date>2005-09-16T03:31:27Z</dc:date>
    <item>
      <title>Comparing Real Numbers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841348#M59317</link>
      <description>&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;the following two variables are declared real: CavNum and MinSig0.&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;However, whenthe following if() statement is executed it writes&lt;/DIV&gt;
&lt;DIV&gt;the values to afile as:&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;CavNum = 0.5001566410&lt;/DIV&gt;
&lt;DIV&gt;MinSig0 = 0.5001566410&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;and the numbers appear identical.&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;if(CavNum .LT. MinSig0) then&lt;/DIV&gt;
&lt;DIV&gt; .... code for write CavNum &amp;amp; MinSig0 to file using F16.10 fmt &lt;/DIV&gt;
&lt;DIV&gt;end if&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;Two questions:&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;1) Why does the If() statement above consider the two numbers not equal?&lt;/DIV&gt;
&lt;DIV&gt;2) Ifmemory represents the numbers with 32 bits, why are the numberswriten to file with 9 significant digits instead of 7?&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;Thanks for any comments.&lt;/DIV&gt;
&lt;P&gt;Message Edited by halcyong@fcc.net on &lt;SPAN class="date_text"&gt;09-15-2005&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;01:33 PM&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Message Edited by halcyong@fcc.net on &lt;SPAN class="date_text"&gt;09-15-2005&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;01:35 PM&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Sep 2005 03:31:27 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841348#M59317</guid>
      <dc:creator>ahasan</dc:creator>
      <dc:date>2005-09-16T03:31:27Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing Real Numbers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841349#M59318</link>
      <description>Just because they look identical when formatted in decimal doesn't mean they really are.  Write out the hexadecimal form using a Z8.8 or Z16.16 (for double) format.&lt;BR /&gt;&lt;BR /&gt;Single precision has 23 binary bits for the fraction.  But if you used an F16.10 format, you'll get 10 digits to the right of the decimal point no matter what.</description>
      <pubDate>Fri, 16 Sep 2005 03:54:47 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841349#M59318</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2005-09-16T03:54:47Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing Real Numbers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841350#M59319</link>
      <description>&lt;DIV&gt;
&lt;P&gt;Thanks Steve, Iwill try the hexadecimal format.&lt;/P&gt;
&lt;P&gt;I thought that 23 bits would give a max of 7 significant digits, but don't understand why the 8th and 9th digits after the decimal pointof 0.5001566410 are greater than 0. I thought the mantissa of a real number (32 bits) could have only 7digits (or 7digits that have the possibility to be.GT. 0), all other displayed digits would be 0, but I am missing something. &lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 16 Sep 2005 19:43:38 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841350#M59319</guid>
      <dc:creator>ahasan</dc:creator>
      <dc:date>2005-09-16T19:43:38Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing Real Numbers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841351#M59320</link>
      <description>Yes, you're missing that the fraction is in base 2.  The value you convert to decimal has (implied) zeroes beyond the 23 bits, but they are binary zeroes, not decimal zeroes.  When converted to decimal, you get some value which is the "more precise" representation of the binary value.&lt;BR /&gt;&lt;BR /&gt;I know that there are some implementations which just display zeroes after some number of digits.  Those implementations are broken, in my opinion.</description>
      <pubDate>Fri, 16 Sep 2005 21:02:40 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841351#M59320</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2005-09-16T21:02:40Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing Real Numbers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841352#M59321</link>
      <description>When the same library functions are employed to process single and double precision, they must be able to display 17 significant decimal places, for standard IEEE double precision.      For single precision, 9 significant decimals are required to avoid degradation under conversion from binary to decimal and back.  If you specify that degree of accuracy in formatted conversion, the run-time system is obligated to do what you ask.&lt;BR /&gt;What is missing in the preceding discussion is sufficient context to answer the question of why 2 numbers which appear to be the same after being written to a file were not the same when compared with an IF() test.  It was not even specified whether the compiler was instructed to use SSE instructions (-xK, -xW, et al, without -fltconsistency or the like), thus supporting single precision expressions, or to use the default x87 instructions, where this type of inconsistency between double precision expressions and single precision stored values is expected.</description>
      <pubDate>Fri, 16 Sep 2005 22:55:24 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841352#M59321</guid>
      <dc:creator>TimP</dc:creator>
      <dc:date>2005-09-16T22:55:24Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing Real Numbers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841353#M59322</link>
      <description>&lt;DIV&gt;Well, it is very simple to check if two variables are really the same. Do not output them, just calculate the difference and send it to the screen&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;write(*,*) CavNum-MinSig0&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;If the variables are the same up the the last digit captured by the system, then you'll get 0, if not you'll get your difference.&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;Personally I do not like to make a strict comparison of reals in the way it is done in the first post. I had an experience, when I input 1.00000000, and machine (compiler) interperets it as 0.99999999999.... or sm. like that.&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;IMHO: It is more "safe" to compare real numbers with certain tolerance:&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;tol=0.000001&lt;/DIV&gt;
&lt;DIV&gt;IF ( CavNum-MinSig0 .LT. tol) THEN&lt;/DIV&gt;
&lt;DIV&gt;! I assume that CavNum=MinSig0&lt;/DIV&gt;
&lt;DIV&gt;END IF&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;And tolerance can be set as "precision dependent" or "precision independent".&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 16 Sep 2005 23:44:24 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841353#M59322</guid>
      <dc:creator>dima333a</dc:creator>
      <dc:date>2005-09-16T23:44:24Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing Real Numbers</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841354#M59323</link>
      <description>&lt;DIV&gt;Really appreciatethe informative discussions above. Thanks.&lt;/DIV&gt;</description>
      <pubDate>Sat, 17 Sep 2005 06:14:23 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Comparing-Real-Numbers/m-p/841354#M59323</guid>
      <dc:creator>ahasan</dc:creator>
      <dc:date>2005-09-17T06:14:23Z</dc:date>
    </item>
  </channel>
</rss>

