Search code examples
libreofficebasic

Operate with large numbers in LibreOffice BASIC


I'm trying to operate the MOD of a number but it returns overflow data type.

This operation works:

VariableDouble = 2147483647 - (97 * (2147483647 \ 97))
MsgBox VariableDouble

This operation returns error:

VariableDouble = 2147483648 - (97 * (2147483648 \ 97))
MsgBox VariableDouble

Shouldn't be Double data type larger than Long one? Of course, variable declaration is as follows: Dim VariableDouble As Double.

It happens the same if I use the built-in operator MOD: a MOD b.

I need to do operations with 10 digits numbers. Is there a way I can do this using BASIC? What is happening?


Solution

  • I found a solution making my own MOD implementation [partially] with an iterative bucle. It works —I didn't test it using numbers with a lenght greater than 50 digits, though—, so this is the code:

    Dim LargeNumber As Double
    Dim result As Integer
    Dim dividend As Integer
    Dim index As Integer
    
    For index = 1 To Len(LargeNumber)
        dividend = result & Mid(LargeNumber, index, 1)
        result = dividend Mod 97
    Next
    

    It seems BASIC can't operate with large numbers, despite it can save them into variables. An another solution would be to call Python externally (API can do that). Python is awesome, however I need to keep the portability of this database for several systems (some use Windows OS so won't have Python preinstalled).