<?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: 2.5 &#8212; Floating point numbers</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/25-floating-point-numbers/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/25-floating-point-numbers/</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 12:30:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Relational operators (comparisons) &#171; Learn C++ Programming</title>
		<link>http://www.learncpp.com/cpp-tutorial/25-floating-point-numbers/comment-page-1/#comment-96838</link>
		<dc:creator>Relational operators (comparisons) &#171; Learn C++ Programming</dc:creator>
		<pubDate>Fri, 13 Jan 2012 09:19:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=46#comment-96838</guid>
		<description>[...] rounding errors in the floating point operands may cause an unexpected result. See the section on floating point numbers for more [...]</description>
		<content:encoded><![CDATA[<p>[...] rounding errors in the floating point operands may cause an unexpected result. See the section on floating point numbers for more [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Find The Average Using an Array &#8211; Omit Highest And Lowest Scores &#124; My Programming Notes</title>
		<link>http://www.learncpp.com/cpp-tutorial/25-floating-point-numbers/comment-page-1/#comment-96805</link>
		<dc:creator>Find The Average Using an Array &#8211; Omit Highest And Lowest Scores &#124; My Programming Notes</dc:creator>
		<pubDate>Tue, 10 Jan 2012 00:35:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=46#comment-96805</guid>
		<description>[...] KNOWLEDGE FOR BOTH PROGRAMS Float Data Type Constant Values Arrays For Loops Assignment Operators Basic [...]</description>
		<content:encoded><![CDATA[<p>[...] KNOWLEDGE FOR BOTH PROGRAMS Float Data Type Constant Values Arrays For Loops Assignment Operators Basic [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: koriroxx</title>
		<link>http://www.learncpp.com/cpp-tutorial/25-floating-point-numbers/comment-page-1/#comment-96120</link>
		<dc:creator>koriroxx</dc:creator>
		<pubDate>Sat, 08 Oct 2011 20:03:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=46#comment-96120</guid>
		<description>Regarding the example where you added two floats and got a &quot;rounding error,&quot; the problem is that you did not include &quot;f&quot; at the end of the number.

#include 
#include 
using namespace std;

int main()
{
    cout &lt;&lt; setprecision(7);
    float fValue1 = 1.345f;
    float fValue2 = 1.123f;
    float fTotal = fValue1 + fValue2; // should be 2.468

    if (fTotal == 2.468f)
    {
        cout &lt;&lt; &quot;fTotal is 2.468&quot; &lt;&lt; endl;
        cout &lt;&lt; fTotal;
    }

    return 0;
}

It will evaluate properly. If you&#039;re checking for floats, you need to check it using a float value (ie, it needs the f on the end.) It is not a rounding error.</description>
		<content:encoded><![CDATA[<p>Regarding the example where you added two floats and got a &#8220;rounding error,&#8221; the problem is that you did not include &#8220;f&#8221; at the end of the number.</p>
<p>#include<br />
#include<br />
using namespace std;</p>
<p>int main()<br />
{<br />
    cout &lt;&lt; setprecision(7);<br />
    float fValue1 = 1.345f;<br />
    float fValue2 = 1.123f;<br />
    float fTotal = fValue1 + fValue2; // should be 2.468</p>
<p>    if (fTotal == 2.468f)<br />
    {<br />
        cout &lt;&lt; &quot;fTotal is 2.468&quot; &lt;&lt; endl;<br />
        cout &lt;&lt; fTotal;<br />
    }</p>
<p>    return 0;<br />
}</p>
<p>It will evaluate properly. If you&#039;re checking for floats, you need to check it using a float value (ie, it needs the f on the end.) It is not a rounding error.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kostas81</title>
		<link>http://www.learncpp.com/cpp-tutorial/25-floating-point-numbers/comment-page-1/#comment-95909</link>
		<dc:creator>Kostas81</dc:creator>
		<pubDate>Sun, 21 Aug 2011 08:50:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=46#comment-95909</guid>
		<description>The question marks are the minus sign. I do not know why this has been false printed in my previous comment.</description>
		<content:encoded><![CDATA[<p>The question marks are the minus sign. I do not know why this has been false printed in my previous comment.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kostas81</title>
		<link>http://www.learncpp.com/cpp-tutorial/25-floating-point-numbers/comment-page-1/#comment-95908</link>
		<dc:creator>Kostas81</dc:creator>
		<pubDate>Sun, 21 Aug 2011 08:48:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=46#comment-95908</guid>
		<description>There is also a good explanation in Wikipedia (yes, sometimes - not often though - Wikipedia has good articles):

&quot;Fractions in binary

Fractions in binary only terminate if the denominator has 2 as the only prime factor. As a result, 1/10 does not have a finite binary representation, and this causes 10 × 0.1 not to be precisely equal to 1 in floating point arithmetic. As an example, to interpret the binary expression for 1/3 = .010101..., this means: 1/3 = 0 × 2^(?1) + 1 × 2^(?2) + 0 × 2^(?3) + 1 × 2^(?4) + ... = 0.3125 + ... An exact value cannot be found with a sum of a finite number of inverse powers of two, and zeros and ones alternate forever.&quot;

Follows a table of the conversion (fractional approximations) for fractions from decimal to binary. For the ones who are interested: 

http://en.wikipedia.org/wiki/Binary_numeral_system</description>
		<content:encoded><![CDATA[<p>There is also a good explanation in Wikipedia (yes, sometimes &#8211; not often though &#8211; Wikipedia has good articles):</p>
<p>&#8220;Fractions in binary</p>
<p>Fractions in binary only terminate if the denominator has 2 as the only prime factor. As a result, 1/10 does not have a finite binary representation, and this causes 10 × 0.1 not to be precisely equal to 1 in floating point arithmetic. As an example, to interpret the binary expression for 1/3 = .010101&#8230;, this means: 1/3 = 0 × 2^(?1) + 1 × 2^(?2) + 0 × 2^(?3) + 1 × 2^(?4) + &#8230; = 0.3125 + &#8230; An exact value cannot be found with a sum of a finite number of inverse powers of two, and zeros and ones alternate forever.&#8221;</p>
<p>Follows a table of the conversion (fractional approximations) for fractions from decimal to binary. For the ones who are interested: </p>
<p><a href="http://en.wikipedia.org/wiki/Binary_numeral_system" rel="nofollow">http://en.wikipedia.org/wiki/Binary_numeral_system</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kostas81</title>
		<link>http://www.learncpp.com/cpp-tutorial/25-floating-point-numbers/comment-page-1/#comment-95907</link>
		<dc:creator>Kostas81</dc:creator>
		<pubDate>Sun, 21 Aug 2011 08:40:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=46#comment-95907</guid>
		<description>Yes, you are right, this sum indeed converges to 0.1, whereas the  sum I used converges to 1.0. Thank you for the clear and concise explanation!</description>
		<content:encoded><![CDATA[<p>Yes, you are right, this sum indeed converges to 0.1, whereas the  sum I used converges to 1.0. Thank you for the clear and concise explanation!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: johnbobz</title>
		<link>http://www.learncpp.com/cpp-tutorial/25-floating-point-numbers/comment-page-1/#comment-95841</link>
		<dc:creator>johnbobz</dc:creator>
		<pubDate>Tue, 09 Aug 2011 08:51:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=46#comment-95841</guid>
		<description>That&#039;s exactly why you can&#039;t use every power of 1/2. The infinite sum would add up to 1, which is ten times the number we require. In order for the sum to add up to 0.1, you would need to add Sum[(1/2)^4n + (1/2)^(4n+1)], taking n from 1 to infinity. You can try it yourself if you want.</description>
		<content:encoded><![CDATA[<p>That&#8217;s exactly why you can&#8217;t use every power of 1/2. The infinite sum would add up to 1, which is ten times the number we require. In order for the sum to add up to 0.1, you would need to add Sum[(1/2)^4n + (1/2)^(4n+1)], taking n from 1 to infinity. You can try it yourself if you want.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kostas81</title>
		<link>http://www.learncpp.com/cpp-tutorial/25-floating-point-numbers/comment-page-1/#comment-95835</link>
		<dc:creator>Kostas81</dc:creator>
		<pubDate>Sun, 07 Aug 2011 20:30:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=46#comment-95835</guid>
		<description>Ok, so in binary, we have to approximate the 1 of decimal 0.1 with an infinite sum. What I still don&#039;t understand is why we don&#039;t use all the &quot;weights&quot;, that is, all the powers of 2, but only 1/16, 1/32, 1/256, 1/512 and so on, that is, the 4th position (2^4 = 16), the 5th, the 8th, the 9th, and so on. In other words, why don&#039;t we have 0.011111111........ which is equal to 1/2 + 1/4 + 1/8 + 1/16 + ...? It also approaches 1! (I am referring of course to the decimal part of 0.1, that is, the 1.)</description>
		<content:encoded><![CDATA[<p>Ok, so in binary, we have to approximate the 1 of decimal 0.1 with an infinite sum. What I still don&#8217;t understand is why we don&#8217;t use all the &#8220;weights&#8221;, that is, all the powers of 2, but only 1/16, 1/32, 1/256, 1/512 and so on, that is, the 4th position (2^4 = 16), the 5th, the 8th, the 9th, and so on. In other words, why don&#8217;t we have 0.011111111&#8230;&#8230;.. which is equal to 1/2 + 1/4 + 1/8 + 1/16 + &#8230;? It also approaches 1! (I am referring of course to the decimal part of 0.1, that is, the 1.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lharriger</title>
		<link>http://www.learncpp.com/cpp-tutorial/25-floating-point-numbers/comment-page-1/#comment-95441</link>
		<dc:creator>lharriger</dc:creator>
		<pubDate>Thu, 12 May 2011 19:07:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=46#comment-95441</guid>
		<description>Think it through as follows:

float x = 1 reads &quot;put INT 1 into FLOAT x.&quot; This changes its type from int to float. The same is true for float y = 3.
Thus float z = x/y divides two floats and returns a float.

However for float q = (1/3), this is a two part statement. 
The first part (1/3) reads &quot;divide INT 1 by INT 3&quot;. Since this is division of two integers, this means it must return an integer (the floor), which in this case is 0.
The second part is then q = 0, which reads &quot;put INT 0 into float q.&quot;

An important thing to keep in mind is that division on a float is different than division on an integer. The literal 1 is read as an integer, however, the literal 1.0 is read as a float/double. This is why q = (1.0/3.0) is different than q = (1/3).

Hope this helped.</description>
		<content:encoded><![CDATA[<p>Think it through as follows:</p>
<p>float x = 1 reads &#8220;put INT 1 into FLOAT x.&#8221; This changes its type from int to float. The same is true for float y = 3.<br />
Thus float z = x/y divides two floats and returns a float.</p>
<p>However for float q = (1/3), this is a two part statement.<br />
The first part (1/3) reads &#8220;divide INT 1 by INT 3&#8243;. Since this is division of two integers, this means it must return an integer (the floor), which in this case is 0.<br />
The second part is then q = 0, which reads &#8220;put INT 0 into float q.&#8221;</p>
<p>An important thing to keep in mind is that division on a float is different than division on an integer. The literal 1 is read as an integer, however, the literal 1.0 is read as a float/double. This is why q = (1.0/3.0) is different than q = (1/3).</p>
<p>Hope this helped.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jimboshack</title>
		<link>http://www.learncpp.com/cpp-tutorial/25-floating-point-numbers/comment-page-1/#comment-95198</link>
		<dc:creator>jimboshack</dc:creator>
		<pubDate>Mon, 21 Feb 2011 15:03:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/?p=46#comment-95198</guid>
		<description>When I run this code, I get z = 0.333333 and q = 0

    float x = 1;
    float y = 3;
    float z = (x/y);
    float q = (1/3);


Can someone explain why? I realize that if I write

    float q = (1.0/3.0);

that this problem doesn&#039;t occur, but I&#039;m just wondering why I can&#039;t use (1/3) since q is defined as a float. This page says it&#039;s just a convention to have the decimal point.</description>
		<content:encoded><![CDATA[<p>When I run this code, I get z = 0.333333 and q = 0</p>
<p>    float x = 1;<br />
    float y = 3;<br />
    float z = (x/y);<br />
    float q = (1/3);</p>
<p>Can someone explain why? I realize that if I write</p>
<p>    float q = (1.0/3.0);</p>
<p>that this problem doesn&#8217;t occur, but I&#8217;m just wondering why I can&#8217;t use (1/3) since q is defined as a float. This page says it&#8217;s just a convention to have the decimal point.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

