<?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: sprintf in c compiles in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-in-c-compiles/m-p/2668508#M723002</link>
    <description>Hi Ted:&lt;BR /&gt;&lt;BR /&gt;Welcome to floating-point math; I'm sure that you are aware either value is equally correct but in most cases you want to round up. Anytime, I'm doing any floating-point stuff, I assume this is going to happen and allow for it.&lt;BR /&gt;&lt;BR /&gt;Typically, you want to add a small bias to the value before rounding.&lt;BR /&gt;&lt;BR /&gt;e.g.&lt;BR /&gt;&lt;BR /&gt;#define X_BIAS 0.0050000001&lt;BR /&gt;&lt;BR /&gt;sprintf(xxx,"%.2lf",doublefield + X_BIAS);&lt;BR /&gt;&lt;BR /&gt;Obviously it's a bit more complicated because you also need to handle negative amount so that generally a function is in order.&lt;BR /&gt;&lt;BR /&gt;BTW: NEVER,EVER compare floating points for equality but rather do something like this:&lt;BR /&gt;&lt;BR /&gt;if (atof(x - y) &amp;lt;= X_BIAS))&lt;BR /&gt;  printf("x equals y"); &lt;BR /&gt;else printf ("x not equal to y");&lt;BR /&gt;&lt;BR /&gt;Regards, Clay&lt;BR /&gt;</description>
    <pubDate>Wed, 20 Feb 2002 19:28:51 GMT</pubDate>
    <dc:creator>A. Clay Stephenson</dc:creator>
    <dc:date>2002-02-20T19:28:51Z</dc:date>
    <item>
      <title>sprintf in c compiles</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-in-c-compiles/m-p/2668506#M723000</link>
      <description>I am experiencing some differences in output for sprintf for code compiled in 10.20 and 11.0.&lt;BR /&gt;&lt;BR /&gt;sprintf(xxx, "%.2lf",doublefield);&lt;BR /&gt;&lt;BR /&gt;when doublefield=0.735 &lt;BR /&gt;obtains:&lt;BR /&gt;&lt;BR /&gt;0.74 under 10.20&lt;BR /&gt;0.73 under 11.0</description>
      <pubDate>Wed, 20 Feb 2002 19:17:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-in-c-compiles/m-p/2668506#M723000</guid>
      <dc:creator>Ted Mims</dc:creator>
      <dc:date>2002-02-20T19:17:23Z</dc:date>
    </item>
    <item>
      <title>Re: sprintf in c compiles</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-in-c-compiles/m-p/2668507#M723001</link>
      <description>You running 11/64?&lt;BR /&gt;&lt;BR /&gt;live free or die&lt;BR /&gt;harry</description>
      <pubDate>Wed, 20 Feb 2002 19:21:35 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-in-c-compiles/m-p/2668507#M723001</guid>
      <dc:creator>harry d brown jr</dc:creator>
      <dc:date>2002-02-20T19:21:35Z</dc:date>
    </item>
    <item>
      <title>Re: sprintf in c compiles</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-in-c-compiles/m-p/2668508#M723002</link>
      <description>Hi Ted:&lt;BR /&gt;&lt;BR /&gt;Welcome to floating-point math; I'm sure that you are aware either value is equally correct but in most cases you want to round up. Anytime, I'm doing any floating-point stuff, I assume this is going to happen and allow for it.&lt;BR /&gt;&lt;BR /&gt;Typically, you want to add a small bias to the value before rounding.&lt;BR /&gt;&lt;BR /&gt;e.g.&lt;BR /&gt;&lt;BR /&gt;#define X_BIAS 0.0050000001&lt;BR /&gt;&lt;BR /&gt;sprintf(xxx,"%.2lf",doublefield + X_BIAS);&lt;BR /&gt;&lt;BR /&gt;Obviously it's a bit more complicated because you also need to handle negative amount so that generally a function is in order.&lt;BR /&gt;&lt;BR /&gt;BTW: NEVER,EVER compare floating points for equality but rather do something like this:&lt;BR /&gt;&lt;BR /&gt;if (atof(x - y) &amp;lt;= X_BIAS))&lt;BR /&gt;  printf("x equals y"); &lt;BR /&gt;else printf ("x not equal to y");&lt;BR /&gt;&lt;BR /&gt;Regards, Clay&lt;BR /&gt;</description>
      <pubDate>Wed, 20 Feb 2002 19:28:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-in-c-compiles/m-p/2668508#M723002</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2002-02-20T19:28:51Z</dc:date>
    </item>
    <item>
      <title>Re: sprintf in c compiles</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-in-c-compiles/m-p/2668509#M723003</link>
      <description>of course if you do this, you get your desired results:&lt;BR /&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;&lt;BR /&gt;main()&lt;BR /&gt;{&lt;BR /&gt;        float doublefield = 0.735;&lt;BR /&gt;        printf("%.2lf",doublefield);&lt;BR /&gt;        exit(0);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Clay,&lt;BR /&gt;&lt;BR /&gt;Years ago I had to calculate what was deemed to be "close enough" (for a banking app), and of course that required me to learn Logarithms AGAIN! YUCK!!!!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;live free or die&lt;BR /&gt;harry&lt;BR /&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Wed, 20 Feb 2002 19:40:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-in-c-compiles/m-p/2668509#M723003</guid>
      <dc:creator>harry d brown jr</dc:creator>
      <dc:date>2002-02-20T19:40:58Z</dc:date>
    </item>
    <item>
      <title>Re: sprintf in c compiles</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-in-c-compiles/m-p/2668510#M723004</link>
      <description>Hi Again Ted:&lt;BR /&gt;&lt;BR /&gt;I'm an idiot. In my equality test I should have used fabs() rather than atof().&lt;BR /&gt;&lt;BR /&gt;if (fabs(x - y) &amp;lt;= X_BIAS)) &lt;BR /&gt;printf("x equals y"); &lt;BR /&gt;else printf ("x not equal to y");&lt;BR /&gt;&lt;BR /&gt;In any event, you really need to use the bias method because you are probably also going to see this problem (feature?) if you need to port your code to another platform/OS. Anytime you are doing floating-point calculations, you simply must take this feature into account.&lt;BR /&gt; &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 20 Feb 2002 20:06:00 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-in-c-compiles/m-p/2668510#M723004</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2002-02-20T20:06:00Z</dc:date>
    </item>
  </channel>
</rss>

