<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>BCL Refresher: Floating Point Types - The Good, The Bad and The Ugly [Inbar Gazit, Matthew Greig]</title><link>http://blogs.msdn.com/bclteam/archive/2007/05/29/bcl-refresher-floating-point-types-the-good-the-bad-and-the-ugly-inbar-gazit-matthew-greig.aspx</link><description>So here is another BCL refresher on the topic of floating point types in the BCL. Believe it or not, we have 3 different floating point types: Single (float) Double and Decimal. Each has their own characteristics and abilities and so let’s try to learn</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: BCL Refresher: Floating Point Types - The Good, The Bad and The Ugly [Inbar Gazit, Matthew Greig]</title><link>http://blogs.msdn.com/bclteam/archive/2007/05/29/bcl-refresher-floating-point-types-the-good-the-bad-and-the-ugly-inbar-gazit-matthew-greig.aspx#2981398</link><pubDate>Wed, 30 May 2007 08:30:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2981398</guid><dc:creator>Wesner Moise</dc:creator><description>&lt;p&gt;Decimals can more precisely represent fractions entered by the user, since the base 10 values entered by user matched the base of the representation. &lt;/p&gt;
&lt;p&gt;Doubles seem to work better than with division operation, as repeating decimals (non-divisible by 10) are represented more closely. For example, with doubles (1.0/3)*3 == 1. With decimals, (1m/3)*3 = .9999{28 times}. The repeating fraction gets chopped off.&lt;/p&gt;
&lt;p&gt;Also, if any of the other complex math operations are used, trig, exp, logs, etc, since they often involve irrational or nonterminating values.&lt;/p&gt;
</description></item><item><title>re: BCL Refresher: Floating Point Types - The Good, The Bad and The Ugly [Inbar Gazit, Matthew Greig]</title><link>http://blogs.msdn.com/bclteam/archive/2007/05/29/bcl-refresher-floating-point-types-the-good-the-bad-and-the-ugly-inbar-gazit-matthew-greig.aspx#2983158</link><pubDate>Wed, 30 May 2007 10:34:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2983158</guid><dc:creator>Chris Nahr</dc:creator><description>&lt;p&gt;Nice trick with the BitConverter and GetBits calls... I'll have to remember that.&lt;/p&gt;
&lt;p&gt;You mixed up the terms &amp;quot;fixed point&amp;quot; and &amp;quot;floating point&amp;quot; in your third paragraph, by the way (the one starting with &amp;quot;So here's a question&amp;quot;). &amp;nbsp;The method used by the BCL is the first described, which is floating-point representation.&lt;/p&gt;
</description></item><item><title>re: BCL Refresher: Floating Point Types - The Good, The Bad and The Ugly [Inbar Gazit, Matthew Greig]</title><link>http://blogs.msdn.com/bclteam/archive/2007/05/29/bcl-refresher-floating-point-types-the-good-the-bad-and-the-ugly-inbar-gazit-matthew-greig.aspx#2988811</link><pubDate>Wed, 30 May 2007 17:12:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2988811</guid><dc:creator>Jeffrey Sax</dc:creator><description>&lt;p&gt;1. In the last example comparing the three types, I think you have the values for Single and Double reversed.&lt;/p&gt;
&lt;p&gt;2. You say the decimal type has 'limited precision.' Near the end you again put decimal nd 'little precision' in the same sentence Decimal is actually the type with the highest precision: 96 bits. &lt;/p&gt;
&lt;p&gt;3. A drawback of the Decimal type is that it is an order of magnitude slower than the other types. Using a consistent rounding policy can eliminate nearly all the problems with inexact representations.&lt;/p&gt;
&lt;p&gt;4. Another reason to use Singles is that it takes less time to load them into the CPU, and they take up less room in the cache. This makes a significant difference only with arrays.&lt;/p&gt;
</description></item><item><title>re: BCL Refresher: Floating Point Types - The Good, The Bad and The Ugly [Inbar Gazit, Matthew Greig]</title><link>http://blogs.msdn.com/bclteam/archive/2007/05/29/bcl-refresher-floating-point-types-the-good-the-bad-and-the-ugly-inbar-gazit-matthew-greig.aspx#2990908</link><pubDate>Wed, 30 May 2007 20:32:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2990908</guid><dc:creator>C. Watford</dc:creator><description>&lt;p&gt;IEEE754 is the relevant standard for the Single and Double data types and does have a (fairly) good set of rules governing rounding. Many older formats had no such nice rules and left a lot to be desired.&lt;/p&gt;
&lt;p&gt;An interesting issue to note which choosing Single, Double, or Decimal is that Single and Double are native and more importantly operations on Single and Double can be atomic. On some architectures the bit-representation of the Decimal structure cannot be modified atomically. Interesting thread safety implications there.&lt;/p&gt;
</description></item><item><title>re: BCL Refresher: Floating Point Types - The Good, The Bad and The Ugly [Inbar Gazit, Matthew Greig]</title><link>http://blogs.msdn.com/bclteam/archive/2007/05/29/bcl-refresher-floating-point-types-the-good-the-bad-and-the-ugly-inbar-gazit-matthew-greig.aspx#2991695</link><pubDate>Wed, 30 May 2007 21:19:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2991695</guid><dc:creator>Matthew Greig</dc:creator><description>&lt;p&gt;Jeff, thanks for your feedback. &amp;nbsp;Here are my comments on it.&lt;/p&gt;
&lt;p&gt;1) Yes, you are right. Should be fixed now.&lt;/p&gt;
&lt;p&gt;2) Decimal does have limited precision. It has a higher precision than Single or Double, but still limited. I will discuss the two mentions of presion seperately.&lt;/p&gt;
&lt;p&gt;&amp;quot;limited precision&amp;quot;:&lt;/p&gt;
&lt;p&gt;It may seem somewhat strange to mention that it has limited precision since it has the highest precision of the three types, but necessary when talking about how it represents numbers exactly if it is in the given range (limited range) and given number of base-10 digits (limited precision). &amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;quot;relatively little precision&amp;quot;:&lt;/p&gt;
&lt;p&gt;Maybe this was a little misleading so I will try to explain better what was meant here. &amp;nbsp;Generally we were talking about that Decimal will suffer from greater problems with underflow than Single or Double. &amp;nbsp;Consider the output of:&lt;/p&gt;
&lt;p&gt;Double d3 = Double.Parse(&amp;quot;1e-30&amp;quot;);&lt;/p&gt;
&lt;p&gt;Double d4 = d3/10;&lt;/p&gt;
&lt;p&gt;Decimal e3 = (Decimal) d3;&lt;/p&gt;
&lt;p&gt;Decimal e4 = (Decimal) d4;&lt;/p&gt;
&lt;p&gt;Console.WriteLine(&amp;quot;Double (1e-30): &amp;quot; + d3);&lt;/p&gt;
&lt;p&gt;Console.WriteLine(&amp;quot;Double (1e-31): &amp;quot; + d4);&lt;/p&gt;
&lt;p&gt;Console.WriteLine(&amp;quot;Decimal (1e-30): &amp;quot; + e3);&lt;/p&gt;
&lt;p&gt;Console.WriteLine(&amp;quot;Decimal (1e-31): &amp;quot; + e4);&lt;/p&gt;
&lt;p&gt;with values in this range Double is more precise than Decimal since it will underflow and be masked to zero.&lt;/p&gt;
&lt;p&gt;3) Agree that Decimal is generally slower than the other types but we are not really trying to focus on the performance of the types here although this may be important in the choice when using them. &amp;nbsp;The consistent rounding policy definitely helps with inexact representations, but not sure I would go as far as to say it “eliminates nearly all the problems”&lt;/p&gt;
&lt;p&gt;4) I agree with your statements. These are typical side effects of using less memory and not anything intrinsic about the way Singles are stored.&lt;/p&gt;
</description></item><item><title>re: BCL Refresher: Floating Point Types - The Good, The Bad and The Ugly [Inbar Gazit, Matthew Greig]</title><link>http://blogs.msdn.com/bclteam/archive/2007/05/29/bcl-refresher-floating-point-types-the-good-the-bad-and-the-ugly-inbar-gazit-matthew-greig.aspx#3014085</link><pubDate>Fri, 01 Jun 2007 01:29:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3014085</guid><dc:creator>Jon Skeet</dc:creator><description>&lt;p&gt;(Fixed/floating still appears to be wrong at the moment in the third paragraph, btw.)&lt;/p&gt;
&lt;p&gt;Oh dear - I'm constantly fighting against things like this:&lt;/p&gt;
&lt;p&gt;&amp;quot;The difference between Decimal and Single/Double is that Decimal is an exact representation of the number whereas Single/Double only gives us the closest estimate.&amp;quot;&lt;/p&gt;
&lt;p&gt;Both of them give closest estimates to a particular real number. It's just that a literal expressed as a decimal within the appropriate range and precision can always be exactly represented as a decimal. Now, your statement is correct within the context of storing 231.312, but when taken out of context (as statements often are) it's incorrect. If &amp;quot;the number&amp;quot; is a third, both of them will only store an approximation. (Both will store an exact number, but it'll be an approximation to the original one.)&lt;/p&gt;
&lt;p&gt;Statements like the one quoted give rise to the myth that decimal is &amp;quot;accurate&amp;quot; and double/single are &amp;quot;approximate&amp;quot; - as if they're very different, with decimal never losing data. Decimal and float/double are more similar than most people realise, basically being an integer and a point - it's mostly the base of point (10 or 2) and the limits on precision/scaling which differ. (Yes, there are some other differences in terms of remembering &amp;quot;extra&amp;quot; digits, denormal values, infinite values etc - but the biggest difference is the base of the point, IMO.)&lt;/p&gt;
&lt;p&gt;Still, at least MS does seem firmly behind the idea that decimal is a floating point type. It was documented in MSDN as being a fixed point type, and I spent *many* messages arguing with a documentation maintainer about whether it was floating or not. The fact that there was a mantissa and an exponent as part of the value didn't seem to be evidence enough ;)&lt;/p&gt;
&lt;p&gt;Jon&lt;/p&gt;
</description></item><item><title>re: BCL Refresher: Floating Point Types - The Good, The Bad and The Ugly [Inbar Gazit, Matthew Greig]</title><link>http://blogs.msdn.com/bclteam/archive/2007/05/29/bcl-refresher-floating-point-types-the-good-the-bad-and-the-ugly-inbar-gazit-matthew-greig.aspx#3019451</link><pubDate>Fri, 01 Jun 2007 09:38:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3019451</guid><dc:creator>Chris Nahr</dc:creator><description>&lt;p&gt;Matthew, you've still got fixed-point and floating-point confused in the third paragraph...&lt;/p&gt;
</description></item><item><title>re: BCL Refresher: Floating Point Types - The Good, The Bad and The Ugly [Inbar Gazit, Matthew Greig]</title><link>http://blogs.msdn.com/bclteam/archive/2007/05/29/bcl-refresher-floating-point-types-the-good-the-bad-and-the-ugly-inbar-gazit-matthew-greig.aspx#3027603</link><pubDate>Fri, 01 Jun 2007 20:05:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3027603</guid><dc:creator>Matthew Greig</dc:creator><description>&lt;p&gt;Thanks to everyone who pointed out the errors in the third paragraph (they should be corrected now). &amp;nbsp;I reworded it somewhat to better describe the Single/Double vs Decimal differences.&lt;/p&gt;
</description></item><item><title>re: BCL Refresher: Floating Point Types - The Good, The Bad and The Ugly [Inbar Gazit, Matthew Greig]</title><link>http://blogs.msdn.com/bclteam/archive/2007/05/29/bcl-refresher-floating-point-types-the-good-the-bad-and-the-ugly-inbar-gazit-matthew-greig.aspx#3048238</link><pubDate>Sat, 02 Jun 2007 20:44:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3048238</guid><dc:creator>Jon Skeet</dc:creator><description>&lt;p&gt;Much better - thanks for being so responsive :)&lt;/p&gt;
</description></item><item><title>BCL Team Blog : BCL Refresher: Floating Point Types - The Good, The Bad and The Ugly [Inbar Gazit, Matthew Greig]</title><link>http://blogs.msdn.com/bclteam/archive/2007/05/29/bcl-refresher-floating-point-types-the-good-the-bad-and-the-ugly-inbar-gazit-matthew-greig.aspx#8565737</link><pubDate>Sat, 31 May 2008 08:58:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8565737</guid><dc:creator>Dating</dc:creator><description>&lt;p&gt;So here is another BCL refresher on the topic of floating point types in the BCL. Believe it or not, we have 3 different floating point types: Single (float) Double and Decimal. Each has their own characteristics and abilities and so let’s try to lear&lt;/p&gt;
</description></item><item><title>BCL Team Blog : BCL Refresher: Floating Point Types - The Good, The Bad and The Ugly [Inbar Gazit, Matthew Greig]</title><link>http://blogs.msdn.com/bclteam/archive/2007/05/29/bcl-refresher-floating-point-types-the-good-the-bad-and-the-ugly-inbar-gazit-matthew-greig.aspx#8574933</link><pubDate>Thu, 05 Jun 2008 13:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8574933</guid><dc:creator>Weddings</dc:creator><description>&lt;p&gt;So here is another BCL refresher on the topic of floating point types in the BCL. Believe it or not, we have 3 different floating point types: Single (float) Double and Decimal. Each has their own characteristics and abilities and so let’s try to lear&lt;/p&gt;
</description></item></channel></rss>