0

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?

Maestro13
  • 3,656
  • 8
  • 42
  • 72
  • How big of a number is it? for example long = -2,147,483,647 to 2,147,483,647. After some googling found this....https://visualstudiomagazine.com/articles/2011/01/25/biginteger.aspx – BobSki May 23 '17 at 18:59
  • Can System.Numerics from vb.net be imported / linked into vb in MS access (or Excel) in Office 2016 somehow? – Maestro13 May 23 '17 at 20:52
  • How big is the number – BobSki May 23 '17 at 20:56
  • @BobSki as long as it takes - but I will probably not go beyond 50 digits. In decimal notation that is, so $10^50-1$ is my max. – Maestro13 May 24 '17 at 04:54
  • If your question is about MS Access macro scripting use the VBA tag. VB6 is not VBA. – Bob77 May 24 '17 at 10:17

1 Answers1

2

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/

June7
  • 19,874
  • 8
  • 24
  • 34
  • Still works and useful enough - so I will try things out! Thanks. – Maestro13 May 23 '17 at 19:35
  • I changed the code to suit my standards and enhanced with subtraction - what you supplied was a good lead - thx – Maestro13 Jun 08 '17 at 17:39
  • Original source is http://www.tushar-mehta.com/misc_tutorials/project_euler/LargeNumberArithmetic.htm ; unfortunately this solution lacks division, or multiplication by decimal. – jumpjack May 28 '20 at 16:23