Excel VBA Double Addition Error

泄露秘密 提交于 2019-12-20 04:37:23

问题


I'm having a hard time explaining this one. The following function is being used as a worksheet formula. A value of "empty" simply means that the cell was empty and, therefore, there is no value. Given the values {empty, empty, 0.8, 0.2}, the following function is sometimes returning off the wall values like 5.55111512312578E-17. In the debugger, it looks like everything is correct until the last value in the ParamArray (in this case 0.2) is processed. Any thoughts?

   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

回答1:


That error is the result of floating point accuracy problems. Even if your first two values are 0 and 0, it will still have the same result. So will {0.1, 0.2, 0.3, 0.4}.

Round it to some reasonable number of decimal places before returning it and call it a day.



来源:https://stackoverflow.com/questions/3207453/excel-vba-double-addition-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!