Package org.hipparchus.util
Class Precision
- java.lang.Object
-
- org.hipparchus.util.Precision
-
public class Precision extends Object
Utilities for comparing numbers.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intcompareTo(double x, double y, double eps)Compares two numbers given some amount of allowed error.static intcompareTo(double x, double y, int maxUlps)Compares two numbers given some amount of allowed error.static booleanequals(double x, double y)Returns true iff they are equal as defined byequals(x, y, 1).static booleanequals(double x, double y, double eps)Returnstrueif there is no double value strictly between the arguments or the difference between them is within the range of allowed error (inclusive).static booleanequals(double x, double y, int maxUlps)Returns true if the arguments are equal or within the range of allowed error (inclusive).static booleanequals(float x, float y)Returns true iff they are equal as defined byequals(x, y, 1).static booleanequals(float x, float y, float eps)Returns true if the arguments are equal or within the range of allowed error (inclusive).static booleanequals(float x, float y, int maxUlps)Returns true if the arguments are equal or within the range of allowed error (inclusive).static booleanequalsIncludingNaN(double x, double y)Returns true if the arguments are both NaN or they are equal as defined byequals(x, y, 1).static booleanequalsIncludingNaN(double x, double y, double eps)Returns true if the arguments are both NaN, are equal or are within the range of allowed error (inclusive).static booleanequalsIncludingNaN(double x, double y, int maxUlps)Returns true if both arguments are NaN or if they are equal as defined byequals(x, y, maxUlps).static booleanequalsIncludingNaN(float x, float y)Returns true if both arguments are NaN or they are equal as defined byequals(x, y, 1).static booleanequalsIncludingNaN(float x, float y, float eps)Returns true if the arguments are both NaN, are equal, or are within the range of allowed error (inclusive).static booleanequalsIncludingNaN(float x, float y, int maxUlps)Returns true if the arguments are both NaN or if they are equal as defined byequals(x, y, maxUlps).static booleanequalsWithRelativeTolerance(double x, double y, double eps)Returnstrueif there is no double value strictly between the arguments or the relative difference between them is less than or equal to the given tolerance.static booleanisMathematicalInteger(double x)Check is x is a mathematical integer.static booleanisMathematicalInteger(float x)Check is x is a mathematical integer.static doublerepresentableDelta(double x, double originalDelta)Computes a numberdeltaclose tooriginalDeltawith the property thatstatic doubleround(double x, int scale)Rounds the given value to the specified number of decimal places.static doubleround(double x, int scale, RoundingMode roundingMethod)Rounds the given value to the specified number of decimal places.static floatround(float x, int scale)Rounds the given value to the specified number of decimal places.static floatround(float x, int scale, RoundingMode roundingMethod)Rounds the given value to the specified number of decimal places.
-
-
-
Field Detail
-
EPSILON
public static final double EPSILON
Largest double-precision floating-point number such that1 + EPSILONis numerically equal to 1. This value is an upper bound on the relative error due to rounding real numbers to double precision floating-point numbers.In IEEE 754 arithmetic, this is 2-53.
- See Also:
- Machine epsilon
-
SAFE_MIN
public static final double SAFE_MIN
Safe minimum, such that1 / SAFE_MINdoes not overflow.
In IEEE 754 arithmetic, this is also the smallest normalized number 2-1022.
-
-
Method Detail
-
compareTo
public static int compareTo(double x, double y, double eps)Compares two numbers given some amount of allowed error.- Parameters:
x- the first numbery- the second numbereps- the amount of error to allow when checking for equality- Returns:
- 0 if
equals(x, y, eps) - < 0 if !
equals(x, y, eps)&& x < y - > 0 if !
equals(x, y, eps)&& x > y or either argument is NaN
- 0 if
-
compareTo
public static int compareTo(double x, double y, int maxUlps)Compares two numbers given some amount of allowed error. Two float numbers are considered equal if there are(maxUlps - 1)(or fewer) floating point numbers between them, i.e. two adjacent floating point numbers are considered equal. Adapted from Bruce Dawson. Returnsfalseif either of the arguments is NaN.- Parameters:
x- first valuey- second valuemaxUlps-(maxUlps - 1)is the number of floating point values betweenxandy.- Returns:
- 0 if
equals(x, y, maxUlps) - < 0 if !
equals(x, y, maxUlps)&& x < y - > 0 if !
equals(x, y, maxUlps)&& x > y or either argument is NaN
- 0 if
-
equals
public static boolean equals(float x, float y)Returns true iff they are equal as defined byequals(x, y, 1).- Parameters:
x- first valuey- second value- Returns:
trueif the values are equal.
-
equalsIncludingNaN
public static boolean equalsIncludingNaN(float x, float y)Returns true if both arguments are NaN or they are equal as defined byequals(x, y, 1).- Parameters:
x- first valuey- second value- Returns:
trueif the values are equal or both are NaN.
-
equals
public static boolean equals(float x, float y, float eps)Returns true if the arguments are equal or within the range of allowed error (inclusive). Returnsfalseif either of the arguments is NaN.- Parameters:
x- first valuey- second valueeps- the amount of absolute error to allow.- Returns:
trueif the values are equal or within range of each other.
-
equalsIncludingNaN
public static boolean equalsIncludingNaN(float x, float y, float eps)Returns true if the arguments are both NaN, are equal, or are within the range of allowed error (inclusive).- Parameters:
x- first valuey- second valueeps- the amount of absolute error to allow.- Returns:
trueif the values are equal or within range of each other, or both are NaN.
-
equals
public static boolean equals(float x, float y, int maxUlps)Returns true if the arguments are equal or within the range of allowed error (inclusive). Two float numbers are considered equal if there are(maxUlps - 1)(or fewer) floating point numbers between them, i.e. two adjacent floating point numbers are considered equal. Adapted from Bruce Dawson. Returnsfalseif either of the arguments is NaN.- Parameters:
x- first valuey- second valuemaxUlps-(maxUlps - 1)is the number of floating point values betweenxandy.- Returns:
trueif there are fewer thanmaxUlpsfloating point values betweenxandy.
-
equalsIncludingNaN
public static boolean equalsIncludingNaN(float x, float y, int maxUlps)Returns true if the arguments are both NaN or if they are equal as defined byequals(x, y, maxUlps).- Parameters:
x- first valuey- second valuemaxUlps-(maxUlps - 1)is the number of floating point values betweenxandy.- Returns:
trueif both arguments are NaN or if there are less thanmaxUlpsfloating point values betweenxandy.
-
equals
public static boolean equals(double x, double y)Returns true iff they are equal as defined byequals(x, y, 1).- Parameters:
x- first valuey- second value- Returns:
trueif the values are equal.
-
equalsIncludingNaN
public static boolean equalsIncludingNaN(double x, double y)Returns true if the arguments are both NaN or they are equal as defined byequals(x, y, 1).- Parameters:
x- first valuey- second value- Returns:
trueif the values are equal or both are NaN.
-
equals
public static boolean equals(double x, double y, double eps)Returnstrueif there is no double value strictly between the arguments or the difference between them is within the range of allowed error (inclusive). Returnsfalseif either of the arguments is NaN.- Parameters:
x- First value.y- Second value.eps- Amount of allowed absolute error.- Returns:
trueif the values are two adjacent floating point numbers or they are within range of each other.
-
equalsWithRelativeTolerance
public static boolean equalsWithRelativeTolerance(double x, double y, double eps)Returnstrueif there is no double value strictly between the arguments or the relative difference between them is less than or equal to the given tolerance. Returnsfalseif either of the arguments is NaN.- Parameters:
x- First value.y- Second value.eps- Amount of allowed relative error.- Returns:
trueif the values are two adjacent floating point numbers or they are within range of each other.
-
equalsIncludingNaN
public static boolean equalsIncludingNaN(double x, double y, double eps)Returns true if the arguments are both NaN, are equal or are within the range of allowed error (inclusive).- Parameters:
x- first valuey- second valueeps- the amount of absolute error to allow.- Returns:
trueif the values are equal or within range of each other, or both are NaN.
-
equals
public static boolean equals(double x, double y, int maxUlps)Returns true if the arguments are equal or within the range of allowed error (inclusive).Two float numbers are considered equal if there are
(maxUlps - 1)(or fewer) floating point numbers between them, i.e. two adjacent floating point numbers are considered equal.Adapted from Bruce Dawson. Returns
falseif either of the arguments is NaN.- Parameters:
x- first valuey- second valuemaxUlps-(maxUlps - 1)is the number of floating point values betweenxandy.- Returns:
trueif there are fewer thanmaxUlpsfloating point values betweenxandy.
-
equalsIncludingNaN
public static boolean equalsIncludingNaN(double x, double y, int maxUlps)Returns true if both arguments are NaN or if they are equal as defined byequals(x, y, maxUlps).- Parameters:
x- first valuey- second valuemaxUlps-(maxUlps - 1)is the number of floating point values betweenxandy.- Returns:
trueif both arguments are NaN or if there are less thanmaxUlpsfloating point values betweenxandy.
-
round
public static double round(double x, int scale)Rounds the given value to the specified number of decimal places. The value is rounded using theBigDecimal.ROUND_HALF_UPmethod.- Parameters:
x- Value to round.scale- Number of digits to the right of the decimal point.- Returns:
- the rounded value.
-
round
public static double round(double x, int scale, RoundingMode roundingMethod)Rounds the given value to the specified number of decimal places. The value is rounded using the given method which is any method defined inBigDecimal. Ifxis infinite orNaN, then the value ofxis returned unchanged, regardless of the other parameters.- Parameters:
x- Value to round.scale- Number of digits to the right of the decimal point.roundingMethod- Rounding method as defined inBigDecimal.- Returns:
- the rounded value.
- Throws:
ArithmeticException- ifroundingMethod == ROUND_UNNECESSARYand the specified scaling operation would require rounding.IllegalArgumentException- ifroundingMethoddoes not represent a valid rounding mode.
-
round
public static float round(float x, int scale)Rounds the given value to the specified number of decimal places. The value is rounded using theBigDecimal.ROUND_HALF_UPmethod.- Parameters:
x- Value to round.scale- Number of digits to the right of the decimal point.- Returns:
- the rounded value.
-
round
public static float round(float x, int scale, RoundingMode roundingMethod) throws MathRuntimeException, MathIllegalArgumentExceptionRounds the given value to the specified number of decimal places. The value is rounded using the given method which is any method defined inBigDecimal.- Parameters:
x- Value to round.scale- Number of digits to the right of the decimal point.roundingMethod- Rounding method as defined inBigDecimal.- Returns:
- the rounded value.
- Throws:
MathRuntimeException- if an exact operation is required but result is not exactMathIllegalArgumentException- ifroundingMethodis not a valid rounding method.
-
isMathematicalInteger
public static boolean isMathematicalInteger(double x)
Check is x is a mathematical integer.- Parameters:
x- number to check- Returns:
- true if x is a mathematical integer
- Since:
- 1.7
-
isMathematicalInteger
public static boolean isMathematicalInteger(float x)
Check is x is a mathematical integer.- Parameters:
x- number to check- Returns:
- true if x is a mathematical integer
- Since:
- 1.7
-
representableDelta
public static double representableDelta(double x, double originalDelta)Computes a numberdeltaclose tooriginalDeltawith the property that
is exactly machine-representable. This is useful when computing numerical derivatives, in order to reduce roundoff errors.x + delta - x- Parameters:
x- Value.originalDelta- Offset value.- Returns:
- a number
deltaso thatx + deltaandxdiffer by a representable floating number.
-
-