比较双精度问题 [英] Compare double in VBA precision problem

查看:148
本文介绍了比较双精度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Excel VBA中比较2 double时遇到问题



假设我有以下代码:

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

在b上进行一些操作之后,b现在等于0.6



然而,与double数据类型相关的不精确性给了我头痛,因为

 如果a = b then 
//这将永远不会触发
end if $ b $你是否知道如何去除双精度型的尾随不精确度?

div class =h2_lin>解决方案

您不能比较浮点值的相等性。请参阅有关比较浮点数,讨论如何处理内在错误。



这不是一个简单的比较一个常量的错误余地,除非你知道肯定什么浮动的绝对范围是事先确定的。


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?

解决方案

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.

这篇关于比较双精度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆