【问题标题】:Excel VBA Double Addition ErrorExcel VBA双重加法错误
【发布时间】:2010-07-08 19:59:37
【问题描述】:

我很难解释这一点。以下函数用作工作表公式。 “空”值仅表示单元格为空,因此没有任何值。给定值 {empty, empty, 0.8, 0.2},以下函数有时会返回像 5.55111512312578E-17 这样的值。在调试器中,看起来一切都是正确的,直到 ParamArray 中的最后一个值(在本例中为 0.2)被处理。有什么想法吗?

   Private Function getOvertimeEP(ParamArray epAllocations() As Variant)
        Dim overtimeEP As Double
        overtimeEP = -1

        For Each nextVal In epAllocations
            overtimeEP = overtimeEP + nextVal
        Next

        If overtimeEP < 0 Then
            overtimeEP = 0
        End If

        getOvertimeEP = overtimeEP
    End Function

【问题讨论】:

    标签: excel double addition vba


    【解决方案1】:

    该错误是floating point accuracy problems 的结果。即使您的前两个值是 0 和 0,它仍然会有相同的结果。 {0.1, 0.2, 0.3, 0.4} 也是如此。

    在返回之前将其四舍五入到合理的小数位数并结束。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 2017-04-15
      • 2016-11-30
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      相关资源
      最近更新 更多