Class MathOps

java.lang.Object
org.bzdev.lang.MathOps

public class MathOps extends Object
Mathematical operations related to java primitive types and some trivial extensions to the package Math. This class contains only static methods.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    asDouble(double value)
    Convert a double to a double.
    static double
    asDouble(int value)
    Convert an int to a double.
    static double
    asDouble(long value)
    Convert a long to a double.
    static int
    asInt(double value)
    Turn a double into an int.
    static long
    asLong(double value)
    Turn a double into a long.
    static float
    fceil(double value)
    Round a double-precision floating point number to the nearest single-precision floating point number larger than or equal to its value.
    static float
    ffloor(double value)
    Round a double-precision floating point number to the nearest single-precision floating point number less than or equal to its value.
    static float
    froundTowardInf(double value)
    Round a double-precision floating point number to the nearest single-precision floating point number whose absolute value is greater than or equal to the double-precision number's value.
    static float
    froundTowardZero(double value)
    Round a double-precision floating point number to the nearest single-precision floating point number whose absolute value is less than or equal to the double-precision number's value.
    static int
    gcd(int a, int b)
    Compute the greatest common divisor of two integers
    static long
    gcd(long a, long b)
    Compute the greatest common divisor of two non-negative long integers
    static int
    intRound(double value)
    Round a double-precision number to produce an integer.
    static double
    log2(int n)
    Compute the base-2 logarithm of an integer n.
    static double
    log2(int n, double accuracy)
    Compute the base-2 logarithm of an integer n, specifying its accuracy.
    static double
    log2(long n)
    Compute the base-2 logarithm of a long integer n.
    static double
    log2(long n, double accuracy)
    Compute the base-2 logarithm of a long integer n, specifying its accuracy.
    static long
    lPow(int x, int n)
    Raise an integer to to a non-negative integer power.
    static double
    pow(double x, int n)
    Raise a number to an integer power.
    static double
    pow(double z, int xn, int xd)
    Raise a number x to a rational power.
    static double
    pow(double x, long n)
    Raise a number to a long integer power.
    static double
    root(int n, double x)
    Compute the nth root of a real number.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • asInt

      public static int asInt(double value) throws IllegalArgumentException
      Turn a double into an int. This method is provided for convenience when scripting languages such as the ones supported by ExpressionParser are used: ExpressionParser has no explicit iteration constructs and instead relies on Java streams. In this case one may have to force an expression to provide a specific numberical type.
      Parameters:
      value - the double-precision value
      Returns:
      the corresponding integer
      Throws:
      IllegalArgumentException - if the double value is not the value one would obtain by converting the closest integer into a double-precision value or if the integer value is too large or too small to be prepresented as an int
    • asLong

      public static long asLong(double value) throws IllegalArgumentException
      Turn a double into a long. This method is provided for convenience when scripting languages such as the ones supported by ExpressionParser are used: ExpressionParser has no explicit iteration constructs and instead relies on Java streams. In this case one may have to force an expression to provide a specific numberical type.
      Parameters:
      value - the double-precision value
      Returns:
      the corresponding integer
      Throws:
      IllegalArgumentException - if the double value is not the value one would obtain by converting the closest integer into a double-precision value
    • asDouble

      public static double asDouble(int value)
      Convert an int to a double. This method is provided for convenience when scripting languages such as the ones supported by ExpressionParser are used: ExpressionParser has no explicit iteration constructs and instead relies on Java streams. In this case one may have to force an expression to provide a specific numberical type.
      Parameters:
      value - the value to convert
      Returns:
      the corresponding double-precision value
    • asDouble

      public static double asDouble(long value)
      Convert a long to a double. This method is provided for convenience when scripting languages such as the ones supported by ExpressionParser are used: ExpressionParser has no explicit iteration constructs and instead relies on Java streams. In this case one may have to force an expression to provide a specific numberical type.
      Parameters:
      value - the value to convert
      Returns:
      the corresponding double-precision value
    • asDouble

      public static double asDouble(double value)
      Convert a double to a double. This method simply returns its value, and is provided for convenience when scripting languages such as the ones supported by ExpressionParser are used: ExpressionParser has no explicit iteration constructs and instead relies on Java streams. In this case one may have to force an expression to provide a specific numberical type.
      Parameters:
      value - the value to convert
      Returns:
      the corresponding double-precision value
    • fceil

      public static float fceil(double value)
      Round a double-precision floating point number to the nearest single-precision floating point number larger than or equal to its value.
      Parameters:
      value - the value to round
      Returns:
      the closest single-precision floating point number greater than or equal to the argument
    • ffloor

      public static float ffloor(double value)
      Round a double-precision floating point number to the nearest single-precision floating point number less than or equal to its value.
      Parameters:
      value - the value to round
      Returns:
      the closest single-precision floating point number less than or equal to the argument
    • froundTowardZero

      public static float froundTowardZero(double value)
      Round a double-precision floating point number to the nearest single-precision floating point number whose absolute value is less than or equal to the double-precision number's value.
      Parameters:
      value - the value to round
      Returns:
      the closest single-precision floating point number less than or equal to the argument
    • froundTowardInf

      public static float froundTowardInf(double value)
      Round a double-precision floating point number to the nearest single-precision floating point number whose absolute value is greater than or equal to the double-precision number's value.
      Parameters:
      value - the value to round
      Returns:
      the closest single-precision floating point number greater than or equal to the argument
    • gcd

      public static int gcd(int a, int b) throws IllegalArgumentException
      Compute the greatest common divisor of two integers
      Parameters:
      a - the first integer
      b - the second integer
      Returns:
      the greatest common divisor of a and b
      Throws:
      IllegalArgumentException - both arguments were zero
    • gcd

      public static long gcd(long a, long b) throws IllegalArgumentException
      Compute the greatest common divisor of two non-negative long integers
      Parameters:
      a - the first integer
      b - the second integer
      Returns:
      the greatest common divisor of a and b
      Throws:
      IllegalArgumentException - both arguments were zero
    • intRound

      public static int intRound(double value) throws IllegalArgumentException
      Round a double-precision number to produce an integer.

      This method was added specifically to support the class ExpressionParser: Java does not let one cast a double-precision method to an integer and Math.round(double) returns a long integer.

      Parameters:
      value - the value to round
      Returns:
      the value rounded to the nearest integer
      Throws:
      IllegalArgumentException - the value is out of range and cannot be converted to an integer
    • pow

      public static double pow(double x, int n)
      Raise a number to an integer power. This method is provided to supplement the method Math.pow(double, double) by providing a method whose second argument is an integer. This is useful in some infinite series expansions of special functions. For example, for small z, the spherical Bessel function jn(z) has a first term proportional to zn in its Taylor series expansion (for larger values of |z|, the implementation in Functions does not use this Taylor series).

      The time complexity is O(log2 n). Timing measurements indicate that it is faster than java.lang.Math.pow (which handles more cases).

      Parameters:
      x - a real number
      n - the power
      Returns:
      the value of xn
    • pow

      public static double pow(double x, long n)
      Raise a number to a long integer power. This method is provided to supplement the method Math.pow(double, double) by providing a method whose second argument is a long integer. This is useful in some infinite series expansions of special functions. For example, for small z, the spherical Bessel function jn(z) has a first term proportional to zn in its Taylor series expansion (for larger values of |z|, the implementation in Functions does not use this Taylor series).

      The time complexity is O(log2 n). Timing measurements indicate that it is faster than java.lang.Math.pow (which handles more cases).

      Parameters:
      x - a real number
      n - the power
      Returns:
      the value of xn
    • root

      public static double root(int n, double x)
      Compute the nth root of a real number. The second argument must be non-negative when the first argument is an even integer.
      Parameters:
      n - the root index (2 for the square root, 3 for the cube root, etc.)
      x - the number whose root is to be computed.
      Returns:
      the nth root
      Throws:
      IllegalArgumentException - an argument was out of range
    • pow

      public static double pow(double z, int xn, int xd)
      Raise a number x to a rational power. This method is provided to supplement the method Math.pow(double, double) by providing a method whose second argument is the numerator of the power and whose third argument is the denominator.
      Parameters:
      z - a real number
      xn - the numerator of fraction representing a rational number for the power
      xd - the denominator of a fraction representing a rational number for the power
      Returns:
      the value of xxn/xd
    • lPow

      public static long lPow(int x, int n) throws IllegalArgumentException, ArithmeticException
      Raise an integer to to a non-negative integer power. This method is provided to supplement the method Math.pow(double, double) by providing a method whose second argument is an integer. This is useful in some infinite series expansions of special functions. For example, for small z, the spherical Bessel function jn(z) has a first term proportional to zn in its Taylor series expansion.

      The time complexity is O(log2 n). Timing measurements indicate that it is faster than java.lang.Math.pow (which handles more cases).

      Parameters:
      x - an integer
      n - the power
      Returns:
      the value of xn
      Throws:
      IllegalArgumentException - an argument was out of bounds
      ArithmeticException - the combination of arguments would overflow the integer representation used
    • log2

      public static double log2(int n) throws IllegalArgumentException
      Compute the base-2 logarithm of an integer n.
      Parameters:
      n - an integer that is larger than 0
      Returns:
      the base-2 logarithm of n
      Throws:
      IllegalArgumentException - an argument was out of range
    • log2

      public static double log2(int n, double accuracy) throws IllegalArgumentException
      Compute the base-2 logarithm of an integer n, specifying its accuracy. One use of this method is in estimating the time or space needed by an algorithm, where precise values are not needed.

      Note: when the accuracy is set to 1.0, the value returned is floor(log2(n)). The accuracy is an upper bound on the difference between the returned value and the actual value.

      Parameters:
      n - an integer that is larger than 0
      accuracy - the accuracy of the fractional part of the returned value (values should be in the range (0.0, 1.0])
      Returns:
      the base-2 logarithm of n
      Throws:
      IllegalArgumentException - an argument was out of range
    • log2

      public static double log2(long n) throws IllegalArgumentException
      Compute the base-2 logarithm of a long integer n.
      Parameters:
      n - an integer that is larger than 0
      Returns:
      the base-2 logarithm of n
      Throws:
      IllegalArgumentException - an argument was out of range
    • log2

      public static double log2(long n, double accuracy) throws IllegalArgumentException
      Compute the base-2 logarithm of a long integer n, specifying its accuracy. One use of this method is in estimating the time or space needed by an algorithm, where precise values are not needed.
      Parameters:
      n - a long integer that is larger than 0
      accuracy - the accuracy of the fractional part of the returned value (values should be in the range (0.0, 1.0])
      Returns:
      the base-2 logarithm of n
      Throws:
      IllegalArgumentException - an argument was out of range