<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>blog of ducklink.com &#187; C++</title>
	<atom:link href="http://libg.org/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://libg.org</link>
	<description></description>
	<lastBuildDate>Tue, 18 Jan 2011 04:54:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Comparison of Float and Double Precision</title>
		<link>http://libg.org/2008/09/07/comparison-of-float-and-double-precision/</link>
		<comments>http://libg.org/2008/09/07/comparison-of-float-and-double-precision/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 08:50:33 +0000</pubDate>
		<dc:creator>Mason</dc:creator>
				<category><![CDATA[Numeral]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[floating point]]></category>

		<guid isPermaLink="false">http://libg.org/2008/09/07/comparison-of-float-and-double-precision/</guid>
		<description><![CDATA[C++ supports two primitive floating point types: float and double. These are based on the IEEE 754 standard, which defines a binary standard for 32-bit floating point and 64-bit double precision floating point binary-decimal numbers. IEEE 754 represents floating point numbers as base 2 decimal numbers in scientific notation. An IEEE floating point number dedicates [...]]]></description>
			<content:encoded><![CDATA[<p>C++ supports two primitive floating point types: <code>float</code> and <code>double</code>. These are based on the IEEE 754 standard, which defines a binary standard for 32-bit floating point and 64-bit double precision floating point binary-decimal numbers. IEEE 754 represents floating point numbers as base 2 decimal numbers in scientific notation. An IEEE floating point number dedicates 1 bit to the sign of the number, 8 bits to the exponent, and 23 bits to the mantissa, or fractional part. The exponent is interpreted as a signed integer, allowing negative as well as positive exponents. The fraction is represented as a binary (base 2) decimal, meaning the highest-order bit corresponds to a value of ½ (2<sup>-1</sup>), the second bit ¼ (2<sup>-2</sup>), and so on. For double-precision floating point, 11 bits are dedicated to the exponent and 52 bits to the mantissa. The layout of IEEE floating point values is shown in Figure 1.</p>
<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" src="http://libg.org/wp-content/uploads/2008/09/float1.gif" border="0" alt="float" width="579" height="177" /></p>
<p>Because any given number can be represented in scientific notation in multiple ways, floating point numbers are normalized so that they are represented as a base 2 decimal with a 1 to the left of the decimal point, adjusting the exponent as necessary to make this requirement hold. So, for example, the number 1.25 would be represented with a mantissa of 1.01 and an exponent of 0:<br />
<code>(-1) </code></p>
<p>The number 10.0 would be represented with a mantissa of 1.01 and an exponent of 3:<br />
<code>(-1)</code></p>
<table class="main" border="0" width="2">
<tbody>
<tr>
<td class="content" valign="top">
<div class="content2">
<p>here are some sample floating point representations:</p>
<pre>0      0x00000000
1.0    0x3f800000
0.5    0x3f000000
3      0x40400000
+inf   0x7f800000
-inf   0xff800000
+NaN   0x7fc00000 or 0x7ff00000
in general: number = (sign ? -1:1) * 2^(exponent) * 1.(mantissa bits)</pre>
<p>As a programmer, it is important to know certain characteristics of your FP representation. These are listed below, with example values for both single- and double-precision IEEE floating point numbers:</p>
<table border="1" cellpadding="2">
<tbody>
<tr>
<td><strong>Property </strong></td>
<td><strong>Value for float </strong></td>
<td><strong>Value for double </strong></td>
</tr>
<tr>
<td>Largest representable number</td>
<td>3.402823466e+38</td>
<td>1.7976931348623157e+308</td>
</tr>
<tr>
<td>Smallest number without losing precision</td>
<td>1.175494351e-38</td>
<td>2.2250738585072014e-308</td>
</tr>
<tr>
<td>Smallest representable number(*)</td>
<td>1.401298464e-45</td>
<td>5e-324</td>
</tr>
<tr>
<td>Mantissa bits</td>
<td>23</td>
<td>52</td>
</tr>
<tr>
<td>Exponent bits</td>
<td>8</td>
<td>11</td>
</tr>
<tr>
<td>Epsilon(**)</td>
<td>1.1929093e-7</td>
<td>2.220446049250313e-16</td>
</tr>
</tbody>
</table>
<p>Note that all numbers in the text of this article assume single-precision floats; doubles are included above for comparison and reference purposes.</p>
<p>(*)</p>
<p>Just to make life interesting, here we have yet another special case. It turns out that if you set the exponent bits to zero, you can represent numbers other than zero by setting mantissa bits. As long as we have an implied leading 1, the smallest number we can get is clearly 2^-126, so to get these lower values we make an exception. The &#8220;1.m&#8221; interpretation disappears, and the number&#8217;s magnitude is determined only by bit positions; if you shift the mantissa to the right, the apparent exponent will change (try it!). It may help clarify matters to point out that 1.401298464e-45 = 2^(-126-23), in other words the smallest exponent minus the number of mantissa bits.</p>
<p>However, as I have implied in the above table, when using these extra-small numbers you sacrifice precision. When there is no implied 1, all bits to the left of the lowest set bit are leading zeros, which add no information to a number (as you know, you can write zeros to the left of any number all day long if you want). Therefore the absolute smallest representable number (1.401298464e-45, with only the lowest bit of the FP word set) has an appalling mere single bit of precision!</p>
<p>(**)</p>
<p>Epsilon is the smallest x such that 1+x &gt; 1. It is the place value of the least significant bit when the exponent is zero (i.e., stored as 0x7f).</p></div>
</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://libg.org/2008/09/07/comparison-of-float-and-double-precision/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Include Guard: #pragma once vs. #ifndef #define #endif</title>
		<link>http://libg.org/2008/09/06/include-guard-pragma-once-vs-ifdef-define-endif/</link>
		<comments>http://libg.org/2008/09/06/include-guard-pragma-once-vs-ifdef-define-endif/#comments</comments>
		<pubDate>Sat, 06 Sep 2008 04:08:38 +0000</pubDate>
		<dc:creator>Mason</dc:creator>
				<category><![CDATA[General C++]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[compile]]></category>

		<guid isPermaLink="false">http://libg.org/2008/09/06/include-guard-pragma-once-vs-ifdef-define-endif/</guid>
		<description><![CDATA[In the C and C++ programming languages, an include guard, sometimes called a macro guard, is a particular construct used to avoid the problem of double inclusion when dealing with the #include directive. The addition of include guards to a header file is one way to achieve this. pragma once is a non-standard but widely [...]]]></description>
			<content:encoded><![CDATA[<p>In the C and C++ programming languages, an <strong>include guard</strong>, sometimes called a macro guard, is a particular construct used to avoid the problem of double inclusion when dealing with the #include directive. The addition of include guards to a header file is one way to achieve this. <strong>pragma once </strong>is a non-standard but widely supported preprocessor directive designed to cause the current source file to be included only once in a single compilation. Both approaches specify that the file will be included only once by the compiler when compiling a source code file.</p>
<p>The table below compared the details of <strong>pragma once</strong> and <strong>#ifndef #define #endif </strong>approach:</p>
<table border="1" cellspacing="0" cellpadding="2" width="598">
<tbody>
<tr>
<td width="157" valign="top"></td>
<td width="202" valign="top"><strong>pragma once</strong></td>
<td width="236" valign="top"><strong>#ifndef #define #endif</strong></td>
</tr>
<tr>
<td width="157" valign="top"><strong>Sample code</strong></td>
<td width="202" valign="top">
<pre name="code" class="cpp:nocontrols">
// header file
#pragma once
class foo { };
</pre>
</td>
<td width="236" valign="top">
<pre name="code" class="cpp:nocontrols">
// header file
#ifndef FOO_HEADER
#define FOO_HEADER
class foo { };
#endif // FOO_HEADER
</pre>
</td>
</tr>
<tr>
<td width="157" valign="top"><strong>C++ Standard</strong></td>
<td width="202" valign="top"><strong>No</strong></p>
<p>But both GCC and Microsoft Visual C++ support it.</td>
<td width="236" valign="top"><strong>Yes</strong></td>
</tr>
<tr>
<td width="157" valign="top"><strong>Compiling Performance</strong></td>
<td width="202" valign="top"><strong>Better</strong></p>
<p><strong></strong>This can reduce build times as the compiler will not open and read the file after the first #include of the module.</td>
<td width="236" valign="top">It will still have to open the file multiple times, and discard the guard part when compiler find the macro guard. In a large project this could cause increased compile times.</p>
<p>But you can also optimize the compiling performance of #ifdef #define #endif approach by <a href="http://www.bobarcher.org/software/include/index.html" target="_blank">this way</a>.</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://libg.org/2008/09/06/include-guard-pragma-once-vs-ifdef-define-endif/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

