Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 601
Eric Avatar asked Oct 24 '08 22:10

Eric


People also ask

How do I compare two values in Excel VBA?

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.

How do I compare two strings in VBA?

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.

What is double in VBA?

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.


1 Answers

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.

like image 181
Rob Walker Avatar answered Oct 05 '22 06:10

Rob Walker