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?
VBA has a built in function called StrComp, which can compare two separate strings. This function returns an integer based on the result of the comparison. Zero '0' means a perfect match, and the sample code we give below highlights the cell if the result is NOT equal to 0.
Now, follow the below steps to compare strings in VBA. Step 1: Define sub-procedure which can hold your macro. Step 2: Define a variable Result as String so that we can assign a value of StrComp function to it. Step 3: Now use the Assignment operator to assign the value of StrComp to the variable named Result.
A double data type is one of the data types that allows for decimals, as opposed to the integer data type. It's not necessary to always declare the data type. Sometimes, it's sufficient to declare the name, and VBA can infer the data type when the variable is used in the code later on.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With