在VB中使用Biginteger

时间:2017-05-23 18:40:08

标签: ms-access access-vba biginteger bigint

在哪里获取Biginteger模块以及如何将它们导入到作为ms访问一部分的visual basic中?

我希望在visual basic中使用(非常)大整数并避免整数溢出。据说有Biginteger模块可用于此 - 但在哪里获取它们以及使用哪一个?然后,如何将它们导入我的VB环境?

1 个答案:

答案 0 :(得分:2)

我很久以前就找到了一些这样的代码,不记得从哪里开始并且很长时间没有运行它,但这里是:

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

还找到了这个How do you configure logging in Hibernate 4 to use SLF4J