public final class DoubleMath
extends java.lang.Object
Class provides static utility methods to perform simple numerical operations on double value. For example, direct double comparison is not safe, so double values need to be compared with a delta. This class provides methods for common check and compare operations.
Note:
These methods take argument to configure "accuracy" of an operation. But! These methods select appropriate "accuracy" automatically. Note again: there's no default constant epsilon (delta) to compare two double values. Instead, methods select "last affected" decimal place, based on the exponent of the specified value. See method:getLastAffectedDecimal(double)| Modifier and Type | Field and Description |
|---|---|
static int |
MAXIMUM_DEFAULT_DECIMALS
Maximum value that can be automatically selected for
round(double) method. |
static int |
MAXIMUM_POSSIBLE_DECIMALS
Maximum absolute value that can be used in the
roundTo(double, int) method. |
| Modifier and Type | Method and Description |
|---|---|
static java.lang.Double |
asDouble(java.lang.Number x)
If specified number is
null - returns null. |
static int |
compare(double a,
double b) |
static int |
compare(double a,
double b,
double accuracy) |
static boolean |
equals(java.lang.Double a,
java.lang.Double b) |
static boolean |
equals(java.lang.Double a,
java.lang.Double b,
double accuracy) |
static int |
getLastAffectedDecimal(double d)
Method returns dynamically calculated "index" (deximal position)
of the digit that is considered as last for rounding or comparison.
|
static boolean |
isFinite(java.lang.Double d) |
static boolean |
isNegative(java.lang.Double d) |
static boolean |
isNumber(java.lang.Double d) |
static boolean |
isPositive(java.lang.Double d) |
static boolean |
isZero(java.lang.Double d) |
static double |
round(double a) |
static double |
roundTo(double a,
int decimals) |
public static final int MAXIMUM_DEFAULT_DECIMALS
round(double) method. Also maximum decimal position to be used
for compare(double, double) method.public static final int MAXIMUM_POSSIBLE_DECIMALS
roundTo(double, int) method.public static java.lang.Double asDouble(java.lang.Number x)
If specified number is null - returns null.
If specified number is instance of Double - the same instance is returned.
In any other case - x.doubleValue() is returned.
public static boolean isZero(java.lang.Double d)
public static boolean isPositive(java.lang.Double d)
true if specified number is not null,
not Double.NaN, and greater than zero.public static boolean isNegative(java.lang.Double d)
true if specified number is not null,
not Double.NaN, and lower than zero.public static boolean isFinite(java.lang.Double d)
true if specified number is not null,
not Double.NaN, not Double.POSITIVE_INFINITY, and
not Double.NEGATIVE_INFINITYpublic static boolean isNumber(java.lang.Double d)
true if specified number is not null
and not Double.NaNpublic static boolean equals(java.lang.Double a,
java.lang.Double b)
public static boolean equals(java.lang.Double a,
java.lang.Double b,
double accuracy)
public static int compare(double a,
double b)
public static int compare(double a,
double b,
double accuracy)
public static double round(double a)
public static int getLastAffectedDecimal(double d)
Method returns dynamically calculated "index" (deximal position) of the digit that is considered as last for rounding or comparison. Used to dinamically calculate "accuracy" for the equality, comparison, or rounding operations.
Note again: there's no default constant epsilon (delta) to compare two double values. Instead, methods select "last affected" decimal place, based on the exponent of the specified value. Basic formula is:
(14 + 1) - number of digits before decimal pointNote that zero before decimal point is treated as one digit. For example:
0.01 selected decimal place will be: 14
0.1 selected decimal place will be: 14
1.0 selected decimal place will be: 14
10.0 selected decimal place will be: 14 - 1
10 000.0 selected decimal place will be: 14 - 4
Therefore the bigger the integer part of a number - the lower its "accuracy". Just the way doubles are.
But it doesn't work the other way around - selected decimal place never will be greater
than MAXIMUM_DEFAULT_DECIMALS.
Basically for any x where |x| < 10.0 - maximum possible accuracy will be used.
For any double with more than 14 digits before decimal point - 0 is returned.
public static double roundTo(double a,
int decimals)
MAXIMUM_POSSIBLE_DECIMALS