(10 * 1.11 = 11.1)评估为FALSE.如何纠正 [英] (10*1.11=11.1) evaluates as FALSE. How to correct it

查看:88
本文介绍了(10 * 1.11 = 11.1)评估为FALSE.如何纠正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(10 * 1.11 = 11.1)评估为FALSE.

(10*1.11=11.1) evaluates as FALSE.

a = 10
b = 1.11
c = 11.1
' (mathematically: 10*1.11=11.1)
debug.print a*b = c

我得到False(不正确)而不是True(正确).
我知道发生这种情况是因为二进制的计数方式,因此我必须忍受它.

I get False (incorrect) rather than True (correct).
I know this happens because of the binary way of counting and therefore I have to live with it.

使其正常工作的最佳方法是什么?
将数字与小数进行比较时,以下方法是否是最佳解决方案?可靠吗?

What is the best way to make it work correctly?
Is the following the best solution when comparing numbers with decimals? Is it reliable?

round(a*b,2) = round(c,2) 

推荐答案

涉及到.1时,浮点运算几乎是不正确的.原因是在n的16位数字之内不能将.1表示为1/2^n.

Floating point arithmetics is almost never correct, when .1 is involved. The reason is that .1 cannot be represented as a 1/2^n within 16 digits of n.

一些数字,使用浮点数不会出现问题,因为它们可以表示为1/2^n:

These are a few numbers, with which there would not exist problems in floating point, because they can be represented as 1/2^n:

  • 0.125 = 1/2 ^ 3
  • 0.5 = 1/2 ^ 1
  • 0.375 = 1/2 ^ 2 + 1/2 ^ 3

因此,

Thus, the type decimal comes to solve the problem. This is the best way:

Sub TestMe()

    Dim a, b, c 'declaring as Variant, as Decimal cannot be declared in VBA

    a = CDec(10)
    b = CDec(1.11)
    c = CDec(11.1)
    Debug.Print a * b = c

End Sub

这篇关于(10 * 1.11 = 11.1)评估为FALSE.如何纠正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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