Ok, I’m SURE I’m missing something VERY obvious here, the problem is: I’m not getting what…
Please help, I’m about to tear out most of my hair!
I wrote this agent to test a problem a user was having:
Sub Initialize
On Error Goto PrintError
Dim s As New NotesSession
Dim mylog As NotesLog
Dim x As Double
Dim y As Double
Set mylog = s.createlog("WTF")
Call mylog.openagentlog
x = 4.08 + 18.84 + 18.84 + 18.84 + 18.84 + 20.56
y = 4.11 + 18.84 + 18.84 + 18.84 + 18.84 + 20.53
Call mylog.logaction("--")
If (x <> 100) Then
Call mylog.logaction("X IS NOT 100, BUT " & Cstr(x))
Else
Call mylog.logaction("X IS 100 !")
End If
If (y <> 100) Then
Call mylog.logaction("Y IS NOT 100, BUT " & Cstr(y))
Else
Call mylog.logaction("Y IS 100 !")
End If
Call mylog.logaction("--")
Call mylog.close
Exit Sub
PrintError:
Call mylog.logerror(Err(),Erl() & ": " & Error())
Exit Sub
End Sub
If you just read it like this, you would think the output would be for the log to report both instances (x & y) being 100. But what do I see?
Started running agent ‘wtf’ on 06/04/2005 10:56:52
06/04/2005 10:56:52: –
06/04/2005 10:56:52: X IS NOT 100, BUT 100
06/04/2005 10:56:52: Y IS 100 !
06/04/2005 10:56:52: –
Ran LotusScript code
Done running agent ‘wtf’ on 06/04/2005 10:56:52
“X IS NOT 100, BUT 100” ?! What the…
Even in the debugger I can clearly see X as a Double with 100 as value. Help! (I just feel I’m going to smack my head against the wall after hearing the explanation)
Thanks in advance for those pointing me in the right direction!
Subject: I’m going bananas… 100 does not equal 100 ?
There are many posts out on the web about this sort of problem, but it comes down to the fact that floating point numbers can’t be represented exactly by binary computers. Different languages/computers/operating systems handle the details slightly differently, but it is not a bug in LotusScript or Formula language, but an inherent issue with floating point values. The answer is to round the number and check for that, or compare within a degree of precision. For example, here is a post about SAS which relates to the problem, and here is another about C/C++, and one about Excel 97. From that last link, there is this quote:
The second problem arises from the fact that a computer, any computer, cannot store most fractional numbers with total accuracy. Computers, in general, use the IEEE (Institute Of Electrical And Electronic Engineers) standard for floating point numbers. This standard provides a way to store fractional numbers in the limited space of an 8-byte number. Of course, for most numbers, some approximation must be made.