Solved

Artimetic bug in .Net

Posted on 2006-11-13
7
191 Views
Last Modified: 2010-04-24
It seems that .Net cannot make some simple numerical operations.

Try to write this in the debugger:
587.67 + 146.67

The result should be 734.34, but .Net calculates it to (586.67 + 146.67 = 733.33999999999992 Double)

Can somebody explain to me why?

I've found also other similar bugs but I think that showing one is enough.
0
Comment
Question by:fulgeru99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 10

Expert Comment

by:Kinger247
ID: 17928781
Running console.WriteLine(587.67 + 146.67) produces the right result.

Where are you running this ? and what version of vs are you using ?
0
 

Author Comment

by:fulgeru99
ID: 17928841
Yes, if you write something like this is DISPLAYS correctly:

Sub Main()
    Dim sum As Double = 587.67 + 146.67
    Console.WriteLine(sum)
End Sub

But calling the WriteLine functions actualy calls Double.ToString() that formats the number and that seems to work. However, I am not using the ToString() function. I am calculating a sum in a foreach loop and then I am comparing it to another variable.

The problem is the way .Net internally stores that result.

Make the following checks also:
a) put a breakpoint on the line Console.WriteLine(sum) and add a watch for the variable sum
b) try this piece of code:
 Sub Main()
    Dim sum As Double = 587.67 + 146.67
    Dim damnIt As Boolean = (734.34 = sum)

    Console.WriteLine(damnIt)
  End Sub


0
 
LVL 34

Accepted Solution

by:
Sancler earned 200 total points
ID: 17928931
Unless your numbers are too large, or performance is crucial (and in that case, I am not sure that any of the alternatives - such as

    Dim damnIt As Boolean = (734.34 = sum.ToString)

- would be faster), it sounds like you want to use the Decimal datatype rather than the Double.

From the documentation

>>
The Decimal data type provides the greatest number of significant digits for a number. It supports up to 29 significant digits and can represent values in excess of 7.9228 x 10^28. It is particularly suitable for calculations, such as financial, that require a large number of digits but cannot tolerate rounding errors.

The default value of Decimal is 0.

Programming Tips
Precision. Decimal is not a floating-point data type. The Decimal structure holds a binary integer value, together with a sign bit and an integer scaling factor that specifies what portion of the value is a decimal fraction. Because of this, Decimal numbers have a more precise representation in memory than floating-point types (Single and Double).

Performance. The Decimal data type is the slowest of all the numeric types. You should weigh the importance of precision against performance before choosing a data type.
<<

Roger
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Assisted Solution

by:VoteyDisciple
VoteyDisciple earned 50 total points
ID: 17929336
It sounds like you're just seeing the messy internals of the IEEE-754 floating-point standard.  Just like there are numbers that we can't accurately express in decimal format (like 1/3, for instance) there are numbers that we can't accuraetly express in IEEE-754 notation, which is essentially just a binary decimal format.

Take a look at some of the references/toys here: http://babbage.cs.qc.edu/IEEE-754/

This is definitely not unique to .NET; it's common to how all computers process floating-point numbers.  

As Sancler suggests, the workaround when decimal precison matters the Decimal format is more appropriate.  It makes no attempt to convert your decimal floating-point numbers to binary floating-point numbers, but rather stores each digit separately.  There is, thus, no chance of rounding errors in conversion.
0
 

Author Comment

by:fulgeru99
ID: 17929588
On this example, having the variable Decimal works.
However, I remember that I found a bug in Decimal also.

Anyway, try this: start debugging and add a new watch were you write: 587.674 + 146.67
No data type, just the calculation and see the result.

Moreover, type ?587.674 + 146.67 in the immediate output window.

Can anyone explain why?

I found that there are some special cases on some special sums where the calculation is wrong.
0
 
LVL 6

Expert Comment

by:riyazthad
ID: 17930832
This how this is showing in , but you will lose anything. this is how they storing as bits. in many times, if ur number is ending 1 4 or 8,you may get this lengthy result. But this is NOT true for many cases.

But you use round function, u will not lose anything.

Thad
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question