## Class MachineArithmetic

• ```public final class MachineArithmetic
extends Object```
Helper methods for arithmetic with machine numbers.
Since:
1.0
• ### Field Summary

Fields
Modifier and Type Field Description
`static BigInteger` `b_MAX_SUPPORTED_MODULUS`
Max supported modulus
`static long` `MAX_SUPPORTED_MODULUS`
Max supported modulus which fits into machine word
`static int` `MAX_SUPPORTED_MODULUS_BITS`
Max supported modulus bits which fits into machine word
• ### Method Summary

Modifier and Type Method Description
`static boolean` `fits31bitWord​(long val)`
Returns true if `val` fits into 32-bit machine word (unsigned) and false otherwise
`static boolean` `fits32bitWord​(long val)`
Returns true if `val` fits into 32-bit machine word (unsigned) and false otherwise
`static long` `gcd​(int... integers)`
Returns the greatest common an array of integers
`static long` ```gcd​(int[] integers, int from, int to)```
Returns the greatest common an array of integers
`static int` ```gcd​(int p, int q)```
Computes the greatest common divisor of the absolute value of two numbers, using a modified version of the "binary gcd" method.
`static long` `gcd​(long... integers)`
Returns the greatest common an array of longs
`static long` ```gcd​(long[] integers, int from, int to)```
Returns the greatest common an array of longs
`static long` ```gcd​(long p, long q)```
Returns the greatest common divisor of two longs.
`static long[]` ```gcdExtended​(long a, long b)```
Runs extended Euclidean algorithm to compute `[gcd(a,b), x, y]` such that ```x * a + y * b = gcd(a, b)```
`static boolean` ```isOverflowAdd​(long x, long y)```
Tests whether the addition of `x + y` will cause long overflow
`static boolean` ```isOverflowMultiply​(long x, long y)```
Tests whether the multiplication of `x*y` will cause long overflow
`static int` ```lcm​(int a, int b)```
Returns the least common multiple of two integers
`static long` ```lcm​(long a, long b)```
Returns the least common multiple of two longs
`static long` ```mod​(long num, long modulus)```
`static long` ```modInverse​(long num, long modulus)```
Returns a solution of congruence `num * x = 1 mod modulus`
`static long[]` `perfectPowerDecomposition​(long n)`
Tests whether `n` is a perfect power `n == a^b` and returns `{a, b}` if so and `null` otherwise
`static long` ```powMod​(long base, long exponent, long modulus)```
Returns `base` in a power of non-negative `e` modulo `modulus`
`static long` ```powModSigned​(long base, long exponent, cc.redberry.libdivide4j.FastDivision.Magic magic)```
Returns `base` in a power of non-negative `e` modulo `magic.modulus`
`static long` ```powModUnsigned​(long base, long exponent, cc.redberry.libdivide4j.FastDivision.Magic magic)```
Returns `base` in a power of non-negative `e` modulo `magic.modulus`
`static long` ```safeAdd​(long x, long y)```
`static long` ```safeMultiply​(long x, long y)```
`static long` ```safeMultiply​(long x, long y, long z)```
`static long` `safeNegate​(long x)`
`static long` ```safePow​(long base, long exponent)```
Returns `base` in a power of `e` (non negative)
`static long` ```safeSubtract​(long a, long b)```
`static int` `safeToInt​(long value)`
Casts `long` to signed `int` throwing exception in case of overflow.
`static long` ```symMod​(long value, long modulus)```
Returns `value mod modulus` in the symmetric representation (`-modulus/2 <= result <= modulus/2`)
`static long` ```unsafePow​(long base, long exponent)```
Returns `base` in a power of `e` (non negative)
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Field Detail

• #### MAX_SUPPORTED_MODULUS_BITS

`public static final int MAX_SUPPORTED_MODULUS_BITS`
Max supported modulus bits which fits into machine word
Constant Field Values
• #### MAX_SUPPORTED_MODULUS

`public static final long MAX_SUPPORTED_MODULUS`
Max supported modulus which fits into machine word
Constant Field Values
• #### b_MAX_SUPPORTED_MODULUS

`public static final BigInteger b_MAX_SUPPORTED_MODULUS`
Max supported modulus
• ### Method Detail

• #### fits32bitWord

`public static boolean fits32bitWord​(long val)`
Returns true if `val` fits into 32-bit machine word (unsigned) and false otherwise
Parameters:
`val` - the value
Returns:
true if `val` fits into 32-bit machine word (unsigned) and false otherwise
• #### fits31bitWord

`public static boolean fits31bitWord​(long val)`
Returns true if `val` fits into 32-bit machine word (unsigned) and false otherwise
Parameters:
`val` - the value
Returns:
true if `val` fits into 32-bit machine word (unsigned) and false otherwise
• #### safeMultiply

```public static long safeMultiply​(long x,
long y)```
Throws:
`ArithmeticException` - if the result overflows a long
• #### safeMultiply

```public static long safeMultiply​(long x,
long y,
long z)```
Throws:
`ArithmeticException` - if the result overflows a long

```public static long safeAdd​(long x,
long y)```
Throws:
`ArithmeticException` - if the result overflows a long
• #### safeSubtract

```public static long safeSubtract​(long a,
long b)```
Throws:
`ArithmeticException` - if the result overflows a long
• #### safeNegate

`public static long safeNegate​(long x)`
Throws:
`ArithmeticException` - if the result overflows a long
• #### isOverflowMultiply

```public static boolean isOverflowMultiply​(long x,
long y)```
Tests whether the multiplication of `x*y` will cause long overflow

```public static boolean isOverflowAdd​(long x,
long y)```
Tests whether the addition of `x + y` will cause long overflow
• #### safePow

```public static long safePow​(long base,
long exponent)```
Returns `base` in a power of `e` (non negative)
Parameters:
`base` - base
`exponent` - exponent (non negative)
Returns:
`base` in a power of `e`
Throws:
`ArithmeticException` - if the result overflows a long
• #### unsafePow

```public static long unsafePow​(long base,
long exponent)```
Returns `base` in a power of `e` (non negative)
Parameters:
`base` - base
`exponent` - exponent (non negative)
Returns:
`base` in a power of `e`
• #### gcd

```public static long gcd​(long p,
long q)```
Returns the greatest common divisor of two longs.
Parameters:
`p` - a long
`q` - a long
Returns:
greatest common divisor of `a` and `b`
• #### gcd

```public static int gcd​(int p,
int q)```
Computes the greatest common divisor of the absolute value of two numbers, using a modified version of the "binary gcd" method. See Knuth 4.5.2 algorithm B. The algorithm is due to Josef Stein (1961).
Special cases:
• The invocations `gcd(Integer.MIN_VALUE, Integer.MIN_VALUE)`, ```gcd(Integer.MIN_VALUE, 0)``` and `gcd(0, Integer.MIN_VALUE)` throw an `ArithmeticException`, because the result would be 2^31, which is too large for an int value.
• The result of `gcd(x, x)`, `gcd(0, x)` and ``` gcd(x, 0)``` is the absolute value of `x`, except for the special cases above.
• The invocation ``` gcd(0, 0)``` is the only one which returns `0`.
Parameters:
`p` - Number.
`q` - Number.
Returns:
the greatest common divisor (never negative).
• #### gcdExtended

```public static long[] gcdExtended​(long a,
long b)```
Runs extended Euclidean algorithm to compute `[gcd(a,b), x, y]` such that ```x * a + y * b = gcd(a, b)```
Parameters:
`a` - a long
`b` - a long
Returns:
array of `[gcd(a,b), x, y]` such that `x * a + y * b = gcd(a, b)`
• #### lcm

```public static long lcm​(long a,
long b)```
Returns the least common multiple of two longs
Parameters:
`a` - a long
`b` - a long
Returns:
least common multiple of `a` and `b`
Throws:
`ArithmeticException` - if the result overflows a long
• #### lcm

```public static int lcm​(int a,
int b)```
Returns the least common multiple of two integers
Parameters:
`a` - a number
`b` - a number
Returns:
least common multiple of `a` and `b`
• #### gcd

```public static long gcd​(long[] integers,
int from,
int to)```
Returns the greatest common an array of longs
Parameters:
`integers` - array of longs
`from` - from position (inclusive)
`to` - to position (exclusive)
Returns:
greatest common divisor of array
• #### gcd

`public static long gcd​(long... integers)`
Returns the greatest common an array of longs
Parameters:
`integers` - array of longs
Returns:
greatest common divisor of array
• #### gcd

```public static long gcd​(int[] integers,
int from,
int to)```
Returns the greatest common an array of integers
Parameters:
`integers` - array of integers
`from` - from position (inclusive)
`to` - to position (exclusive)
Returns:
greatest common divisor of array
• #### gcd

`public static long gcd​(int... integers)`
Returns the greatest common an array of integers
Parameters:
`integers` - array of integers
Returns:
greatest common divisor of array
• #### symMod

```public static long symMod​(long value,
long modulus)```
Returns `value mod modulus` in the symmetric representation (`-modulus/2 <= result <= modulus/2`)
Parameters:
`value` - a long
`modulus` - modulus
Returns:
`value mod modulus` in the symmetric representation (`-modulus/2 <= result <= modulus/2`)
• #### modInverse

```public static long modInverse​(long num,
long modulus)```
Returns a solution of congruence `num * x = 1 mod modulus`
Parameters:
`num` - base
`modulus` - modulus
Returns:
`a^(-1) mod p`
Throws:
`IllegalArgumentException` - `a` and `modulus` are not coprime
• #### safeToInt

`public static int safeToInt​(long value)`
Casts `long` to signed `int` throwing exception in case of overflow.
Parameters:
`value` - the long
Returns:
int value
Throws:
`ArithmeticException` - if the result overflows a long
• #### powMod

```public static long powMod​(long base,
long exponent,
long modulus)```
Returns `base` in a power of non-negative `e` modulo `modulus`
Parameters:
`base` - the base
`exponent` - the non-negative exponent
`modulus` - the modulus
Returns:
`base` in a power of `e`
• #### powModSigned

```public static long powModSigned​(long base,
long exponent,
cc.redberry.libdivide4j.FastDivision.Magic magic)```
Returns `base` in a power of non-negative `e` modulo `magic.modulus`
Parameters:
`base` - the base
`exponent` - the non-negative exponent
`magic` - magic modulus
Returns:
`base` in a power of `e`
• #### powModUnsigned

```public static long powModUnsigned​(long base,
long exponent,
cc.redberry.libdivide4j.FastDivision.Magic magic)```
Returns `base` in a power of non-negative `e` modulo `magic.modulus`
Parameters:
`base` - the base
`exponent` - the non-negative exponent
`magic` - magic modulus
Returns:
`base` in a power of `e`
• #### perfectPowerDecomposition

`public static long[] perfectPowerDecomposition​(long n)`
Tests whether `n` is a perfect power `n == a^b` and returns `{a, b}` if so and `null` otherwise
Parameters:
`n` - the number
Returns:
array `{a, b}` so that `n = a^b` or `null` is `n` is not a perfect power