-3

Hello I´m using Microsoft VB 2012 and have a problem with sum of doubles.

This simple code reproduces it:

Dim result As Double = 0
Dim amount1 As Double = 81.83
Dim amount2 As Double = 50.0
result += amount1
result += amount2

result is equal to 131.82999999998 instead of 131.83

In fact:

Dim test1 As Double = 81.83 + 50.0   '(= 131.82999999998 wrong)
Dim test2 As Double = 81.83 + 50     '(= 131.82999999998 wrong)
Dim test3 As Double = 81.83 + 10     '(=  91.83 OK)
Dim test4 As Double = 81.82 + 50.0   '(=  131.82 OK)

Can anyone explain it?

Best Regards David

  • 3
    [what every developer should know about floating point](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=what%20every%20developer%20should%20know%20about%20floating%20point) – crashmstr Jun 27 '14 at 11:40
  • 2
    This one is probably a duplicate of about a hundred thousand questions. If you are dealing with currency, don't use floating point, use integer values (cents rather than dollars for instance). – jcaron Jun 27 '14 at 11:40

1 Answers1

-1

A double value can only represent certain numbers exactly. Most values can only be approximated. See Wikipedia for some more detailed explanation. You basically have a fixed number of decimal digits, depending on the size of the floating point number (compare Single and Double) and an exponent that represents the number. Due to this even simple calculations may produce some rounding errors.

To avoid this you can use a datatype like Decimal that is represented exactly and is often used for financial calculation for example to avoid these errors.

Jens
  • 6,275
  • 2
  • 25
  • 51
  • 4
    A double value *is* exact. It's just not exactly the same as the decimal representation provided to start with. Is a `Decimal` value "not exact", just because it can't represent 1/3 exactly? – Jon Skeet Jun 27 '14 at 11:44
  • You are probably right about the semantics. But in the meantime what matters to him is that a lot of floating point calculations with Single or Double types will lead to rounding errors that can often be avoided by using Decimal. – Jens Jun 27 '14 at 12:09
  • That much is fine, yes - so why start it off with an incorrect statement? It's important to understand that floating binary point types represent exact values, just a different set of exact values. – Jon Skeet Jun 27 '14 at 12:12
  • I understood my sentence differently while writing but I see that it wasn't really correct after you pointed it out and that it could lead to confusion. I changed it. – Jens Jun 27 '14 at 12:33
  • Better - but saying you have a fixed number of *decimal* digits sort of misses the point. There's a fixed number of *binary* digits and an exponent. And the problem isn't that "simple calculations may produce some rounding errors" - that's the case with `Decimal` too - the problem is that decimal values (e.g. 81.83 in the question) can't be represented exactly. See http://csharpindepth.com/Articles/General/FloatingPoint.aspx for my description of this. – Jon Skeet Jun 27 '14 at 12:37