Home » excel » excel – How to debug function values

excel – How to debug function values

Posted by: admin May 14, 2020 Leave a comment

Questions:

Trying to understan what is wrong in function? but cant find what is wrong

Sub test()
Dim a as double, b as double
    a = "10,31" 
    b = "10,31"
Msgbox check(a,b)
End Sub

Function check(a as double, b as double)
    ff = TypeName(a) ' double
    gg = TypeName(b) ' double
    If a= b Then GoodExVat = True Else GoodExVat = False: diffExVAt = b - a ' here a = 10,31 b=10,31 diffExVAt = -3.5527136788005-15
check = diffExVAt 
End Function

if a and b seted as 10,31 everything is ok, but when 10,31 comes from other function or expression then i got -3.5527136788005-15

What is that? It is not first time i got same thing. What is wrong with Excel?

How to&Answers:

Do not use : in ItElse, it is a bit confusing. If you write it normally, it should work ok:

Sub Test()
    Dim a As Double, b As Double
    a = "10,31"
    b = "10,31"
    MsgBox check(a, b)
End Sub

Function check(a As Double, b As Double)
    ff = TypeName(a)    ' double
    gg = TypeName(b)    ' double
    If a = b Then
        GoodExVat = True
    Else
        GoodExVat = False
    End If

    diffExVAt = b - a    ' here a = 10,31 b=10,31 diffExVAt = -3.5527136788005-15
    check = diffExVAt

End Function

And concerning debugging – simply use F8 and the Step-by-step method.


Concerning the 10,31 coming from other function and expression and getting a -3.55...-15, then the problem is in the floating point mathematics: