Class Numbers

java.lang.Object
com.landawn.abacus.util.Numbers

public final class Numbers extends Object
Note: A lot of codes in this classed are copied from Google Guava, Apache Commons Math and Apache Commons Lang under under the Apache License, Version 2.0. The purpose of copying the code is to re-organize the APIs.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Byte
    Reusable Byte constant for minus one.
    static final Byte
    Reusable Byte constant for one.
    static final Byte
    Reusable Byte constant for zero.
    static final Double
    Reusable Double constant for minus one.
    static final Double
    Reusable Double constant for one.
    static final Double
    Reusable Double constant for zero.
    static final Float
    Reusable Float constant for minus one.
    static final Float
    Reusable Float constant for one.
    static final Float
    Reusable Float constant for zero.
    static final Integer
    Reusable Integer constant for minus one.
    static final Integer
    Reusable Integer constant for one.
    static final Integer
    Reusable Integer constant for two
    static final Integer
    Reusable Integer constant for zero.
    static final Long
    Reusable Long constant for minus one.
    static final Long
    Reusable Long constant for one.
    static final Long
    Reusable Long constant for zero.
    static final Short
    Reusable Short constant for minus one.
    static final Short
    Reusable Short constant for one.
    static final Short
    Reusable Short constant for zero.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    acosh(double a)
    Compute the inverse hyperbolic cosine of a number.
    static int
    addExact(int a, int b)
    Returns the sum of a and b, provided it does not overflow.
    static long
    addExact(long a, long b)
    Returns the sum of a and b, provided it does not overflow.
    static double
    asinh(double a)
    Compute the inverse hyperbolic sine of a number.
    static double
    atanh(double a)
    Compute the inverse hyperbolic tangent of a number.
    static int
    binomial(int n, int k)
    Returns n choose k, also known as the binomial coefficient of n and k, or Integer.MAX_VALUE if the result does not fit in an int.
    static BigInteger
    binomialToBigInteger(int n, int k)
    Returns n choose k, also known as the binomial coefficient of n and k, that is, n! / (k! (n - k)!).
    static long
    binomialToLong(int n, int k)
    Returns n choose k, also known as the binomial coefficient of n and k, or Long.MAX_VALUE if the result does not fit in a long.
    static int
    castExact(long value)
    Returns the int value that is equal to value, if possible.
    static long
    Returns the smallest power of two greater than or equal to x.
    static BigInteger
    Ceiling power of two.
    static <T extends Number>
    T
    convert(Number value, com.landawn.abacus.type.Type<? extends T> targetType)
     
    static <T extends Number>
    T
    convert(Number value, Class<? extends T> targetType)
     
    static BigDecimal
    Convert a String to a BigDecimal.
    static BigInteger
    Convert a String to a BigInteger; since 3.2 it handles hex (0x or #) and octal (0) notations.
    static Double
    Convert a String to a Double.
    static Float
    Convert a String to a Float.
    static Integer
    Convert a String to a Integer, handling hex (0xhhhh) and octal (0dddd) notations.
    static Long
    Convert a String to a Long; since 3.1 it handles hex (0Xhhhh) and octal (0ddd) notations.
    static Number
    Turns a string value into a java.lang.Number.
    static int
    divide(int p, int q, RoundingMode mode)
    Returns the result of dividing p by q, rounding using the specified RoundingMode.
    static long
    divide(long p, long q, RoundingMode mode)
    Returns the result of dividing p by q, rounding using the specified RoundingMode.
    static BigInteger
     
    static int
    factorial(int n)
    Returns n!, that is, the product of the first n positive integers, 1 if n == 0, or Integer.MAX_VALUE if the result does not fit in a int.
    static BigInteger
    Returns n!, that is, the product of the first n positive integers, or 1 if n == 0.
    static double
    Returns n!, that is, the product of the first n positive integers, 1 if n == 0, or n!, or Double.POSITIVE_INFINITY if n! > Double.MAX_VALUE.
    static long
    Returns n!, that is, the product of the first n positive integers, 1 if n == 0, or Long.MAX_VALUE if the result does not fit in a long.
    static long
    Returns the largest power of two less than or equal to x.
    static BigInteger
    Floor power of two.
    static String
    format(double x, String decimalFormat)
    Numbers.format(val, "0.0"); --> 12.1 Numbers.format(val, "#.#"); --> 12.1 Numbers.format(val, "0.00"); --> 12.10 Numbers.format(val, "#.##"); --> 12.1 Numbers.format(val, "0.000"); --> 12.105 Numbers.format(val, "#.###"); --> 12.105 Numbers.format(val, "0.0000"); --> 12.1050 Numbers.format(val, "#.####"); --> 12.105 Numbers.format(val, "0.00000"); --> 12.10500 Numbers.format(val, "#.#####"); --> 12.105
    static String
    format(float x, String decimalFormat)
    Numbers.format(val, "0.0"); --> 12.1 Numbers.format(val, "#.#"); --> 12.1 Numbers.format(val, "0.00"); --> 12.10 Numbers.format(val, "#.##"); --> 12.1 Numbers.format(val, "0.000"); --> 12.105 Numbers.format(val, "#.###"); --> 12.105 Numbers.format(val, "0.0000"); --> 12.1050 Numbers.format(val, "#.####"); --> 12.105 Numbers.format(val, "0.00000"); --> 12.10500 Numbers.format(val, "#.#####"); --> 12.105
    static String
    format(Double x, String decimalFormat)
     
    static String
    format(Float x, String decimalFormat)
     
    static int
    fuzzyCompare(double a, double b, double tolerance)
    Compares a and b "fuzzily," with a tolerance for nearly-equal values.
    static boolean
    fuzzyEquals(double a, double b, double tolerance)
    Returns true if a and b are within tolerance of each other.
    static int
    gcd(int a, int b)
    Returns the greatest common divisor of a, b.
    static long
    gcd(long a, long b)
    Returns the greatest common divisor of a, b.
    static boolean
    Checks whether the String a valid Java number.
    static boolean
    Checks whether the String contains only digit characters.
    static boolean
    Returns true if x represents a mathematical integer.
    static boolean
    Note: It's copied from NumberUtils in Apache Commons Lang under Apache License 2.0

    It's same as isCreatable(String).
    static boolean
    Checks whether the given String is a parsable number.
    static boolean
    Checks if is perfect square.
    static boolean
    Checks if is perfect square.
    static boolean
    isPowerOfTwo(double x)
    Checks if is power of two.
    static boolean
    isPowerOfTwo(int x)
    Checks if is power of two.
    static boolean
    isPowerOfTwo(long x)
    Checks if is power of two.
    static boolean
    Returns true if x represents a power of two.
    static boolean
    isPrime(long n)
    Returns true if n is a prime number: an integer greater than one that cannot be factored into a product of smaller positive integers.
    static int
    lcm(int a, int b)
    Returns the least common multiple of the absolute value of two numbers, using the formula lcm(a,b) = (a / gcd(a,b)) * b.
    static long
    lcm(long a, long b)
    Returns the least common multiple of the absolute value of two numbers, using the formula lcm(a,b) = (a / gcd(a,b)) * b.
    static double
    log(double a)
     
    static double
    log10(double x)
     
    static int
    log10(int x, RoundingMode mode)
     
    static int
    log10(long x, RoundingMode mode)
    Returns the base-10 logarithm of x, rounded according to the specified rounding mode.
    static int
    Returns the base-10 logarithm of x, rounded according to the specified rounding mode.
    static double
    log2(double x)
    Returns the base 2 logarithm of a double value.
    static int
    log2(double x, RoundingMode mode)
    Returns the base 2 logarithm of a double value, rounded with the specified rounding mode to an int.
    static int
    log2(int x, RoundingMode mode)
     
    static int
    log2(long x, RoundingMode mode)
    Returns the base-2 logarithm of x, rounded according to the specified rounding mode.
    static int
    Returns the base-2 logarithm of x, rounded according to the specified rounding mode.
    static double
    mean(double... values)
     
    static double
    mean(double x, double y)
     
    static double
    mean(int... values)
    Returns the arithmetic mean of values.
    static int
    mean(int x, int y)
    Returns the arithmetic mean of x and y, rounded towards negative infinity.
    static double
    mean(long... values)
    Returns the arithmetic mean of values.
    static long
    mean(long x, long y)
    Returns the arithmetic mean of x and y, rounded toward negative infinity.
    static int
    mod(int x, int m)
    Returns x mod m, a non-negative value less than m.
    static int
    mod(long x, int m)
    Returns x mod m, a non-negative value less than m.
    static long
    mod(long x, long m)
    Returns x mod m, a non-negative value less than m.
    static int
    multiplyExact(int a, int b)
    Returns the product of a and b, provided it does not overflow.
    static long
    multiplyExact(long a, long b)
    Returns the product of a and b, provided it does not overflow.
    static int
    pow(int b, int k)
     
    static long
    pow(long b, int k)
    Returns b to the kth power.
    static int
    powExact(int b, int k)
    Returns the b to the kth power, provided it does not overflow.
    static long
    powExact(long b, int k)
    Returns the b to the kth power, provided it does not overflow.
    static double
    round(double x, int scale)
     
    static double
    round(double x, int scale, RoundingMode roundingMode)
     
    static double
    round(double x, String decimalFormat)
     
    static double
    round(double x, DecimalFormat decimalFormat)
     
    static float
    round(float x, int scale)
     
    static float
    round(float x, int scale, RoundingMode roundingMode)
     
    static float
    round(float x, String decimalFormat)
     
    static float
    round(float x, DecimalFormat decimalFormat)
     
    static BigInteger
    Returns the BigInteger value that is equal to x rounded with the specified rounding mode, if possible.
    static int
    roundToInt(double x, RoundingMode mode)
    Returns the int value that is equal to x rounded with the specified rounding mode, if possible.
    static long
    roundToLong(double x, RoundingMode mode)
    Returns the long value that is equal to x rounded with the specified rounding mode, if possible.
    static int
    saturatedAdd(int a, int b)
    Returns the sum of a and b unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.
    static long
    saturatedAdd(long a, long b)
    Returns the sum of a and b unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.
    static int
    saturatedCast(long value)
    Returns the int nearest in value to value.
    static int
    saturatedMultiply(int a, int b)
    Returns the product of a and b unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.
    static long
    saturatedMultiply(long a, long b)
    Returns the product of a and b unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.
    static int
    saturatedPow(int b, int k)
    Returns the b to the kth power, unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.
    static long
    saturatedPow(long b, int k)
    Returns the b to the kth power, unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.
    static int
    saturatedSubtract(int a, int b)
    Returns the difference of a and b unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.
    static long
    saturatedSubtract(long a, long b)
    Returns the difference of a and b unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.
    static int
    sqrt(int x, RoundingMode mode)
    Returns the square root of x, rounded with the specified rounding mode.
    static long
    sqrt(long x, RoundingMode mode)
    Returns the square root of x, rounded with the specified rounding mode.
    static BigInteger
     
    static int
    subtractExact(int a, int b)
    Returns the difference of a and b, provided it does not overflow.
    static long
    subtractExact(long a, long b)
    Returns the difference of a and b, provided it does not overflow.
    static byte
     
    static byte
    toByte(Object obj, byte defaultValueForNull)
     
    static byte
     
    static byte
    toByte(String str, byte defaultValueForNull)
     
    static double
     
    static double
    toDouble(Object obj, double defaultValueForNull)
     
    static double
     
    static double
    toDouble(String str, double defaultValueForNull)
     
    static double
    Convert a BigDecimal to a double.
    static double
    toDouble(BigDecimal value, double defaultValueForNull)
    Convert a BigDecimal to a double.
    static float
     
    static float
    toFloat(Object obj, float defaultValueForNull)
     
    static float
     
    static float
    toFloat(String str, float defaultValueForNull)
     
    static int
     
    static int
    toInt(Object obj, int defaultValueForNull)
     
    static int
     
    static int
    toInt(String str, int defaultValueForNull)
     
    static int
    toIntExact(long value)
    Returns the value of the long argument; throwing an exception if the value overflows an int.
    static long
     
    static long
    toLong(Object obj, long defaultValueForNull)
     
    static long
     
    static long
    toLong(String str, long defaultValueForNull)
     
    static BigDecimal
    Convert a Double to a BigDecimal with a scale of two that has been rounded using RoundingMode.HALF_EVEN.
    static BigDecimal
    toScaledBigDecimal(Double value, int scale, RoundingMode roundingMode)
    Convert a Double to a BigDecimal whose scale is the specified value with a RoundingMode applied.
    static BigDecimal
    Convert a Float to a BigDecimal with a scale of two that has been rounded using RoundingMode.HALF_EVEN.
    static BigDecimal
    toScaledBigDecimal(Float value, int scale, RoundingMode roundingMode)
    Convert a Float to a BigDecimal whose scale is the specified value with a RoundingMode applied.
    static BigDecimal
    Convert a String to a BigDecimal with a scale of two that has been rounded using RoundingMode.HALF_EVEN.
    static BigDecimal
    toScaledBigDecimal(String value, int scale, RoundingMode roundingMode)
    Convert a String to a BigDecimal whose scale is the specified value with a RoundingMode applied.
    static BigDecimal
    Convert a BigDecimal to a BigDecimal with a scale of two that has been rounded using RoundingMode.HALF_EVEN.
    static BigDecimal
    toScaledBigDecimal(BigDecimal value, int scale, RoundingMode roundingMode)
    Convert a BigDecimal to a BigDecimal whose scale is the specified value with a RoundingMode applied.
    static short
     
    static short
    toShort(Object obj, short defaultValueForNull)
     
    static short
     
    static short
    toShort(String str, short defaultValueForNull)
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • BYTE_ZERO

      public static final Byte BYTE_ZERO
      Reusable Byte constant for zero.
    • BYTE_ONE

      public static final Byte BYTE_ONE
      Reusable Byte constant for one.
    • BYTE_MINUS_ONE

      public static final Byte BYTE_MINUS_ONE
      Reusable Byte constant for minus one.
    • SHORT_ZERO

      public static final Short SHORT_ZERO
      Reusable Short constant for zero.
    • SHORT_ONE

      public static final Short SHORT_ONE
      Reusable Short constant for one.
    • SHORT_MINUS_ONE

      public static final Short SHORT_MINUS_ONE
      Reusable Short constant for minus one.
    • INTEGER_ZERO

      public static final Integer INTEGER_ZERO
      Reusable Integer constant for zero.
    • INTEGER_ONE

      public static final Integer INTEGER_ONE
      Reusable Integer constant for one.
    • INTEGER_TWO

      public static final Integer INTEGER_TWO
      Reusable Integer constant for two
    • INTEGER_MINUS_ONE

      public static final Integer INTEGER_MINUS_ONE
      Reusable Integer constant for minus one.
    • LONG_ZERO

      public static final Long LONG_ZERO
      Reusable Long constant for zero.
    • LONG_ONE

      public static final Long LONG_ONE
      Reusable Long constant for one.
    • LONG_MINUS_ONE

      public static final Long LONG_MINUS_ONE
      Reusable Long constant for minus one.
    • FLOAT_ZERO

      public static final Float FLOAT_ZERO
      Reusable Float constant for zero.
    • FLOAT_ONE

      public static final Float FLOAT_ONE
      Reusable Float constant for one.
    • FLOAT_MINUS_ONE

      public static final Float FLOAT_MINUS_ONE
      Reusable Float constant for minus one.
    • DOUBLE_ZERO

      public static final Double DOUBLE_ZERO
      Reusable Double constant for zero.
    • DOUBLE_ONE

      public static final Double DOUBLE_ONE
      Reusable Double constant for one.
    • DOUBLE_MINUS_ONE

      public static final Double DOUBLE_MINUS_ONE
      Reusable Double constant for minus one.
  • Method Details

    • convert

      public static <T extends Number> T convert(Number value, Class<? extends T> targetType)
      Type Parameters:
      T -
      Parameters:
      value -
      targetType -
      Returns:
    • convert

      public static <T extends Number> T convert(Number value, com.landawn.abacus.type.Type<? extends T> targetType)
      Type Parameters:
      T -
      Parameters:
      value -
      targetType -
      Returns:
    • format

      public static String format(double x, String decimalFormat)
       
       Numbers.format(val, "0.0"); --> 12.1
       Numbers.format(val, "#.#"); --> 12.1
       Numbers.format(val, "0.00"); --> 12.10
       Numbers.format(val, "#.##"); --> 12.1
       Numbers.format(val, "0.000"); --> 12.105
       Numbers.format(val, "#.###"); --> 12.105
       Numbers.format(val, "0.0000"); --> 12.1050
       Numbers.format(val, "#.####"); --> 12.105
       Numbers.format(val, "0.00000"); --> 12.10500
       Numbers.format(val, "#.#####"); --> 12.105
       
       
      Parameters:
      x -
      decimalFormat -
      Returns:
      See Also:
    • format

      public static String format(float x, String decimalFormat)
       
       Numbers.format(val, "0.0"); --> 12.1
       Numbers.format(val, "#.#"); --> 12.1
       Numbers.format(val, "0.00"); --> 12.10
       Numbers.format(val, "#.##"); --> 12.1
       Numbers.format(val, "0.000"); --> 12.105
       Numbers.format(val, "#.###"); --> 12.105
       Numbers.format(val, "0.0000"); --> 12.1050
       Numbers.format(val, "#.####"); --> 12.105
       Numbers.format(val, "0.00000"); --> 12.10500
       Numbers.format(val, "#.#####"); --> 12.105
       
       
      Parameters:
      x -
      decimalFormat -
      Returns:
      See Also:
    • format

      public static String format(Float x, String decimalFormat)
      Parameters:
      x -
      decimalFormat -
      Returns:
      See Also:
    • format

      public static String format(Double x, String decimalFormat)
      Parameters:
      x -
      decimalFormat -
      Returns:
      See Also:
    • toByte

      public static byte toByte(String str) throws NumberFormatException
      Parameters:
      str -
      Returns:
      0 if the specified str is null or empty.
      Throws:
      NumberFormatException - If the string is not a parsable byte.
      See Also:
    • toByte

      public static byte toByte(Object obj) throws NumberFormatException
      Parameters:
      obj -
      Returns:
      0 if the specified obj is null.
      Throws:
      NumberFormatException - If the specified obj is not a Number and obj.toString() is not a parsable byte.
      See Also:
    • toByte

      public static byte toByte(String str, byte defaultValueForNull) throws NumberFormatException
      Parameters:
      str -
      defaultValueForNull -
      Returns:
      defaultValue if the specified str is null or empty.
      Throws:
      NumberFormatException - If the string is not a parsable byte.
      See Also:
    • toByte

      public static byte toByte(Object obj, byte defaultValueForNull) throws NumberFormatException
      Parameters:
      obj -
      defaultValueForNull -
      Returns:
      defaultValueForNull if the specified obj is null.
      Throws:
      NumberFormatException - If the specified obj is not a Number and obj.toString() is not a parsable byte.
      See Also:
    • toShort

      public static short toShort(String str) throws NumberFormatException
      Parameters:
      str -
      Returns:
      0 if the specified str is null or empty.
      Throws:
      NumberFormatException - If the string is not a parsable short.
      See Also:
    • toShort

      public static short toShort(Object obj) throws NumberFormatException
      Parameters:
      obj -
      Returns:
      0 if the specified obj is null.
      Throws:
      NumberFormatException - If the specified obj is not a Number and obj.toString() is not a parsable short.
      See Also:
    • toShort

      public static short toShort(String str, short defaultValueForNull) throws NumberFormatException
      Parameters:
      str -
      defaultValueForNull -
      Returns:
      defaultValue if the specified str is null or empty.
      Throws:
      NumberFormatException - If the string is not a parsable short.
      See Also:
    • toShort

      public static short toShort(Object obj, short defaultValueForNull) throws NumberFormatException
      Parameters:
      obj -
      defaultValueForNull -
      Returns:
      defaultValueForNull if the specified obj is null.
      Throws:
      NumberFormatException - If the specified obj is not a Number and obj.toString() is not a parsable short.
      See Also:
    • toInt

      public static int toInt(String str) throws NumberFormatException
      Parameters:
      str -
      Returns:
      0 if the specified str is null or empty.
      Throws:
      NumberFormatException - If the string is not a parsable int.
      See Also:
    • toInt

      public static int toInt(Object obj) throws NumberFormatException
      Parameters:
      obj -
      Returns:
      0 if the specified obj is null.
      Throws:
      NumberFormatException - If the specified obj is not a Number and obj.toString() is not a parsable int.
      See Also:
    • toInt

      public static int toInt(String str, int defaultValueForNull) throws NumberFormatException
      Parameters:
      str -
      defaultValueForNull -
      Returns:
      defaultValue if the specified str is null or empty.
      Throws:
      NumberFormatException - If the string is not a parsable int.
      See Also:
    • toInt

      public static int toInt(Object obj, int defaultValueForNull) throws NumberFormatException
      Parameters:
      obj -
      defaultValueForNull -
      Returns:
      defaultValueForNull if the specified obj is null.
      Throws:
      NumberFormatException - If the specified obj is not a Number and obj.toString() is not a parsable int.
      See Also:
      • #toInteger(String, int)
    • toLong

      public static long toLong(String str) throws NumberFormatException
      Parameters:
      str -
      Returns:
      0 if the specified str is null or empty.
      Throws:
      NumberFormatException - If the string is not a parsable long.
      See Also:
    • toLong

      public static long toLong(Object obj) throws NumberFormatException
      Parameters:
      obj -
      Returns:
      0 if the specified obj is null.
      Throws:
      NumberFormatException - If the specified obj is not a Number and obj.toString() is not a parsable long.
      See Also:
    • toLong

      public static long toLong(String str, long defaultValueForNull) throws NumberFormatException
      Parameters:
      str -
      defaultValueForNull -
      Returns:
      defaultValue if the specified str is null or empty.
      Throws:
      NumberFormatException - If the string is not a parsable long.
      See Also:
    • toLong

      public static long toLong(Object obj, long defaultValueForNull) throws NumberFormatException
      Parameters:
      obj -
      defaultValueForNull -
      Returns:
      defaultValueForNull if the specified obj is null.
      Throws:
      NumberFormatException - If the specified obj is not a Number and obj.toString() is not a parsable long.
      See Also:
    • toFloat

      public static float toFloat(String str) throws NumberFormatException
      Parameters:
      str -
      Returns:
      0 if the specified str is null or empty.
      Throws:
      NumberFormatException - If the string is not a parsable float.
      See Also:
    • toFloat

      public static float toFloat(Object obj) throws NumberFormatException
      Parameters:
      obj -
      Returns:
      0 if the specified obj is null.
      Throws:
      NumberFormatException - If the specified obj is not a Number and obj.toString() is not a parsable float.
      See Also:
    • toFloat

      public static float toFloat(String str, float defaultValueForNull) throws NumberFormatException
      Parameters:
      str -
      defaultValueForNull -
      Returns:
      defaultValue if the specified str is null or empty.
      Throws:
      NumberFormatException - If the string is not a parsable float.
      See Also:
    • toFloat

      public static float toFloat(Object obj, float defaultValueForNull) throws NumberFormatException
      Parameters:
      obj -
      defaultValueForNull -
      Returns:
      defaultValueForNull if the specified obj is null.
      Throws:
      NumberFormatException - If the specified obj is not a Number and obj.toString() is not a parsable float.
      See Also:
    • toDouble

      public static double toDouble(String str) throws NumberFormatException
      Parameters:
      str -
      Returns:
      0.0d if the specified str is null or empty.
      Throws:
      NumberFormatException - If the string is not a parsable double.
      See Also:
    • toDouble

      public static double toDouble(Object obj) throws NumberFormatException
      Parameters:
      obj -
      Returns:
      0 if the specified obj is null.
      Throws:
      NumberFormatException - If the specified obj is not a Number and obj.toString() is not a parsable double.
      See Also:
    • toDouble

      public static double toDouble(String str, double defaultValueForNull) throws NumberFormatException
      Parameters:
      str -
      defaultValueForNull -
      Returns:
      defaultValue if the specified str is null or empty.
      Throws:
      NumberFormatException - If the string is not a parsable double.
      See Also:
    • toDouble

      public static double toDouble(Object obj, double defaultValueForNull) throws NumberFormatException
      Parameters:
      obj -
      defaultValueForNull -
      Returns:
      defaultValueForNull if the specified obj is null.
      Throws:
      NumberFormatException - If the specified obj is not a Number and obj.toString() is not a parsable double.
      See Also:
    • toDouble

      public static double toDouble(BigDecimal value)

      Convert a BigDecimal to a double.

      If the BigDecimal value is null, then the specified default value is returned.

         NumberUtils.toDouble(null)                     = 0.0d
         NumberUtils.toDouble(BigDecimal.valudOf(8.5d)) = 8.5d
       
      Parameters:
      value - the BigDecimal to convert, may be null.
      Returns:
      the double represented by the BigDecimal or 0.0d if the BigDecimal is null.
      Since:
      3.8
    • toDouble

      public static double toDouble(BigDecimal value, double defaultValueForNull)

      Convert a BigDecimal to a double.

      If the BigDecimal value is null, then the specified default value is returned.

         NumberUtils.toDouble(null, 1.1d)                     = 1.1d
         NumberUtils.toDouble(BigDecimal.valudOf(8.5d), 1.1d) = 8.5d
       
      Parameters:
      value - the BigDecimal to convert, may be null.
      defaultValueForNull -
      Returns:
      the double represented by the BigDecimal or the defaultValue if the BigDecimal is null.
      Since:
      3.8
    • toScaledBigDecimal

      public static BigDecimal toScaledBigDecimal(BigDecimal value)
      Convert a BigDecimal to a BigDecimal with a scale of two that has been rounded using RoundingMode.HALF_EVEN. If the supplied value is null, then BigDecimal.ZERO is returned.

      Note, the scale of a BigDecimal is the number of digits to the right of the decimal point.

      Parameters:
      value - the BigDecimal to convert, may be null.
      Returns:
      the scaled, with appropriate rounding, BigDecimal.
      Since:
      3.8
    • toScaledBigDecimal

      public static BigDecimal toScaledBigDecimal(BigDecimal value, int scale, RoundingMode roundingMode)
      Convert a BigDecimal to a BigDecimal whose scale is the specified value with a RoundingMode applied. If the input value is null, we simply return BigDecimal.ZERO.
      Parameters:
      value - the BigDecimal to convert, may be null.
      scale - the number of digits to the right of the decimal point.
      roundingMode - a rounding behavior for numerical operations capable of discarding precision.
      Returns:
      the scaled, with appropriate rounding, BigDecimal.
      Since:
      3.8
    • toScaledBigDecimal

      public static BigDecimal toScaledBigDecimal(Float value)
      Convert a Float to a BigDecimal with a scale of two that has been rounded using RoundingMode.HALF_EVEN. If the supplied value is null, then BigDecimal.ZERO is returned.

      Note, the scale of a BigDecimal is the number of digits to the right of the decimal point.

      Parameters:
      value - the Float to convert, may be null.
      Returns:
      the scaled, with appropriate rounding, BigDecimal.
      Since:
      3.8
    • toScaledBigDecimal

      public static BigDecimal toScaledBigDecimal(Float value, int scale, RoundingMode roundingMode)
      Convert a Float to a BigDecimal whose scale is the specified value with a RoundingMode applied. If the input value is null, we simply return BigDecimal.ZERO.
      Parameters:
      value - the Float to convert, may be null.
      scale - the number of digits to the right of the decimal point.
      roundingMode - a rounding behavior for numerical operations capable of discarding precision.
      Returns:
      the scaled, with appropriate rounding, BigDecimal.
      Since:
      3.8
    • toScaledBigDecimal

      public static BigDecimal toScaledBigDecimal(Double value)
      Convert a Double to a BigDecimal with a scale of two that has been rounded using RoundingMode.HALF_EVEN. If the supplied value is null, then BigDecimal.ZERO is returned.

      Note, the scale of a BigDecimal is the number of digits to the right of the decimal point.

      Parameters:
      value - the Double to convert, may be null.
      Returns:
      the scaled, with appropriate rounding, BigDecimal.
      Since:
      3.8
    • toScaledBigDecimal

      public static BigDecimal toScaledBigDecimal(Double value, int scale, RoundingMode roundingMode)
      Convert a Double to a BigDecimal whose scale is the specified value with a RoundingMode applied. If the input value is null, we simply return BigDecimal.ZERO.
      Parameters:
      value - the Double to convert, may be null.
      scale - the number of digits to the right of the decimal point.
      roundingMode - a rounding behavior for numerical operations capable of discarding precision.
      Returns:
      the scaled, with appropriate rounding, BigDecimal.
      Since:
      3.8
    • toScaledBigDecimal

      public static BigDecimal toScaledBigDecimal(String value)
      Convert a String to a BigDecimal with a scale of two that has been rounded using RoundingMode.HALF_EVEN. If the supplied value is null, then BigDecimal.ZERO is returned.

      Note, the scale of a BigDecimal is the number of digits to the right of the decimal point.

      Parameters:
      value - the String to convert, may be null.
      Returns:
      the scaled, with appropriate rounding, BigDecimal.
      Since:
      3.8
    • toScaledBigDecimal

      public static BigDecimal toScaledBigDecimal(String value, int scale, RoundingMode roundingMode)
      Convert a String to a BigDecimal whose scale is the specified value with a RoundingMode applied. If the input value is null, we simply return BigDecimal.ZERO.
      Parameters:
      value - the String to convert, may be null.
      scale - the number of digits to the right of the decimal point.
      roundingMode - a rounding behavior for numerical operations capable of discarding precision.
      Returns:
      the scaled, with appropriate rounding, BigDecimal.
      Since:
      3.8
    • toIntExact

      public static int toIntExact(long value)
      Returns the value of the long argument; throwing an exception if the value overflows an int.
      Parameters:
      value - the long value
      Returns:
      Throws:
      ArithmeticException - if the argument overflows an int
      See Also:
    • createInteger

      public static Integer createInteger(String str) throws NumberFormatException

      Convert a String to a Integer, handling hex (0xhhhh) and octal (0dddd) notations. N.B. a leading zero means octal; spaces are not trimmed.

      null is returned if the specified str is null.

      Parameters:
      str - a String to convert, may be null
      Returns:
      Throws:
      NumberFormatException - if the value cannot be converted
      See Also:
    • createLong

      public static Long createLong(String str) throws NumberFormatException

      Convert a String to a Long; since 3.1 it handles hex (0Xhhhh) and octal (0ddd) notations. N.B. a leading zero means octal; spaces are not trimmed.

      null is returned if the specified str is null.

      Parameters:
      str - a String to convert, may be null
      Returns:
      Throws:
      NumberFormatException - if the value cannot be converted
      See Also:
    • createFloat

      public static Float createFloat(String str) throws NumberFormatException

      Convert a String to a Float.

      null is returned if the specified str is null.

      Parameters:
      str - a String to convert, may be null
      Returns:
      Throws:
      NumberFormatException - if the value cannot be converted
      See Also:
    • createDouble

      public static Double createDouble(String str) throws NumberFormatException

      Convert a String to a Double.

      null is returned if the specified str is null.

      Parameters:
      str - a String to convert, may be null
      Returns:
      Throws:
      NumberFormatException - if the value cannot be converted
      See Also:
    • createBigInteger

      public static BigInteger createBigInteger(String str) throws NumberFormatException

      Convert a String to a BigInteger; since 3.2 it handles hex (0x or #) and octal (0) notations.

      null is returned if the specified str is null.

      Parameters:
      str - a String to convert, may be null
      Returns:
      Throws:
      NumberFormatException - if the value cannot be converted
      See Also:
    • createBigDecimal

      public static BigDecimal createBigDecimal(String str) throws NumberFormatException

      Convert a String to a BigDecimal.

      null is returned if the specified str is null.

      Parameters:
      str - a String to convert, may be null
      Returns:
      Throws:
      NumberFormatException - if the value cannot be converted
      See Also:
    • createNumber

      public static Number createNumber(String str) throws NumberFormatException

      Turns a string value into a java.lang.Number.

      If the string starts with 0x or -0x (lower or upper case) or # or -#, it will be interpreted as a hexadecimal Integer - or Long, if the number of digits after the prefix is more than 8 - or BigInteger if there are more than 16 digits.

      Then, the value is examined for a type qualifier on the end, i.e. one of 'f','F','d','D','l','L'. If it is found, it starts trying to create successively larger types from the type specified until one is found that can represent the value.

      If a type specifier is not found, it will check for a decimal point and then try successively larger types from Integer to BigInteger and from double to BigDecimal.

      Integral values with a leading 0 will be interpreted as octal; the returned number will be Integer, Long or BigDecimal as appropriate.

      null is returned if the specified str is null.

      Parameters:
      str - a String containing a number, may be null
      Returns:
      Throws:
      NumberFormatException - if the value cannot be converted
      See Also:
    • isDigits

      public static boolean isDigits(String str)
      Checks whether the String contains only digit characters.

      null and empty String will return false.

      Parameters:
      str - the String to check
      Returns:
      true if str contains only Unicode numeric
    • isNumber

      public static boolean isNumber(String str)
      Note: It's copied from NumberUtils in Apache Commons Lang under Apache License 2.0

      It's same as isCreatable(String).
      See Also:
    • isCreatable

      public static boolean isCreatable(String str)

      Checks whether the String a valid Java number.

      Valid numbers include hexadecimal marked with the 0x or 0X qualifier, octal numbers, scientific notation and numbers marked with a type qualifier (e.g. 123L).

      Non-hexadecimal strings beginning with a leading zero are treated as octal values. Thus the string 09 will return false, since 9 is not a valid octal value. However, numbers beginning with 0. are treated as decimal.

      null and empty/blank String will return false.

      Note, createNumber(String) should return a number for every input resulting in true.

      Parameters:
      str - the String to check
      Returns:
      true if the string is a correctly formatted number
      See Also:
    • isParsable

      public static boolean isParsable(String str)

      Checks whether the given String is a parsable number.

      Parsable numbers include those Strings understood by Integer.parseInt(String), Long.parseLong(String), Float.parseFloat(String) or Double.parseDouble(String). This method can be used instead of catching NumberFormatException when calling one of those methods.

      Hexadecimal and scientific notations are not considered parsable. See isCreatable(String) on those cases.

      Null and empty String will return false.

      Parameters:
      str - the String to check.
      Returns:
      true if the string is a parsable number.
      Since:
      3.4
    • isPrime

      public static boolean isPrime(long n)
      Returns true if n is a prime number: an integer greater than one that cannot be factored into a product of smaller positive integers. Returns false if n is zero, one, or a composite number (one which can be factored into smaller positive integers).

      To test larger numbers, use BigInteger.isProbablePrime(int).

      Parameters:
      n -
      Returns:
      true, if is prime
      Throws:
      IllegalArgumentException - if n is negative
      Since:
      20.0
    • isPerfectSquare

      public static boolean isPerfectSquare(int n)
      Checks if is perfect square.
      Parameters:
      n -
      Returns:
      true, if is perfect square
    • isPerfectSquare

      public static boolean isPerfectSquare(long n)
      Checks if is perfect square.
      Parameters:
      n -
      Returns:
      true, if is perfect square
    • isPowerOfTwo

      public static boolean isPowerOfTwo(int x)
      Checks if is power of two.
      Parameters:
      x -
      Returns:
      true, if is power of two
    • isPowerOfTwo

      public static boolean isPowerOfTwo(long x)
      Checks if is power of two.
      Parameters:
      x -
      Returns:
      true, if is power of two
    • isPowerOfTwo

      public static boolean isPowerOfTwo(double x)
      Checks if is power of two.
      Parameters:
      x -
      Returns:
      true, if is power of two
    • isPowerOfTwo

      public static boolean isPowerOfTwo(BigInteger x)
      Returns true if x represents a power of two.
      Parameters:
      x -
      Returns:
      true, if is power of two
    • log

      public static double log(double a)
      Parameters:
      a -
      Returns:
    • log2

      public static int log2(int x, RoundingMode mode)
      Parameters:
      x -
      mode -
      Returns:
    • log2

      public static int log2(long x, RoundingMode mode)
      Returns the base-2 logarithm of x, rounded according to the specified rounding mode.
      Parameters:
      x -
      mode -
      Returns:
      Throws:
      IllegalArgumentException - if x <= 0
      ArithmeticException - if mode is RoundingMode.UNNECESSARY and x is not a power of two
    • log2

      public static double log2(double x)
      Returns the base 2 logarithm of a double value.

      Special cases:

      • If x is NaN or less than zero, the result is NaN.
      • If x is positive infinity, the result is positive infinity.
      • If x is positive or negative zero, the result is negative infinity.

      The computed result is within 1 ulp of the exact result.

      If the result of this method will be immediately rounded to an int, log2(double, RoundingMode) is faster.

      Parameters:
      x -
      Returns:
    • log2

      public static int log2(double x, RoundingMode mode)
      Returns the base 2 logarithm of a double value, rounded with the specified rounding mode to an int.

      Regardless of the rounding mode, this is faster than (int) log2(x).

      Parameters:
      x -
      mode -
      Returns:
      Throws:
      IllegalArgumentException - if x <= 0.0, x is NaN, or x is infinite
    • log2

      public static int log2(BigInteger x, RoundingMode mode)
      Returns the base-2 logarithm of x, rounded according to the specified rounding mode.
      Parameters:
      x -
      mode -
      Returns:
      Throws:
      IllegalArgumentException - if x <= 0
      ArithmeticException - if mode is RoundingMode.UNNECESSARY and x is not a power of two
    • log10

      public static int log10(int x, RoundingMode mode)
      Parameters:
      x -
      mode -
      Returns:
    • log10

      public static int log10(long x, RoundingMode mode)
      Returns the base-10 logarithm of x, rounded according to the specified rounding mode.
      Parameters:
      x -
      mode -
      Returns:
      Throws:
      IllegalArgumentException - if x <= 0
      ArithmeticException - if mode is RoundingMode.UNNECESSARY and x is not a power of ten
    • log10

      public static double log10(double x)
      Parameters:
      x -
      Returns:
    • log10

      public static int log10(BigInteger x, RoundingMode mode)
      Returns the base-10 logarithm of x, rounded according to the specified rounding mode.
      Parameters:
      x -
      mode -
      Returns:
      Throws:
      IllegalArgumentException - if x <= 0
      ArithmeticException - if mode is RoundingMode.UNNECESSARY and x is not a power of ten
    • pow

      public static int pow(int b, int k)
      Parameters:
      b -
      k -
      Returns:
    • pow

      public static long pow(long b, int k)
      Returns b to the kth power. Even if the result overflows, it will be equal to BigInteger.valueOf(b).pow(k).longValue(). This implementation runs in O(log k) time.
      Parameters:
      b -
      k -
      Returns:
      Throws:
      IllegalArgumentException - if k < 0
    • ceilingPowerOfTwo

      public static long ceilingPowerOfTwo(long x)
      Returns the smallest power of two greater than or equal to x. This is equivalent to checkedPow(2, log2(x, CEILING)).
      Parameters:
      x -
      Returns:
      Throws:
      IllegalArgumentException - if x <= 0
      ArithmeticException - of the next-higher power of two is not representable as a long, i.e. when x > 2^62
      Since:
      20.0
    • ceilingPowerOfTwo

      public static BigInteger ceilingPowerOfTwo(BigInteger x)
      Ceiling power of two.
      Parameters:
      x -
      Returns:
    • floorPowerOfTwo

      public static long floorPowerOfTwo(long x)
      Returns the largest power of two less than or equal to x. This is equivalent to checkedPow(2, log2(x, FLOOR)).
      Parameters:
      x -
      Returns:
      Throws:
      IllegalArgumentException - if x <= 0
      Since:
      20.0
    • floorPowerOfTwo

      public static BigInteger floorPowerOfTwo(BigInteger x)
      Floor power of two.
      Parameters:
      x -
      Returns:
    • sqrt

      public static int sqrt(int x, RoundingMode mode)
      Returns the square root of x, rounded with the specified rounding mode.
      Parameters:
      x -
      mode -
      Returns:
      Throws:
      IllegalArgumentException - if x < 0
      ArithmeticException - if mode is RoundingMode.UNNECESSARY and sqrt(x) is not an integer
    • sqrt

      public static long sqrt(long x, RoundingMode mode)
      Returns the square root of x, rounded with the specified rounding mode.
      Parameters:
      x -
      mode -
      Returns:
      Throws:
      IllegalArgumentException - if x < 0
      ArithmeticException - if mode is RoundingMode.UNNECESSARY and sqrt(x) is not an integer
    • sqrt

      public static BigInteger sqrt(BigInteger x, RoundingMode mode)
      Parameters:
      x -
      mode -
      Returns:
    • divide

      public static int divide(int p, int q, RoundingMode mode)
      Returns the result of dividing p by q, rounding using the specified RoundingMode.
      Parameters:
      p -
      q -
      mode -
      Returns:
      Throws:
      ArithmeticException - if q == 0, or if mode == UNNECESSARY and a is not an integer multiple of b
    • divide

      public static long divide(long p, long q, RoundingMode mode)
      Returns the result of dividing p by q, rounding using the specified RoundingMode.
      Parameters:
      p -
      q -
      mode -
      Returns:
      Throws:
      ArithmeticException - if q == 0, or if mode == UNNECESSARY and a is not an integer multiple of b
    • divide

      public static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode)
      Parameters:
      p -
      q -
      mode -
      Returns:
    • mod

      public static int mod(int x, int m)
      Returns x mod m, a non-negative value less than m. This differs from x % m, which might be negative.

      For example:

       
      
       mod(7, 4) == 3
       mod(-7, 4) == 1
       mod(-1, 4) == 3
       mod(-8, 4) == 0
       mod(8, 4) == 0
      Parameters:
      x -
      m -
      Returns:
      Throws:
      ArithmeticException - if m <= 0
      See Also:
    • mod

      public static int mod(long x, int m)
      Returns x mod m, a non-negative value less than m. This differs from x % m, which might be negative.

      For example:

       
      
       mod(7, 4) == 3
       mod(-7, 4) == 1
       mod(-1, 4) == 3
       mod(-8, 4) == 0
       mod(8, 4) == 0
      Parameters:
      x -
      m -
      Returns:
      Throws:
      ArithmeticException - if m <= 0
      See Also:
    • mod

      public static long mod(long x, long m)
      Returns x mod m, a non-negative value less than m. This differs from x % m, which might be negative.

      For example:

       
      
       mod(7, 4) == 3
       mod(-7, 4) == 1
       mod(-1, 4) == 3
       mod(-8, 4) == 0
       mod(8, 4) == 0
      Parameters:
      x -
      m -
      Returns:
      Throws:
      ArithmeticException - if m <= 0
      See Also:
    • gcd

      public static int gcd(int a, int b)
      Returns the greatest common divisor of a, b. Returns 0 if a == 0 && b == 0.
      Parameters:
      a -
      b -
      Returns:
      Throws:
      IllegalArgumentException - if a < 0 or b < 0
    • gcd

      public static long gcd(long a, long b)
      Returns the greatest common divisor of a, b. Returns 0 if a == 0 && b == 0.
      Parameters:
      a -
      b -
      Returns:
      Throws:
      IllegalArgumentException - if a < 0 or b < 0
    • lcm

      public static int lcm(int a, int b) throws ArithmeticException

      Returns the least common multiple of the absolute value of two numbers, using the formula lcm(a,b) = (a / gcd(a,b)) * b.

      Special cases:
      • The invocations lcm(Integer.MIN_VALUE, n) and lcm(n, Integer.MIN_VALUE), where abs(n) is a power of 2, throw an ArithmeticException, because the result would be 2^31, which is too large for an int value.
      • The result of lcm(0, x) and lcm(x, 0) is 0 for any x.
      Parameters:
      a - Number.
      b - Number.
      Returns:
      Throws:
      ArithmeticException - if the result cannot be represented as a non-negative int value.
      Since:
      1.1
    • lcm

      public static long lcm(long a, long b) throws ArithmeticException

      Returns the least common multiple of the absolute value of two numbers, using the formula lcm(a,b) = (a / gcd(a,b)) * b.

      Special cases:
      • The invocations lcm(Long.MIN_VALUE, n) and lcm(n, Long.MIN_VALUE), where abs(n) is a power of 2, throw an ArithmeticException, because the result would be 2^63, which is too large for an int value.
      • The result of lcm(0L, x) and lcm(x, 0L) is 0L for any x.
      Parameters:
      a - Number.
      b - Number.
      Returns:
      Throws:
      ArithmeticException - if the result cannot be represented as a non-negative long value.
      Since:
      2.1
    • addExact

      public static int addExact(int a, int b)
      Returns the sum of a and b, provided it does not overflow.
      Parameters:
      a -
      b -
      Returns:
      Throws:
      ArithmeticException - if a + b overflows in signed int arithmetic
    • addExact

      public static long addExact(long a, long b)
      Returns the sum of a and b, provided it does not overflow.
      Parameters:
      a -
      b -
      Returns:
      Throws:
      ArithmeticException - if a + b overflows in signed long arithmetic
    • subtractExact

      public static int subtractExact(int a, int b)
      Returns the difference of a and b, provided it does not overflow.
      Parameters:
      a -
      b -
      Returns:
      Throws:
      ArithmeticException - if a - b overflows in signed int arithmetic
    • subtractExact

      public static long subtractExact(long a, long b)
      Returns the difference of a and b, provided it does not overflow.
      Parameters:
      a -
      b -
      Returns:
      Throws:
      ArithmeticException - if a - b overflows in signed long arithmetic
    • multiplyExact

      public static int multiplyExact(int a, int b)
      Returns the product of a and b, provided it does not overflow.
      Parameters:
      a -
      b -
      Returns:
      Throws:
      ArithmeticException - if a * b overflows in signed int arithmetic
    • multiplyExact

      public static long multiplyExact(long a, long b)
      Returns the product of a and b, provided it does not overflow.
      Parameters:
      a -
      b -
      Returns:
      Throws:
      ArithmeticException - if a * b overflows in signed long arithmetic
    • powExact

      public static int powExact(int b, int k)
      Returns the b to the kth power, provided it does not overflow.

      pow(int, int) may be faster, but does not check for overflow.

      Parameters:
      b -
      k -
      Returns:
      Throws:
      ArithmeticException - if b to the kth power overflows in signed int arithmetic
    • powExact

      public static long powExact(long b, int k)
      Returns the b to the kth power, provided it does not overflow.
      Parameters:
      b -
      k -
      Returns:
      Throws:
      ArithmeticException - if b to the kth power overflows in signed long arithmetic
    • castExact

      public static int castExact(long value)
      Returns the int value that is equal to value, if possible.
      Parameters:
      value - any value in the range of the int type
      Returns:
      Throws:
      IllegalArgumentException - if value is greater than Integer.MAX_VALUE or less than Integer.MIN_VALUE
    • saturatedAdd

      public static int saturatedAdd(int a, int b)
      Returns the sum of a and b unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.
      Parameters:
      a -
      b -
      Returns:
      Since:
      20.0
    • saturatedAdd

      public static long saturatedAdd(long a, long b)
      Returns the sum of a and b unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.
      Parameters:
      a -
      b -
      Returns:
      Since:
      20.0
    • saturatedSubtract

      public static int saturatedSubtract(int a, int b)
      Returns the difference of a and b unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.
      Parameters:
      a -
      b -
      Returns:
      Since:
      20.0
    • saturatedSubtract

      public static long saturatedSubtract(long a, long b)
      Returns the difference of a and b unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.
      Parameters:
      a -
      b -
      Returns:
      Since:
      20.0
    • saturatedMultiply

      public static int saturatedMultiply(int a, int b)
      Returns the product of a and b unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.
      Parameters:
      a -
      b -
      Returns:
      Since:
      20.0
    • saturatedMultiply

      public static long saturatedMultiply(long a, long b)
      Returns the product of a and b unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.
      Parameters:
      a -
      b -
      Returns:
      Since:
      20.0
    • saturatedPow

      public static int saturatedPow(int b, int k)
      Returns the b to the kth power, unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.
      Parameters:
      b -
      k -
      Returns:
      Since:
      20.0
    • saturatedPow

      public static long saturatedPow(long b, int k)
      Returns the b to the kth power, unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.
      Parameters:
      b -
      k -
      Returns:
      Since:
      20.0
    • saturatedCast

      public static int saturatedCast(long value)
      Returns the int nearest in value to value.
      Parameters:
      value - any long value
      Returns:
      Integer.MAX_VALUE if it is too large, or Integer.MIN_VALUE if it is too small
    • factorial

      public static int factorial(int n)
      Returns n!, that is, the product of the first n positive integers, 1 if n == 0, or Integer.MAX_VALUE if the result does not fit in a int.
      Parameters:
      n -
      Returns:
      Throws:
      IllegalArgumentException - if n < 0
    • factorialToLong

      public static long factorialToLong(int n)
      Returns n!, that is, the product of the first n positive integers, 1 if n == 0, or Long.MAX_VALUE if the result does not fit in a long.
      Parameters:
      n -
      Returns:
      Throws:
      IllegalArgumentException - if n < 0
    • factorialToDouble

      public static double factorialToDouble(int n)
      Returns n!, that is, the product of the first n positive integers, 1 if n == 0, or n!, or Double.POSITIVE_INFINITY if n! > Double.MAX_VALUE.

      The result is within 1 ulp of the true value.

      Parameters:
      n -
      Returns:
      Throws:
      IllegalArgumentException - if n < 0
    • factorialToBigInteger

      public static BigInteger factorialToBigInteger(int n)
      Returns n!, that is, the product of the first n positive integers, or 1 if n == 0.

      Warning: the result takes O(n log n) space, so use cautiously.

      This uses an efficient binary recursive algorithm to compute the factorial with balanced multiplies. It also removes all the 2s from the intermediate products (shifting them back in at the end).

      Parameters:
      n -
      Returns:
      Throws:
      IllegalArgumentException - if n < 0
    • binomial

      public static int binomial(int n, int k)
      Returns n choose k, also known as the binomial coefficient of n and k, or Integer.MAX_VALUE if the result does not fit in an int.
      Parameters:
      n -
      k -
      Returns:
      Throws:
      IllegalArgumentException - if n < 0, k < 0 or k > n
    • binomialToLong

      public static long binomialToLong(int n, int k)
      Returns n choose k, also known as the binomial coefficient of n and k, or Long.MAX_VALUE if the result does not fit in a long.
      Parameters:
      n -
      k -
      Returns:
      Throws:
      IllegalArgumentException - if n < 0, k < 0, or k > n
    • binomialToBigInteger

      public static BigInteger binomialToBigInteger(int n, int k)
      Returns n choose k, also known as the binomial coefficient of n and k, that is, n! / (k! (n - k)!).

      Warning: the result can take as much as O(k log n) space.

      Parameters:
      n -
      k -
      Returns:
      Throws:
      IllegalArgumentException - if n < 0, k < 0, or k > n
    • mean

      public static int mean(int x, int y)
      Returns the arithmetic mean of x and y, rounded towards negative infinity. This method is overflow resilient.
      Parameters:
      x -
      y -
      Returns:
      Since:
      14.0
    • mean

      public static long mean(long x, long y)
      Returns the arithmetic mean of x and y, rounded toward negative infinity. This method is resilient to overflow.
      Parameters:
      x -
      y -
      Returns:
      Since:
      14.0
    • mean

      public static double mean(double x, double y)
      Parameters:
      x -
      y -
      Returns:
    • mean

      @SafeVarargs public static double mean(int... values)
      Returns the arithmetic mean of values.

      If these values are a sample drawn from a population, this is also an unbiased estimator of the arithmetic mean of the population.

      Parameters:
      values - a nonempty series of values
      Returns:
      Throws:
      IllegalArgumentException - if values is empty
    • mean

      @SafeVarargs public static double mean(long... values)
      Returns the arithmetic mean of values.

      If these values are a sample drawn from a population, this is also an unbiased estimator of the arithmetic mean of the population.

      Parameters:
      values - a nonempty series of values, which will be converted to double values (this may cause loss of precision for longs of magnitude over 2^53 (slightly over 9e15))
      Returns:
      Throws:
      IllegalArgumentException - if values is empty
    • mean

      @SafeVarargs public static double mean(double... values)
      Parameters:
      values -
      Returns:
    • round

      public static float round(float x, int scale)
      Parameters:
      x -
      scale -
      Returns:
      See Also:
    • round

      public static double round(double x, int scale)
      Parameters:
      x -
      scale -
      Returns:
      See Also:
    • round

      public static float round(float x, int scale, RoundingMode roundingMode)
      Parameters:
      x -
      scale -
      roundingMode -
      Returns:
      See Also:
    • round

      public static double round(double x, int scale, RoundingMode roundingMode)
      Parameters:
      x -
      scale -
      roundingMode -
      Returns:
      See Also:
    • round

      public static float round(float x, String decimalFormat)
      Parameters:
      x -
      decimalFormat -
      Returns:
      See Also:
    • round

      public static double round(double x, String decimalFormat)
      Parameters:
      x -
      decimalFormat -
      Returns:
      See Also:
    • round

      public static float round(float x, DecimalFormat decimalFormat)
      Parameters:
      x -
      decimalFormat -
      Returns:
      See Also:
    • round

      public static double round(double x, DecimalFormat decimalFormat)
      Parameters:
      x -
      decimalFormat -
      Returns:
      See Also:
    • roundToInt

      public static int roundToInt(double x, RoundingMode mode)
      Returns the int value that is equal to x rounded with the specified rounding mode, if possible.
      Parameters:
      x -
      mode -
      Returns:
      Throws:
      ArithmeticException - if
      • x is infinite or NaN
      • x, after being rounded to a mathematical integer using the specified rounding mode, is either less than Integer.MIN_VALUE or greater than Integer.MAX_VALUE
      • x is not a mathematical integer and mode is RoundingMode.UNNECESSARY
    • roundToLong

      public static long roundToLong(double x, RoundingMode mode)
      Returns the long value that is equal to x rounded with the specified rounding mode, if possible.
      Parameters:
      x -
      mode -
      Returns:
      Throws:
      ArithmeticException - if
      • x is infinite or NaN
      • x, after being rounded to a mathematical integer using the specified rounding mode, is either less than Long.MIN_VALUE or greater than Long.MAX_VALUE
      • x is not a mathematical integer and mode is RoundingMode.UNNECESSARY
    • roundToBigInteger

      public static BigInteger roundToBigInteger(double x, RoundingMode mode)
      Returns the BigInteger value that is equal to x rounded with the specified rounding mode, if possible.
      Parameters:
      x -
      mode -
      Returns:
      Throws:
      ArithmeticException - if
    • fuzzyEquals

      public static boolean fuzzyEquals(double a, double b, double tolerance)
      Returns true if a and b are within tolerance of each other.

      Technically speaking, this is equivalent to Math.abs(a - b) <= tolerance || Double.valueOf(a).equals(Double.valueOf(b)).

      Notable special cases include:

      • All NaNs are fuzzily equal.
      • If a == b, then a and b are always fuzzily equal.
      • Positive and negative zero are always fuzzily equal.
      • If tolerance is zero, and neither a nor b is NaN, then a and b are fuzzily equal if and only if a == b.
      • With Double.POSITIVE_INFINITY tolerance, all non-NaN values are fuzzily equal.
      • With finite tolerance, Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY are fuzzily equal only to themselves.

        This is reflexive and symmetric, but not transitive, so it is not an equivalence relation and not suitable for use in Object.equals(java.lang.Object) implementations.

      Parameters:
      a -
      b -
      tolerance -
      Returns:
      Throws:
      IllegalArgumentException - if tolerance is < 0 or NaN
      Since:
      13.0
    • fuzzyCompare

      public static int fuzzyCompare(double a, double b, double tolerance)
      Compares a and b "fuzzily," with a tolerance for nearly-equal values.

      This method is equivalent to fuzzyEquals(a, b, tolerance) ? 0 : Double.compare(a, b). In particular, like Double.compare(double, double), it treats all NaN values as equal and greater than all other values (including Double.POSITIVE_INFINITY).

      This is not a total ordering and is not suitable for use in Comparable.compareTo(T) implementations. In particular, it is not transitive.

      Parameters:
      a -
      b -
      tolerance -
      Returns:
      Throws:
      IllegalArgumentException - if tolerance is < 0 or NaN
      Since:
      13.0
    • isMathematicalInteger

      public static boolean isMathematicalInteger(double x)
      Returns true if x represents a mathematical integer.

      This is equivalent to, but not necessarily implemented as, the expression !Double.isNaN(x) && !Double.isInfinite(x) && x == Math.rint(x).

      Parameters:
      x -
      Returns:
      true, if is mathematical integer
    • asinh

      public static double asinh(double a)
      Compute the inverse hyperbolic sine of a number.
      Parameters:
      a - number on which evaluation is done
      Returns:
      inverse hyperbolic sine of a
    • acosh

      public static double acosh(double a)
      Compute the inverse hyperbolic cosine of a number.
      Parameters:
      a - number on which evaluation is done
      Returns:
      inverse hyperbolic cosine of a
    • atanh

      public static double atanh(double a)
      Compute the inverse hyperbolic tangent of a number.
      Parameters:
      a - number on which evaluation is done
      Returns:
      inverse hyperbolic tangent of a