Package es.urjc.etsii.grafo.util
Class DoubleComparator
java.lang.Object
es.urjc.etsii.grafo.util.DoubleComparator
Helper methods to perform comparisons between doubles (or floats)
DANGER: DoubleComparator VIOLATES COMPARISON CONTRACT, DO NOT USE IN SORTS
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic int
comparator
(double d1, double d2) Compares the two specifieddouble
values.static int
comparator
(double d1, double d2, double eps) Compares the two specifieddouble
values.static boolean
equals
(double d1, double d2) Test two doubles for equality, uses default error marginstatic boolean
equals
(double d1, double d2, double eps) Test two doubles for equalitystatic boolean
isGreater
(double d1, double d2) Check if the first double is stricly greater than the secondstatic boolean
isGreaterOrEquals
(double d1, double d2) Check if the first double is greater than or equals the secondstatic boolean
isLess
(double d1, double d2) Check if the first double is strictly smaller than the secondstatic boolean
isLessOrEquals
(double d1, double d2) Check if the first double is smaller than or equals the secondstatic boolean
isNegative
(double d1) Check if a given double has a negative value.static boolean
isNegativeOrZero
(double d1) Check if a given double has a negative value or equals zero.static boolean
isPositive
(double d1) Check if a given double has a positive value.static boolean
isPositiveOrZero
(double d1) Check if a given double has a positive value or equals to zero.static boolean
isZero
(double d1) Check if a given double equals zero.static void
setPrecision
(double epsilon) BE CAREFUL WITH THIS METHOD!
-
Field Details
-
DEFAULT_EPSILON
public static final double DEFAULT_EPSILONDefault epsilon value. If the difference between two numbers is less than epsilon, the numbers are considered equal.- See Also:
-
-
Method Details
-
setPrecision
public static void setPrecision(double epsilon) BE CAREFUL WITH THIS METHOD! AFFECTS ALL THREADS Change default comparator precision. Defaults to DEFAULT_EPSILON- Parameters:
epsilon
- differences less than epsilon will not be counted as true differences. See the Wikipedia page for an intro on IEE754
-
equals
public static boolean equals(double d1, double d2) Test two doubles for equality, uses default error margin- Parameters:
d1
- first double to testd2
- second double to test- Returns:
- True, if the difference between them is less than 0.001%, false otherwise
-
equals
public static boolean equals(double d1, double d2, double eps) Test two doubles for equality- Parameters:
d1
- first double to testd2
- second double to testeps
- Error margin- Returns:
- True, if the difference between them is less than the error margin, false otherwise
-
comparator
public static int comparator(double d1, double d2, double eps) Compares the two specifieddouble
values. The sign of the integer value returned is the same as that of the integer that would be returned by the call:new Double(d1).compareTo(new Double(d2))
- Parameters:
d1
- the firstdouble
to compared2
- the seconddouble
to compareeps
- epsilon to use in comparison- Returns:
- the value
0
ifd1
is numerically equal tod2
; a value less than0
ifd1
is numerically less thand2
; and a value greater than0
ifd1
is numerically greater thand2
.
-
comparator
public static int comparator(double d1, double d2) Compares the two specifieddouble
values. The sign of the integer value returned is the same as that of the integer that would be returned by the call:new Double(d1).compareTo(new Double(d2))
- Parameters:
d1
- the firstdouble
to compared2
- the seconddouble
to compare- Returns:
- the value
0
ifd1
is numerically equal tod2
; a value less than0
ifd1
is numerically less thand2
; and a value greater than0
ifd1
is numerically greater thand2
.
-
isNegative
public static boolean isNegative(double d1) Check if a given double has a negative value.- Parameters:
d1
- double to check- Returns:
- true if d1 is strictly less than 0, false otherwise.
-
isNegativeOrZero
public static boolean isNegativeOrZero(double d1) Check if a given double has a negative value or equals zero.- Parameters:
d1
- double to check- Returns:
- true if d1 is less than or equal to 0, false otherwise.
-
isZero
public static boolean isZero(double d1) Check if a given double equals zero.- Parameters:
d1
- double to check- Returns:
- true if d1 equals 0, false otherwise.
-
isPositive
public static boolean isPositive(double d1) Check if a given double has a positive value.- Parameters:
d1
- double to check- Returns:
- true if d1 is strictly greater than 0, false otherwise.
-
isPositiveOrZero
public static boolean isPositiveOrZero(double d1) Check if a given double has a positive value or equals to zero.- Parameters:
d1
- double to check- Returns:
- true if d1 is greater than or equals to 0, false otherwise.
-
isGreater
public static boolean isGreater(double d1, double d2) Check if the first double is stricly greater than the second- Parameters:
d1
- first doubled2
- second double- Returns:
- true if d1 > d2, false otherwise
-
isGreaterOrEquals
public static boolean isGreaterOrEquals(double d1, double d2) Check if the first double is greater than or equals the second- Parameters:
d1
- first doubled2
- second double- Returns:
- true if d1 >= d2, false otherwise
-
isLess
public static boolean isLess(double d1, double d2) Check if the first double is strictly smaller than the second- Parameters:
d1
- first doubled2
- second double- Returns:
- true if d1 > d2, false otherwise
-
isLessOrEquals
public static boolean isLessOrEquals(double d1, double d2) Check if the first double is smaller than or equals the second- Parameters:
d1
- first doubled2
- second double- Returns:
- true if d1 <= d2, false otherwise
-