All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.monitorenter.util.math.MathUtil Maven / Gradle / Ivy

Go to download

JChart2D is an easy to use component for displaying two- dimensional traces in a coordinate system written in Java.

The newest version!
/*
 *  MathUtil, utility class for math operations.
 *  Copyright (C) 2004 - 2011 Achim Westermann.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 * 
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  If you modify or optimize the code in a useful way please let me know.
 *  [email protected]
 *
 */
package info.monitorenter.util.math;

/**
 * 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. *

* * @author [email protected] * * @version $Revision: 1.3 $ */ public final class MathUtil { /** Singleton instance. */ private static MathUtil instance = null; /** * Asserts that the given double is not invalid for calculation. *

* It must not be one of: *

    *
  • {@link Double#NaN}
  • *
  • {@link Double#NEGATIVE_INFINITY}
  • *
  • {@link Double#POSITIVE_INFINITY}
  • *
*

* * @param d * the double to test. * * @throws IllegalArgumentException * if the assertion fails. */ public static void assertDouble(final double d) throws IllegalArgumentException { if (!MathUtil.isDouble(d)) { throw new IllegalArgumentException(d + " is not valid."); } } /** * 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. *

* * @param d1 * a double to check equality to the other given double. * * @param d2 * a double to check equality to the other given double. * * @param precisionRange * the range to allow differences of the two doubles without * judging a difference - this is typically a small value below * 0.5. * * @return true if both given doubles are equal within the given precision * range. */ public static boolean assertEqual(final double d1, final double d2, final double precisionRange) { return Math.abs(d1 - d2) < precisionRange; } /** * 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. *

* # * * @return the singleton instance of this class. */ public static MathUtil getInstance() { if (MathUtil.instance == null) { MathUtil.instance = new MathUtil(); } return MathUtil.instance; } /** * 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); * * *

* * @param value * the value to increase, if null is used a new instance will be * initialized with value 0 and incremented. * * @return a (potentially new by the means of * {@link Integer#valueOf(int)}) value increased by one. */ public static Integer increment(final Integer value) { Integer result; int ival = 0; if (value != null) { ival = value.intValue(); } result = Integer.valueOf(ival + 1); return result; } /** * Tests that the given double is not invalid for calculation. *

* It must not be one of: *

    *
  • {@link Double#NaN}
  • *
  • {@link Double#NEGATIVE_INFINITY}
  • *
  • {@link Double#POSITIVE_INFINITY}
  • *
*

* * @param d * the double to test. * * @return true if the given double is valid for calculation (not infinite * or NaN). */ public static boolean isDouble(final double d) { return !(Double.isInfinite(d) || Double.isNaN(d)); } /** * Avoids creation from outside for singleton support. *

* */ private MathUtil() { // nop } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy