public final class MathFunctions extends Static
Math
.
Some methods in this class are very similar to the standard Math
methods
or could be implemented with straightforward formulas.
However the methods in this class put an emphasis on:
magnitude
,
pow10
.isPositive
,
isNegative
,
isSameSign
,
xorSign
.toNanFloat
,
toNanOrdinal
.Math
are:
atanh
,
nextPrimeNumber
.DecimalFunctions
,
Numbers
Defined in the sis-utility
module
Modifier and Type | Field and Description |
---|---|
static int |
HIGHEST_SUPPORTED_PRIME_NUMBER
The highest prime number supported by the
nextPrimeNumber(int) method. |
static double |
LOG10_2
The logarithm of 2 in base 10, which is approximated by 0.3010299956639812.
|
static double |
SQRT_2
The square root of 2, which is approximated by 1.4142135623730951.
|
Modifier and Type | Method and Description |
---|---|
static double |
acosh(double x)
Returns the inverse hyperbolic cosine of the given value.
|
static double |
asinh(double x)
Returns the inverse hyperbolic sine of the given value.
|
static double |
atanh(double x)
Returns the inverse hyperbolic tangent of the given value.
|
static int[] |
commonDivisors(int... numbers)
Returns the divisors which are common to all the specified numbers.
|
static int[] |
divisors(int number)
Returns the divisors of the specified number as positive integers.
|
static boolean |
epsilonEqual(double v1,
double v2,
double ε)
Returns
true if the given values are equal
or if their difference is not greater than the given threshold. |
static boolean |
epsilonEqual(float v1,
float v2,
float ε)
Returns
true if the given values are equal
or if their difference is not greater than the given threshold. |
static int |
getExponent(double value)
Returns the unbiased exponent used in the representation of a
double , with correction for
sub-normal numbers. |
static boolean |
isNegative(double value)
Returns
true if the given value is negative, including negative zero. |
static boolean |
isNegativeZero(double value)
Returns
true if the given value is the negative zero (-0.0 ). |
static boolean |
isPositive(double value)
Returns
true if the given value is positive, excluding negative zero. |
static boolean |
isPositiveZero(double value)
Returns
true if the given value is the positive zero (+0.0 ). |
static boolean |
isSameSign(double v1,
double v2)
Returns
true if the given values have the same sign, differentiating positive
and negative zeros. |
static double |
magnitude(double... vector)
Returns the magnitude of the given vector.
|
static int |
nextPrimeNumber(int number)
Returns the first prime number equals or greater than the given value.
|
static double |
pow10(double x)
Computes 10 raised to the power of x.
|
static double |
pow10(int x)
Computes 10 raised to the power of x.
|
static float |
toNanFloat(int ordinal)
Returns a NaN number for the specified ordinal value.
|
static int |
toNanOrdinal(float value)
Returns the ordinal value of the given NaN number.
|
static double |
truncate(double value)
Truncates the given value toward zero.
|
static double |
xorSign(double value,
double sign)
Returns the first floating-point argument with the sign reversed if the second floating-point
argument is negative.
|
public static final double SQRT_2
Math.sqrt(double)
,
Constant Field Valuespublic static final double LOG10_2
double exp10 = exp2 * LOG10_2;
Math.log10(double)
,
getExponent(double)
,
Constant Field Valuespublic static final int HIGHEST_SUPPORTED_PRIME_NUMBER
nextPrimeNumber(int)
method.
In the current implementation, this value is 65521. However this limit may
change in any future Apache SIS version.
nextPrimeNumber(int)
,
Constant Field Valuespublic static double truncate(double value)
Math.floor(double)
if the value is positive, or Math.ceil(double)
if
the value is negative.value
- The value to truncate.public static double magnitude(double... vector)
sqrt(vector[0]² + vector[1]² + … + vector[length-1]²)
sqrt(v²)
, in order to avoid rounding error. This special case
has been implemented because this method is often invoked for computing the length of
offset vectors,
typically aligned with the axes of a Cartesian coordinate system.vector
- The vector for which to compute the magnitude.Math.hypot(double, double)
public static int getExponent(double value)
double
, with correction for
sub-normal numbers. This method is related to Math.getExponent(double)
in the following ways:
Double.MIN_NORMAL
in magnitude (including
infinities), this method returns results that are identical to Math.getExponent(double)
.Double.MIN_NORMAL
in magnitude (including zero), the correction
for sub-normal numbers results in return values smaller than what Math.getExponent(double)
would return.Double.MAX_EXPONENT
+ 1.Double.MAX_VALUE
, then the result is 1023.Double.MIN_NORMAL
, then the result is -1022.Double.MIN_VALUE
, then the result is -1074.getExponent(Math.scalb(1.0, p)) == p
Math.scalb(1.0, getExponent(value)) == value
Math.floor(LOG10_2 * getExponent(value)) == Math.floor(Math.log10(value))
value
- The value for which to get the exponent.Math.getExponent(double)
,
Math.scalb(double, int)
public static double pow10(double x)
Math.pow(10, x)
, but is slightly more accurate
in the special case where the given argument is an integer.x
- The exponent.pow10(int)
,
Math.pow(double, double)
,
Math.log10(double)
public static double pow10(int x)
Math.pow(10, x)
.
Math.pow(10, x)
method does not always return
the closest IEEE floating point representation. Slight departures (1 or 2 ULP) are often allowed in math
functions for performance reasons. The most accurate calculations are usually not necessary, but the base
10 is a special case since it is used for scaling axes or formatting human-readable output.x
- The exponent.pow10(double)
,
LOG10_2
,
DecimalFunctions
public static double asinh(double x)
Math.sinh(double)
method.x
- The value for which to compute the inverse hyperbolic sine.Math.sinh(double)
public static double acosh(double x)
Math.cosh(double)
method.x
- The value for which to compute the inverse hyperbolic cosine.Math.cosh(double)
public static double atanh(double x)
Math.tanh(double)
method.
The range of input values shall be in the [-1 … 1].
Special cases:
x
- The value for which to compute the inverse hyperbolic tangent.Math.tanh(double)
public static boolean isPositive(double value)
true
if the given value is positive, excluding negative zero.
Special cases:
+0.0
, returns true
-0.0
, returns false
NaN
, returns false
isPositive(double)
and testing if (value >= 0).value
- The value to test.true
if the given value is positive, excluding negative zero.isPositiveZero(double)
,
isNegative(double)
public static boolean isPositiveZero(double value)
true
if the given value is the positive zero (+0.0
).
This method returns false
for the negative zero (-0.0
).
This method is equivalent to the following code, but potentially faster:
return (value == 0) && isPositive(value);
value
- The value to test.true
if the given value is +0.0 (not -0.0).isPositive(double)
,
isNegativeZero(double)
public static boolean isNegative(double value)
true
if the given value is negative, including negative zero.
Special cases:
+0.0
, returns false
-0.0
, returns true
NaN
, returns false
isNegative(double)
and testing if (value < 0).value
- The value to test.true
if the given value is negative, including negative zero.isNegativeZero(double)
,
isPositive(double)
public static boolean isNegativeZero(double value)
true
if the given value is the negative zero (-0.0
).
This method returns false
for the positive zero (+0.0
).
This method is equivalent to the following code, but potentially faster:
return (value == 0) && isNegative(value);
value
- The value to test.true
if the given value is -0.0 (not +0.0).isNegative(double)
,
isPositiveZero(double)
public static boolean isSameSign(double v1, double v2)
true
if the given values have the same sign, differentiating positive
and negative zeros.
Special cases:
+0.0
and -0.0
are considered to have opposite signNaN
, returns false
v1
- The first value.v2
- The second value, to compare the sign with the first value.true
if the given values are not NaN and have the same sign.Math.signum(double)
public static double xorSign(double value, double sign)
Math.copySign(value, sign)
except that the sign is combined with an exclusive
or operation instead than being copied.
This method makes no guarantee about whether NaN
values are handled as positive
or negative numbers. This is the same policy than Math.copySign(double, double)
.
value
- The parameter providing the value that may need a sign change.sign
- The parameter providing the sign to xor with the value.sign
parameter is negative.Math.copySign(double, double)
public static boolean epsilonEqual(float v1, float v2, float ε)
true
if the given values are equal
or if their difference is not greater than the given threshold. More specifically:
true
.true
.
Note that this method does not differentiate the various NaN values.abs(v1 - v2) <= ε
comparison.v1
- The first value to compare.v2
- The second value to compare.ε
- The tolerance threshold, which must be positive.true
If both values are equal given the tolerance threshold.public static boolean epsilonEqual(double v1, double v2, double ε)
true
if the given values are equal
or if their difference is not greater than the given threshold. More specifically:
true
.true
.
Note that this method does not differentiate the various NaN values.abs(v1 - v2) <= ε
comparison.v1
- The first value to compare.v2
- The second value to compare.ε
- The tolerance threshold, which must be positive.true
If both values are equal given the tolerance threshold.public static float toNanFloat(int ordinal) throws IllegalArgumentException
0x7F800001
… 0x7FFFFFFF
], with
0x7FC00000
as the bit fields of the standard Float.NaN
value0xFF800001
… 0xFFFFFFFF
]ordinal
- The NaN ordinal value, from -0x200000
to 0x1FFFFF
inclusive.IllegalArgumentException
- if the specified ordinal is out of range.Float.intBitsToFloat(int)
public static int toNanOrdinal(float value) throws IllegalArgumentException
toNanFloat(int)
.value
- The value from which to get the NaN ordinal value.IllegalArgumentException
- If the given value is not a NaN value,
or does not use a supported bits pattern.public static int nextPrimeNumber(int number) throws IllegalArgumentException
number
- The number for which to find the next prime.IllegalArgumentException
- If the given value is outside the supported range.BigInteger.isProbablePrime(int)
public static int[] divisors(int number)
O
(which returns an empty array), the first element in the returned array
is always 1
and the last element is always the absolute value of number
.number
- The number for which to compute the divisors.public static int[] commonDivisors(int... numbers)
numbers
- The numbers for which to compute the divisors.Copyright © 2010–2015 The Apache Software Foundation. All rights reserved.