<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Daily Musings ...</title>
	<atom:link href="http://ofwlife.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://ofwlife.wordpress.com</link>
	<description>- contemplation of things past; &#34;in retrospect&#34;</description>
	<lastBuildDate>Sun, 05 Jul 2009 07:34:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='ofwlife.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Daily Musings ...</title>
		<link>http://ofwlife.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://ofwlife.wordpress.com/osd.xml" title="Daily Musings ..." />
	<atom:link rel='hub' href='http://ofwlife.wordpress.com/?pushpress=hub'/>
		<item>
		<title>C++ Decimal Wrapper Class</title>
		<link>http://ofwlife.wordpress.com/2009/07/05/c-decimal-wrapper-class/</link>
		<comments>http://ofwlife.wordpress.com/2009/07/05/c-decimal-wrapper-class/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 07:34:35 +0000</pubDate>
		<dc:creator>vpcola</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://ofwlife.wordpress.com/?p=5</guid>
		<description><![CDATA[I&#8217;ve been searching round the internet for a C# compatible C++ class for handling Decimal data types to no avail.  I discovered a neat and fast IEEE 754 implementation in C from Intel, but alas &#8230; no C++ implementation. IEEE 754 (Decimal Floating Point) has been suggested to be integrated in the upcoming C++0x standard, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ofwlife.wordpress.com&amp;blog=8280028&amp;post=5&amp;subd=ofwlife&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been searching round the internet for a C# compatible C++ class for handling Decimal data types to no avail.  I discovered a neat and fast IEEE 754 implementation in C from Intel, but alas &#8230; no C++ implementation. IEEE 754 (Decimal Floating Point) has been suggested to be integrated in the upcoming C++0x standard, but untill compilers will support it, we&#8217;d be stuck in C++ with float and double values <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> .</p>
<p>I have looked at other implementations including decNumber C++ wrapper and Marc Clifton&#8217;s article on his Decimal class, but they don&#8217;t suit my needs. I need to handle conversion from C# decimal to C++, values of C# decimal are serialized and sent via TCP/IP and deserialized with the C++ application on the server side. The class needs to convert from a C# Decimal to a C++ Decimal type.</p>
<p>To those who are unfamiliar with IEEE 754 or Decimal floating point, please note that C and C++ float and double have trouble storing a floating point numbers &#8211; it stores them in base-2, and computers have trouble representing them in binary format. Take for example the following :</p>
<p><code><br />
	double n = 0.3;<br />
	n -= 0.099;<br />
	n *= 1000;</p>
<p>	if (n == 201) printf("true\n");<br />
	else printf("false\n");<br />
</code></p>
<p>As per the example above, you may have guessed that the final value of n is 201, but it&#8217;s not. In reality, double and float has trouble storing the value of 1/3 &#8211;  which .333333&#8230; and repeats to infinity. Although doing operations in binary as opposed to BCD/Decimal is much faster,  it sacrifices accuracy. This has a huge impact on Financial and high-precision applications where accuracy of a price or value is paramount.</p>
<p>C# provides a Decimal data type which has the accuracy of storing numbers with a precision of 28-29 significant digits. It does this by storing allocating a 128 bit Decimal value, storing a significand in a 96-bit unsigned int, a byte for the exponent (-127 + 128), an a flag byte for the sign (most significant bit), the rest of the bits are unused. So if you are to store 12345.6789m in C#&#8217;s decimal, its significand (0-95th bit) will contain the value of 123456789, the exponent byte will contain -4 (this determines the posiiton of the decimal point), and the flag bit which is zero. </p>
<p>C#&#8217;s internal representation isn&#8217;t compatible with IEEE 754&#8242;s implementation, in fact the IEEE 754 implementation is much more precise than C#&#8217;s. In Intel&#8217;s BID implementation, the significand is stored in a 113 bit number, the exponent is 17 bits wide, and a sign flag bit (128 bit total).  This is where I needed a class to convert from/to C#&#8217;s decimal number. </p>
<p>Before one can compile the code below, my implementation relies on Intel&#8217;s Decimal Floating Point Math library (http://software.intel.com/en-us/articles/intel-decimal-floating-point-math-library/). You can link the C++ implementation of the class to the Intel&#8217;s built libraries (using pass by value). I&#8217;ll explain more on this later.</p>
<p>The class is generally a C++ wrapper class to most of Intel&#8217;s DFP functions.<br />
<code><br />
#ifndef __DECIMAL_H<br />
#define __DECIMAL_H</p>
<p>#ifdef WIN32<br />
  #define LX "%I64x"<br />
#else<br />
  #ifdef HPUX_OS<br />
    #define LX "%llx"<br />
  #else<br />
    #define LX "%Lx"<br />
  #endif<br />
#endif</p>
<p>#if BID_BIG_ENDIAN<br />
#define HIGH_128W 0<br />
#define LOW_128W  1<br />
#else<br />
#define HIGH_128W 1<br />
#define LOW_128W  0<br />
#endif</p>
<p>/* basic decimal floating-point types */</p>
<p>#if defined _MSC_VER<br />
#if defined _M_IX86 &amp;&amp; !defined __INTEL_COMPILER // Win IA-32, MS compiler<br />
#define ALIGN(n)<br />
#else<br />
#define ALIGN(n) __declspec(align(n))<br />
#endif<br />
#else<br />
#define ALIGN(n) __attribute__ ((aligned(n)))<br />
#endif</p>
<p>extern "C"<br />
{<br />
#include "IntelDFP/bid_conf.h"<br />
#include "IntelDFP/bid_functions.h"<br />
#include "IntelDFP/bid_internal.h"<br />
}</p>
<p>#include<br />
#include </p>
<p>typedef unsigned int Decimal32;<br />
typedef unsigned long long Decimal64;<br />
typedef ALIGN(16) struct { unsigned long long w[2]; } Decimal128;</p>
<p>/* rounding modes */<br />
/* Values for _IDEC_round */<br />
typedef enum _IDEC_roundingmode {<br />
    _IDEC_nearesteven = 0,<br />
    _IDEC_downward    = 1,<br />
    _IDEC_upward      = 2,<br />
    _IDEC_towardzero  = 3,<br />
    _IDEC_nearestaway = 4,<br />
    _IDEC_dflround    = _IDEC_nearesteven<br />
} _IDEC_roundingmode;</p>
<p>/* exception flags */<br />
typedef enum _IDEC_flagbits {<br />
    _IDEC_invalid       = 0x01,<br />
    _IDEC_zerodivide    = 0x04,<br />
    _IDEC_overflow      = 0x08,<br />
    _IDEC_underflow     = 0x10,<br />
    _IDEC_inexact       = 0x20,<br />
    _IDEC_allflagsclear = 0x00<br />
} _IDEC_flagbits;</p>
<p>// Just wrap the data bytes in a CS decimal<br />
// The logic of this class can be combined in Decimal<br />
// if you want to. However, that would mean sizeof(Decimal)<br />
// to exceed 16 bytes (128 bit which is the size of C#'s decimal), as<br />
// Decimal class below has some other private member values.<br />
// So I end up declaring this wrapper class/type instead.<br />
struct CSDecimal{<br />
	CSDecimal();</p>
<p>	unsigned char value[16];</p>
<p>	unsigned int hi();<br />
	unsigned int mid();<br />
	unsigned int lo();<br />
	unsigned int flags();</p>
<p>	// Set the value of the 96 bit significand<br />
	void setcoeff(unsigned int l, unsigned int m, unsigned int h);</p>
<p>	int sign();<br />
	// Set the sign bit (value[0]'s most significnat bit)<br />
	void setsign(int sign);<br />
	unsigned int scale();<br />
	// Set the scale<br />
	void setscale(unsigned int scale);<br />
};</p>
<p>// The actual Decimal class. A wrapper to the BID functions<br />
// for IEEE 754 implementation by Intel. This calls library<br />
// functions. Under gcc, this calls the built-in libs.<br />
// The underlying type is a BID_UINT128<br />
class Decimal{<br />
	public:<br />
		Decimal();<br />
		virtual ~Decimal(void);</p>
<p>		// From a string<br />
		Decimal(std::string str);<br />
		// From floating types<br />
		Decimal(const Decimal&amp; d);<br />
		Decimal(const float v); // Construct from float<br />
		Decimal(const double v); // Construct from double<br />
		// From integers<br />
		Decimal(const unsigned int v); // Int32<br />
		Decimal(const unsigned long long v); // Uint64<br />
		Decimal(const int v);<br />
		Decimal(const long long v);<br />
		// From a CSharp Decimal<br />
		Decimal(CSDecimal &amp;); // Construct form CSharp decimal bytes</p>
<p>		// Assignments<br />
		template<br />
		Decimal &amp; operator=(const T &amp;val)<br />
		{<br />
			Decimal tmpVal(val);<br />
			n = tmpVal.n;<br />
			return *this;<br />
		}<br />
		// Converters<br />
		std::string ToString(void);<br />
		float ToFloat(void) const;<br />
		double ToDouble(void) const;<br />
		int ToInt32(bool isFloor=false) const;<br />
		unsigned int ToUInt32(bool isFloor=false) const;<br />
		long long ToInt64(bool isFloor=false) const;<br />
		unsigned long long ToUInt64(bool isFloor=false) const;<br />
		// To CSDecimal<br />
		CSDecimal ToBits(void);</p>
<p>		Decimal operator+(const Decimal&amp;);<br />
		Decimal operator-(const Decimal&amp;);<br />
		Decimal operator*(const Decimal&amp;);<br />
		Decimal operator/(const Decimal&amp;);</p>
<p>		Decimal &amp; operator+=(const Decimal &amp;);<br />
		Decimal &amp; operator-=(const Decimal &amp;);<br />
		Decimal &amp; operator*=(const Decimal &amp;);<br />
		Decimal &amp; operator/=(const Decimal &amp;);</p>
<p>		Decimal &amp; operator++(); // prefix<br />
		Decimal operator++(int); // postfix<br />
		Decimal &amp; operator--(); // prefix<br />
		Decimal operator--(int); //postfix</p>
<p>#if 1<br />
		// TODO:<br />
		Decimal operator%(const Decimal &amp;);<br />
#endif</p>
<p>		bool operator==(const Decimal&amp;) const;<br />
		bool operator!=(const Decimal&amp;) const;<br />
		bool operator&lt;(const Decimal&amp;) const;<br />
		bool operator(const Decimal&amp;) const;<br />
		bool operator&gt;=(const Decimal&amp;) const;</p>
<p>		void UnPack(Decimal32 &amp;lo, Decimal32 &amp;mid, Decimal32 &amp;hi, int &amp;exp, int &amp;sign);<br />
		void Pack(Decimal32 lo, Decimal32 mid, Decimal32 hi, int exp, int sign);</p>
<p>		friend std::ostream &amp; operator&lt;&lt;(std::ostream &amp;os, Decimal &amp;d);</p>
<p>	protected:<br />
		/* Internal representation of our Decimal class */<br />
		//_Quad n;<br />
		BID_UINT128 n;<br />
		/* Internal representation is a 128 bit floating point<br />
		 * number. Using IEEE 754 standard for floating point<br />
		 * arithmetic. This also utilizes the co-processor for<br />
		 * doing computations of this data type. This is much<br />
		 * more precise than C#&#39;s 96 bit  mantissa.<br />
		 */<br />
};<br />
#endif<br />
</code></p>
<p>To use the class, consider the following code snippit below:</p>
<p><code><br />
Decimal myDecimal("123.456");<br />
myDecimal += 4.5; // Add a float value to the decimal<br />
cout &lt;&lt; myDecimal &lt;&lt; endl;<br />
</code></p>
<p>I&#8217;ll discuss more on the usage specially on converting from C# to c++ on my next post.</p>
<p>More to this later &#8230;.</p>
<p>Links to the code:</p>
<p>http://ofwlife.files.wordpress.com/2009/07/decimal-cpp.doc<br />
http://ofwlife.files.wordpress.com/2009/07/decimal-h.doc</p>
<p>Reove the .doc extensions.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ofwlife.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ofwlife.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ofwlife.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ofwlife.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ofwlife.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ofwlife.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ofwlife.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ofwlife.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ofwlife.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ofwlife.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ofwlife.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ofwlife.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ofwlife.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ofwlife.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ofwlife.wordpress.com&amp;blog=8280028&amp;post=5&amp;subd=ofwlife&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ofwlife.wordpress.com/2009/07/05/c-decimal-wrapper-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3d8e6e9babe2302c2b7698797a95f34e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vpcola</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello world!</title>
		<link>http://ofwlife.wordpress.com/2009/06/22/hello-world/</link>
		<comments>http://ofwlife.wordpress.com/2009/06/22/hello-world/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 15:28:30 +0000</pubDate>
		<dc:creator>vpcola</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Ahh &#8230; I&#8217;m blogging again . It has been a year and 5 months to the exact date since the last posted a blog on my old blogsite. So what&#8217;s causing the long hiatus? &#8230; I got busy, busy with work and something else entirely. Now my fingers are just aching to write something, but [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ofwlife.wordpress.com&amp;blog=8280028&amp;post=1&amp;subd=ofwlife&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ahh &#8230; I&#8217;m blogging again <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . It has been a year and 5 months to the exact date since the last posted a blog on my old blogsite. So what&#8217;s causing the long hiatus? &#8230; I got busy, busy with work and something else entirely. Now my fingers are just aching to write something, but honestly I could not think of anything interesting at the moment. I&#8217;m just happy I started this thing up again.</p>
<p>I&#8217;ve been looking to buy a property back home lately, but I couldn&#8217;t find one that really interests me. The last house and lot I was looking, was already sold before I could notify the agent. I&#8217;ve been looking for a property back in Cebu, somewhere not too far away from the City. I was contenplating on just buying a lot (around 300-400 sq. m.) and build a house, or buying a prebuilt house and lot instead. My budget is tight at the moment, but if you got a good deal, ping me. I&#8217;d be interested.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ofwlife.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ofwlife.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ofwlife.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ofwlife.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ofwlife.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ofwlife.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ofwlife.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ofwlife.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ofwlife.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ofwlife.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ofwlife.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ofwlife.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ofwlife.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ofwlife.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ofwlife.wordpress.com&amp;blog=8280028&amp;post=1&amp;subd=ofwlife&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ofwlife.wordpress.com/2009/06/22/hello-world/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3d8e6e9babe2302c2b7698797a95f34e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vpcola</media:title>
		</media:content>
	</item>
	</channel>
</rss>
