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

问题描述 投票:3回答:3

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

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

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

excel numbers modulus
3个回答
-1
投票

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。


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

0
投票

我需要它来处理大量数据。如果我在3后面加上0的7个部分,则上面的示例有效,但如果我继续前进,则会中断。

所以我想出了如何在纸上做的,并编写了一个可以用这种方式处理的函数。因为它一次只能处理一个部分(加上前一部分的其余部分),所以它永远不会以大于一百万的值工作。

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
        current = remainder * 1000 + subs
        remainder = current Mod denominator

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

    ' handle the last section
    current = remainder * 1000 + subs
    remainder = current Mod denominator

    ' return the response
    AnyMod = remainder

End Function

命名为= AnyMod(Text(A1,“ 0”),2017)

在您的情况下,您需要获取2 ^ 288的值,但是如果您在单元格中具有较大的值并希望获得余数,则此方法有效。

© www.soinside.com 2019 - 2024. All rights reserved.