public final class MachineArithmetic extends Object
Modifier and Type | Field and 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
|
Modifier and Type | Method and 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)
Delegates to
Math.floorMod(long, long) |
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)
Delegates to
Math.addExact(long, long) |
static long |
safeMultiply(long x,
long y)
Delegates to
Math.multiplyExact(long, long) |
static long |
safeMultiply(long x,
long y,
long z)
Delegates to
Math.multiplyExact(long, long) |
static long |
safeNegate(long x)
Delegates to
Math.negateExact(long) |
static long |
safePow(long base,
long exponent)
Returns
base in a power of e (non negative) |
static long |
safeSubtract(long a,
long b)
Delegates to
Math.subtractExact(long, long) |
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) |
public static final int MAX_SUPPORTED_MODULUS_BITS
public static final long MAX_SUPPORTED_MODULUS
public static final BigInteger b_MAX_SUPPORTED_MODULUS
public static boolean fits32bitWord(long val)
val
fits into 32-bit machine word (unsigned) and false otherwiseval
- the valueval
fits into 32-bit machine word (unsigned) and false otherwisepublic static boolean fits31bitWord(long val)
val
fits into 32-bit machine word (unsigned) and false otherwiseval
- the valueval
fits into 32-bit machine word (unsigned) and false otherwisepublic static long safeMultiply(long x, long y)
Math.multiplyExact(long, long)
ArithmeticException
- if the result overflows a longpublic static long safeMultiply(long x, long y, long z)
Math.multiplyExact(long, long)
ArithmeticException
- if the result overflows a longpublic static long safeAdd(long x, long y)
Math.addExact(long, long)
ArithmeticException
- if the result overflows a longpublic static long safeSubtract(long a, long b)
Math.subtractExact(long, long)
ArithmeticException
- if the result overflows a longpublic static long safeNegate(long x)
Math.negateExact(long)
ArithmeticException
- if the result overflows a longpublic static boolean isOverflowMultiply(long x, long y)
x*y
will cause long overflowpublic static boolean isOverflowAdd(long x, long y)
x + y
will cause long overflowpublic static long safePow(long base, long exponent)
base
in a power of e
(non negative)base
- baseexponent
- exponent (non negative)base
in a power of e
ArithmeticException
- if the result overflows a longpublic static long unsafePow(long base, long exponent)
base
in a power of e
(non negative)base
- baseexponent
- exponent (non negative)base
in a power of e
public static long gcd(long p, long q)
p
- a longq
- a longa
and b
public static int gcd(int p, int q)
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.gcd(x, x)
, gcd(0, x)
and gcd(x, 0)
is the absolute value of x
, except for the special cases above.gcd(0, 0)
is the only one which returns 0
.p
- Number.q
- Number.public static long[] gcdExtended(long a, long b)
[gcd(a,b), x, y]
such that x * a + y * b = gcd(a,
b)
a
- a longb
- a long[gcd(a,b), x, y]
such that x * a + y * b = gcd(a, b)
public static long lcm(long a, long b)
a
- a longb
- a longa
and b
ArithmeticException
- if the result overflows a longpublic static int lcm(int a, int b)
a
- a numberb
- a numbera
and b
public static long gcd(long[] integers, int from, int to)
integers
- array of longsfrom
- from position (inclusive)to
- to position (exclusive)public static long gcd(long... integers)
integers
- array of longspublic static long gcd(int[] integers, int from, int to)
integers
- array of integersfrom
- from position (inclusive)to
- to position (exclusive)public static long gcd(int... integers)
integers
- array of integerspublic static long mod(long num, long modulus)
Math.floorMod(long, long)
public static long symMod(long value, long modulus)
value mod modulus
in the symmetric representation (-modulus/2 <= result <= modulus/2
)value
- a longmodulus
- modulusvalue mod modulus
in the symmetric representation (-modulus/2 <= result <= modulus/2
)public static long modInverse(long num, long modulus)
num * x = 1 mod modulus
num
- basemodulus
- modulusa^(-1) mod p
IllegalArgumentException
- a
and modulus
are not coprimepublic static int safeToInt(long value)
long
to signed int
throwing exception in case of overflow.value
- the longArithmeticException
- if the result overflows a longpublic static long powMod(long base, long exponent, long modulus)
base
in a power of non-negative e
modulo modulus
base
- the baseexponent
- the non-negative exponentmodulus
- the modulusbase
in a power of e
public static long powModSigned(long base, long exponent, cc.redberry.libdivide4j.FastDivision.Magic magic)
base
in a power of non-negative e
modulo magic.modulus
base
- the baseexponent
- the non-negative exponentmagic
- magic modulusbase
in a power of e
public static long powModUnsigned(long base, long exponent, cc.redberry.libdivide4j.FastDivision.Magic magic)
base
in a power of non-negative e
modulo magic.modulus
base
- the baseexponent
- the non-negative exponentmagic
- magic modulusbase
in a power of e
public static long[] perfectPowerDecomposition(long n)
n
is a perfect power n == a^b
and returns {a, b}
if so and null
otherwisen
- the number{a, b}
so that n = a^b
or null
is n
is not a perfect powerCopyright © 2018. All rights reserved.