Package org.geotools.metadata.math
Class XMath
Object
XMath
Simple mathematical functions in addition to the ones provided in
Math.- Since:
- 2.5
- Author:
- Martin Desruisseaux (IRD)
-
Method Summary
Modifier and TypeMethodDescriptionstatic intcountDecimalFractionDigits(double value) Counts the fraction digits in the string representation of the specified value.static int[]divisors(int number) Returns the divisors of the specified number as positive integers.static doublepow10(double x) Computes 10 raised to the power of x.static doublepow10(int x) Computes 10 raised to the power of x.static intprimeNumber(int index) Returns the ith prime number.static doubleroundIfAlmostInteger(double value, int maxULP) Rounds the specified value, providing that the difference between the original value and the rounded value is not greater than the specified amount of floating point units.static bytesgn(byte x) Returns the sign of x.static intsgn(double x) Returns the sign of x.static intsgn(float x) Returns the sign of x.static intsgn(int x) Returns the sign of x.static intsgn(long x) Returns the sign of x.static shortsgn(short x) Returns the sign of x.static floattoNaN(int index) Returns aNaNnumber for the specified index.static doubletrimDecimalFractionDigits(double value, int maxULP, int n) Tries to remove at leastnfraction digits in the decimal representation of the specified value.
-
Method Details
-
pow10
public static double pow10(double x) Computes 10 raised to the power of x. This method delegates topow10(int)if x is an integer, or toMath.pow(double, double)otherwise.- Parameters:
x- The exponent.- Returns:
- 10 raised to the given exponent.
-
pow10
public static double pow10(int x) Computes 10 raised to the power of x. This method tries to be slightly more accurate thanMath.pow(10,x), sometime at the cost of performance.The
Math.pow(10,x)method doesn't always return the closest IEEE floating point representation. More accurate calculations are slower and usually not necessary, but the base 10 is a special case since it is used for scaling axes or formatting human-readable output, in which case the precision may matter.- Parameters:
x- The exponent.- Returns:
- 10 raised to the given exponent.
-
sgn
public static int sgn(double x) Returns the sign of x. This method returns -1 if x is negative, 0 if x is zero orNaNand +1 if x is positive.- Parameters:
x- The number from which to get the sign.- Returns:
+1if x is positive,-1if negative, or 0 otherwise.- See Also:
-
sgn
public static int sgn(float x) Returns the sign of x. This method returns -1 if x is negative, 0 if x is zero orNaNand +1 if x is positive.- Parameters:
x- The number from which to get the sign.- Returns:
+1if x is positive,-1if negative, or 0 otherwise.- See Also:
-
sgn
public static int sgn(long x) Returns the sign of x. This method returns -1 if x is negative, 0 if x is zero and +1 if x is positive.- Parameters:
x- The number from which to get the sign.- Returns:
+1if x is positive,-1if negative, or 0 otherwise.
-
sgn
public static int sgn(int x) Returns the sign of x. This method returns -1 if x is negative, 0 if x is zero and +1 if x is positive.- Parameters:
x- The number from which to get the sign.- Returns:
+1if x is positive,-1if negative, or 0 otherwise.
-
sgn
public static short sgn(short x) Returns the sign of x. This method returns -1 if x is negative, 0 if x is zero and +1 if x is positive.- Parameters:
x- The number from which to get the sign.- Returns:
+1if x is positive,-1if negative, or 0 otherwise.
-
sgn
public static byte sgn(byte x) Returns the sign of x. This method returns -1 if x is negative, 0 if x is zero and +1 if x is positive.- Parameters:
x- The number from which to get the sign.- Returns:
+1if x is positive,-1if negative, or 0 otherwise.
-
roundIfAlmostInteger
public static double roundIfAlmostInteger(double value, int maxULP) Rounds the specified value, providing that the difference between the original value and the rounded value is not greater than the specified amount of floating point units. This method can be used for hiding floating point error likes 2.9999999996.- Parameters:
value- The value to round.maxULP- The maximal change allowed in ULPs (Unit in the Last Place).- Returns:
- The rounded value, of
valueif it was not close enough to an integer.
-
trimDecimalFractionDigits
public static double trimDecimalFractionDigits(double value, int maxULP, int n) Tries to remove at leastnfraction digits in the decimal representation of the specified value. This method tries small changes tovalue, by adding or substracting up tomaxULP(Unit in the Last Place). If there is no small change that remove at leastnfraction digits, then the value is returned unchanged. This method is used for hiding rounding errors, like in conversions from radians to degrees.Example:
XMath.trimLastDecimalDigits(-61.500000000000014, 12, 4)returns-61.5.- Parameters:
value- The value to fix.maxULP- The maximal change allowed in ULPs (Unit in the Last Place). A typical value is 4.n- The minimum amount of fraction digits.- Returns:
- The trimmed value, or the unchanged
valueif there is no small change that remove at leastnfraction digits.
-
countDecimalFractionDigits
public static int countDecimalFractionDigits(double value) Counts the fraction digits in the string representation of the specified value. This method is equivalent to a callingDouble.toString(value)and counting the number of digits after the decimal separator.- Parameters:
value- The value for which to count the fraction digits.- Returns:
- The number of fraction digits.
-
toNaN
Returns aNaNnumber for the specified index. Valid NaN numbers have bit fields ranging from0x7f800001through0x7fffffffor0xff800001through0xffffffff. The standardFloat.NaNhas bit fields0x7fc00000. SeeFloat.intBitsToFloat(int)for more details on NaN bit values.- Parameters:
index- The index, from -2097152 to 2097151 inclusive.- Returns:
- One of the legal
NaNvalues as a float. - Throws:
IndexOutOfBoundsException- if the specified index is out of bounds.- See Also:
-
primeNumber
Returns the ith prime number. This method returns (2,3,5,7,11...) for index (0,1,2,3,4...). This method is designed for relatively small prime numbers only; don't use it for large values.- Parameters:
index- The prime number index, starting at index 0 for prime number 2.- Returns:
- The prime number at the specified index.
- Throws:
IndexOutOfBoundsException- if the specified index is too large.- See Also:
-
divisors
public static int[] divisors(int number) Returns the divisors of the specified number as positive integers. For any value other thanO(which returns an empty array), the first element in the returned array is always1and the last element is always the absolute value ofnumber.- Parameters:
number- The number for which to compute the divisors.- Returns:
- The divisors in strictly increasing order.
-