Class NumberArithmetic

java.lang.Object
org.moeaframework.util.tree.NumberArithmetic

public class NumberArithmetic extends Object
Provides many arithmetic and trigonometric functions that operate on Numbers, performing any necessary implicit casting. An integer number remains an integer unless the specific function requires floating-point values. These methods favor Long and Double representations for integer and floating-point values, respectively.

The arithmetic functions provided herein support optional function protection, which is enabled by default. Function protection prevents values like Inf and NaN from appearing due to invalid inputs. For example, this protects against division-by-zero. To disable function protection, set the property org.moeaframework.util.tree.protected_functions = false in the file "moeaframework.properties". Inf and NaN values can still occur with function protection enabled, so outputs should still be validated.

  • Method Details

    • equals

      public static boolean equals(Number a, Number b)
      Returns true if the two numbers are equal; false otherwise.
      Parameters:
      a - the first number
      b - the second number
      Returns:
      true if the two numbers are equal; false otherwise
    • lessThan

      public static boolean lessThan(Number a, Number b)
      Returns true if the first number is less than the second; false otherwise.
      Parameters:
      a - the first number
      b - the second number
      Returns:
      true if the first number is less than the second; false otherwise
    • lessThanOrEqual

      public static boolean lessThanOrEqual(Number a, Number b)
      Returns true if the first number is less than or equal to the second; false otherwise.
      Parameters:
      a - the first number
      b - the second number
      Returns:
      true if the first number is less than or equal to the second; false otherwise
    • greaterThan

      public static boolean greaterThan(Number a, Number b)
      Returns true if the first number is greater than the second; false otherwise.
      Parameters:
      a - the first number
      b - the second number
      Returns:
      true if the first number is greater than the second; false otherwise
    • greaterThanOrEqual

      public static boolean greaterThanOrEqual(Number a, Number b)
      Returns true if the first number is greater than or equal to the second; false otherwise.
      Parameters:
      a - the first number
      b - the second number
      Returns:
      true if the first number is greater than or equal to the second; false otherwise
    • add

      public static Number add(Number a, Number b)
      Returns the value of adding the two numbers
      Parameters:
      a - the first number
      b - the second number
      Returns:
      the value of adding the two numbers
    • sqrt

      public static Number sqrt(Number a)
      Returns the square root of the number. If the number is less than zero and function protection is enabled, this functions the square root of the absolute value of the number.
      Parameters:
      a - the number
      Returns:
      the square root of the number
      See Also:
    • pow

      public static Number pow(Number a, Number b)
      Returns the value of the first number to the power of the second.
      Parameters:
      a - the first number
      b - the second number
      Returns:
      the value of the first number to the power of the second
      See Also:
    • sub

      public static Number sub(Number a, Number b)
      Returns the value of subtracting the first from the second number.
      Parameters:
      a - the first number
      b - the second number
      Returns:
      the value of subtracting the first from the second number
    • mul

      public static Number mul(Number a, Number b)
      Returns the value of multiplying the two numbers.
      Parameters:
      a - the first number
      b - the second number
      Returns:
      the value of multiplying the two numbers
    • div

      public static Number div(Number a, Number b)
      Returns the value of dividing the first number by the second. If the second argument is 0 and function protection is enabled, this function returns 1 regardless of the first argument's value.
      Parameters:
      a - the first number
      b - the second number
      Returns:
      the value of dividing the first number by the second
    • mod

      public static Number mod(Number a, Number b)
      Returns the remainder from dividing the first number by the second. If the second argument is 0 and function protection is enabled, this function returns 0 regardless of the first argument's value.
      Parameters:
      a - the first number
      b - the second number
      Returns:
      the remainder from dividing the first number by the second
    • floor

      public static Number floor(Number a)
      Returns the largest integer value less than or equal to the given number.
      Parameters:
      a - the number
      Returns:
      the largest integer value less than or equal to the given number
      See Also:
    • ceil

      public static Number ceil(Number a)
      Returns the smallest integer value greater than or equal to the given number.
      Parameters:
      a - the number
      Returns:
      the smallest integer value greater than or equal to the given number
      See Also:
    • round

      public static Number round(Number a)
      Returns the value of the number rounded to the nearest integer.
      Parameters:
      a - the number
      Returns:
      the value of the number rounded to the nearest integer
      See Also:
    • abs

      public static Number abs(Number a)
      Returns the absolute value of the number.
      Parameters:
      a - the number
      Returns:
      the absolute value of the number
      See Also:
    • log

      public static Number log(Number a)
      Returns the natural logarithm of the number. If the numbers is negative and function protection is enabled, then this function returns the natural logarithm of the absolute value of the number. If the number is near zero and function protection is enabled, this function returns 0.0.
      Parameters:
      a - the number
      Returns:
      the natural logarithm of the number
      See Also:
    • log10

      public static Number log10(Number a)
      Returns the base-10 logarithm of the number. If the numbers is negative and function protection is enabled, then this function returns the base-10 logarithm of the absolute value of the number. If the number is near zero and function protection is enabled, this function returns 0.0.
      Parameters:
      a - the number
      Returns:
      the base-10 logarithm of the number
      See Also:
    • exp

      public static Number exp(Number a)
      Returns the value from taking Euler's number e to the power of the given number.
      Parameters:
      a - the number
      Returns:
      the value from taking Euler's number e to the power of the given number
      See Also:
    • sin

      public static Number sin(Number a)
      Returns the trigonometric sine of the number.
      Parameters:
      a - the number
      Returns:
      the trigonometric sine of the number
      See Also:
    • cos

      public static Number cos(Number a)
      Returns the trigonometric cosine of the number.
      Parameters:
      a - the number
      Returns:
      the trigonometric cosine of the number
      See Also:
    • tan

      public static Number tan(Number a)
      Returns the trigonometric tangent of the number.
      Parameters:
      a - the number
      Returns:
      the trigonometric tangent of the number
      See Also:
    • asin

      public static Number asin(Number a)
      Returns the arc sine of the number.
      Parameters:
      a - the number
      Returns:
      the arc sine of the number
      See Also:
    • acos

      public static Number acos(Number a)
      Returns the arc cosine of the number.
      Parameters:
      a - the number
      Returns:
      the arc cosine of the number
      See Also:
    • atan

      public static Number atan(Number a)
      Returns the arc tangent of the number.
      Parameters:
      a - the number
      Returns:
      the arc tangent of the number
      See Also:
    • sinh

      public static Number sinh(Number a)
      Returns the hyperbolic sine of the number.
      Parameters:
      a - the number
      Returns:
      the hyperbolic sine of the number
      See Also:
    • cosh

      public static Number cosh(Number a)
      Returns the hyperbolic cosine of the number.
      Parameters:
      a - the number
      Returns:
      the hyperbolic cosine of the number
      See Also:
    • tanh

      public static Number tanh(Number a)
      Returns the hyperbolic tangent of the number.
      Parameters:
      a - the number
      Returns:
      the hyperbolic tangent of the number
      See Also:
    • asinh

      public static Number asinh(Number a)
      Returns the hyperbolic arc sine of the number.
      Parameters:
      a - the number
      Returns:
      the hyperbolic arc sine of the number
      See Also:
      • FastMath.asinh(double)
    • acosh

      public static Number acosh(Number a)
      Returns the hyperbolic arc cosine of the number.
      Parameters:
      a - the number
      Returns:
      the hyperbolic arc cosine of the number
      See Also:
      • FastMath.acosh(double)
    • atanh

      public static Number atanh(Number a)
      Returns the hyperbolic arc tangent of the number.
      Parameters:
      a - the number
      Returns:
      the hyperbolic arc tangent of the number
      See Also:
      • FastMath.atanh(double)
    • max

      public static Number max(Number a, Number b)
      Returns the maximum value of two numbers.
      Parameters:
      a - the first number
      b - the second number
      Returns:
      the maximum value of two numbers
      See Also:
    • min

      public static Number min(Number a, Number b)
      Returns the minimum value of two numbers.
      Parameters:
      a - the first number
      b - the second number
      Returns:
      the minimum value of two numbers
      See Also:
    • sign

      public static Number sign(Number a)
      Returns the sign of the number.
      Parameters:
      a - the number
      Returns:
      the sign of the number
      See Also:
    • isFloatingPoint

      public static boolean isFloatingPoint(Number a)
      Returns true if the number is a floating-point value; false otherwise.
      Parameters:
      a - the number
      Returns:
      true if the number is a floating-point value; false otherwise