Package org.pkl.core.util
Class MathUtils
java.lang.Object
org.pkl.core.util.MathUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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, RoundingMode mode)
-
Method Details
-
roundToLong
-
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:
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:
IllegalArgumentException- ifa < 0orb < 0
-
checkedMultiply
public static long checkedMultiply(long a, long b) Returns the product ofaandb, provided it does not overflow.- Throws:
ArithmeticException- ifa * boverflows in signedlongarithmetic
-