A very serious bug in MS Visual C++  <p style="margin:0cm 0cm 0pt"><span>Try the next code:</span></p> <p style="margin:0cm 0cm 0pt"> </p> <p style="margin:0cm 0cm 0pt"><span>            double a=111.567,b=111,c;</span></p> <p style="margin:0cm 0cm 0pt"><span>            c=a-b;</span></p> <p style="margin:0cm 0cm 0pt"> </p> <p style="margin:0cm 0cm 0pt"><span>            // and you will receive</span></p> <p style="margin:0cm 0cm 0pt"><span>            //</span></p> <p style="margin:0cm 0cm 0pt"><span>            //a=111.56699999999999</span></p> <p style="margin:0cm 0cm 0pt"><span>            //b=111.00000000000000</span></p> <p style="margin:0cm 0cm 0pt"><span>            //c=0.56699999999999307</span></p> <p style="margin:0cm 0cm 0pt"><span>            //</span></p> <p style="margin:0cm 0cm 0pt"><span>            //instead =&gt; a=111.567, b=111, c=0.567;</span></p> <p style="margin:0cm 0cm 0pt"> </p> <p style="margin:0cm 0cm 0pt">I found more fractional numbers that show a similar error. </p> <p style="margin:0cm 0cm 0pt"> </p> <p style="margin:0cm 0cm 0pt">The problem is that the fractional numbers and their actions can not be produced otherwise.</p> <p style="margin:0cm 0cm 0pt"> </p> <p style="margin:0cm 0cm 0pt">I try this example in all MS Visual C/C++ compilers from version 6.0 to version 2008 and the bug appears everywhere. </p> <p style="margin:0cm 0cm 0pt"> </p> <p style="margin:0cm 0cm 10pt"><span style="font-size:9pt;font-family:Verdana">Regards,</span></p> <p style="margin:0cm 0cm 10pt"><span style="font-size:9pt;font-family:Verdana">Hristo Markov</span></p> <p style="margin:0cm 0cm 0pt"> </p> <p style="margin:0cm 0cm 0pt"><span style="font-size:9pt;font-family:Verdana">--- <br>Software development - <a href="http://www.markovandmarkov.com/">http://www.markovandmarkov.com/</a> | <a href="http://www.cargofreightexchange.com/">http://www.cargofreightexchange.com/</a> <br>---</span> </p> <p style="margin:0cm 0cm 0pt"> </p>© 2009 Microsoft Corporation. All rights reserved.Fri, 26 Sep 2008 03:21:05 Z6b48c8ad-6920-41ba-ba9b-ae2054a35bcfhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#6b48c8ad-6920-41ba-ba9b-ae2054a35bcfhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#6b48c8ad-6920-41ba-ba9b-ae2054a35bcfHristo Markovhttp://social.msdn.microsoft.com/Profile/en-US/?user=Hristo%20MarkovA very serious bug in MS Visual C++  <p style="margin:0cm 0cm 0pt"><span>Try the next code:</span></p> <p style="margin:0cm 0cm 0pt"> </p> <p style="margin:0cm 0cm 0pt"><span>            double a=111.567,b=111,c;</span></p> <p style="margin:0cm 0cm 0pt"><span>            c=a-b;</span></p> <p style="margin:0cm 0cm 0pt"> </p> <p style="margin:0cm 0cm 0pt"><span>            // and you will receive</span></p> <p style="margin:0cm 0cm 0pt"><span>            //</span></p> <p style="margin:0cm 0cm 0pt"><span>            //a=111.56699999999999</span></p> <p style="margin:0cm 0cm 0pt"><span>            //b=111.00000000000000</span></p> <p style="margin:0cm 0cm 0pt"><span>            //c=0.56699999999999307</span></p> <p style="margin:0cm 0cm 0pt"><span>            //</span></p> <p style="margin:0cm 0cm 0pt"><span>            //instead =&gt; a=111.567, b=111, c=0.567;</span></p> <p style="margin:0cm 0cm 0pt"> </p> <p style="margin:0cm 0cm 0pt">I found more fractional numbers that show a similar error. </p> <p style="margin:0cm 0cm 0pt"> </p> <p style="margin:0cm 0cm 0pt">The problem is that the fractional numbers and their actions can not be produced otherwise.</p> <p style="margin:0cm 0cm 0pt"> </p> <p style="margin:0cm 0cm 0pt">I try this example in all MS Visual C/C++ compilers from version 6.0 to version 2008 and the bug appears everywhere. </p> <p style="margin:0cm 0cm 0pt"> </p> <p style="margin:0cm 0cm 10pt"><span style="font-size:9pt;font-family:Verdana">Regards,</span></p> <p style="margin:0cm 0cm 10pt"><span style="font-size:9pt;font-family:Verdana">Hristo Markov</span></p> <p style="margin:0cm 0cm 0pt"> </p> <p style="margin:0cm 0cm 0pt"><span style="font-size:9pt;font-family:Verdana">--- <br>Software development - <a href="http://www.markovandmarkov.com/">http://www.markovandmarkov.com/</a> | <a href="http://www.cargofreightexchange.com/">http://www.cargofreightexchange.com/</a> <br>---</span> </p> <p style="margin:0cm 0cm 0pt"> </p>Sat, 02 Aug 2008 15:58:10 Z2008-08-02T15:58:10Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#d9c44ada-5f39-41c0-8ae2-f0331f162c75http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#d9c44ada-5f39-41c0-8ae2-f0331f162c75Mark Salsberyhttp://social.msdn.microsoft.com/Profile/en-US/?user=Mark%20SalsberyA very serious bug in MS Visual C++<p>Wow - you'd think they would fix that bug by now :-)<br><br>But seriously, floating point numbers are not capable of representing every possible real number.</p> <p>This topic comes up often.  Here's some basic rules from <a href="http://support.microsoft.com/kb/125056">INFO: Precision and Accuracy in Floating-Point Calculations</a></p> <p><em>here are many situations in which precision, rounding, and accuracy in floating-point calculations can work to generate results that are surprising to the programmer. There are four general rules that should be followed:<font style="font-size:11px"> </font></em> <table class="list ol"> <tbody> <tr> <td class=number><em><font style="font-size:11px">1.</font></em></td> <td class=text><em><font style="font-size:11px">In a calculation involving both single and double precision, the result will not usually be any more accurate than single precision. If double precision is required, be certain all terms in the calculation, including constants, are specified in double precision. </font></em></td></tr> <tr> <td class=number><em><font style="font-size:11px">2.</font></em></td> <td class=text><em><font style="font-size:11px">Never assume that a simple numeric value is accurately represented in the computer. Most floating-point values can't be precisely represented as a finite binary value. For example .1 is .0001100110011... in binary (it repeats forever), so it can't be represented with complete accuracy on a computer using binary arithmetic, which includes all PCs. </font></em></td></tr> <tr> <td class=number><em><font style="font-size:11px">3.</font></em></td> <td class=text><em><font style="font-size:11px">Never assume that the result is accurate to the last decimal place. There are always small differences between the &quot;true&quot; answer and what can be calculated with the finite precision of any floating point processing unit. </font></em></td></tr> <tr> <td class=number><em><font style="font-size:11px">4.</font></em></td> <td class=text><em><font style="font-size:11px">Never compare two floating-point values to see if they are equal or not- equal. This is a corollary to rule 3. There are almost always going to be small differences between numbers that &quot;should&quot; be equal. Instead, always check to see if the numbers are nearly equal. In other words, check to see if the difference between them is very small or insignificant.</font></em></td></tr></tbody></table><font style="font-size:11px"> </font></p>Sat, 02 Aug 2008 19:14:16 Z2008-08-02T19:14:16Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#836dbf30-b7c3-450b-ab0e-74c245a122d0http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#836dbf30-b7c3-450b-ab0e-74c245a122d0Hristo Markovhttp://social.msdn.microsoft.com/Profile/en-US/?user=Hristo%20MarkovA very serious bug in MS Visual C++  <p>Hello Mark,</p> <p>Thank you for your reply. I realized that this problem is known, but not fix. Unfortunately, the actions with floating point numbers are essential and I do not see how they can be organized in other ways. </p> <p>I will be very grateful if someone has any idea or decision on how to avoid this problem.</p> <p>Does someone know when Microsoft will remedy this problem?<br></p>Mon, 04 Aug 2008 08:02:58 Z2008-08-04T08:02:58Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#d0a6b484-a210-4bfe-b97b-3658d34e8983http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#d0a6b484-a210-4bfe-b97b-3658d34e8983Sheng Jiang 蒋晟http://social.msdn.microsoft.com/Profile/en-US/?user=Sheng%20Jiang%20%u848b%u665fA very serious bug in MS Visual C++This is not a bug but an industry standard. Your problem is, you expect a general purpose C++ compiler to include a math library. There is no such library in the C++ standard. Ask your question a forum devoted to math libraries or math talks in general, may be one of the mathematicians can help you. <hr size="1" align="left" width="25%">MSMVP VC++Mon, 04 Aug 2008 15:23:32 Z2008-08-04T15:23:32Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#4f06ebd5-8320-49c4-b2ea-907884ae7f68http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#4f06ebd5-8320-49c4-b2ea-907884ae7f68Hristo Markovhttp://social.msdn.microsoft.com/Profile/en-US/?user=Hristo%20MarkovA very serious bug in MS Visual C++  <p>Industry standard ??? I am not agreeing with you. There are laws of mathematics which must be respected by all. </p> <p>I can not agree that 111,567 is equal to 111.56699999999999 because of simple reason that it is not equal.</p> <p>Usually these floating point numbers involved in complex formulas. How do you feel would be received accurate results from inaccurate data?</p> <p>And what program you would spell for the calculation of the example which I pointed in the first post?<br></p>Tue, 05 Aug 2008 06:46:20 Z2008-08-05T06:46:20Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#62391adf-3f68-40bf-83cd-683bff2236dfhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#62391adf-3f68-40bf-83cd-683bff2236dfJackson McCannhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jackson%20McCannA very serious bug in MS Visual C++ No - for 32 bit and 64 bit floating point number as represented in C,C++ and virtually every other language that I know of you are <strong>always</strong> going to run into anomalies such as this.  Pick and two real numbers; no matter how close together you pick them there are an infinite number of other number between them. How can this be represented in 64 bits of data? It can't.  Therefore the agreed industry standard, IEEE standard for floating point numbers, is used so while the results will be wrong at least they will all be wrong in the same way.<br><br>If this situation isn't acceptable to you in a particular environment then you need to use a more specialized arithmetic library to do your math.  These will be a lot slower, they don't do math in the same bit manipulation way that your processors in built FP maths routine will but you can get a lot more precision in the results<br><br>A search for 'C++ arbitrary precision arithmetic ' finds lots of references.  Some time spent reading Donald Knuth's book 'The Art of Computer Programming' (I think it's volume 2 that covers this material) will explain this a lot better than I can.<br><br>The laws of mathematics as we currently understand them as very lovely - but when you need to map them onto the binary world inside your computer you have to make some compromises; you can't squeeze infinity into 64 bits!<hr size="1" align="left" width="25%">JacksonTue, 05 Aug 2008 09:45:01 Z2008-08-05T09:45:01Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#9027748d-335f-4f95-abe0-54a47a2075b2http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#9027748d-335f-4f95-abe0-54a47a2075b2Brian Muthhttp://social.msdn.microsoft.com/Profile/en-US/?user=Brian%20MuthA very serious bug in MS Visual C++ Hristo,<br><br><a href="http://docs.sun.com/source/806-3568/ncg_goldberg.html">What Every Computer Scientist Should Know About Floating-Point Arithmetic</a>Tue, 05 Aug 2008 19:30:07 Z2008-08-05T19:30:07Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#c5a7364f-9650-4bfc-b1fc-9ea8d58b261ahttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#c5a7364f-9650-4bfc-b1fc-9ea8d58b261aMark Salsberyhttp://social.msdn.microsoft.com/Profile/en-US/?user=Mark%20SalsberyA very serious bug in MS Visual C++ There's that link!  Bookmarked, thanks!<br><br>Cheers,<br>Mark<br><br><hr size="1" align="left" width="25%"> Mark Salsbery Microsoft MVP - Visual C++ Tue, 05 Aug 2008 20:13:40 Z2008-08-05T20:13:40Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#f284be00-743e-4987-81d4-a184311296fbhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#f284be00-743e-4987-81d4-a184311296fbHristo Markovhttp://social.msdn.microsoft.com/Profile/en-US/?user=Hristo%20MarkovA very serious bug in MS Visual C++<p>You cover me with theory. Thank you. But things are much simpler. </p> <p>We develop software systems. For one customer we have developed ERP system and now he tests it. </p> <p>Before some time I meet with the customer to discuss whether there are problems in dealing with the software system.</p> <p>He told me: &quot;Everything is ok, but see - the software system is unable to properly calculate. Fix the software or I will have to address the other software companies.” He makes the calculations of a calculator and shows me the difference.</p> <p>Can someone write a few lines of source code that properly subtract two numbers with floating point? </p> <p>/ How calculated calculator? I do not have in to the calculations with accuracy of infinity. /<br></p><br>Wed, 06 Aug 2008 07:44:20 Z2008-08-06T07:44:20Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#a09c7c87-e423-45b4-a23c-8e67ed36f4e7http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#a09c7c87-e423-45b4-a23c-8e67ed36f4e7Jackson McCannhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jackson%20McCannA very serious bug in MS Visual C++<p>The short answer is no; if you use floating point arithmetic then you will continue to run into these discrepancies.  So how do you work around them is the question and there are a number of ways that you can do this:<br></p> <ol> <li>Live with it.</li> <li>Limit the precision you allow in numbers and use rounding.  Why do you need 14 decimal places of accuracy? If you only allow 4 decimal places say and round appropriately the problem will become much less apparent.  I don't know you application so this approach may not be suitable.</li> <li>Don't use floating point numbers use integers.  Integer arithmetic is lot easier and until you start getting overflow problems will give you the results you require.  So lets say you're dealing with money, where I live that Pounds and Pence where 1 Pound is 100 pence (apologies for the blindingly obvious if you are in the uk) so I might store all my data in an integer where 1 represented 1 penny.  This works fine for addition, subtraction and multiplication - however division can be a problem and lead to rounding errors.</li> <li>Use a multiple precision arithmetic library such as <a href="http://gmplib.org/">http://gmplib.org/</a>.  I've never used it in anger myself but it's out there along with other commercial products that do the same job.</li></ol> <p>I know that this is a frustrating problem, it seems like such a simple thing to want to do, but you may need to completely rethink the way your application handles numbers if you want a solution.  <br></p> <hr align=left width="25%" size=1> JacksonWed, 06 Aug 2008 10:00:42 Z2008-08-06T10:05:31Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#a55d1ef4-e408-4789-9202-d74fa11923a6http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#a55d1ef4-e408-4789-9202-d74fa11923a6Brian Muthhttp://social.msdn.microsoft.com/Profile/en-US/?user=Brian%20MuthA very serious bug in MS Visual C++ There is very little to add to this thread, since it has been thoroughly answered. <br><br>To round out the discussion, however, it is worthwhile to point out the <a href="http://msdn.microsoft.com/en-us/library/system.decimal.aspx">Decimal data type</a>, which is quite useful in certain circumstances where one wants to avoid round-off errors inherent in floating point arithmetic. Although not a native C++ data type, it is accessible through the .NET Framework as System::Decimal.Wed, 06 Aug 2008 16:49:33 Z2008-08-06T16:49:33Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#13508a3d-66c5-4d5d-a4ac-f084e535b50dhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#13508a3d-66c5-4d5d-a4ac-f084e535b50dHristo Markovhttp://social.msdn.microsoft.com/Profile/en-US/?user=Hristo%20MarkovA very serious bug in MS Visual C++<p><br>I explained it elementary, but the calculations being made in the ERP system in general are not simple. </p> <p>I'll give an example: The customer is holding company, which deals with trade and production of goods. Only one of its suppliers sent (average per month) five lorries (on 20 tons) in goods and materials – (this makes 100 tons monthly goods and materials). The supplier sent invoices. Each invoice for delivery has at least 100 lines. The goods and materials entering into the store and are recorded in the ERP system. At the end of the month, however, arrives invoice from the company-carrier, which is usually common to all trucks. So ERP system must allocate a proportion of all transport over goods and materials supplied from this supplier during the month. And to increase the value of the delivered goods and materials in the warehouse with those transport proportions. Thus, received their fair value delivery. </p> <p>You can only imagine what serious differences are obtained in the calculation of transport if ERP system not work with floating point numbers with greater accuracy.</p> <p>This is only one simple example. Things become more serious when the materials come into production…</p> <p>/Please note that ERP system is a combination of software modules that work together with common database. ERP system, for which I speak, covers the entire business of a company that is engaged in trade and production./</p> <p>For these reasons, I need calculations with greater accuracy. Unfortunately, in this forum, I still do not see something that will help me in practice.<br></p> <br><br>Thu, 07 Aug 2008 05:44:42 Z2008-08-07T05:44:42Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#d439b87d-db34-4ab5-85d1-f493e04f1296http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#d439b87d-db34-4ab5-85d1-f493e04f1296ildjarnhttp://social.msdn.microsoft.com/Profile/en-US/?user=ildjarnA very serious bug in MS Visual C++<div class=quote><font class=quoteHeader>Hristo Markov said:</font> <p> <p>Unfortunately, in this forum, I still do not see something that will help me in practice. <br><br></p> <p></p></div><br><br>Jackson McCann gave you the exact response your situation appears to call for: &quot;Use a multiple precision arithmetic library such as <a href="http://gmplib.org/">http://gmplib.org/</a>.&quot; A multiple precision arithmetic library is generally integer based, and does not suffer from the accuracy/precision problems that plague floating point arithmetic, however as a tradeoff, it performs slower (sometimes much slower).Thu, 07 Aug 2008 07:06:37 Z2008-08-07T07:06:37Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#bd94f1bc-284d-4775-acf9-907bd0db133ehttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#bd94f1bc-284d-4775-acf9-907bd0db133eSheng Jiang 蒋晟http://social.msdn.microsoft.com/Profile/en-US/?user=Sheng%20Jiang%20%u848b%u665fA very serious bug in MS Visual C++Still don't understand why you consider this as a bug.  If built-in types in a computer language do not have precisions you need, create your own types or use a class library. There is no law prohibit you to use other types. <br><br>You can not expect the compiler to abandon a wide-adopted industry standard, which is not only used by computer languages, but also implemented by most CPUs. If the compiler or the CPU change the length of float or double, I can not imagine how many programs would have portablity issues.<br><hr size="1" align="left" width="25%">MSMVP VC++Thu, 07 Aug 2008 13:26:00 Z2008-08-07T13:26:00Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#0ecac46e-8eb3-4f3c-9bf5-82f9c1bd5343http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#0ecac46e-8eb3-4f3c-9bf5-82f9c1bd5343Brian Muthhttp://social.msdn.microsoft.com/Profile/en-US/?user=Brian%20MuthA very serious bug in MS Visual C++I agree with the other replies. You should not be using floating point arithmetic at all for this application.Thu, 07 Aug 2008 16:09:39 Z2008-08-07T16:10:16Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#027d9883-646e-40c3-828f-0322dffb0786http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#027d9883-646e-40c3-828f-0322dffb0786ZozoGhttp://social.msdn.microsoft.com/Profile/en-US/?user=ZozoGA very serious bug in MS Visual C++ This is no bug. Floats in x86 CPUs (and most other CPUs) follow the IEEE floating-point standard (<a href="http://en.wikipedia.org/wiki/IEEE_754">http://en.wikipedia.org/wiki/IEEE_754</a>). Floats will almost never produce exact results (you will notice it when you debug). You must keep this in mind when doing float computation especially about the precision as small errors will propagate into larger errors over multiple computations. Your application should use a different data type if you need exact fractional results.<br>Thu, 07 Aug 2008 18:50:10 Z2008-08-07T18:59:41Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#0f9c52e4-8ae1-4c27-a618-ed877dd81badhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#0f9c52e4-8ae1-4c27-a618-ed877dd81badHristo Markovhttp://social.msdn.microsoft.com/Profile/en-US/?user=Hristo%20MarkovA very serious bug in MS Visual C++<p>ildjarn, you are most likely right. Yes, the library which Jackson McCann offers will done work, only that it is specialized to work under Linux. Developed by us ERP systems operate under Windows. Gmplib requires installation of an additional driver to emulate Linux - Cygwin. This will significantly slow down the speed of the ERP systems. Keep in mind that in the ERP systems work together multiple users in real time.</p> <p>Brian Muth and ZozoG - What other different data type you'd suggested to calculate the fractional numbers?<br></p> <br><br>Fri, 08 Aug 2008 07:18:28 Z2008-08-08T07:18:28Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#5b935ecd-a90d-4ee8-9b74-fc54f9c2a6a6http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#5b935ecd-a90d-4ee8-9b74-fc54f9c2a6a6Sipu4uhttp://social.msdn.microsoft.com/Profile/en-US/?user=Sipu4uA very serious bug in MS Visual C++ Hi <span style="font-size:9pt;font-family:Verdana">Hristo Markov,<br>                        Its working file in visual C++ 2008.It was giving c value as 0.567.<br><br>regards,<br>sipu..</span>Fri, 08 Aug 2008 11:23:49 Z2008-08-08T11:23:49Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#3d7d0e85-1e78-4541-b990-48c361e55ed3http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#3d7d0e85-1e78-4541-b990-48c361e55ed3David Thornleyhttp://social.msdn.microsoft.com/Profile/en-US/?user=David%20ThornleyA very serious bug in MS Visual C++I notice that gmplib supports extended precision floating point numbers, so you can try those.  Whether or not they work for your purposes is another matter, which you need to figure out for yourself.  You apparently aren't looking for accuracy so much as you're looking for a specific brand of inaccuracy:  base 10 floating point approximations as opposed to base 2 floating point approximations.  Neither are precise, but the former is what is presumably showing up on your client's calculator.<br><br>However, there's an alternative to using fractions:  use integers for everything.  Don't represent dollar amounts as floating-point numbers of dollars, but as integral cents.  Barring overflow (which you avoid by using extended integer variables like in Python or Lisp or, in this case, gmplib), the calculations will be exact.  It's an old trick for financial calculations, over forty years old, and a good one.  It also has the virtue of being obviously correct.  Since you are unfamiliar with how floating-point calculations actually work, you may find it easier to get integer calculations correct.  Despite decades of work, there's a lot of smart people still working out how to best do various sorts of floating-point calculations, in a subfield of computer science called numerical analysis.<br><br>Also, check the license on gmplib, which is LGPL.  What will probably affect you most is the requirement that the user must be able to change gmplib and relink with your application, if the user desires (which is almost certainly not going to happen).   This is limited to legitimately owned copies, and the license does not grant any extra copying or redistribution rights concerning your application.  There's a whole lot of very useful open source software out there, but like proprietary software you use you do need to make sure you're operating within the limits of the license.<br> Fri, 08 Aug 2008 16:04:22 Z2008-08-08T16:04:22Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#f91adcc1-7169-48ac-8da6-9eeb7c5d3925http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#f91adcc1-7169-48ac-8da6-9eeb7c5d3925Brian Muthhttp://social.msdn.microsoft.com/Profile/en-US/?user=Brian%20MuthA very serious bug in MS Visual C++<div class=quote><font class=quoteHeader>Hristo Markov said:</font> <p> <p>Brian Muth and ZozoG - What other different data type you'd suggested to calculate the fractional numbers<br><br></p> <p></p></div><br><br>Personally, I'd use the System::Decimal class. If you want to avoid the dependence on the .NET Framework, then I'd build my own C++ class around the  VARIANT Decimal type, using <a href="http://msdn.microsoft.com/en-us/library/ms221612(VS.85).aspx">Decimal Arithmetic Functions</a>.<br><br>Decimal arithmetic is not part of C or C++ but there is a current working group (WG14) working on a <a href="http://www.open-std.org/JTC1/SC22/WG14/www/projects#24732">technical report </a>proposing the addition of data types <a href="http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1312.pdf">_Decimal32, _Decimal64 and _Decimal128</a> along with the corresponding arithmetic operations to enable decimal arithmetic natively. This will likely become part of TR2 (Note that Microsoft has recently released TR1 features as part of the VS 2008 Feature Pack.)Fri, 08 Aug 2008 16:48:30 Z2008-08-08T16:48:30Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#3cec4c64-add7-4312-a8cc-e1358bba71dahttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#3cec4c64-add7-4312-a8cc-e1358bba71daildjarnhttp://social.msdn.microsoft.com/Profile/en-US/?user=ildjarnA very serious bug in MS Visual C++gmplib is just one example of the many libraries available. A simple google search for e.g. &quot;arbitrary precision c++&quot; gives many results, the first of which is <a href="http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic">http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic</a>, which contains a list of various libraries you could use.<br>Fri, 08 Aug 2008 18:34:46 Z2008-08-08T18:34:46Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#15e3cc90-f597-4bde-af49-cec721347b90http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#15e3cc90-f597-4bde-af49-cec721347b90Hristo Markovhttp://social.msdn.microsoft.com/Profile/en-US/?user=Hristo%20MarkovA very serious bug in MS Visual C++<p>Dear Colleagues, </p> <p>Thank you for answers. Unfortunately I can not make comments on any response.</p> <p>I thought to &quot;ask&quot; google for a solution, but first I want to know the opinion of participants in the forum of Microsoft. Of course, I will try libraries, which recommends ildjarn to me.</p> <p>Sipu4u, your reply is very interesting for me. How you achieved this result? Please explain. The results of my example, you can check in a new Win32 project. Just embed the example in some event of the window. Then in debug mode, you'll see the results that I indicated.</p> <p>I think the idea of Brian Muth to use variables of VARIANT DECIMAL TYPE is very good. I tried it and the results are very good.</p> <p>If we follow this logic, however, there will not be a need for different types of variables, because all kinds of variables may be submitted during VARIANT variables. </p> <p>I think that something fundamental such as a declaration of fractional numbers and their actions should not be in so surrounded way. I think that the variables with floating point ('double') are unusable at this time, because they do not always give accurate results. And I would like experts from Microsoft, which deal with these issues, in some way to offer basic solution to this problem.</p><br><br>Mon, 11 Aug 2008 08:09:18 Z2008-08-11T08:09:18Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#43d829be-6db3-4afe-86e5-e8735aa78ea5http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#43d829be-6db3-4afe-86e5-e8735aa78ea5Holger Grund [MSFT]http://social.msdn.microsoft.com/Profile/en-US/?user=Holger%20Grund%20%5bMSFT%5dA very serious bug in MS Visual C++ While many have already pointed out that floating-point arithmetic can never be correct for all possible inputs (and where I live you can get into very serious legal trouble if you use FP for financial calculations), but the concrete example you gave would indeed indicate a bug in the compiler with the default settings targeting IA-32.<br><br>All of the numbers in the example are exactly representable in IEEE-754 double precision (which is the format of VC++'s double on IA-32). In the code snippet c is guaranteed to be .567. If you have a reproducible example where this is not the case, we'd definitely want to know.<br><br>That being said, for many &quot;similar&quot; numbers there are no exact representations and these kinds of errors are expected.<br><br>-hg<hr size="1" align="left" width="25%">Visual C++ Libraries TeamMon, 11 Aug 2008 11:20:51 Z2008-08-11T11:20:51Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#30c0f51e-f8d1-496b-bfe9-fe5290373f96http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#30c0f51e-f8d1-496b-bfe9-fe5290373f96Hristo Markovhttp://social.msdn.microsoft.com/Profile/en-US/?user=Hristo%20MarkovA very serious bug in MS Visual C++  <p>I run “Microsoft Visual C++ 2008” from “Microsoft Visual Studio 2008 Version 9.0.21022.8 RTM” under “Windows Vista with Service Pack 1”.</p> <p>Simply start new project =&gt; “New project” – “Visual C++” – “Win32” – “Win32 project”</p> <p>If you include the next code in standard callback function WndProc and after compilation if you click on window then you will see the wrong results.</p> <p>Also in debug mode you can see the wrong results.</p> <p><br> case WM_LBUTTONDOWN:<br>  {<br>   char s[100];<br>   double a=111.567,b=111,c;<br>   c=a-b;<br>   sprintf(s,&quot;a=%10.20f,b=%10.20f,c=%10.20f&quot;,a,b,c);<br>   MessageBox(hWnd,s,&quot;&quot;,0);<br>  }<br>  break;</p> <p>If you have any problems, I can send the whole simple example project. <br><br></p>Tue, 12 Aug 2008 07:35:00 Z2008-08-12T07:35:00Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#1b7e5031-aeff-4632-801d-8a2fcae418f0http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#1b7e5031-aeff-4632-801d-8a2fcae418f0Carl Danielhttp://social.msdn.microsoft.com/Profile/en-US/?user=Carl%20DanielA very serious bug in MS Visual C++<div class=quote><font class=quoteHeader>Brian Muth said:</font> <p></p><br>Personally, I'd use the System::Decimal class. If you want to avoid the dependence on the .NET Framework, then I'd build my own C++ class around the  VARIANT Decimal type, using <a href="http://msdn.microsoft.com/en-us/library/ms221612(VS.85).aspx">Decimal Arithmetic Functions</a>.<br><br></div><br><br><font style="font-size:12px"></font><font style="background-color:#ffffff">I've got just such a wrapper for both the decimal and currency types from OLE automation.  If anyone's interested, I'll look into securing rights to distribute it.<br></font><br> <hr align=left width="25%" size=1> -cd [VC++ MVP] Mark the best replies as answers!Tue, 12 Aug 2008 14:38:08 Z2008-08-12T14:48:13Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#7448a8cf-cc65-407a-85d5-f05cc614a963http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#7448a8cf-cc65-407a-85d5-f05cc614a963c_jensenhttp://social.msdn.microsoft.com/Profile/en-US/?user=c_jensenA very serious bug in MS Visual C++<div class=quote><font class=quoteHeader>Hristo Markov said:</font> <p>  <p>I run “Microsoft Visual C++ 2008” from “Microsoft Visual Studio 2008 Version 9.0.21022.8 RTM” under “Windows Vista with Service Pack 1”.</p> <p>Simply start new project =&gt; “New project” – “Visual C++” – “Win32” – “Win32 project”</p> <p>If you include the next code in standard callback function WndProc and after compilation if you click on window then you will see the wrong results.</p> <p>Also in debug mode you can see the wrong results.</p> <p><br> case WM_LBUTTONDOWN:<br>  {<br>   char s[100];<br>   double a=111.567,b=111,c;<br>   c=a-b;<br>   sprintf(s,&quot;a=%10.20f,b=%10.20f,c=%10.20f&quot;,a,b,c);<br>   MessageBox(hWnd,s,&quot;&quot;,0);<br>  }<br>  break;</p> <p>If you have any problems, I can send the whole simple example project. <br><br></p> <p></p></div><br><br>That's odd.  If  you cout the values, they're correct.Tue, 12 Aug 2008 17:09:10 Z2008-08-12T17:09:10Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#dc94098b-8c72-4a66-a5f8-22bca4b86b36http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#dc94098b-8c72-4a66-a5f8-22bca4b86b36Carl Danielhttp://social.msdn.microsoft.com/Profile/en-US/?user=Carl%20DanielA very serious bug in MS Visual C++<div class=quote><font class=quoteHeader>Hristo Markov said:</font> <p> <p>I think that something fundamental such as a declaration of fractional numbers and their actions should not be in so surrounded way. I think that the variables with floating point ('double') are unusable at this time, because they do not always give accurate results. And I would like experts from Microsoft, which deal with these issues, in some way to offer basic solution to this problem.<br></p></div><br><font style="font-size:12px">I think you need to learn what you can reasonably expect from floating point arithmetic.  Floating point arithmetic gives results accurate within a certain specification (such as IEEE 754, as another respondent mentioned).  In the case of the VC++ type double, the accuracy is approximately +/- 1 in the 17th significant digit in the best possible case.  If you want absolute accuracy, don't use floating point math unless you understand the limitations very well - which you clearly do not.<br><br>Taking the numbers from your example:<br><br>a = 111.567.  This number cannot be represented exactly by binary floating point and is approximated by the number having the representation:<br><br>0x405be449ba5e353f<br><br>Or, separating the fields and converting to binary:<br><br>0 10000000101  1011111001000100100110111010010111100011010100111111<br><br>Simlarly, b is:<br><br>0x405bc00000000000<br>0 10000000101  1011110000000000000000000000000000000000000000000000<br><br>Now, to subtract floating point numbers, you may first have to denormalize one of the numbers so that both have the same exponent. In this case, a and b already have the same exponent (0x405) so no denormalization is needed.  Once the numbers are put into a common exponent, you can simply subtract the mantissas.  Doing that subtraction yields:<br><br>0000001001000100100110111010010111100011010100111111<br><br>Finally, the result must be normalized by removing leading zeros and the first leading 1 and adjusting the exponent accordingly.  In this case, there are 6 leading zero bits, plus the first 1, so we shift left by 7 bits and decrease the exponent by 7 yielding:<br><br>0 01111111110  0010001001001101110100101111000110101001111110000000<br><br>or<br><br>0x3fe224dd2f1a9f80<br><br>Which is exactly the result that VC++ calculated.  We lost 7 bits of accuracy because of the difference in magnitude between the original numbers and their difference.<br><br></font> <hr align=left width="25%" size=1> -cd [VC++ MVP] Mark the best replies as answers!Tue, 12 Aug 2008 17:10:16 Z2008-08-12T17:12:38Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#07daec99-47ba-4750-b6f7-e8eed8c2d964http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#07daec99-47ba-4750-b6f7-e8eed8c2d964Holger Grund [MSFT]http://social.msdn.microsoft.com/Profile/en-US/?user=Holger%20Grund%20%5bMSFT%5dA very serious bug in MS Visual C++ Ah my bad. Apparently, the IEEE-754 calculator I used has a bug (rounding where it shouldnt). In fact, there is no accurate representation for 111.567 and therefore what you get is expected (17 digits is the required precision for IEEE-754 binary-to-decimal conversions in the relevant standards) The number you see is accurate to 17 digits (note that the result of the substraction is off from the mathematical result due to the nature of floating point arithmetic)<br><br>Sorry for the noise. I'll need to find a better IEEE-754 calculator for the next time ...<br><br>-hg<hr size="1" align="left" width="25%">Visual C++ Libraries TeamTue, 12 Aug 2008 21:06:17 Z2008-08-12T21:06:17Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#6aeb6ffa-4667-443f-87f9-6bde93fa635dhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#6aeb6ffa-4667-443f-87f9-6bde93fa635dWayneAKinghttp://social.msdn.microsoft.com/Profile/en-US/?user=WayneAKingA very serious bug in MS Visual C++<p><br><font style="font-size:11px">C. Jensen said:<br>Quote&gt;That's odd.  If  you cout the values, they're correct.</font></p> <p><font style="font-size:11px">They'll be correct in the OP's example as well if you use:<br>sprintf(s,&quot;a=%10.3f,b=%10.3f,c=%10.3f&quot;,a,b,c);</font></p> <p><font style="font-size:11px">- Wayne<br></font></p>Tue, 12 Aug 2008 21:12:08 Z2008-08-12T21:12:08Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#fa9f6c63-25b4-41dc-9044-71620168cbd7http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#fa9f6c63-25b4-41dc-9044-71620168cbd7WayneAKinghttp://social.msdn.microsoft.com/Profile/en-US/?user=WayneAKingA very serious bug in MS Visual C++<p><br><font style="font-size:11px">Also with respect to the values displayed by cout, try this:</font></p> <p><font style="font-size:11px">std::cout.precision(20);<br>std::cout &lt;&lt; std::fixed &lt;&lt; a &lt;&lt; '\t' &lt;&lt; b &lt;&lt; '\t' &lt;&lt; c &lt;&lt; std::endl;</font></p> <p><font style="font-size:11px">The results should be similar to those from the OP's original<br>sprintf example.</font></p> <p><font style="font-size:11px">- Wayne<br></font></p>Tue, 12 Aug 2008 21:30:20 Z2008-08-12T21:30:20Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#8447a157-04f1-4820-97db-449e4e5c9525http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#8447a157-04f1-4820-97db-449e4e5c9525c_jensenhttp://social.msdn.microsoft.com/Profile/en-US/?user=c_jensenA very serious bug in MS Visual C++<div class=quote><font class=quoteHeader>WayneAKing said:</font> <p> <p><br><font style="font-size:11px">Also with respect to the values displayed by cout, try this:</font></p> <p><font style="font-size:11px">std::cout.precision(20);<br>std::cout &lt;&lt; std::fixed &lt;&lt; a &lt;&lt; '\t' &lt;&lt; b &lt;&lt; '\t' &lt;&lt; c &lt;&lt; std::endl;</font></p> <p><font style="font-size:11px">The results should be similar to those from the OP's original<br>sprintf example.</font></p> <p><font style="font-size:11px">- Wayne<br></font></p> <p></p></div><br><br>Ah.  You are correct sir.Tue, 12 Aug 2008 23:00:17 Z2008-08-12T23:00:17Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#a4c89627-9d69-480a-8ad3-74ec57304168http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#a4c89627-9d69-480a-8ad3-74ec57304168Brian Muthhttp://social.msdn.microsoft.com/Profile/en-US/?user=Brian%20MuthA very serious bug in MS Visual C++<div class=quote><font class=quoteHeader>Carl Daniel said:</font> <p></p><br><font size=3></font><font style="background-color:#ffffff">I've got just such a wrapper for both the decimal and currency types from OLE automation.  If anyone's interested, I'll look into securing rights to distribute it.<br></font><br> <hr align=left width="25%" size=1> <br><br></div><br>There you go, Hristo. Carl is handing you a solution on a platter.Tue, 12 Aug 2008 23:44:26 Z2008-08-12T23:44:26Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#3998bf99-ad07-4778-9f0b-3f21a5a15d53http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#3998bf99-ad07-4778-9f0b-3f21a5a15d53Hristo Markovhttp://social.msdn.microsoft.com/Profile/en-US/?user=Hristo%20MarkovA very serious bug in MS Visual C++  <p><br>Whether it will be used &quot;%10.20f&quot; or &quot;%10.3f&quot; or &quot;%f&quot; or … has no meaning. The problem is that the numbers are not presented correctly in the program.</p> <p>Maybe example should also be simplified:</p> <p> case WM_LBUTTONDOWN:<br>  {<br>   double a=111.567;<br>  }<br>  break;<br><br>And check up in debug mode.<br></p> <p>All indicate the standard IEEE as a dogma. I am not familiar with the IEEE simply because I do not have time. But once the standard makes it impossible to use a certain type of fundamental variables and actions with them, maybe it is better to consider changes in the standard.</p><br><br>Wed, 13 Aug 2008 06:13:45 Z2008-08-13T06:13:45Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#745295a6-8ec6-4e0c-a06e-01fe697d644chttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#745295a6-8ec6-4e0c-a06e-01fe697d644cSheng Jiang 蒋晟http://social.msdn.microsoft.com/Profile/en-US/?user=Sheng%20Jiang%20%u848b%u665fA very serious bug in MS Visual C++Fast floating point operations are essential for other programs and there is no reason to slow them down for your particular needs.<br><hr size="1" align="left" width="25%">MSMVP VC++Wed, 13 Aug 2008 13:32:37 Z2008-08-13T13:32:37Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#553f3dd6-6ac0-4316-ae33-9a08e7edb5c6http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#553f3dd6-6ac0-4316-ae33-9a08e7edb5c6Hristo Markovhttp://social.msdn.microsoft.com/Profile/en-US/?user=Hristo%20MarkovA very serious bug in MS Visual C++<p><br>I agree with you that fast floating point operations are essential. I think however that it became clear that the actions with a floating point numbers do not always provide accurate results and therefore become unusable.</p> <p>Please provide an example, in which cases the actions by numbers with floating point are right for you?<br></p>Thu, 14 Aug 2008 05:56:14 Z2008-08-14T05:56:14Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#67cb94d8-d1e0-409d-960c-049c995373a5http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#67cb94d8-d1e0-409d-960c-049c995373a5davewilkhttp://social.msdn.microsoft.com/Profile/en-US/?user=davewilkA very serious bug in MS Visual C++<div class=quote><font class=quoteHeader>Hristo Markov said:</font><p></p><p><br>I agree with you that fast floating point operations are essential. I think however that it became clear that the actions with a floating point numbers do not always provide accurate results and therefore become unusable.</p> <p>Please provide an example, in which cases the actions by numbers with floating point are right for you?<br></p><p></p></div>Hristo:<br><br>Floating point operations are designed for use with (possibly very complex) computations in which every input, output, and intermediate value is represented in floating point. They are always &quot;right for me&quot; if that is the kind of calculation I am doing. This does not necessarily mean that the answer is always exact, and a badly designed algorithm can give a completely wrong answer. In fact there is a whole field of study called numerical analysis that is devoted to constructing algorithms which perform in a satisfactory way in the presence of rounding errors.<br><br>In your particular example, you could multiply your numbers by 1000 and treat them as integers. This might be what you want, but it is a solution applicable only to your problem (or ones like it). It would not be suitable for a general numerical algorithm.<br><br><hr size="1" align="left" width="25%">David Wilkinson | Visual C++ MVPThu, 14 Aug 2008 14:31:53 Z2008-08-14T14:31:53Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#b52d655b-7e33-4e29-9aa3-d4b93ea3eafdhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#b52d655b-7e33-4e29-9aa3-d4b93ea3eafdBrian Muthhttp://social.msdn.microsoft.com/Profile/en-US/?user=Brian%20MuthA very serious bug in MS Visual C++<div class=quote><font class=quoteHeader>Hristo Markov said:</font> <p> <p>Please provide an example, in which cases the actions by numbers with floating point are right for you?<br></p> <p></p></div><br>Most scientific problems (physics, chemistry, engineering) do their calculations in floating point. These problems are only interested in maintaining a certain number of <a href="http://en.wikipedia.org/wiki/Significant_digits">significant digits</a>, which is what floating point addresses.<br><br>Your question is actually quite amusing to me. I don't know how old you are, but it is like asking, &quot;what problems can be solved using a slide ruler?&quot;. Certainly, one would never use a slide ruler for a business computation, but for scientific computations they were a must in the pre-calculator days. And the Golden Gate bridge is a testament to that tool.<br><br>You need to pick your tools more carefully. You saw a nail and picked up a wrench.<br>Thu, 14 Aug 2008 16:19:19 Z2008-08-14T16:19:19Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#158897e5-2d6c-4f59-b6e1-a8479566a4d9http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#158897e5-2d6c-4f59-b6e1-a8479566a4d9Hristo Markovhttp://social.msdn.microsoft.com/Profile/en-US/?user=Hristo%20MarkovA very serious bug in MS Visual C++<p>Colleagues,</p> <p>I think that you don't understand the nature of the problem. <strong>There are many other such numbers with floating point, which is not presented correctly</strong>. When I ask Sheng Jiang to give an example, I had to that in any example to give, I'll be able to prove that under certain values of variables with floating point the results will be wrong. And this is very simply because it can not by false data to obtain accurate results.</p> <p>I think that the physics, chemistry, engineering, etc. are the exact sciences and there also apply rules on the precise presentation of the numbers. Floating point numbers participate in formulas, which would increase the error of their submission.</p> <p>Imagine what would have happened if you develop software system for managing chemical processes in which the data are submitted by the sensors. These data are processed by complex formulas and the results will serve for the management of chemical processes.</p> <p>In your view what would happen if the results are wrong? (which inevitably would happen if the variables are with floating point)</p> <p>In my view - most likely would be reached explosion.</p> <p>I think however that the dispute begins to move from the topic.</p> <br>Fri, 15 Aug 2008 05:20:02 Z2008-08-15T05:20:02Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#3db890af-02f1-4f91-a169-3a01f9e326behttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#3db890af-02f1-4f91-a169-3a01f9e326beSheng Jiang 蒋晟http://social.msdn.microsoft.com/Profile/en-US/?user=Sheng%20Jiang%20%u848b%u665fA very serious bug in MS Visual C++As others suggested, you need to use another data type. Your design is wrong if you use the data type not designed for your computation. The float point standard is a convention for most people's convenience. If it is not for you, use another data type. Which is harder, using another data type, or expecting the hardware and software industry invest billions and many years to change the float point operation to fit your needs? <br><br>Find the right tool is essential for a computer programmer. If you are still not sure what to do, ask your project manager or ask a mathematician who has experience in math libraries. <br><hr size="1" align="left" width="25%">MSMVP VC++Fri, 15 Aug 2008 13:16:14 Z2008-08-15T13:16:14Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#afe1bcd6-676f-48aa-9a05-936ea0713cddhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#afe1bcd6-676f-48aa-9a05-936ea0713cddDavid Thornleyhttp://social.msdn.microsoft.com/Profile/en-US/?user=David%20ThornleyA very serious bug in MS Visual C++No, it is you who doesn't understand the nature of the problem.<br><br>The problem is that, given finite-length representation of numbers, it is not possible to represent an infinite number of numbers.  This is true whether we're talking about IEEE-standard floating point on computers, a pocket calculator calculating in decimal, or pencil and paper.  The exact sciences have survived for quite a few centuries without the ability to represent arbitrary numbers exactly, and they're likely to survive for a while longer.  Scientists are not usually interested in absolutely precise calculations, since they can't get absolutely precise measurements in the first place, and physical constants do not come in conveniently precise numbers unless one uses units designed to make it so.<br><br>Your big mistake is the idea that there is a way to calculate to get inherently correct answers.<br><br>Financial calculations can have correct answers for two reasons.  First, the numbers involved are normally selected as exact decimals.  You might have an interest rate of 3.14%, but you will not have one of 22/7% or pi%.  Second, there are conventions to handle fractions.  As you know, getting the same answer as everybody else in financial calculations can be a useful thing, and therefore there are packages that do calculations in decimals, and use the appropriate conventions.  This is an exact analog to IEEE-754<br>floating point, in that there are sets of numbers that can be exactly represented, anything else is approximated, and there are conventions built into the standard so different people doing the same calculations can get identical results.<br><br>If you think that your pocket calculator, or your pad of paper, can give the right answers, try this:  write down, using digits and decimal points, one number that, when multiplied by three, gives precisely 1.0.<br><br>For a scientist or engineer, there is no particular advantage for using financially correct calculations, and a disadvantage, in that they'd be a lot slower.  Most people who use floating-point calculations seem to be reasonably satisfied.  You've got the special need, and you need to come up with the exact tools you need.  Other people have had the same need, and so there are tools available for you; you need to look at how suitable they are to you and what the terms of use are.<br><br>By now, you should know what you have to do (for a further hint:  search the web for things like C++ decimal arithmetic, or follow up on some of the specific suggestions people have made).  If, at this point, you don't know why C++ numerics are the way they are, that's too bad, and I see no reason to continue this conversation.  Think about what people have written here, and look up that excellent link on understanding floating point.<br> Fri, 15 Aug 2008 15:26:53 Z2008-08-15T15:26:53Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#b7ec78ad-b2f8-408e-a88d-c7605292dd99http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#b7ec78ad-b2f8-408e-a88d-c7605292dd99Micky Dhttp://social.msdn.microsoft.com/Profile/en-US/?user=Micky%20DA very serious bug in MS Visual C++<div class=quote><font class=quoteHeader>Jackson McCann said:</font><p></p><p>The short answer is no; if you use floating point arithmetic then you will continue to run into these discrepancies.  So how do you work around them is the question and there are a number of ways that you can do this:<br></p> <ol> <li>Live with it.</li> <li>Limit the precision you allow in numbers and use rounding.  Why do you need 14 decimal places of accuracy? If you only allow 4 decimal places say and round appropriately the problem will become much less apparent.  I don't know you application so this approach may not be suitable.</li> <li>Don't use floating point numbers use integers.  Integer arithmetic is lot easier and until you start getting overflow problems will give you the results you require.  So lets say you're dealing with money, where I live that Pounds and Pence where 1 Pound is 100 pence (apologies for the blindingly obvious if you are in the uk) so I might store all my data in an integer where 1 represented 1 penny.  This works fine for addition, subtraction and multiplication - however division can be a problem and lead to rounding errors.</li> <li>Use a multiple precision arithmetic library such as <a href="http://gmplib.org/">http://gmplib.org/</a>.  I've never used it in anger myself but it's out there along with other commercial products that do the same job.</li></ol> <p>I know that this is a frustrating problem, it seems like such a simple thing to want to do, but you may need to completely rethink the way your application handles numbers if you want a solution.  <br></p> <hr size=1 width="25%" align=left> Jackson<p></p></div>I am absolutely astounded by two things here:<br><br>1. That this math bug is still floating around (I remember when it was a CPU issue)<br>2. That people here claim that it is not a bug in c++ in VS2003 or later.<br><br>The fact of the matter is, the same c++ source code which works fine in say VS6 will yield different results in VS2003.<br><br><br>Here is a VS 2003 c++ project which:<br><ol><li>Cannot divide properly</li><li>Has conflicting results</li></ol><br><br>int _tmain(int argc, _TCHAR* argv[])<br>{<br>    double range = 50, ticksMajor = 10;<br>    // 10 divided by 50 should equal 0.20, but it doesn't, see below.<br><br>    double delta = ticksMajor / range;    // 0.20000000000000001  (wot tha?)<br>    <br>    // multiply it back out, should get original<br>    double t = delta * range;            // 10.000000000000000    (weird, too hard basket)<br>    _ASSERT (t == ticksMajor);            // true<br><br>    //<br>    // simulate multiply by looping an 'add'.  I just don't trust mulitply now<br>    //<br>    double sum = 0;<br>    for (int i = 0;  i &lt; range; i++)<br>    {<br>        sum += delta;<br>    }<br><br>    _ASSERT (sum == ticksMajor);        // this FAILS!  sum = 9.9999999999999964 and not 10<br><br>    return 0;<br>}<br><br><br>Why is it that the 2nd step fails?  <br><br>Though it is true that my above program does not require double why should I have to resort to using single precision or use truncate operations everywhere?<br><br><ol><li>My old c++ ray tracer will not like running in single precision floating point which is insufficient for ray tracing purposes</li><li>I don't see why one should have to go any use a 3rd party math library just so as to avoid using the onboard MCPU<br></li></ol>Glad that I have no need for developing in c++ these days.<br><hr size="1" align="left" width="25%">Micky DMon, 15 Sep 2008 07:46:42 Z2008-09-15T07:46:42Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#070bd1bf-c903-4ad9-a78c-16a40edc255fhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#070bd1bf-c903-4ad9-a78c-16a40edc255fJackson McCannhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jackson%20McCannA very serious bug in MS Visual C++ Well if it's a bug then it's a very common one.  On our Solaris SPARC based system:<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px"></font><font style="color:gray">#include &lt;stdio.h&gt; </font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td></font><font style="color:blue">int</font><font style="font-size:11px"> main(</font><font style="color:blue">int</font><font style="font-size:11px"> argc, </font><font style="color:blue">char</font><font style="font-size:11px">* argv[])  </font></td></tr> <tr> <td style="background-color:#f7f7f7">{  </td></tr> <tr> <td>    </font><font style="color:blue">double</font><font style="font-size:11px"> fifty = 50.0 ;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">double</font><font style="font-size:11px"> ten = 10.0 ;  </font></td></tr> <tr> <td>    </font><font style="color:blue">double</font><font style="font-size:11px"> div = ten/fifty  ;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">    printf(</font><font style="color:blue">&quot;%24.23lf\n&quot;</font><font style="font-size:11px">, div) ;  </font></td></tr> <tr> <td>    </font><font style="color:blue">return</font><font style="font-size:11px"> 0 ;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">}  </td></tr> <tr> <td> </td></tr></tbody></table></div> Gives the result: 0.20000000000000001110223<br><br>I go back to my original points:  <br><br> <ol> <li>There are an infinite number of real numbers between any two real numbers you care to pick.  You can't represent this richness of numbers with 100% accuarcy in 64 binary bits; therefore you get inaccuracy.</li> <li>Most people want to see base 10 results; your code is operating in base 2.  The conversion between these two bases (for real numbers) isn't 100% accurate.</li></ol> <p>If you require better accuracy than this then you have to do some extra work.</p><hr size="1" align="left" width="25%">JacksonMon, 15 Sep 2008 08:48:23 Z2008-09-15T08:48:23Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#28ad6647-0d46-4782-8f8b-1f72737355f5http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#28ad6647-0d46-4782-8f8b-1f72737355f5Micky Dhttp://social.msdn.microsoft.com/Profile/en-US/?user=Micky%20DA very serious bug in MS Visual C++Thanks for testing it for me on the SPARC system Jackson.  <br><br>Perhaps it is some bizarre feature of hardware-based floating point present in modern CPUs?  This may account for why similar code compiled on older compilers (VS6) (who don't have modern FCPU knowledge) yields expected results.<br><i><br>'If you require better accuracy than this then you have to do some extra work.'</i><br><br>Yes I have seen alot of comments along these lines.  It's a  bit amusing when the system can't even handle a result with one decimal place!<br><br>In the end though it's no major drama for me, since my team and myself all use .NET nowadays which has the Decimal type others mentioned.<br><br>NASA must really laugh when they hear comments like &quot;<i>...computers today have more power than the first space shuttle!</i>&quot;.<br><br>I would argue that at least the first Space Shuttle most likely knew how to divide properly.<br> <hr size="1" align="left" width="25%">Micky DMon, 15 Sep 2008 13:17:36 Z2008-09-15T13:17:36Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#f7cf8c9b-83d3-453e-8b26-54166f680e0ehttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#f7cf8c9b-83d3-453e-8b26-54166f680e0eildjarnhttp://social.msdn.microsoft.com/Profile/en-US/?user=ildjarnA very serious bug in MS Visual C++<div class=quote><font class=quoteHeader>Micky D said:</font><p>I would argue that at least the first Space Shuttle most likely knew how to divide properly.<br> </p><hr size=1 width="25%" align=left>Micky D<p></p></div><br>I would argue that the first space shuttle probably used BCD or integer arithmetic rather than IEEE 754 floating point arithmetic. You have the option of doing the exact same thing, as has been said ten times over in this thread -- and you are, actually, with System.Decimal, which is <b>not</b> a floating point construct.<br>Mon, 15 Sep 2008 17:44:56 Z2008-09-15T17:44:56Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#b090419b-82db-49cb-b1ad-f385acaca4d0http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#b090419b-82db-49cb-b1ad-f385acaca4d0Sheng Jiang 蒋晟http://social.msdn.microsoft.com/Profile/en-US/?user=Sheng%20Jiang%20%u848b%u665fA very serious bug in MS Visual C++<span class=Apple-style-span style="color:rgb(8, 8, 8);font-size:13px"><span class=Apple-style-span style="color:rgb(0, 0, 0);font-family:'Times New Roman';font-size:16px"><div style="font-family:Verdana;font-size:12px"><span class=Apple-style-span style="color:rgb(8, 8, 8);font-size:13px">Bizarre feature? If it is documented on computer books, used in every computer language and every computer chip, then it should be called normal. A bug is unexpected. If it is a standard of dialy life, then it is not a bug. </span></div><div style="font-family:Verdana;font-size:12px"><span class=Apple-style-span style="color:rgb(8, 8, 8);font-size:13px"><br></span></div><div style="font-family:Verdana;font-size:12px"><span class=Apple-style-span style="color:rgb(8, 8, 8);font-size:13px">For example, if you want to go from one place to another, you can take a bus, but you are expected to wait at a bus stop, and to go from the bus stop to your final destination.  If you need a more accurate service, you can call a taxi. You can not count on the bus service to have the service level of taxi. The bus service is designed for the public, not for some individuals' door-to-door trips. </span></div></span></span><hr size="1" align="left" width="25%">MSMVP VC++Mon, 15 Sep 2008 18:09:09 Z2008-09-15T18:09:09Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#5a77ef50-3e19-4cd8-97e0-a6fc2a7cf8eahttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#5a77ef50-3e19-4cd8-97e0-a6fc2a7cf8eaPetr Kodlhttp://social.msdn.microsoft.com/Profile/en-US/?user=Petr%20KodlA very serious bug in MS Visual C++The thing is - Visual C++ 6.0 allowed usage of native extended prevision Intel CPU registers - those were 10 bytes instead of regular 8 byte as prescribed by standard IEEE 754 floating point standard. This could explain why you see different results between two compilers. This feature has been disabled starting Visual C++ 2003. <br> <br> Without seeing exact compiler switches for both compiler versions used this is the best guess. <br> <br> As noted before - the occasional &quot;imprecision&quot; is an inherent property of every finite size representation of floating point numbers. Search google (can one say google here?) for William Kahan - one of the fathers of IEEE 754 standard - his homepage will give you many more examples of peculiarities of floating point calculations - some of them more disturbing than this simple case.<br><br>This imperfection is one of the reasons DECIMAL type has been invented for databases. <br><br>pk<br> Wed, 24 Sep 2008 00:35:32 Z2008-09-24T00:35:32Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#a71a9bea-079e-4ee4-ae08-cd30c19196cfhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#a71a9bea-079e-4ee4-ae08-cd30c19196cfPetr Kodlhttp://social.msdn.microsoft.com/Profile/en-US/?user=Petr%20KodlA very serious bug in MS Visual C++The thing is - Visual C++ 6.0 allowed usage of native extended prevision Intel CPU registers - those were 10 bytes instead of regular 8 byte as prescribed by standard IEEE 754 floating point standard. This could explain why you see different results between two compilers. This feature has been disabled starting Visual C++ 2003. <br> <br> Without seeing exact compiler switches for both compiler versions used this is the best guess. <br> <br> As noted before - the occasional &quot;imprecision&quot; is an inherent property of every finite size representation of floating point numbers. Search google (can one say google here?) for William Kahan - one of the fathers of IEEE 754 standard - his homepage will give you many more examples of peculiarities of floating point calculations - some of them more disturbing than this simple case.<br><br>This imperfection is one of the reasons DECIMAL type has been invented for databases. <br><br>pk<br> Wed, 24 Sep 2008 00:35:35 Z2008-09-24T00:35:35Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#6dd1ec1f-4767-4874-b50d-6559f4c5b038http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#6dd1ec1f-4767-4874-b50d-6559f4c5b038Petr Kodlhttp://social.msdn.microsoft.com/Profile/en-US/?user=Petr%20KodlA very serious bug in MS Visual C++The thing is - Visual C++ 6.0 allowed usage of native extended prevision Intel CPU registers - those were 10 bytes instead of regular 8 byte as prescribed by standard IEEE 754 floating point standard. This could explain why you see different results between two compilers. This feature has been disabled starting Visual C++ 2003. <br> <br> Without seeing exact compiler switches for both compiler versions used this is the best guess. <br> <br> As noted before - the occasional &quot;imprecision&quot; is an inherent property of every finite size representation of floating point numbers. Search google (can one say google here?) for William Kahan - one of the fathers of IEEE 754 standard - his homepage will give you many more examples of peculiarities of floating point calculations - some of them more disturbing than this simple case.<br><br>This imperfection is one of the reasons DECIMAL type has been invented for databases. <br><br>pk<br> Wed, 24 Sep 2008 00:35:39 Z2008-09-24T00:35:39Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#3e44d2e6-db06-4e6e-a6c5-b5659c288165http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#3e44d2e6-db06-4e6e-a6c5-b5659c288165Tim Robertshttp://social.msdn.microsoft.com/Profile/en-US/?user=Tim%20RobertsA very serious bug in MS Visual C++&gt;  I am absolutely astounded by two things here:<br>&gt;<br>&gt; 1. That this math bug is still floating around (I remember when it was a CPU issue)<br>&gt; 2. That people here claim that it is not a bug in c++ in VS2003 or later.<br><br>I am astounded that this thread is still going on after six weeks.<br><br>This is NOT a bug.  This is the way binary floating point has always worked.  It's just that simple.  If you plan to use binary floating point arithmetic, it is your responsibility to understand how to do so safely.  It CAN be done safely, but you need to be aware of its inherent limitations.<br><br>Let's say that I assert that 1/3 = 0.33333333.  That assertion is false.  It is true that 1/3 is between 0.33333333 and 0.33333334.  It is true that 1/3 is approximately 0.33333333, but it is not equal to it.  For example, if I multiply 0.33333333 by 3, I get 0.99999999.  That's not 1, even though (1/3) x 3 should be 1.<br><br>However, if I were transported to a planet that used base 3 for all its arithmetic, they would think I was nuts.  In base 3, 1/3 = 0.1, exactly.  No repeating decimals, no approximations.  The value 1/3 can be represented exactly in base 3.  It cannot be represented exactly in base 10.<br><br>What you are seeing is EXACTLY the same issue.  You have set your expectations in base 10, but you are working in base 2.  If your CPU used base 10 floating point, then 10/50 could be represented exactly.  However, it doesn't; it uses base 2.<br><br>&gt;  double delta = ticksMajor / range;    // 0.20000000000000001  (wot tha?)<br><br>That's not correct, either.  The exact value of 10/50 in a 64-bit double, expressed in decimal, is this:<br>    0.20000000000000017763568394002504646778106689453125<br>Your debugger rounds it to 16 places for you.  The next smaller value that a double can represent is this:<br>    0.199999999999999733546474089962430298328399658203125<br>Clearly, the first value is a better approximation.<br><br>&gt; NASA must really laugh when they hear comments like &quot;<i>...computers today have more power than the first space shuttle!</i>&quot;.<br><br>My watch has more computational power than the first space shuttle.<br><br>&gt; I would argue that at least the first Space Shuttle most likely knew how to divide properly.<br><br>You would be completely wrong.  The IBM AP-101B processors in the Space Shuttle were specially modified to do floating point division to match the results from the IBM 360 mainframes (which had been used to do simulations), which did the same kind of division you see here.  And, actually, a revision of the AP-101 broke division so badly that they had to modify their compiler to substitute integer division.<br><br>Your CPU does know how to divide properly.  It divides in base 2.  As long as you understand that, you can get the results you need.  As soon as you start thinking that it divides in base 10, you will get incorrect results.<br><br>- Tim<br><hr size="1" align="left" width="25%">Tim Roberts, DDK MVPWed, 24 Sep 2008 18:40:44 Z2008-09-24T18:40:44Zhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#52eb6cd8-dfba-43c4-a5bc-79023b27e48dhttp://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6b48c8ad-6920-41ba-ba9b-ae2054a35bcf#52eb6cd8-dfba-43c4-a5bc-79023b27e48dchong kyong kimhttp://social.msdn.microsoft.com/Profile/en-US/?user=chong%20kyong%20kimA very serious bug in MS Visual C++ Hi<br>It is not really a bug!  it has something to do with the way that a computer is capable of storing a real number!!  A computer can not deal with realy numbers exactly!!  Get a computer maths book to get it explained to you.  The computer has no problem with integers so what I suggest you to do is that you do all the arithmetics with integers and then at the last moment convert them back to real numbers.  The result will give you  small error but it is bearable!  To understand this, I recommend you to read some books about Nmerical Anaysis!<br>Best regards<br>ChongFri, 26 Sep 2008 03:21:05 Z2008-09-26T03:21:05Z