sirius.kernel.commons.Doubles Maven / Gradle / Ivy
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.kernel.commons;
/**
* Helper class for working with double numbers.
*/
public class Doubles {
/**
* Used as maximal epsilon (difference) when comparing numbers. Doubles are not compared using == due to numeric
* constraints when using floating point numbers.
*/
public static final double EPSILON = 0.000001d;
private Doubles() {
}
/**
* Returns the fractional part of the given value.
*
* The fractional part is "everything behind the decimal separator". So for 1.454 it will be 0.454. This method
* always returns an absolute value so the fractional part of -4.5656 will be 0.5656.
*
* @param val the number for return the fractional part of
* @return the fractional part [0..1) of the number
*/
public static double frac(double val) {
return Math.abs(Math.abs(val) - Math.floor(Math.abs(val)));
}
/**
* Determines if the given numbers are equal.
*
* The numbers are not compared for absolute exactness (as == would do) but a minimal delta ({@link #EPSILON})
* is permitted to make up for rounding errors which are in the nature of floating point numbers.
*
* @param a the first number to compare
* @param b the second number to compare
* @return true if the absolute difference of the given numbers is less than {@link #EPSILON},
* false otherwise
*/
public static boolean areEqual(double a, double b) {
return Math.abs(a - b) < EPSILON;
}
/**
* Determines if the given number is zero (or very very close to).
*
* Determines if the given number is zero or less than {@link #EPSILON} away from it. This is used to make up for
* rounding errors which are in the nature of floating point numbers.
*
* @param val the number to check if it is 0.
* @return true if the absolute value of the given numbers is less than {@link #EPSILON},
* false otherwise
*/
public static boolean isZero(double val) {
return Math.abs(val) < EPSILON;
}
}