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

时间:2015-12-23 16:54:31

标签: excel numbers modulus

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

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

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

3 个答案:

答案 0 :(得分:0)

要解决此问题,请在excel中添加此功能:alt + f11 - >模块 - >加 并使用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

答案 1 :(得分:0)

我需要它来处理大量的工作。上面的示例适用于以下情况:如果我采用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的值,但是如果您在单元格中具有较大的值并希望获得余数,则此方法有效。

答案 2 :(得分:-1)

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

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

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

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

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

  BigMod = numerator - intval * denominator

End Function