Search code examples
androidvariablesbasic4android

long - big numbers in map don't calc as should


i am using big numbers (18 digits), so i store it into long variables. I have found some misbehavior in my calculations, hence i made a simple test program.

The calculation does not work as it should with big numbers... More precise it is linked to the Map i use for storing. Any suggestions?

    'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim b1,b2,b3 As Button
    Dim MapData As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
    b1.Initialize("Set100")
    b1.Text="Set100"
    Activity.AddView(b1,10%x,10%y,80%x,20%y)
    b2.Initialize("SetBig")
    b2.Text="SetBig"
    Activity.AddView(b2,10%x,40%y,80%x,20%y)
    b3.Initialize("Increase")
    b3.Text="long++"
    Activity.AddView(b3,10%x,70%y,80%x,20%y)
    MapData.Initialize
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Set100_Click
    Dim l As Long
    l = 100
    Log("set to: " & l)
    MapData.Put("Token",l)
End Sub

Sub SetBig_Click
    ' long range: -9223372036854775808 to 9223372036854775807
    Dim l As Long
    l = 640345893847300380
    Log("set to: " & l)
    MapData.Put("Token",l)
End Sub

Sub Increase_Click
    Dim l As Long
    l = MapData.Get("Token")
    Log("from map: " & l)
    l = l + 1
    Log("new val : " & l)
    MapData.Put("Token",l)
End Sub

Solution

  • You are correct. There is a bug in the conversion from an unknown object to Long which causes this error. This bug will be fixed in the next update. In the meantime you can create a custom type and store it instead.

    Sub Globals
        'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.
        Dim b1,b2,b3 As Button
        Dim MapData As Map
        Type MyLong(l As Long)
    End Sub
    
    Sub Activity_Create(FirstTime As Boolean)
        b1.Initialize("Set100")
        b1.Text="Set100"
        Activity.AddView(b1,10%x,10%y,80%x,20%y)
        b2.Initialize("SetBig")
        b2.Text="SetBig"
        Activity.AddView(b2,10%x,40%y,80%x,20%y)
        b3.Initialize("Increase")
        b3.Text="long++"
        Activity.AddView(b3,10%x,70%y,80%x,20%y)
        MapData.Initialize
    End Sub
    
    Sub Activity_Resume
    
    End Sub
    
    Sub Activity_Pause (UserClosed As Boolean)
    
    End Sub
    
    Sub Set100_Click
        Dim l As Long
        l = 100
        Log("set to: " & l)
        MapData.Put("Token",l)
    
    End Sub
    
    Sub SetBig_Click
        ' long range: -9223372036854775808 to 9223372036854775807
        Dim l As MyLong
        l.l = 640345893847300380
        Log("set to: " & l)
        MapData.Put("Token",l)
    End Sub
    
    Sub Increase_Click
        Log(MapData.Get("Token"))
        Dim l As MyLong
        l = MapData.Get("Token")
        l.l = l.l + 1
        'If you want to create a copy of the original MyLong object:
        Dim l2 As MyLong
        l2.l = l.l
        Log("new val : " & l.l)
        MapData.Put("Token",l2)
    End Sub