双数据类型编号错误 [英] Double data type numbers error

查看:96
本文介绍了双数据类型编号错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用VB或C#或VBA进行校准时

0.43 + 0.000001 - 0.430001

结果不会为零!为什么?

请试一试!



我的尝试:



When i try to cal in VB or C# or VBA
0.43 + 0.000001 - 0.430001
The result will not be zero! Why?
Please try it!

What I have tried:

Sub Test()
    Dim V As Double
    V = 0.43 + 0.000001 - 0.430001
    If V <> 0 Then
        MsgBox "Why we goes hear?"
    End If
End Sub

推荐答案

双数是数字的近似值,而不是确切的数字,因此在不需要精度的情况下不应使用它们,因为你会遇到这些问题。
Doubles are an approximation of the numbers, not the exact numbers so they shouldn't be used where precision is required as you'll get these kinds of issues.


您遇到了浮点算术精度问题。

请查看每个计算机科学家应该知道的关于浮点算术的内容 [ ^ ]



顺便提一下,有一个e来自John Simmons / outlaw程序员的优秀文章,关于CP;它提供了一种在浮点值之间的相等比较中规避这个问题的方法。那里:

可靠的浮点平等比较 [ ^ ]



由于本文中的代码适用于C ++,下面是我在C#中使用的扩展方法,用于所有与浮点值类型的相等比较:

You are facing an issue with floating-point arithmetic precision.
Please have a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic[^]

By the way, there exists an excellent article from John Simmons / outlaw programmer here on CP; it presents a way to circumvent this problem in equality comparisons between floating-point values. There:
Reliable Floating Point Equality Comparison[^]

As the code in this article is for C++, here is the extension method I'm using in C# for all equality comparisons with floating-point valuetypes:
public static bool AlmostEquals(this double value, double other, double epsilon = 1e-15)
{
   return (((other - epsilon) < value) && (value < (other + epsilon)));
}



这是基于B. Clay Shannon在同一篇文章中的评论。



适用于您的案例的此代码段的使用可能是:


It is based on the comment from B. Clay Shannon in the same article.

Usage of this snippet applied to your case could be:

Sub Test()
    Dim V As Double
    V = 0.43 + 0.000001 - 0.430001
    If Not V.AlmostEquals(0) Then
        MsgBox "Why we goes hear?"
    End If
End Sub


使用十进制而不是Double。



如果必须使用Double,请避免使用相等/不等式比较。为您的应用选择精度。作为六位小数精度的示例,将if(x<> 0.0)替换为if(Abs(x)< 0.000001)。
Use Decimal instead of Double.

If you have to use Double, avoid equality / inequality comparisons. Pick a precision for your application. As an example of six decimals precision, replace "if (x <> 0.0)" with "if ( Abs(x) < 0.000001)".


这篇关于双数据类型编号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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