Division of Integer Strings

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. Windows
While working on Big Integer arithmetic I cam across a post at StackExchange. Is there a way to accept integers of length greater than 15?

VBA Code:
Function SuperMod2(ByVal a$, b$)
    Do While Len(a) > 15
        a = Mid$(a, 1, 15) - Int(Mid$(a, 1, 15) / b) * b & Mid$(a, 16)
    Loop
    SuperMod2 = a - Int(a / b) * b
End Function

This version allows for:
SuperMod2("654654321902010548705495321500805040401121210900506078798456849816241968462130000051204789540453745898347543753489751261610093245892737467263468234623689898054456361110010151021101827", 999999999999999)
... to correctly display: 194745570864677.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
While working on Big Integer arithmetic I cam across a post at StackExchange. Is there a way to accept integers of length greater than 15?

Change Dim integers to Long
I don't think that will work. Excel can only remember up to 15 significant digits, and I believe VBA is the same.
Tests seem to confirm this. Try entering any number more than 15 digits, and you will see that.

The way to get it to remember more is to convert it to text, but then you would not be able to do arithmetic operations on it.
 
Upvote 0
This won't handle numbers anywhere near as large as your question posed, but it will hand numbers (inputted as text) larger than 15 digits...
VBA Code:
'  The arguments to the BigMod function can be up to 29 digits as long as
'  either number is less than 79,228,162,514,264,337,593,543,950,335
'  (a limitation of the Decimal data type used in the calculations).
'  And, of course, if you are going to feed the BigMod function super
'  large numbers, they must be passed in as quoted text strings.
Function BigMod(Number As Variant, Modulus As Variant) As Variant
  Number = CDec(Number)
  Modulus = CDec(Modulus)
  BigMod = Number - Modulus * Int(Number / Modulus)
End Function
 
Upvote 0
Rick,

What do they need to use for the "Modulus" argument/parameter when using that function?
Might be helpful to show an example, so they can see how to use it.
 
Upvote 0
What do they need to use for the "Modulus" argument/parameter when using that function?
Any number up to the 29 digits... same as the number whose modulus is being calculated. For example, let's say we wanted to find 9876543210987654321098765432 mod 12345678901234567890 for some reason. You could use it like this...
VBA Code:
MsgBox BigMod("9876543210987654321098765432", "12345678901234567890")
or put those numbers in variables and use the variables in the function instead. Now, because Excel likes to convert number automatically, you would need to modify the function slightly to make sure large outputted numbers remain intact...
Rich (BB code):
Function BigMod(Number As Variant, Modulus As Variant) As Variant
  Number = CDec(Number)
  Modulus = CDec(Modulus)
  BigMod = CStr(Number - Modulus * Int(Number / Modulus))
End Function
then you would use something like this on the worksheet...
Excel Formula:
A1: 9876543210987654321098765432 (Text format)
B1: 12345678901234567890
C1: =BigMod(A1,B1)
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,465
Members
448,965
Latest member
grijken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top