# Modular arithmetic

posted 4 years ago

- 0

Hi,

Doe's anyone know how to do modular arithmetic in Java ? I need to do modular subtraction so using the % operator is out of the window.

Cheers

Ernie

Doe's anyone know how to do modular arithmetic in Java ? I need to do modular subtraction so using the % operator is out of the window.

Cheers

Ernie

You're on a gravy train with biscuit wheels Roy.

posted 4 years ago

- 0

Hmm I'm sure there is a shorter way of doing this, but if I think quickly, you could perform subtraction like so:

Where val is the minuend, mod is the modulus, and sub is the subtrahend.

Where val is the minuend, mod is the modulus, and sub is the subtrahend.

*The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.*

posted 4 years ago

- 0

That seems to work nicely, thanks very much. I did notice that you can do it with BigInteger but that would be a pain to implement in my code.

Thanks again.

Thanks again.

You're on a gravy train with biscuit wheels Roy.

Campbell Ritchie

Sheriff

Posts: 47270

52

posted 4 years ago

And it is by no means difficult to use BigInteger. You simply have to try it.

- 0

Should that read . . . instead?Stephan van Hulst wrote: . . . . . .

And it is by no means difficult to use BigInteger. You simply have to try it.

posted 4 years ago

- 0

Not that I know. Why?yields a positive number that is the 'inverse' of sub. If you add this number to you original value, it will wrap around as if you subtracted.

I agree. Here's the link: http://aspose.com/file-tools |