Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Addition of Double values inconsistent

Tags:

c#

.net

vb.net

I came across following issue while developing some engineering rule value engine using eval(...) implementation.

    Dim first As Double = 1.1
    Dim second As Double = 2.2
    Dim sum As Double = first + second
     If (sum = 3.3) Then
        Console.WriteLine("Matched")
    Else
        Console.WriteLine("Not Matched")
    End If

'Above condition returns false because sum's value is 3.3000000000000003 instead of 3.3

It looks like 15th digit is round-tripped. Someone may give better explanation on this pls.

Is Math.Round(...) only solution available OR there is something else also I can attempt?

like image 347
StartingFromScratch Avatar asked Dec 21 '22 14:12

StartingFromScratch


1 Answers

You are not adding decimals - you are adding up doubles.

Not all doubles can be represented accurately in a computer, hence the error. I suggest reading this article for background (What Every Computer Scientist Should Know About Floating-Point Arithmetic).

Use the Decimal type instead, it doesn't suffer from these issues.

Dim first As Decimal = 1.1
Dim second As Decimal = 2.2
Dim sum As Decimal= first + second
 If (sum = 3.3) Then
    Console.WriteLine("Matched")
Else
    Console.WriteLine("Not Matched")
End If
like image 131
Oded Avatar answered Jan 05 '23 11:01

Oded