Excel: 计算一个非常大的数的模而不会出现溢出错误

Excel: calculate modulus of a very large number without getting overflow error

我有一个带 2 的 A1 和一个带 288 的单元格 A2。我想计算 MOD(2^288;2017) 但这给出了 NUM 错误。

我也尝试使用这个公式:=number-(INT(number/divisor)*divisor) 但是当数字太大时,结果为 0。

编辑:不完全重复(请参阅我对excel中函数的回答),我使用了这个算法: How to calculate modulus of large numbers?

Excel 可能会在您的值上强制使用整数上下文。即使是 Int64 也不足以处理那么大的数字。

您可能需要自定义 VBA 函数来处理它。如果您将输入和输出转换为双精度值,然后强制执行模函数,它应该能够进行数学运算。

我的问题是验证输出...我不知道这是否是正确的值。

Public Function BigMod(ByVal numerator As Double, ByVal denominator As Double) As Double

  Dim intval As Double
  intval = Int(numerator / denominator)

  BigMod = numerator - intval * denominator

End Function

这适用于大于 Mod 的值,但最终会中断。

例如,3 后接 5 组 0 将断开 Mod。 3 后跟 6 组 0 将打破 BigMod.

要解决此问题,请在 excel 中添加此功能:alt+f11 -> module -> 添加 并使用 BigMod(2;288;2017) 如果你想计算 2^288 mod 2017

Public Function BigMod(ByVal grondgetal As Double, ByVal exponent As Integer, ByVal modgetal As Double) As Double

  Dim hulp As Integer
  hulp = 1

  Dim i As Integer

    For i = 1 To exponent
        hulp = hulp * grondgetal
        hulp = hulp - Int(hulp / modgetal) * modgetal
    Next i
  BigMod = hulp

End Function

我需要它来处理非常大的数字。如果我取 3,然后是 0 的 7 个部分,上面的示例有效,但如果我继续下去,就会中断。

所以我想出了我将如何在纸上做到这一点,并编写了一个函数来处理它。由于它一次仅适用于一个部分(加上上一节的其余部分),因此它永远不会处理大于 100 万的值。

Public Function AnyMod(ByVal numerator As String, ByVal denominator As Double) As Double
    ' inch worm through the numerator working one section at a time to get the remainder given any size string
    Dim numericalDivider As String
    Dim decimalDivider As String
    Dim sectionLength As Integer

    ' in the US
    numericalDivider = ","
    decimalDivider = "."
    sectionLength = 3

    ' in Europe
    'numericalDivider = "."
    'decimalDivider = ","
    'sectionLength = 3

    ' input cleanup - replace numerical section divider
    numerator = Replace(numerator, numericalDivider, "")

    ' input cleanup - chop any decimal off of it
    If (InStr(1, numerator, decimalDivider)) Then
        numerator = Left(numerator, InStr(1, numerator, decimalDivider) - 1)
    End If

    Dim pos As Integer              ' the next position in numerator to be read
    Dim remainder As Double         ' the remainder of the last division
    Dim subs As String              ' the current section being read
    Dim length As Integer           ' the total length of numerator
    Dim current As Double           ' the current value being worked on
    Dim firstSegment As Integer     ' the length of the first piece

    'set up starting values
    length = Len(numerator)

    firstSegment = length Mod sectionLength
    If firstSegment = 0 Then firstSegment = sectionLength

    remainder = 0
    pos = 1

    'handle the first section
    subs = Mid(numerator, pos, firstSegment)
    pos = pos + firstSegment

    ' handle all of the middle sections
    While pos < length
        ' work with the current section
        ' (10 ^ sectionLength) is 1000 when sectionLength is 3.  basically shifting the decimal
        current = remainder * (10 ^ sectionLength) + subs
        remainder = current Mod denominator

        ' determine the next section
        subs = Mid(numerator, pos, sectionLength)
        pos = pos + sectionLength
    Wend

    ' handle the last section
    current = remainder * (10 ^ sectionLength) + subs
    remainder = current Mod denominator

    ' return the response
    AnyMod = remainder

End Function

可以这样命名 =AnyMod(Text(A1, "0"), 2017)

在您的情况下,您需要获得 2^288 的值,但如果单元格中的值很大并希望获得余数,则此方法可行。