public final class Precision extends Object
Modifier and Type | Field and Description |
---|---|
static double |
DOUBLE_COMPARISON_EPSILON
Epsilon used for doubles relative comparison.
|
static double |
EPSILON
Largest double-precision floating-point number such that
1 + EPSILON is numerically equal to 1. |
static double |
SAFE_MIN
Safe minimum, such that
1 / SAFE_MIN does not overflow. |
Modifier and Type | Method and Description |
---|---|
static int |
compareTo(double x,
double y,
double eps)
Compares two numbers given some amount of allowed error.
|
static int |
compareTo(double x,
double y,
int maxUlps)
Compares two numbers given some amount of allowed error.
|
static boolean |
equals(double x,
double y)
Returns true iff they are equal as defined by
equals(x, y, 1) . |
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). |
static boolean |
equals(double x,
double y,
int maxUlps)
Returns true if both arguments are equal or within the range of allowed
error (inclusive).
|
static boolean |
equals(float x,
float y)
Returns true if they are equal as defined by
equals(x, y, 1) . |
static boolean |
equals(float x,
float y,
float eps)
Returns true if both arguments are equal or within the range of allowed
error (inclusive).
|
static boolean |
equals(float x,
float y,
int maxUlps)
Returns true if both arguments are equal or within the range of allowed
error (inclusive).
|
static boolean |
equalsIncludingNaN(double x,
double y)
Returns true if both arguments are NaN or neither is NaN and they are
equal as defined by
equals(x, y, 1) . |
static boolean |
equalsIncludingNaN(double x,
double y,
double eps)
Returns true if both arguments are NaN or are equal or within the range
of allowed error (inclusive).
|
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) . |
static boolean |
equalsIncludingNaN(float x,
float y)
Returns true if both arguments are NaN or neither is NaN and they are
equal as defined by
equals(x, y, 1) . |
static boolean |
equalsIncludingNaN(float x,
float y,
float eps)
Returns true if both arguments are NaN or are equal or within the range
of allowed error (inclusive).
|
static boolean |
equalsIncludingNaN(float x,
float y,
int maxUlps)
Returns true if both arguments are NaN or if they are equal as defined
by
equals(x, y, maxUlps) . |
static boolean |
equalsWithAbsoluteAndRelativeTolerances(double x,
double y,
double relThreshold,
double absThreshold)
Returns
true if there is no double value strictly between the
arguments or the relative difference between them is smaller or equal
to the given tolerance AND if there is no double value strictly between the
arguments or the difference between them is within the range of allowed
error (inclusive). |
static boolean |
equalsWithAbsoluteOrRelativeTolerances(double x,
double y,
double relThreshold,
double absThreshold)
Returns
true if there is no double value strictly between the
arguments or the relative difference between them is smaller or equal
to the given tolerance OR if there is no double value strictly between the
arguments or the difference between them is within the range of allowed
error (inclusive). |
static boolean |
equalsWithRelativeTolerance(double x,
double y)
Returns
true if both arguments are found equal
with a relative comparison using a default epsilon. |
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 smaller or equal
to the given tolerance. |
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. |
static double |
round(double x,
int scale)
Rounds the given value to the specified number of decimal places.
|
static double |
round(double x,
int scale,
int roundingMethod)
Rounds the given value to the specified number of decimal places.
|
static float |
round(float x,
int scale)
Rounds the given value to the specified number of decimal places.
|
static float |
round(float x,
int scale,
int roundingMethod)
Rounds the given value to the specified number of decimal places.
|
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.
public static final double DOUBLE_COMPARISON_EPSILON
public static final double SAFE_MIN
1 / SAFE_MIN
does not overflow. public static int compareTo(double x, double y, double eps)
x
- the first numbery
- the second numbereps
- the amount of error to allow when checking for equalityequals(x, y, eps)
equals(x, y, eps)
&& x < yequals(x, y, eps)
&& x > ypublic static int compareTo(double x, double y, int maxUlps)
(maxUlps - 1)
(or fewer) floating point numbers
between them, i.e. two adjacent floating
point numbers are considered equal.
Adapted from
Bruce Dawsonx
- first valuey
- second valuemaxUlps
- (maxUlps - 1)
is the number of floating point
values between x
and y
.equals(x, y, maxUlps)
equals(x, y, maxUlps)
&& x < yequals(x, y, maxUlps)
&& x > ypublic static boolean equals(float x, float y)
equals(x, y, 1)
.x
- first valuey
- second valuetrue
if the values are equal.public static boolean equalsIncludingNaN(float x, float y)
equals(x, y, 1)
.x
- first valuey
- second valuetrue
if the values are equal or both are NaN.public static boolean equals(float x, float y, float eps)
x
- first valuey
- second valueeps
- the amount of absolute error to allow.true
if the values are equal or within range of each other.public static boolean equalsIncludingNaN(float x, float y, float eps)
x
- first valuey
- second valueeps
- the amount of absolute error to allow.true
if the values are equal or within range of each other,
or both are NaN.public static boolean equals(float x, float y, int maxUlps)
(maxUlps - 1)
(or fewer) floating point numbers
between them, i.e. two adjacent floating
point numbers are considered equal.
Adapted from
Bruce Dawsonx
- first valuey
- second valuemaxUlps
- (maxUlps - 1)
is the number of floating point
values between x
and y
.true
if there are fewer than maxUlps
floating
point values between x
and y
.public static boolean equalsIncludingNaN(float x, float y, int maxUlps)
equals(x, y, maxUlps)
.x
- first valuey
- second valuemaxUlps
- (maxUlps - 1)
is the number of floating point
values between x
and y
.true
if both arguments are NaN or if there are less than maxUlps
floating point values
between x
and y
.public static boolean equals(double x, double y)
equals(x, y, 1)
.x
- first valuey
- second valuetrue
if the values are equal.public static boolean equalsIncludingNaN(double x, double y)
equals(x, y, 1)
.x
- first valuey
- second valuetrue
if the values are equal or both are NaN.public static boolean equals(double x, double y, double eps)
true
if there is no double value strictly between the
arguments or the difference between them is within the range of allowed
error (inclusive).x
- First value.y
- Second value.eps
- Amount of allowed absolute error.true
if the values are two adjacent floating point
numbers or they are within range of each other.public static boolean equalsWithRelativeTolerance(double x, double y, double eps)
true
if there is no double value strictly between the
arguments or the relative difference between them is smaller or equal
to the given tolerance.x
- First value.y
- Second value.eps
- Amount of allowed relative error.true
if the values are two adjacent floating point
numbers or they are within range of each other.public static boolean equalsIncludingNaN(double x, double y, double eps)
x
- first valuey
- second valueeps
- the amount of absolute error to allow.true
if the values are equal or within range of each other,
or both are NaN.public static boolean equals(double x, double y, int maxUlps)
(maxUlps - 1)
(or fewer) floating point numbers
between them, i.e. two adjacent floating
point numbers are considered equal.
Adapted from
Bruce Dawsonx
- first valuey
- second valuemaxUlps
- (maxUlps - 1)
is the number of floating point
values between x
and y
.true
if there are fewer than maxUlps
floating
point values between x
and y
.public static boolean equalsIncludingNaN(double x, double y, int maxUlps)
equals(x, y, maxUlps)
.x
- first valuey
- second valuemaxUlps
- (maxUlps - 1)
is the number of floating point
values between x
and y
.true
if both arguments are NaN or if there are less than maxUlps
floating point values
between x
and y
.public static boolean equalsWithRelativeTolerance(double x, double y)
true
if both arguments are found equal
with a relative comparison using a default epsilon.x
- first double to be comparedy
- second double to be comparedThe value "Nan" as input always imply the return "false"
public static boolean equalsWithAbsoluteAndRelativeTolerances(double x, double y, double relThreshold, double absThreshold)
true
if there is no double value strictly between the
arguments or the relative difference between them is smaller or equal
to the given tolerance AND if there is no double value strictly between the
arguments or the difference between them is within the range of allowed
error (inclusive).x
- First value.y
- Second value.relThreshold
- Amount of allowed relative error.absThreshold
- Amount of allowed absolute error.true
if the values are two adjacent floating point
numbers or they are within range of each other.Precision#equals(double, double, double)} and
{@link Precision#equalsWithRelativeTolerance(double, double, double)}
public static boolean equalsWithAbsoluteOrRelativeTolerances(double x, double y, double relThreshold, double absThreshold)
true
if there is no double value strictly between the
arguments or the relative difference between them is smaller or equal
to the given tolerance OR if there is no double value strictly between the
arguments or the difference between them is within the range of allowed
error (inclusive).x
- First value.y
- Second value.relThreshold
- Amount of allowed relative error.absThreshold
- Amount of allowed absolute error.true
if the values are two adjacent floating point
numbers or they are within range of each other.Precision#equals(double, double, double)} and
{@link Precision#equalsWithRelativeTolerance(double, double, double)}
public static double round(double x, int scale)
BigDecimal.ROUND_HALF_UP
method.x
- Value to round.scale
- Number of digits to the right of the decimal point.MathUtils
, moved as of version 3.0)public static double round(double x, int scale, int roundingMethod)
BigDecimal
.
If x
is infinite or NaN
, then the value of x
is
returned unchanged, regardless of the other parameters.x
- Value to round.scale
- Number of digits to the right of the decimal point.roundingMethod
- Rounding method as defined in BigDecimal
.ArithmeticException
- if roundingMethod == ROUND_UNNECESSARY
and the specified scaling operation would require
rounding.IllegalArgumentException
- if roundingMethod
does not
represent a valid rounding mode.MathUtils
, moved as of version 3.0)public static float round(float x, int scale)
BigDecimal.ROUND_HALF_UP
method.x
- Value to round.scale
- Number of digits to the right of the decimal point.MathUtils
, moved as of version 3.0)public static float round(float x, int scale, int roundingMethod)
BigDecimal
.x
- Value to round.scale
- Number of digits to the right of the decimal point.roundingMethod
- Rounding method as defined in BigDecimal
.MathArithmeticException
- if an exact operation is required but result is not exactMathIllegalArgumentException
- if roundingMethod
is not a valid rounding method.MathUtils
, moved as of version 3.0)public static double representableDelta(double x, double originalDelta)
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.x
- Value.originalDelta
- Offset value.delta
so that x + delta
and x
differ by a representable floating number.Copyright © 2021 CNES. All rights reserved.