2 pages: 1 2
 How to check if two double values are equal in C# Options
codeling
 Posted: Thursday, January 12, 2017 11:34:04 PM

 Rank: Advanced Member Groups: Member Joined: 12/11/2015 Posts: 978 Points: 4113

### Question

In the following C# code, x, a  double variable, gets assigned a value of 0.1, the 'if' statement compares x with 0.1:

if(x==0.1)

{

----

}

Unfortunately it does not enter the if statement, why does this hppen?

 Posted: Thursday, January 12, 2017 11:34:04 PM
codeling
 Posted: Thursday, January 12, 2017 11:50:02 PM

 Rank: Advanced Member Groups: Member Joined: 12/11/2015 Posts: 978 Points: 4113

It's a standard problem due to how the computer stores floating point values: Float/double are stored as binary fractions, not decimal fractions.  The following examples illustrate the what happens in computre:

12.34 in decimal notation (what we use) means 1*101+2*100+3*10-1+4*10-2.

The computer stores floating point numbers in the same way, except it uses base 2:  the base 10 number 2.25 can be convertd to base 2 number 10.01, which means 1*21+0*20+0*2-1+1*2-2

Now, you probably know that there are some numbers that cannot be represented fully with our decimal notation. For example, 1/3 in decimal notation is 0.3333333... The same thing happens in binary notation, except that the numbers that cannot be represented precisely are different. Among them is the number 1/10. In binary notation that is 0.000110011001100...

In summary, a float/double can't store 0.1 precisely. It will always be a little off.

You can try using the decimal type which stores numbers in decimal notation. Thus 0.1 will be representable precisely.

Since the binary notation cannot store it precisely, it is stored in a rounded-off way. Hence your problem.

codeling
 Posted: Thursday, January 12, 2017 11:52:10 PM

 Rank: Advanced Member Groups: Member Joined: 12/11/2015 Posts: 978 Points: 4113

1/10 = 1/16 + 1/32 + 1/256 + 1/512+ 1/4096 + 1/8192 + ....

1/10 = 2-4 + 2-5 + 2-8 + 2-9 + 2-12 + 2-13 +....

codeling
 Posted: Thursday, January 12, 2017 11:59:49 PM

 Rank: Advanced Member Groups: Member Joined: 12/11/2015 Posts: 978 Points: 4113

double and Double are the same (double is an alias for Double) and can be used interchangeably.

The problem with comparing a double with another value is that doubles are approximate values, not exact values. So when you set x to 0.1 it may in reality be stored as 0.100000001 or something like that.

Instead of checking for equality, you should check that the difference is less than a defined minimum difference (tolerance). Something like:

if (Math.Abs(x - 0.1) < 0.0000001)

{

...

}

Users browsing this topic
Guest
 2 pages: 1 2

 Forum Jump Digcode.com - Digcode Announcements - Suggestions and Comments Software Engineering - Domain and Requirements Analysis - Object-Oriented Analysis and Design - Software Development Methodologies Development and Programming - C# and .NET - C and C++ - HTML, CSS, Javascript, jQuery and PHP - XML Technologies - SQL Programming System and Application Softwares - Microsoft Windows, Azure and Office Suite - Application Development Platforms --- Microsoft Visual Studio & Team Foundation Server --- Microsoft SharePoint Server - Database Servers --- Oracle Database --- Microsoft SQL Server - Internet Browsers --- Internet Exploer --- Microsoft Edge --- Google Chrome --- Mozilla Firefox - Tools and Utilites IT News, Careers and Deals - IT News - IT Careers and Jobs - IT Deals and Coupons You cannot post new topics in this forum. You cannot reply to topics in this forum. You cannot delete your posts in this forum. You cannot edit your posts in this forum. You cannot create polls in this forum. You cannot vote in polls in this forum.