Class MathUtil


  • public final class MathUtil
    extends java.lang.Object
    Static helpers for working with numbers.

    Maybe not always the fastest solution to call in here, but working. Also usable for seeing examples and cutting code for manual inlining.

    Version:
    $Revision: 1.3 $
    Author:
    Achim.Westermann@gmx.de
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertDouble​(double d)
      Asserts that the given double is not invalid for calculation.
      static boolean assertEqual​(double d1, double d2, double precisionRange)
      Asserts if the given two doubles are equal within the given precision range by the operation:
      static MathUtil getInstance()
      Returns the singleton instance of this class.
      static java.lang.Integer increment​(java.lang.Integer value)
      Raises the given integer by one (bad performance).
      static boolean isDouble​(double d)
      Tests that the given double is not invalid for calculation.
      • Methods inherited from class java.lang.Object

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

      • assertDouble

        public static void assertDouble​(double d)
                                 throws java.lang.IllegalArgumentException
        Asserts that the given double is not invalid for calculation.

        It must not be one of:

        • Double.NaN
        • Double.NEGATIVE_INFINITY
        • Double.POSITIVE_INFINITY

        Parameters:
        d - the double to test.
        Throws:
        java.lang.IllegalArgumentException - if the assertion fails.
      • assertEqual

        public static boolean assertEqual​(double d1,
                                          double d2,
                                          double precisionRange)
        Asserts if the given two doubles are equal within the given precision range by the operation:
         Math.abs(d1 - d2) < precision
         
        .

        Because floating point calculations may involve rounding, calculated float and double values may not be accurate. This routine should be used instead. If called with a very small precision range this routine will not be stable against the rounding of calculated floats but at least prevent a bug report of the findbugs tool. See the Java Language Specification, section 4.2.4.

        Parameters:
        d1 - a double to check equality to the other given double.
        d2 - a double to check equality to the other given double.
        precisionRange - the range to allow differences of the two doubles without judging a difference - this is typically a small value below 0.5.
        Returns:
        true if both given doubles are equal within the given precision range.
      • getInstance

        public static MathUtil getInstance()
        Returns the singleton instance of this class.

        This method is useless for now as all methods are static. It may be used in future if VM-global configuration will be put to the state of the instance.

        #

        Returns:
        the singleton instance of this class.
      • increment

        public static java.lang.Integer increment​(java.lang.Integer value)
        Raises the given integer by one (bad performance).

        Warning: Only use this for testing code or prototypes as a new instance might be created for each call. Use primitive data types when fast calculations are required.

        Warning: Never do the following: Integer count = new Integer(6); MathUtil.increment(count); // don't expect count now carries 6. Integers are immutable. Write: Integer count = new Integer(6); count = MathUtil.increment(count);

        Parameters:
        value - the value to increase, if null is used a new instance will be initialized with value 0 and incremented.
        Returns:
        a (potentially new by the means of Integer.valueOf(int)) value increased by one.
      • isDouble

        public static boolean isDouble​(double d)
        Tests that the given double is not invalid for calculation.

        It must not be one of:

        • Double.NaN
        • Double.NEGATIVE_INFINITY
        • Double.POSITIVE_INFINITY

        Parameters:
        d - the double to test.
        Returns:
        true if the given double is valid for calculation (not infinite or NaN).