Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Large Numbers in the MOD Function

Cesarettin noted that the MOD worksheet function cannot produce a result when the number is being evaluated is 268,435,456 or larger and the divisor is 2. If the number is less than this, there is no problem. For example, if the function is MOD(268435455, 2) there is no problem. He wonders if there is a way to use the MOD function with larger numbers and a divisor of 2?

The problem is actually bigger than what Cesarettin proposes. Microsoft knows about this problem; it seems to stem from issues with the internal formulas used by MOD. You can find more information about the error here:

http://support.microsoft.com/kb/119083

Basically, the MOD function returns an error if the divisor (the second argument in the MOD function), multiplied by 134,217,728, is less than or equal to the number being evaluated (the first argument in the MOD function).

Thus, the problem occurs when the number being evaluated is 268,435,456 and the divisor is 2, the number being evaluated is 402,653,184 and the divisor is 3, the number being evaluated is 536,870,912 and the divisor is 4, etc.

The solution suggested by Microsoft is to simply not use the MOD function and instead rely upon the following formula:

=number-(INT(number/divisor)*divisor)

This is not the only solution, however. There are other formulaic approaches you can use, as well. For instance:

=MOD(MOD(number,134217728*divisor),divisor)

This will solve for larger numbers much larger than the limit for MOD, but theoretically will hit the same problem when the number being evaluated reaches 134,217,728*134,217,728*divisor. For most uses, this is limit is large enough that it will never be reached.

If you only need to find the modulus of a number divided by 2, then you can insert a check into your formula in the following manner:

=MOD(IF(A1>=268435456,A1-268435456,A1),2)

This checks if the number being evaluated (in this case, in cell A1) is larger than the limit, and if it is it subtracts the limit from the number before calculating the modulus. You could also effectively remove the MOD limit by using this formula:

=MOD(MOD(number,2^16),2))

This takes the large number modulo 2 to the 16th power, then takes the resulting value modulo 2. If the numbers are viewed as binary, it's easy to see what is happening. MOD(largenum,2^16) just drops all bits to the left of the 16th binary digit. For modulo 2, only the right-most digit is required to determine the result anyway, so the dropped bits never affect the result, regardless of value.

Of course, you could simply create your own MOD function in VBA and use it in your formulas instead of the built-in MOD function.

Function DblMod(Dividend, Divisor)
    ' Declare two double precision variables
    Dim D1 As Double
    Dim D2 As Double

    ' Copy function arguments to local variables
    D1 = Dividend
    D2 = Divisor

    DblMod = D1 Mod D2
End Function

The function simply lets you pass two arguments to the VBA function. It then relies upon the VBA Mod function, which doesn't have the same limitation as the MOD worksheet function.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3302) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Related Tips:

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

Ferd    09 Jun 2016, 09:29
If you only need to know if the number if divisible by 2, using WorksheetFunction.IsEven is a lot simpler....
Leigh    23 Nov 2015, 02:02
Kiran
Mod[23100001180000012345142800, 97]=18
John    23 Apr 2015, 09:44
=mod((20*12.4),12.4)

evaluates "12.4"

Excel 2013 on Windows 7 32-bit
Andrew    22 Dec 2014, 03:32
The Microsoft solution (=number - (int(number / divisor) * divisor) still has size limitations. A quick test showed it will fall over at 2^50 mod 11. It seems Excel is simply not up to big number maths.
kiran    11 Feb 2014, 10:18
I have tried with your approach sir.but i am not able to find the solution
number: 23100001180000012345142800
need mod 97 for the above number..
Please share why i am not able to calculate the mod 97 for this number
Please help me to get the same as i need this as urgent for my requirement.Thanks alot
KIRAN    11 Feb 2014, 10:16
I have tried with your approach sir.but i am not able to find the solution
number: 23100001180000012345142800
need mod 97 for the above number..
Please share why i am not able to calculate the mod 97 for this number
Please help me to get the same as i need this as urgent for my requirement.Thanks alot
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.