Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When i try to cal in VB or C# or VBA
0.43 + 0.000001 - 0.430001
The result will not be zero! Why?
Please try it!

What I have tried:

VB
Sub Test()
    Dim V As Double
    V = 0.43 + 0.000001 - 0.430001
    If V <> 0 Then
        MsgBox "Why we goes hear?"
    End If
End Sub
Posted
Updated 27-Apr-16 6:35am
Comments
ZurdoDev 27-Apr-16 12:05pm    
The Precision of each number not the same so you get these weird issues.
RedDk 27-Apr-16 13:09pm    
As all the solutions are correct, I'd pick the most right, which would be ppolymorphes. Because at the end of the day, all "relevanting" calculations are accumulatively absolute. Programmers do indeed use floating point even though they know they are accumulating error but using it consistantly throughout their system makes it ... ok.
phil.o 27-Apr-16 16:16pm    
I updated my answer with interesting follow-ups and solution.

Doubles are an approximation of the numbers, not the exact numbers so they shouldn't be used where precision is required as you'll get these kinds of issues.
 
Share this answer
 
You are facing an issue with floating-point arithmetic precision.
Please have a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic[^]

[Edit] By the way, there exists an excellent article from John Simmons / outlaw programmer here on CP; it presents a way to circumvent this problem in equality comparisons between floating-point values. There:
Reliable Floating Point Equality Comparison[^]

As the code in this article is for C++, here is the extension method I'm using in C# for all equality comparisons with floating-point valuetypes:
C#
public static bool AlmostEquals(this double value, double other, double epsilon = 1e-15)
{
   return (((other - epsilon) < value) && (value < (other + epsilon)));
}

It is based on the comment from B. Clay Shannon in the same article.

Usage of this snippet applied to your case could be:
VB
Sub Test()
    Dim V As Double
    V = 0.43 + 0.000001 - 0.430001
    If Not V.AlmostEquals(0) Then
        MsgBox "Why we goes hear?"
    End If
End Sub
 
Share this answer
 
v2
Use Decimal instead of Double.

If you have to use Double, avoid equality / inequality comparisons. Pick a precision for your application. As an example of six decimals precision, replace "if (x <> 0.0)" with "if ( Abs(x) < 0.000001)".
 
Share this answer
 
v2
That is what floating point is about.
The downside of floating point is that values are not exact in most cases.
Thus all calcs are accumulating errors.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900