简体   繁体   English

大于等于计算不正确

[英]Greater than and equal to evaluates incorrectly

comparison operation, ">=" statement in "If Then" loop evaluates correctly first 2-3 times, then incorrectly, then correctly in last loop of a For Next outer loop.比较操作,“If Then”循环中的“>=”语句首先正确评估 2-3 次,然后不正确,然后在 For Next 外循环的最后一个循环中正确评估。 Variables that are comparing are in an array and have correct values when I debug step-by-step through the loop.当我通过循环逐步调试时,正在比较的变量在一个数组中并且具有正确的值。 I am baffled why this is occuring.我很困惑为什么会这样。

thought of changing data type for array variable but I think I have to use "variant" for Excel VBA arrays.考虑过更改数组变量的数据类型,但我认为我必须对 Excel VBA 数组使用“变体”。

'Calculate Deviance

    S3 = 0
    For i = 1 To N
        S4 = 0
        If A(i, 4) <= 0 Then
            S3 = S3 + S4
            S4 = 0
        Else
            E2 = A(i, 5) * X
            If E2 < E1 Then
                E2 = Exp(-E2)
                S4 = A(i, 4) * Log(A(i, 4) / (A(i, 3) * (1 - E2)))
                S3 = S3 + S4
                S4 = 0
            Else
                E2 = 0
                S4 = A(i, 4) * Log(A(i, 4) / (A(i, 3) * (1 - E2)))
                S3 = S3 + S4
                S4 = 0
            End If
        End If

        If A(i, 4) >= A(i, 3) Then 'problem is right here. this statement.!
            S3 = S3 + S4
        Else
            S4 = A(i, 3) - A(i, 4)
            S4 = S4 * (Log(S4 / A(i, 3)) + A(i, 5) * X)
            S3 = S3 + S4
        End If
    Next i

'Array input
'   1   2   3   4   5   6   7
'a(i,1) 0.25    0.25    0.5 0.5 1   1   1
'a(i,2) 1   0.1 0.01    0.001   0.001   0.0001  0.00001
'a(I,3) 10  10  8   10  12  12  12
'a(i,4) 10  10  8   5   7   2   0
'a(i,5) a(i,1)*a(i,2)
'N=7                    

It is probably a floating point error, because computers cannot represent numbers accurately in fixed space.很可能是浮点数错误,因为计算机不能在固定空间内准确表示数字。 However normally this error is very small and does not form a problem.然而通常这个错误很小并且不会形成问题。 For more reading visit:https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html如需更多阅读,请访问:https ://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

The following example shows the floating point error: The sub calculates the square root of 2, n times and calculates the total sum in 2 different ways.下面的例子展示了浮点数错误:子程序计算2的平方根,n次,并以2种不同的方式计算总和。

sub FloatingPointError()
Dim x As Double, y As Double, n As Integer
x = 0
n = 100
y = n * (2 ^ 0.5) 'expected result is 1,4142135623731 * 100 = 141,42135623731

    For i = 1 To n
        x = x + (2 ^ 0.5) 'Calculate square root of 2 and add to the previous value
    Next i

Debug.Print "value of x:            "; x
Debug.Print "value of n * sqrt(2):  "; n * (2 ^ 0.5)

    If y - x = 0 Then
        Debug.Print "They are equal"
        Else
        Debug.Print "They are not equal"
    End If

End Sub

The output of the sub shows: sub 的输出显示:

value of x:             141,421356237309 
value of n * sqrt(2):   141,42135623731 
They are not equal

However the local variable tab can sometimes show that the numbers are equal because they are rounded.但是,局部变量选项卡有时会显示数字相等,因为它们是四舍五入的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM