Lotusscript TimeDifference & Long data type – Grrr….

Spent Friday investigating some freakish code problems. This one just blew my mind….
When assigning numbers to a data type what do you expect to happen when you assign a number to big to fit the assigned data type? Right, an overflow error. 

Well explain this then….

Ok, yes I know, you can solve it by using “difference# = notesDateTime.TimeDifferenceDouble( notesDateTime )” method which will return a double. I just can’t get my head around the fact that it didn’t return an error when it overflowed on 2,147,483,647 and instead continued up from it’s negative boundary (-2,147,483,648).
Or is this normal behavior for a Long data type??  I just hate it when I can’t logically explain something…

Code I used to test the effect:

Sub Click(Source As Button)
    Dim todayD As New NotesDateTime(Today)
    Dim AlteredDate As New NotesDateTime(Today)
   
    Set todayD = New NotesDateTime(Today)
    todayD.Localtime = todayD.Dateonly + ” 00:00:00″
   
    Set AlteredDate = New NotesDateTime(Today)
    AlteredDate.Localtime = AlteredDate.Dateonly + ” 00:00:00″
   
    For x = 24850 To 24860
        Set AlteredDate = New NotesDateTime(Today)       
        Call AlteredDate.AdjustDay(-x)
        If TodayD.Timedifference(AlteredDate)<0 Then
            ‘EXPECTED OVERFLOW ERROR AT 24857 NOT HAPPENING
        End If

        Print “TimeDifference between: ” + TodayD.DateOnly + ” and ” +_

        AlteredDate.DateOnly + ” = ” + Cstr(TodayD.Timedifference(AlteredDate)) +_
        ” —- Days: ” + Cstr(x)       
    Next

End Sub



Update: I got this answer back through Twitter: “@FemkeGoedhart normal behavior for int/long. See http://en.m.wikipedia.org/wiki/Integer_overflow” Thanks @Thimo! It explains it, although the logic of Lotusscript returning an error on overflow of Integers but not on Longs still eludes me….

2 comments

  1. Richard Schwartz

    Here's what's really happinging: It is being treated as an 'unsigned long' during arithmetic operations in the NotesDateTime class, which means that the sign bit is not given any special significance (it has twice as many positive values and no negative values), and oveflow detection is disabled. But the LotusScript language does not have an unsigned long data type, CStr in the print operation is just treating it as a regular long, so you see it printed as a negative value.

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

* Copy This Password *

* Type Or Paste Password Here *