Package org.pkl.core.util
Class MathUtils
- java.lang.Object
-
- org.pkl.core.util.MathUtils
-
public final class MathUtils extends java.lang.Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static longcheckedMultiply(long a, long b)Returns the product ofaandb, provided it does not overflow.static longcheckedPow(long b, int k)Returns thebto thekth power, provided it does not overflow.static longgcd(long a, long b)Returns the greatest common divisor ofa, b.static booleanisMathematicalInteger(double x)static booleanisPowerOfTwo(double x)Returnstrueifxis exactly equal to2^kfor some finite integerk.static booleanisPowerOfTwo(long x)Returnstrueifxrepresents a power of two.static longroundToLong(double x, java.math.RoundingMode mode)
-
-
-
Method Detail
-
roundToLong
public static long roundToLong(double x, java.math.RoundingMode mode)
-
isMathematicalInteger
public static boolean isMathematicalInteger(double x)
-
isPowerOfTwo
public static boolean isPowerOfTwo(long x)
Returnstrueifxrepresents a power of two.This differs from
Long.bitCount(x) == 1, becauseLong.bitCount(Long.MIN_VALUE) == 1, butLong.MIN_VALUEis not a power of two.
-
isPowerOfTwo
public static boolean isPowerOfTwo(double x)
Returnstrueifxis exactly equal to2^kfor some finite integerk.
-
checkedPow
public static long checkedPow(long b, int k)Returns thebto thekth power, provided it does not overflow.- Throws:
java.lang.ArithmeticException- ifbto thekth power overflows in signedlongarithmetic
-
gcd
public static long gcd(long a, long b)Returns the greatest common divisor ofa, b. Returns0ifa == 0 && b == 0.- Throws:
java.lang.IllegalArgumentException- ifa < 0orb < 0
-
checkedMultiply
public static long checkedMultiply(long a, long b)Returns the product ofaandb, provided it does not overflow.- Throws:
java.lang.ArithmeticException- ifa * boverflows in signedlongarithmetic
-
-