问题描述
我无法在 Excel VBA 中比较 2 个双精度值
I have trouble comparing 2 double in Excel VBA
假设我有以下代码
Dim a as double
Dim b as double
a = 0.15
b = 0.01
对 b 进行几次操作后,b 现在等于 0.6
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?
推荐答案
您无法比较浮点值是否相等.请参阅有关比较浮点数的文章" 讨论如何处理内在错误.
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.
这篇关于在 VBA 精度问题中比较双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!