Top Description Fields Constructors Methods
org.apache.commons.math3.util

public Class Precision

extends Object
Class Inheritance
Imports
java.math.BigDecimal, org.apache.commons.math3.exception.MathArithmeticException, .MathIllegalArgumentException, org.apache.commons.math3.exception.util.LocalizedFormats

Utilities for comparing numbers.
Since
3.0

Field Summary

Modifier and TypeField and Description
public static final double
EPSILON

Largest double-precision floating-point number such that 1 + EPSILON is numerically equal to 1.

private static final long
EXPONENT_OFFSET

Exponent offset in IEEE754 representation.

private static final long
NEGATIVE_ZERO_DOUBLE_BITS

Negative zero bits.

private static final int
NEGATIVE_ZERO_FLOAT_BITS

Negative zero bits.

private static final double
POSITIVE_ZERO

Positive zero.

private static final long
POSITIVE_ZERO_DOUBLE_BITS

Positive zero bits.

private static final int
POSITIVE_ZERO_FLOAT_BITS

Positive zero bits.

public static final double
SAFE_MIN

Safe minimum, such that 1 / SAFE_MIN does not overflow.

private static final long
SGN_MASK

Offset to order signed double numbers lexicographically.

private static final int
SGN_MASK_FLOAT

Offset to order signed double numbers lexicographically.

Constructor Summary

AccessConstructor and Description
private
Precision()

Private constructor.

Method Summary

Modifier and TypeMethod and Description
public static int

Returns:

compareTo
(double
the first number
x
,
double
the second number
y
,
double
the amount of error to allow when checking for equality
eps
)

Compares two numbers given some amount of allowed error.

public static int

Returns:

compareTo
(final double
first value
x
,
final double
second value
y
,
final int
(maxUlps - 1) is the number of floating point values between x and y.
maxUlps
)

Compares two numbers given some amount of allowed error.

public static boolean

Returns:

true if the values are equal.
equals
(float
first value
x
,
float
second value
y
)

Returns true iff they are equal as defined by equals(x, y, 1).

public static boolean

Returns:

true if the values are equal or within range of each other.
equals
(float
first value
x
,
float
second value
y
,
float
the amount of absolute error to allow.
eps
)

Returns true if the arguments are equal or within the range of allowed error (inclusive).

public static boolean

Returns:

true if there are fewer than maxUlps floating point values between x and y.
equals
(final float
first value
x
,
final float
second value
y
,
final int
(maxUlps - 1) is the number of floating point values between x and y.
maxUlps
)

Returns true if the arguments are equal or within the range of allowed error (inclusive).

public static boolean

Returns:

true if the values are equal.
equals
(double
first value
x
,
double
second value
y
)

Returns true iff they are equal as defined by equals(x, y, 1).

public static boolean

Returns:

true if the values are two adjacent floating point numbers or they are within range of each other.
equals
(double
First value.
x
,
double
Second value.
y
,
double
Amount of allowed absolute error.
eps
)

Returns true if there is no double value strictly between the arguments or the difference between them is within the range of allowed error (inclusive).

public static boolean

Returns:

true if there are fewer than maxUlps floating point values between x and y.
equals
(final double
first value
x
,
final double
second value
y
,
final int
(maxUlps - 1) is the number of floating point values between x and y.
maxUlps
)

Returns true if the arguments are equal or within the range of allowed error (inclusive).

public static boolean

Returns:

true if the values are equal or both are NaN.
equalsIncludingNaN
(float
first value
x
,
float
second value
y
)

Returns true if both arguments are NaN or they are equal as defined by equals(x, y, 1).

public static boolean

Returns:

true if the values are equal or within range of each other, or both are NaN.
equalsIncludingNaN
(float
first value
x
,
float
second value
y
,
float
the amount of absolute error to allow.
eps
)

Returns true if the arguments are both NaN, are equal, or are within the range of allowed error (inclusive).

public static boolean

Returns:

true if both arguments are NaN or if there are less than maxUlps floating point values between x and y.
equalsIncludingNaN
(float
first value
x
,
float
second value
y
,
int
(maxUlps - 1) is the number of floating point values between x and y.
maxUlps
)

Returns true if the arguments are both NaN or if they are equal as defined by equals(x, y, maxUlps).

public static boolean

Returns:

true if the values are equal or both are NaN.
equalsIncludingNaN
(double
first value
x
,
double
second value
y
)

Returns true if the arguments are both NaN or they are equal as defined by equals(x, y, 1).

public static boolean

Returns:

true if the values are equal or within range of each other, or both are NaN.
equalsIncludingNaN
(double
first value
x
,
double
second value
y
,
double
the amount of absolute error to allow.
eps
)

Returns true if the arguments are both NaN, are equal or are within the range of allowed error (inclusive).

public static boolean

Returns:

true if both arguments are NaN or if there are less than maxUlps floating point values between x and y.
equalsIncludingNaN
(double
first value
x
,
double
second value
y
,
int
(maxUlps - 1) is the number of floating point values between x and y.
maxUlps
)

Returns true if both arguments are NaN or if they are equal as defined by equals(x, y, maxUlps).

public static boolean

Returns:

true if the values are two adjacent floating point numbers or they are within range of each other.
equalsWithRelativeTolerance
(double
First value.
x
,
double
Second value.
y
,
double
Amount of allowed relative error.
eps
)

Returns true if there is no double value strictly between the arguments or the relative difference between them is less than or equal to the given tolerance.

public static double

Returns:

a number delta so that x + delta and x differ by a representable floating number.
representableDelta
(double
Value.
x
,
double
Offset value.
originalDelta
)

Computes a number delta close to originalDelta with the property that


  x + delta - x
public static double

Returns:

the rounded value.
round
(double
Value to round.
x
,
int
Number of digits to the right of the decimal point.
scale
)

Rounds the given value to the specified number of decimal places.

public static double

Returns:

the rounded value.
round
(double
Value to round.
x
,
int
Number of digits to the right of the decimal point.
scale
,
int
Rounding method as defined in BigDecimal.
roundingMethod
)

Rounds the given value to the specified number of decimal places.

public static float

Returns:

the rounded value.
round
(float
Value to round.
x
,
int
Number of digits to the right of the decimal point.
scale
)

Rounds the given value to the specified number of decimal places.

public static float

Returns:

the rounded value.
round
(float
Value to round.
x
,
int
Number of digits to the right of the decimal point.
scale
,
int
Rounding method as defined in BigDecimal.
roundingMethod
)

Rounds the given value to the specified number of decimal places.

private static double

Returns:

the rounded value.
roundUnscaled
(double
Value to round.
unscaled
,
double
Sign of the original, scaled value.
sign
,
int
Rounding method, as defined in BigDecimal.
roundingMethod
)

Rounds the given non-negative value to the "nearest" integer.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EPSILONback to summary
public static final double EPSILON

Largest double-precision floating-point number such that 1 + EPSILON is 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
EXPONENT_OFFSETback to summary
private static final long EXPONENT_OFFSET

Exponent offset in IEEE754 representation.

NEGATIVE_ZERO_DOUBLE_BITSback to summary
private static final long NEGATIVE_ZERO_DOUBLE_BITS

Negative zero bits.

NEGATIVE_ZERO_FLOAT_BITSback to summary
private static final int NEGATIVE_ZERO_FLOAT_BITS

Negative zero bits.

POSITIVE_ZEROback to summary
private static final double POSITIVE_ZERO

Positive zero.

POSITIVE_ZERO_DOUBLE_BITSback to summary
private static final long POSITIVE_ZERO_DOUBLE_BITS

Positive zero bits.

POSITIVE_ZERO_FLOAT_BITSback to summary
private static final int POSITIVE_ZERO_FLOAT_BITS

Positive zero bits.

SAFE_MINback to summary
public static final double SAFE_MIN

Safe minimum, such that 1 / SAFE_MIN does not overflow.
In IEEE 754 arithmetic, this is also the smallest normalized number 2-1022.

SGN_MASKback to summary
private static final long SGN_MASK

Offset to order signed double numbers lexicographically.

SGN_MASK_FLOATback to summary
private static final int SGN_MASK_FLOAT

Offset to order signed double numbers lexicographically.

Constructor Detail

Precisionback to summary
private Precision()

Private constructor.

Method Detail

compareToback to summary
public static int compareTo(double x, double y, double eps)

Compares two numbers given some amount of allowed error.

Parameters
x:double

the first number

y:double

the second number

eps:double

the amount of error to allow when checking for equality

Returns:int

compareToback to summary
public static int compareTo(final double x, final double y, final 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. Returns false if either of the arguments is NaN.

Parameters
x:double

first value

y:double

second value

maxUlps:int

(maxUlps - 1) is the number of floating point values between x and y.

Returns:int

equalsback to summary
public static boolean equals(float x, float y)

Returns true iff they are equal as defined by equals(x, y, 1).

Parameters
x:float

first value

y:float

second value

Returns:boolean

true if the values are equal.

equalsback to summary
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). Returns false if either of the arguments is NaN.

Parameters
x:float

first value

y:float

second value

eps:float

the amount of absolute error to allow.

Returns:boolean

true if the values are equal or within range of each other.

Since
2.2
equalsback to summary
public static boolean equals(final float x, final float y, final 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 false if either of the arguments is NaN.

Parameters
x:float

first value

y:float

second value

maxUlps:int

(maxUlps - 1) is the number of floating point values between x and y.

Returns:boolean

true if there are fewer than maxUlps floating point values between x and y.

Since
2.2
equalsback to summary
public static boolean equals(double x, double y)

Returns true iff they are equal as defined by equals(x, y, 1).

Parameters
x:double

first value

y:double

second value

Returns:boolean

true if the values are equal.

equalsback to summary
public static boolean equals(double x, double y, double eps)

Returns true if there is no double value strictly between the arguments or the difference between them is within the range of allowed error (inclusive). Returns false if either of the arguments is NaN.

Parameters
x:double

First value.

y:double

Second value.

eps:double

Amount of allowed absolute error.

Returns:boolean

true if the values are two adjacent floating point numbers or they are within range of each other.

equalsback to summary
public static boolean equals(final double x, final double y, final 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 false if either of the arguments is NaN.

Parameters
x:double

first value

y:double

second value

maxUlps:int

(maxUlps - 1) is the number of floating point values between x and y.

Returns:boolean

true if there are fewer than maxUlps floating point values between x and y.

equalsIncludingNaNback to summary
public static boolean equalsIncludingNaN(float x, float y)

Returns true if both arguments are NaN or they are equal as defined by equals(x, y, 1).

Parameters
x:float

first value

y:float

second value

Returns:boolean

true if the values are equal or both are NaN.

Since
2.2
equalsIncludingNaNback to summary
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:float

first value

y:float

second value

eps:float

the amount of absolute error to allow.

Returns:boolean

true if the values are equal or within range of each other, or both are NaN.

Since
2.2
equalsIncludingNaNback to summary
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 by equals(x, y, maxUlps).

Parameters
x:float

first value

y:float

second value

maxUlps:int

(maxUlps - 1) is the number of floating point values between x and y.

Returns:boolean

true if both arguments are NaN or if there are less than maxUlps floating point values between x and y.

Since
2.2
equalsIncludingNaNback to summary
public static boolean equalsIncludingNaN(double x, double y)

Returns true if the arguments are both NaN or they are equal as defined by equals(x, y, 1).

Parameters
x:double

first value

y:double

second value

Returns:boolean

true if the values are equal or both are NaN.

Since
2.2
equalsIncludingNaNback to summary
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:double

first value

y:double

second value

eps:double

the amount of absolute error to allow.

Returns:boolean

true if the values are equal or within range of each other, or both are NaN.

Since
2.2
equalsIncludingNaNback to summary
public static boolean equalsIncludingNaN(double x, double y, int maxUlps)

Returns true if both arguments are NaN or if they are equal as defined by equals(x, y, maxUlps).

Parameters
x:double

first value

y:double

second value

maxUlps:int

(maxUlps - 1) is the number of floating point values between x and y.

Returns:boolean

true if both arguments are NaN or if there are less than maxUlps floating point values between x and y.

Since
2.2
equalsWithRelativeToleranceback to summary
public static boolean equalsWithRelativeTolerance(double x, double y, double eps)

Returns true if there is no double value strictly between the arguments or the relative difference between them is less than or equal to the given tolerance. Returns false if either of the arguments is NaN.

Parameters
x:double

First value.

y:double

Second value.

eps:double

Amount of allowed relative error.

Returns:boolean

true if the values are two adjacent floating point numbers or they are within range of each other.

Since
3.1
representableDeltaback to summary
public static double representableDelta(double x, double originalDelta)

Computes a number delta close to originalDelta with the property that


  x + delta - x
is exactly machine-representable. This is useful when computing numerical derivatives, in order to reduce roundoff errors.
Parameters
x:double

Value.

originalDelta:double

Offset value.

Returns:double

a number delta so that x + delta and x differ by a representable floating number.

roundback to summary
public static double round(double x, int scale)

Rounds the given value to the specified number of decimal places. The value is rounded using the BigDecimal#ROUND_HALF_UP method.

Parameters
x:double

Value to round.

scale:int

Number of digits to the right of the decimal point.

Returns:double

the rounded value.

Since
1.1 (previously in MathUtils, moved as of version 3.0)
roundback to summary
public static double round(double x, int scale, int 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 in BigDecimal. If x is infinite or NaN, then the value of x is returned unchanged, regardless of the other parameters.

Parameters
x:double

Value to round.

scale:int

Number of digits to the right of the decimal point.

roundingMethod:int

Rounding method as defined in BigDecimal.

Returns:double

the rounded value.

Exceptions
ArithmeticException:
if roundingMethod == ROUND_UNNECESSARY and the specified scaling operation would require rounding.
IllegalArgumentException:
if roundingMethod does not represent a valid rounding mode.
Since
1.1 (previously in MathUtils, moved as of version 3.0)
roundback to summary
public static float round(float x, int scale)

Rounds the given value to the specified number of decimal places. The value is rounded using the BigDecimal#ROUND_HALF_UP method.

Parameters
x:float

Value to round.

scale:int

Number of digits to the right of the decimal point.

Returns:float

the rounded value.

Since
1.1 (previously in MathUtils, moved as of version 3.0)
roundback to summary
public static float round(float x, int scale, int roundingMethod) throws MathArithmeticException, MathIllegalArgumentException

Rounds the given value to the specified number of decimal places. The value is rounded using the given method which is any method defined in BigDecimal.

Parameters
x:float

Value to round.

scale:int

Number of digits to the right of the decimal point.

roundingMethod:int

Rounding method as defined in BigDecimal.

Returns:float

the rounded value.

Exceptions
MathArithmeticException:
if an exact operation is required but result is not exact
MathIllegalArgumentException:
if roundingMethod is not a valid rounding method.
Since
1.1 (previously in MathUtils, moved as of version 3.0)
roundUnscaledback to summary
private static double roundUnscaled(double unscaled, double sign, int roundingMethod) throws MathArithmeticException, MathIllegalArgumentException

Rounds the given non-negative value to the "nearest" integer. Nearest is determined by the rounding method specified. Rounding methods are defined in BigDecimal.

Parameters
unscaled:double

Value to round.

sign:double

Sign of the original, scaled value.

roundingMethod:int

Rounding method, as defined in BigDecimal.

Returns:double

the rounded value.

Exceptions
MathArithmeticException:
if an exact operation is required but result is not exact
MathIllegalArgumentException:
if roundingMethod is not a valid rounding method.
Since
1.1 (previously in MathUtils, moved as of version 3.0)