modulus of numbers > 2.2 billion

snowjoke

New Member
Joined
Nov 15, 2010
Messages
47
VBA seems to handle numbers greater than the maximum value for Long (2,147,483,647) OK, as I understand it by using the Decimal number type, which is a subtype of Variant.

If you go to Immediates and type
Code:
?3000000000 + 1
you get
Code:
 3000000001
But if you type
Code:
?3000000000 mod 2
or try to do the same in a program, you get an overflow error.

Is there any way to get the modulus of such a number?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Code:
if int(dbl/2) = dbl/2 then
   ' dbl mod 2 = 0
 
Upvote 0
... (duplicate)
 
Last edited:
Upvote 0
Thanks - was going to ask what if I don't just want to find if the mod is zero, but I've gone one better and written this which seems to work...

Code:
Function bigMod(a, b)
    bigMod = Round((a / b - Int(a / b)) * b)
End Function
Code:
?bigMod(3000000002,3)
 2
 
Upvote 0
Why would you want to round?

Code:
Function BigMod(a, b) As Double
    BigMod = a - Int(a / b) * b
End Function
 
Upvote 0
It uses Round because it operates on Longs, which are integers (small i). The worksheet MOD function operates on Doubles.

EDIT: More specifically, the Mod operator rounds its operands, not its results.
 
Last edited:
Upvote 0
I tried your version and it doesn't seem to need the Round... yep your version loses less precision; I'll change my function to that! Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,849
Members
452,948
Latest member
UsmanAli786

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