<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Round a Float to # of Significant Digits Following The Decimal Point</title>
	<atom:link href="http://jasonmock.com/2007/02/22/round-a-float-to-of-significant-digits/feed/" rel="self" type="application/rss+xml" />
	<link>http://jasonmock.com/2007/02/22/round-a-float-to-of-significant-digits/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=round-a-float-to-of-significant-digits</link>
	<description>[insert something witty here]</description>
	<lastBuildDate>Tue, 24 Jan 2012 02:45:58 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Jason Mock</title>
		<link>http://jasonmock.com/2007/02/22/round-a-float-to-of-significant-digits/#comment-52600</link>
		<dc:creator>Jason Mock</dc:creator>
		<pubDate>Sat, 10 May 2008 02:41:40 +0000</pubDate>
		<guid isPermaLink="false">http://jasonmock.com/wordpress/2007/02/22/round-a-float-to-of-significant-digits/#comment-52600</guid>
		<description>Actually, your comment made me realize that I didn&#039;t properly describe what it was that I had created.  In my specific case, I didn&#039;t care about the number of significant digits, I only cared about the number digits following the decimal point.  So, I may even re-title the post since it is obviously incorrect.  Thank you for pointing that out and not letting me lead others astray.</description>
		<content:encoded><![CDATA[<p>Actually, your comment made me realize that I didn&#8217;t properly describe what it was that I had created.  In my specific case, I didn&#8217;t care about the number of significant digits, I only cared about the number digits following the decimal point.  So, I may even re-title the post since it is obviously incorrect.  Thank you for pointing that out and not letting me lead others astray.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David A. Pimentel</title>
		<link>http://jasonmock.com/2007/02/22/round-a-float-to-of-significant-digits/#comment-52596</link>
		<dc:creator>David A. Pimentel</dc:creator>
		<pubDate>Wed, 07 May 2008 21:03:59 +0000</pubDate>
		<guid isPermaLink="false">http://jasonmock.com/wordpress/2007/02/22/round-a-float-to-of-significant-digits/#comment-52596</guid>
		<description>Actually, this is not quite correct assuming you want significant figures instead of decimal places. Your function rounds to a spcified number of places after the decimal point. A definition of significant figures (or digits) can be found at http://en.wikipedia.org/wiki/Significant_figures. To retain only a specified number of significant figures, the function should be:

&lt;code&gt;inline double fround(double n, unsigned d)
{
&#160;&#160;unsigned p;
&#160;&#160;p = d - (unsigned)log10(n);
&#160;&#160;return floor(n * pow(10., p) + .5) / pow(10., p);
}&lt;/code&gt;

Given fround(1703.12345689,6), the result will be 1703.12, whereas, your original function will return 1703.123157.

Here is a simple test to verify the results:

&lt;code&gt;#include &lt;stdio.h&gt;
#include &lt;math.h&gt;

inline double fround1(double n, unsigned d)
{
&#160;&#160;return floor(n * pow(10., d) + .5) / pow(10., d);
}

inline double fround(double n, unsigned d)
{
&#160;&#160;unsigned p;
&#160;&#160;p = d - (unsigned)log10(n);
&#160;&#160;return floor(n * pow(10., p) + .5) / pow(10., p);
}

int main() {
&#160;&#160;printf(&quot;old fround: %21.8fn&quot;, fround1(1703.12345689,6));
&#160;&#160;printf(&quot;new fround: %21.8fn&quot;, fround (1703.12345689,6));
}&lt;/code&gt;

Cheers.</description>
		<content:encoded><![CDATA[<p>Actually, this is not quite correct assuming you want significant figures instead of decimal places. Your function rounds to a spcified number of places after the decimal point. A definition of significant figures (or digits) can be found at <a href="http://en.wikipedia.org/wiki/Significant_figures" rel="nofollow">http://en.wikipedia.org/wiki/Significant_figures</a>. To retain only a specified number of significant figures, the function should be:</p>
<p><code>inline double fround(double n, unsigned d)<br />
{<br />
&nbsp;&nbsp;unsigned p;<br />
&nbsp;&nbsp;p = d - (unsigned)log10(n);<br />
&nbsp;&nbsp;return floor(n * pow(10., p) + .5) / pow(10., p);<br />
}</code></p>
<p>Given fround(1703.12345689,6), the result will be 1703.12, whereas, your original function will return 1703.123157.</p>
<p>Here is a simple test to verify the results:</p>
<p><code>#include &lt;stdio.h&gt;<br />
#include &lt;math.h&gt;</p>
<p>inline double fround1(double n, unsigned d)<br />
{<br />
&nbsp;&nbsp;return floor(n * pow(10., d) + .5) / pow(10., d);<br />
}</p>
<p>inline double fround(double n, unsigned d)<br />
{<br />
&nbsp;&nbsp;unsigned p;<br />
&nbsp;&nbsp;p = d - (unsigned)log10(n);<br />
&nbsp;&nbsp;return floor(n * pow(10., p) + .5) / pow(10., p);<br />
}</p>
<p>int main() {<br />
&nbsp;&nbsp;printf("old fround: %21.8fn", fround1(1703.12345689,6));<br />
&nbsp;&nbsp;printf("new fround: %21.8fn", fround (1703.12345689,6));<br />
}</code></p>
<p>Cheers.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

