Maestro13
Maestro13

Reputation: 3696

Use Biginteger in VB

Where to get Biginteger modules and how import them into visual basic that is part of ms access?

I wish to work with (very) large integers in visual basic and avoid the integer overflows. Reportedly there are Biginteger modules that can be used for this - but where to get them and which one to use? And, then how to import them into my VB environment?

Upvotes: 0

Views: 2523

Answers (1)

June7
June7

Reputation: 21370

I found some code for this a very long time ago, don't remember where from and haven't run it in very long while, but here it is:

Option Compare Database
Option Explicit
Public cDecMax As Variant, cDecMaxLen As Integer, cSqrDecMaxLen As Integer   

Function LargeAdd(ByVal Nbr1 As String, ByVal Nbr2 As String) As String
    Initialize
    If Len(Nbr1) <= cDecMaxLen And Len(Nbr2) <= cDecMaxLen Then
        LargeAdd = CStr(CDec(Nbr1) + CDec(Nbr2))
        Exit Function
        End If
    If Len(Nbr1) > cDecMaxLen Then LargeAdd = addByParts(Nbr1, Nbr2) _
    Else LargeAdd = addByParts(Nbr2, Nbr1)
End Function

Function LargeMult(ByVal Nbr1 As String, ByVal Nbr2 As String) As String
    Initialize
    If Len(Nbr1) <= cSqrDecMaxLen And Len(Nbr2) <= cSqrDecMaxLen Then
        LargeMult = CStr(CDec(Nbr1) * CDec(Nbr2))
        Exit Function
    End If
    If Len(Nbr1) > cSqrDecMaxLen Then
        LargeMult = factorOneNbr(Nbr1, Nbr2)
    Else
        LargeMult = factorOneNbr(Nbr2, Nbr1)
    End If
End Function

Public Sub Initialize()
    Static Initialized As Boolean
    If Initialized Then Exit Sub
    Initialized = True
    cDecMax = _
        CDec(Replace("79,228,162,514,264,337,593,543,950,335", ",", ""))
            'this is 2^96-1
    cDecMaxLen = Len(cDecMax) - 1
    cSqrDecMaxLen = cDecMaxLen \ 2
End Sub

Private Function addByParts(ByVal Nbr1 As String, ByVal Nbr2 As String) As String
    Dim NbrChunks As Integer
    If Len(Nbr1) > Len(Nbr2) Then _
        Nbr2 = String(Len(Nbr1) - Len(Nbr2), "0") & Nbr2 _
    Else _
        Nbr1 = String(Len(Nbr2) - Len(Nbr1), "0") & Nbr1
    NbrChunks = Ceil(Len(Nbr1) / cDecMaxLen)
    Dim i As Integer, OverflowDigit As String, Rslt As String
    OverflowDigit = "0"
    For i = NbrChunks - 1 To 0 Step -1
        Dim Nbr1Part As String
        Nbr1Part = Mid(Nbr1, i * cDecMaxLen + 1, cDecMaxLen)
        Rslt = CStr(CDec(Nbr1Part) _
            + CDec(Mid(Nbr2, i * cDecMaxLen + 1, cDecMaxLen)) _
            + CDec(OverflowDigit))
        If Len(Rslt) < Len(Nbr1Part) Then
            Rslt = String(Len(Nbr1Part) - Len(Rslt), "0") & Rslt
            OverflowDigit = "0"
        ElseIf i = 0 Then
        ElseIf Len(Rslt) > Len(Nbr1Part) Then
            OverflowDigit = Left(Rslt, 1): Rslt = Right(Rslt, Len(Rslt) - 1)
        Else
            OverflowDigit = "0"
            End If
        addByParts = Rslt & addByParts
    Next i
End Function

Private Function factorOneNbr(ByVal LargeNbr As String, ByVal Nbr2 As String) As String
    Dim NbrChunks As Integer, i As Integer, _
        Nbr1Part As String, PowersOf10 As Integer, _
        Rslt As String, FinalRslt As String
    FinalRslt = "0"
    NbrChunks = Ceil(Len(LargeNbr) / cSqrDecMaxLen) - 1
    For i = NbrChunks To 0 Step -1
        Nbr1Part = Mid(LargeNbr, i * cSqrDecMaxLen + 1, cSqrDecMaxLen)
        Rslt = LargeMult(Nbr1Part, Nbr2)
        FinalRslt = LargeAdd(FinalRslt, Rslt & String(PowersOf10, "0"))
        PowersOf10 = PowersOf10 + Len(Nbr1Part)
    Next i
    factorOneNbr = FinalRslt
End Function

Function Ceil(x As Single) As Long
    If x < 0 Then Ceil = Fix(x) Else Ceil = -Int(-x)
End Function

Also just found this http://www.excel-ticker.com/calculation-of-very-large-numbers-in-excel-part-4-exponentiation/

Upvotes: 2

Related Questions