Class Doubles

java.lang.Object
com.globalmentor.java.Doubles

public class Doubles extends Object
Utilities for manipulating double objects and values.
Author:
Garret Wilson
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    checkRange(double value, double rangeMin, double rangeMax)
    Checks to make sure that a given value is within the given range.
    static boolean
    equalsLenient(double a, double b)
    Compares to double values to see if there are effectively equals.
    static boolean
    equalsLenient(double a, double b, double tolerance)
    Compares to double values to see if there are effectively equals, with a difference within some tolerance.

    Methods inherited from class java.lang.Object

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

    • Doubles

      public Doubles()
  • Method Details

    • checkRange

      public static double checkRange(double value, double rangeMin, double rangeMax)
      Checks to make sure that a given value is within the given range.
      Parameters:
      value - The value to check.
      rangeMin - The minimum range value, inclusive.
      rangeMax - The maximum range value, inclusive.
      Returns:
      The given value.
      Throws:
      IllegalArgumentException - if the value is less than the range minimum or greater than the range maximum.
    • equalsLenient

      public static boolean equalsLenient(double a, double b)
      Compares to double values to see if there are effectively equals. Double values that are equals as per == are always leniently equal; NaN are leniently equal; and zero is considered equal without regard to whether it is positive or negative.
      API Note:
      This is a convenience method that provides a default tolerance that most of the time is appropriate for comparing double values to see if they are "close enough".
      Implementation Specification:
      This implementation delegates to equalsLenient(double, double, double) using a tolerance derived of 1 ulp.
      Parameters:
      a - The first value to compare.
      b - The second value to compare.
      Returns:
      true if the values are effectively equal, with lenient comparison to allow for floating point rounding.
      See Also:
    • equalsLenient

      public static boolean equalsLenient(double a, double b, double tolerance)
      Compares to double values to see if there are effectively equals, with a difference within some tolerance. Double values that are equals as per == are always leniently equal; NaN are leniently equal; and zero is considered equal without regard to whether it is positive or negative. If the tolerance is zero, then the result is equivalent to == except that NaNs are considered equal.
      API Note:
      This method is equivalent to Math.abs(a - b) <= tolerance || Double.valueOf(a).equals(Double.valueOf(b)).
      Parameters:
      a - The first value to compare.
      b - The second value to compare.
      tolerance - The maximum allowed absolute difference between the values, or 0.0 (or NaN) if non-NaN comparison should work equivalent to ==.
      Returns:
      true if the values are effectively equal, with lenient comparison to allow for floating point rounding.
      See Also: