<?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 Rounding numbers (double- to single- precision) in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910120#M83192</link>
    <description>&lt;P&gt;You should never compare two floating point numbers directly, neither for single nor for double precision. The following code snippet can be used to do the trick:&lt;/P&gt;
&lt;P&gt;real :: a, b&lt;BR /&gt;real, parameter :: eps=1.E-5 ! set to the precision needed&lt;BR /&gt;if ( abs(a-b) .lt. eps) then ! if absolute difference is less than eps, than the term becomes true&lt;/P&gt;</description>
    <pubDate>Fri, 22 Jan 2010 11:04:06 GMT</pubDate>
    <dc:creator>rase</dc:creator>
    <dc:date>2010-01-22T11:04:06Z</dc:date>
    <item>
      <title>Rounding numbers (double- to single- precision)</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910116#M83188</link>
      <description>&lt;P&gt;Hi everyone&lt;/P&gt;
&lt;P&gt;Function D2R is to round a double precision value to asingle one but sometimes does not work. For instance when A1 = .004D0; A2 = .031D0 the programs echoes 4.0000002E-03 3.0999999E-02 instead of 4.0000000E-03 3.1000000E-02.&lt;/P&gt;
&lt;P&gt;Could someone tell me 1- whats wrong and 2- if in general there is a function which rounds anumber to a given decimal place or not.&lt;/P&gt;
&lt;P&gt;Hamid&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt; &lt;B&gt;DOUBLE PRECISION&lt;/B&gt; A1,A2&lt;/P&gt;
&lt;P&gt; &lt;B&gt;REAL*4&lt;/B&gt; D2R&lt;/P&gt;
&lt;P&gt; A1 = .004D0; A2 = .031D0; &lt;B&gt;PRINT&lt;/B&gt;*, D2R(A1), D2R(A2)&lt;/P&gt;
&lt;P&gt; &lt;B&gt;END&lt;/B&gt; &lt;B&gt;PROGRAM&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt; &lt;B&gt;REAL*4&lt;/B&gt; &lt;B&gt;FUNCTION&lt;/B&gt; D2R(A)&lt;/P&gt;
&lt;P&gt; &lt;B&gt;DOUBLE PRECISION&lt;/B&gt; A&lt;/P&gt;
&lt;P&gt; D2R = A&lt;/P&gt;
&lt;P&gt; &lt;B&gt;RETURN&lt;/B&gt;&lt;/P&gt;
&lt;P&gt; &lt;B&gt;END FUNCTION&lt;/B&gt; D2R&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2010 17:45:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910116#M83188</guid>
      <dc:creator>haminin</dc:creator>
      <dc:date>2010-01-21T17:45:00Z</dc:date>
    </item>
    <item>
      <title>Rounding numbers (double- to single- precision)</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910117#M83189</link>
      <description>&lt;P&gt;Nothing wrong. It looks like you are finding the nearest representable single precision binary numbers to the double precision values you set. Those will usually (not always) match to 7 decimal digits when range, not often to 8 decimal digits, except in the cases where the conversion is exact.&lt;/P&gt;
&lt;P&gt;You can set a format to display only 6 decimal digits, which should give results more to your expectation.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2010 20:07:23 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910117#M83189</guid>
      <dc:creator>TimP</dc:creator>
      <dc:date>2010-01-21T20:07:23Z</dc:date>
    </item>
    <item>
      <title>Rounding numbers (double- to single- precision)</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910118#M83190</link>
      <description>&lt;P&gt;Many thanks Tim&lt;/P&gt;
&lt;P&gt;I need to compare their values not displaying them, so do you mean I can use internal writing to write only 6 decimal digits? Is it the best solution to round numbers?&lt;/P&gt;
&lt;P&gt;Hamid&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2010 21:12:51 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910118#M83190</guid>
      <dc:creator>haminin</dc:creator>
      <dc:date>2010-01-21T21:12:51Z</dc:date>
    </item>
    <item>
      <title>Rounding numbers (double- to single- precision)</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910119#M83191</link>
      <description>&lt;P&gt;Your purpose isn't clear. Do you want to find out whether 2 numbers would round (usually, or always) to the same number in decimal format? You could use internal write to make character string representations, and compare them. Or, a more usual goal, to check whether they are within a specified relative tolerance?&lt;/P&gt;
&lt;P&gt;If this is homework, the "point" may be to encourage you to understand something about binary floating point.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2010 21:31:37 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910119#M83191</guid>
      <dc:creator>TimP</dc:creator>
      <dc:date>2010-01-21T21:31:37Z</dc:date>
    </item>
    <item>
      <title>Rounding numbers (double- to single- precision)</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910120#M83192</link>
      <description>&lt;P&gt;You should never compare two floating point numbers directly, neither for single nor for double precision. The following code snippet can be used to do the trick:&lt;/P&gt;
&lt;P&gt;real :: a, b&lt;BR /&gt;real, parameter :: eps=1.E-5 ! set to the precision needed&lt;BR /&gt;if ( abs(a-b) .lt. eps) then ! if absolute difference is less than eps, than the term becomes true&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2010 11:04:06 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910120#M83192</guid>
      <dc:creator>rase</dc:creator>
      <dc:date>2010-01-22T11:04:06Z</dc:date>
    </item>
    <item>
      <title>Rounding numbers (double- to single- precision)</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910121#M83193</link>
      <description>&lt;P&gt;Many thanks again for your comment.&lt;/P&gt;
&lt;P&gt;I wanted to round a number to a given decimal place. I wrote the function using internal writings. If J is not updated it clearly echoes the digits after the decimal point without rounding afterward digits.&lt;/P&gt;
&lt;P&gt;The function set to change double- to single precision (N = 7).&lt;/P&gt;
&lt;P&gt; &lt;B&gt;REAL*8&lt;/B&gt; &lt;B&gt;FUNCTION&lt;/B&gt; D2R(A)&lt;/P&gt;
&lt;P&gt; &lt;B&gt;IMPLICIT NONE&lt;/B&gt;&lt;/P&gt;
&lt;P&gt; &lt;B&gt;DOUBLE PRECISION&lt;/B&gt; A, B, C&lt;/P&gt;
&lt;P&gt; &lt;B&gt;INTEGER&lt;/B&gt; E, J, N, I&lt;/P&gt;
&lt;P&gt; &lt;B&gt;CHARACTER&lt;/B&gt;*23 TEMP&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt; N = 7 ! The number of places after the decimal point&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt; TEMP = ''&lt;/P&gt;
&lt;P&gt; &lt;B&gt;WRITE&lt;/B&gt;(TEMP, FMT = '(E23.16E2)') A&lt;/P&gt;
&lt;P&gt; ! Number without exponent&lt;/P&gt;
&lt;P&gt; &lt;B&gt;READ&lt;/B&gt;(UNIT = TEMP, FMT = "(E&lt;N&gt;.&lt;N&gt;)") B&lt;/N&gt;&lt;/N&gt;&lt;/P&gt;
&lt;P&gt; ! The (N+1)-th digit after decimal point&lt;/P&gt;
&lt;P&gt; &lt;B&gt;READ&lt;/B&gt;(UNIT = TEMP, FMT = "(&amp;lt;3+N&amp;gt;X,I1)") J&lt;/P&gt;
&lt;P&gt; ! The number of digits in the exponent&lt;/P&gt;
&lt;P&gt; &lt;B&gt;READ&lt;/B&gt;(UNIT = TEMP, FMT = "(20X,I3)") E&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;!_____________________&lt;/P&gt;
&lt;P&gt; &lt;B&gt;IF&lt;/B&gt; (J .GE. 5) B = B + SIGN(10.D0 ** -N,B) ! Update last digit&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt; &lt;B&gt;IF&lt;/B&gt; (J .EQ. 4) &lt;B&gt;THEN&lt;/B&gt;&lt;/P&gt;
&lt;P&gt; LOOP_A : &lt;B&gt;DO&lt;/B&gt; I = 1, 8 ! Check next digit&lt;/P&gt;
&lt;P&gt; &lt;B&gt;READ&lt;/B&gt;(UNIT = TEMP, FMT = "(&amp;lt;3+N+I&amp;gt;X,I1)") J&lt;/P&gt;
&lt;P&gt; &lt;B&gt;IF&lt;/B&gt; (J .LT. 4) &lt;B&gt;EXIT&lt;/B&gt;&lt;/P&gt;
&lt;P&gt; &lt;B&gt;IF&lt;/B&gt; (J .GT. 4) &lt;B&gt;THEN&lt;/B&gt;&lt;/P&gt;
&lt;P&gt; B = B + SIGN(10.D0 ** -N,B) ! Update last digit&lt;/P&gt;
&lt;P&gt; &lt;B&gt;EXIT&lt;/B&gt; LOOP_A&lt;/P&gt;
&lt;P&gt; &lt;B&gt;END IF&lt;/B&gt;&lt;/P&gt;
&lt;P&gt; ! If J = 4 --&amp;gt; continue checking next numbers&lt;/P&gt;
&lt;P&gt; &lt;B&gt;END DO&lt;/B&gt; LOOP_A&lt;/P&gt;
&lt;P&gt; &lt;B&gt;END IF&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;!_____________________&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt; D2R = B * 10.D0 ** E&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt; &lt;B&gt;RETURN&lt;/B&gt;&lt;/P&gt;
&lt;P&gt; &lt;B&gt;END FUNCTION&lt;/B&gt; D2R&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2010 18:15:44 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910121#M83193</guid>
      <dc:creator>haminin</dc:creator>
      <dc:date>2010-01-22T18:15:44Z</dc:date>
    </item>
    <item>
      <title>Rounding numbers (double- to single- precision)</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910122#M83194</link>
      <description>&lt;P&gt;Many thanks Rase. I couldnt understand you well, but my problem is now solved by function D2R.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2010 18:17:50 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910122#M83194</guid>
      <dc:creator>haminin</dc:creator>
      <dc:date>2010-01-22T18:17:50Z</dc:date>
    </item>
    <item>
      <title>Rounding numbers (double- to single- precision)</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910123#M83195</link>
      <description>&lt;P&gt;I'd suggest instead something like this:&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;PRE&gt;[plain]REAL(4) FUNCTION D2R (A,PLACES)&lt;BR /&gt;DOUBLE PRECISION A&lt;BR /&gt;INTEGER PLACES&lt;BR /&gt;&lt;BR /&gt;D2R = ANINT(A*(10.D0**PLACES))/(10.D0**PLACES)&lt;BR /&gt;RETURN&lt;BR /&gt;END[/plain]&lt;/PRE&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;This relies on the fact that floating point values can exactly represent integers (within a range based on the precision)&lt;/P&gt;
&lt;P&gt;Do keep in mind that single precision is good to only about six significant digits so that it can't exactly represent all possible rounded values of more digits.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2010 19:03:54 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910123#M83195</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2010-01-22T19:03:54Z</dc:date>
    </item>
    <item>
      <title>Rounding numbers (double- to single- precision)</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910124#M83196</link>
      <description>&lt;P&gt;Its interesting. I didnt know about ANINT function. It should be useful for this purpose, but I think your D2R function needs to be revised, for instance when A = -.031123354445444443D0 * 10.D0 ** 10 it always gives -3.1123354E+08. Anyway thank you very much for this.&lt;/P&gt;
&lt;P&gt;Hamid&lt;/P&gt;</description>
      <pubDate>Sat, 23 Jan 2010 16:28:40 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910124#M83196</guid>
      <dc:creator>haminin</dc:creator>
      <dc:date>2010-01-23T16:28:40Z</dc:date>
    </item>
    <item>
      <title>Rounding numbers (double- to single- precision)</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910125#M83197</link>
      <description>That's why there's a divide after the ANINT. The ANINT rounds and the divide reestablishes the magnitude. But again you're limited by the number of bits available in single precision. Don't expect reliable results when rounding past 6 significant digits (including any to the left of the decimal.</description>
      <pubDate>Sat, 23 Jan 2010 18:26:25 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Rounding-numbers-double-to-single-precision/m-p/910125#M83197</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2010-01-23T18:26:25Z</dc:date>
    </item>
  </channel>
</rss>

