Click here to Skip to main content
15,887,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using VB.Net to compare two numbers upto 4 decimal places.

Dim Num1 as Double = 12.34562
Dim Num2 as Double = 12.34567

Question (1) Which of these methods would be correct to use:

''------------------------------------------

If Math.Round(Num1, 4) >= Math.round(Num2, 4) Then
...
End If

''----- OR -----

Private AccuracyValue As Double = 0.0001

If Math.Abs(Num1 - Num2) <= AccuracyValue Then
...
End If

''-------------------------------------------

Else, what should be the correct way to compare two number within a accuracy range ?

Question (2) What if the two numbers are
12.3456789 and
12.3456782

where the decimal part is much larger than the required accuracy.
Will using Math.Round be safe ?

What I have tried:

If Math.Round(Num1, 4) >= Math.round(Num2, 4) Then
...
End If
Posted
Updated 26-Mar-17 21:45pm

1 solution

1) Comparing the absolute of the difference with a quantity value (often denoted ε - small greek letter epsilon) is the common method:
diff = num1 - num2
if (abs(diff) <= epsilon)
    # numbers are similar
else
    # may use sign of diff here to check if larger or smaller
This method is also much faster than comparing rounded values.

2)
Additional digits are ignored. With your example, both values will be rounded to 12.3457.
 
Share this answer
 
Comments
Tushar Suradkar 31-Mar-17 4:13am    
Thank you Jochen for looking into.
That cleared many doubts and gave critical insight.

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