View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Norman Jones[_2_] Norman Jones[_2_] is offline
external usenet poster
 
Posts: 421
Default Error with a boolean operation

Hi Bob,

From the VBE,

?(-4.4+3.4)=-1.000000000000001
True


?(-4.4+3.4)=-1.0000000000000001
False

See Chip Pearson at:

Rounding Errors In Excel
http://www.cpearson.com/excel/rounding.htm




---
Regards,
Norman


"Bob" wrote in message
...
Hi everyone:

In excel VBA, I have the following code:

Public Function MY_tester(z, w)
On Error GoTo ErrHandler

If Not IsNumeric(z) Then
MY_tester = "Error: Z must be a number"
ElseIf Not IsNumeric(w) Then
MY_tester = "Error: w must be a number"
ElseIf ((z + w) < 0) And ((z + w) = Fix(z + w)) Then
MY_tester = "Error: z+w cannot be zero nor negative integers"
Else
MY_tester = "Answer OK"
End If

ErrHandler:
If Err Then
MY_tester = "Error: " & Err.Description
End If
End Function

Public Sub MY_SUB()
MsgBox MY_tester(-4.4, 3.4)
End Sub

When I run the sub MY_SUB, the program returns false for ((z + w) = Fix(z
+ w)) , even though it is true (after all, -1 is equal to -1). Does
anyone know why? I think this may be a bug in VBA. I appreciate all your
help.

Bob