Class Precision

java.lang.Object
org.apache.commons.numbers.core.Precision

public final class Precision extends Object
Utilities for comparing numbers.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Interface containing comparison operations for doubles that allow values to be considered equal even if they are not exactly equal.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Largest double-precision floating-point number such that 1 + EPSILON is numerically equal to 1.
    private static final long
    Exponent offset in IEEE754 representation.
    private static final double
    Positive zero.
    static final double
    Safe minimum, such that 1 / SAFE_MIN does not overflow.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    compareTo(double x, double y, double eps)
    Compares two numbers given some amount of allowed error.
    static int
    compareTo(double x, double y, int maxUlps)
    Compares two numbers given some amount of allowed error.
    Creates a Precision.DoubleEquivalence instance that uses the given epsilon value for determining equality.
    static boolean
    equals(double x, double y)
    Returns true iff they are equal as defined by equals(x, y, 1).
    static boolean
    equals(double x, double y, double eps)
    Returns true if there is no double value strictly between the arguments or the difference between them is within the range of allowed error (inclusive).
    static boolean
    equals(double x, double y, int maxUlps)
    Returns true if the arguments are equal or within the range of allowed error (inclusive).
    static boolean
    equals(float x, float y)
    Returns true iff they are equal as defined by equals(x, y, 1).
    static boolean
    equals(float x, float y, float eps)
    Returns true if there is no float value strictly between the arguments or the difference between them is within the range of allowed error (inclusive).
    static boolean
    equals(float x, float y, int maxUlps)
    Returns true if the arguments are equal or within the range of allowed error (inclusive).
    static boolean
    equalsIncludingNaN(double x, double y)
    Returns true if the arguments are both NaN or they are equal as defined by equals(x, y, 1).
    static boolean
    equalsIncludingNaN(double x, double y, double eps)
    Returns true if the arguments are both NaN, there are no double value strictly between the arguments or the difference between them is within the range of allowed error (inclusive).
    static boolean
    equalsIncludingNaN(double x, double y, int maxUlps)
    Returns true if both arguments are NaN or if they are equal as defined by equals(x, y, maxUlps).
    static boolean
    equalsIncludingNaN(float x, float y)
    Returns true if both arguments are NaN or they are equal as defined by equals(x, y, 1).
    static boolean
    equalsIncludingNaN(float x, float y, float eps)
    Returns true if the arguments are both NaN, there are no float value strictly between the arguments or the difference between them is within the range of allowed error (inclusive).
    static boolean
    equalsIncludingNaN(float x, float y, int maxUlps)
    Returns true if both arguments are NaN or if they are equal as defined by equals(x, y, maxUlps).
    static boolean
    equalsWithRelativeTolerance(double x, double y, double eps)
    Returns true if there is no double value strictly between the arguments or the relative difference between them is less than or equal to the given tolerance.
    static double
    representableDelta(double x, double delta)
    Computes a number close to delta with the property that (x + delta - x) is exactly machine-representable.
    static double
    round(double x, int scale)
    Rounds the given value to the specified number of decimal places.
    static double
    round(double x, int scale, RoundingMode roundingMethod)
    Rounds the given value to the specified number of decimal places.

    Methods inherited from class java.lang.Object

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

    • EPSILON

      public static final double EPSILON

      Largest double-precision floating-point number such that 1 + EPSILON is numerically equal to 1. This value is an upper bound on the relative error due to rounding real numbers to double precision floating-point numbers.

      In IEEE 754 arithmetic, this is 2-53.

      See Also:
    • SAFE_MIN

      public static final double SAFE_MIN
      Safe minimum, such that 1 / SAFE_MIN does not overflow. In IEEE 754 arithmetic, this is also the smallest normalized number 2-1022.
      See Also:
    • EXPONENT_OFFSET

      private static final long EXPONENT_OFFSET
      Exponent offset in IEEE754 representation.
      See Also:
    • POSITIVE_ZERO

      private static final double POSITIVE_ZERO
      Positive zero.
      See Also:
  • Constructor Details

    • Precision

      private Precision()
      Private constructor.
  • Method Details

    • compareTo

      public static int compareTo(double x, double y, double eps)
      Compares two numbers given some amount of allowed error. The returned value is:
      • zero if considered equal using equals(x, y, eps)
      • negative if not equal and x < y
      • positive if not equal and x > y

      NaN values are handled as if using Double.compare(double, double) where the returned value is:

      • zero if NaN, NaN
      • negative if !NaN, NaN
      • positive if NaN, !NaN
      Parameters:
      x - First value.
      y - Second value.
      eps - Allowed error when checking for equality.
      Returns:
      0 if the value are considered equal, -1 if the first is smaller than the second, 1 if the first is larger than the second.
      See Also:
    • compareTo

      public static int compareTo(double x, double y, int maxUlps)
      Compares two numbers given some amount of allowed error. The returned value is:
      • zero if considered equal using equals(x, y, maxUlps)
      • negative if not equal and x < y
      • positive if not equal and x > y

      NaN values are handled as if using Double.compare(double, double) where the returned value is:

      • zero if NaN, NaN
      • negative if !NaN, NaN
      • positive if NaN, !NaN
      Parameters:
      x - First value.
      y - Second value.
      maxUlps - (maxUlps - 1) is the number of floating point values between x and y.
      Returns:
      0 if the value are considered equal, -1 if the first is smaller than the second, 1 if the first is larger than the second.
      See Also:
    • equals

      public static boolean equals(float x, float y)
      Returns true iff they are equal as defined by equals(x, y, 1).
      Parameters:
      x - first value
      y - second value
      Returns:
      true if the values are equal.
    • equalsIncludingNaN

      public static boolean equalsIncludingNaN(float x, float y)
      Returns true if both arguments are NaN or they are equal as defined by equals(x, y, 1).
      Parameters:
      x - first value
      y - second value
      Returns:
      true if the values are equal or both are NaN.
    • equals

      public static boolean equals(float x, float y, float eps)
      Returns true if there is no float value strictly between the arguments or the difference between them is within the range of allowed error (inclusive). Returns false if either of the arguments is NaN.
      Parameters:
      x - first value
      y - second value
      eps - the amount of absolute error to allow.
      Returns:
      true if the values are equal or within range of each other.
    • equalsIncludingNaN

      public static boolean equalsIncludingNaN(float x, float y, float eps)
      Returns true if the arguments are both NaN, there are no float value strictly between the arguments or the difference between them is within the range of allowed error (inclusive).
      Parameters:
      x - first value
      y - second value
      eps - the amount of absolute error to allow.
      Returns:
      true if the values are equal or within range of each other, or both are NaN.
    • equals

      public static boolean equals(float x, float y, int maxUlps)
      Returns true if the arguments are equal or within the range of allowed error (inclusive). Returns false if either of the arguments is NaN.

      Two double numbers are considered equal if there are (maxUlps - 1) (or fewer) floating point numbers between them, i.e. two adjacent floating point numbers are considered equal.

      Adapted from Bruce Dawson.

      Parameters:
      x - first value
      y - second value
      maxUlps - (maxUlps - 1) is the number of floating point values between x and y.
      Returns:
      true if there are fewer than maxUlps floating point values between x and y.
    • equalsIncludingNaN

      public static boolean equalsIncludingNaN(float x, float y, int maxUlps)
      Returns true if both arguments are NaN or if they are equal as defined by equals(x, y, maxUlps).
      Parameters:
      x - first value
      y - second value
      maxUlps - (maxUlps - 1) is the number of floating point values between x and y.
      Returns:
      true if both arguments are NaN or if there are less than maxUlps floating point values between x and y.
    • equals

      public static boolean equals(double x, double y)
      Returns true iff they are equal as defined by equals(x, y, 1).
      Parameters:
      x - first value
      y - second value
      Returns:
      true if the values are equal.
    • equalsIncludingNaN

      public static boolean equalsIncludingNaN(double x, double y)
      Returns true if the arguments are both NaN or they are equal as defined by equals(x, y, 1).
      Parameters:
      x - first value
      y - second value
      Returns:
      true if the values are equal or both are NaN.
    • equals

      public static boolean equals(double x, double y, double eps)
      Returns true if there is no double value strictly between the arguments or the difference between them is within the range of allowed error (inclusive). Returns false if either of the arguments is NaN.
      Parameters:
      x - First value.
      y - Second value.
      eps - Amount of allowed absolute error.
      Returns:
      true if the values are equal or within range of each other.
    • equalsWithRelativeTolerance

      public static boolean equalsWithRelativeTolerance(double x, double y, double eps)
      Returns true if there is no double value strictly between the arguments or the relative difference between them is less than or equal to the given tolerance. Returns false if either of the arguments is NaN.
      Parameters:
      x - First value.
      y - Second value.
      eps - Amount of allowed relative error.
      Returns:
      true if the values are two adjacent floating point numbers or they are within range of each other.
    • equalsIncludingNaN

      public static boolean equalsIncludingNaN(double x, double y, double eps)
      Returns true if the arguments are both NaN, there are no double value strictly between the arguments or the difference between them is within the range of allowed error (inclusive).
      Parameters:
      x - first value
      y - second value
      eps - the amount of absolute error to allow.
      Returns:
      true if the values are equal or within range of each other, or both are NaN.
    • equals

      public static boolean equals(double x, double y, int maxUlps)
      Returns true if the arguments are equal or within the range of allowed error (inclusive). Returns false if either of the arguments is NaN.

      Two float numbers are considered equal if there are (maxUlps - 1) (or fewer) floating point numbers between them, i.e. two adjacent floating point numbers are considered equal.

      Adapted from Bruce Dawson.

      Parameters:
      x - first value
      y - second value
      maxUlps - (maxUlps - 1) is the number of floating point values between x and y.
      Returns:
      true if there are fewer than maxUlps floating point values between x and y.
    • equalsIncludingNaN

      public static boolean equalsIncludingNaN(double x, double y, int maxUlps)
      Returns true if both arguments are NaN or if they are equal as defined by equals(x, y, maxUlps).
      Parameters:
      x - first value
      y - second value
      maxUlps - (maxUlps - 1) is the number of floating point values between x and y.
      Returns:
      true if both arguments are NaN or if there are less than maxUlps floating point values between x and y.
    • round

      public static double round(double x, int scale)
      Rounds the given value to the specified number of decimal places. The value is rounded using the RoundingMode.HALF_UP method.
      Parameters:
      x - Value to round.
      scale - Number of digits to the right of the decimal point.
      Returns:
      the rounded value.
    • round

      public static double round(double x, int scale, RoundingMode roundingMethod)
      Rounds the given value to the specified number of decimal places. The value is rounded using the given method which is any method defined in BigDecimal. If x is infinite or NaN, then the value of x is returned unchanged, regardless of the other parameters.
      Parameters:
      x - Value to round.
      scale - Number of digits to the right of the decimal point.
      roundingMethod - Rounding method as defined in BigDecimal.
      Returns:
      the rounded value.
      Throws:
      ArithmeticException - if roundingMethod is RoundingMode.UNNECESSARY and the specified scaling operation would require rounding.
    • representableDelta

      public static double representableDelta(double x, double delta)
      Computes a number close to delta with the property that (x + delta - x) is exactly machine-representable. This is useful when computing numerical derivatives, in order to reduce roundoff errors.
      Parameters:
      x - Value.
      delta - Offset value.
      Returns:
      the machine-representable floating number closest to the difference between x + delta and x.
    • doubleEquivalenceOfEpsilon

      public static Precision.DoubleEquivalence doubleEquivalenceOfEpsilon(double eps)
      Creates a Precision.DoubleEquivalence instance that uses the given epsilon value for determining equality.
      Parameters:
      eps - Value to use for determining equality.
      Returns:
      a new instance.