Search code examples
excelvbacomparisondouble

Compare double in VBA precision problem


I have trouble comparing 2 double in Excel VBA

suppose that I have the following code

Dim a as double
Dim b as double
a = 0.15
b = 0.01

After a few manipulations on b, b is now equal to 0.6

however the imprecision related to the double data type gives me headache because

if a = b then
 //this will never trigger
end if

Do you know how I can remove the trailing imprecision on the double type?


Solution

  • You can't compare floating point values for equality. See this article on "Comparing floating point numbers" for a discussion of how to handle the intrinsic error.

    It isn't as simple as comparing to a constant error margin unless you know for sure what the absolute range of the floats is beforehand.