public final class MathTools extends java.lang.Object
Modified by: Joseph A. Huwaldt
Modifier and Type | Field and Description |
---|---|
static double |
EPS
The machine epsilon (macheps) or unit roundoff for
double in the Java
environment. |
static float |
EPSF
The machine epsilon (macheps) or unit roundoff for
float in the Java
environment. |
static double |
LOG10
The natural logarithm of 10.
|
static double |
LOG2
The natural logarithm of 2.
|
static double |
MAX_LOG
The natural logarithm of the maximum double value: log(MAX_VALUE).
|
static double |
MIN_LOG
The natural logarithm of the minimum double value: log(MIN_VALUE).
|
static double |
SQRT_EPS
Square-root of the machine epsilon for
double . |
static float |
SQRT_EPSF
Square-root of the machine epsilon for
float . |
Modifier and Type | Method and Description |
---|---|
static double |
acosh(double x)
Returns the inverse hyperbolic cosine of the specified argument.
|
static double |
asinh(double xx)
Returns the inverse hyperbolic sine of the specified argument.
|
static double |
atanh(double x)
Returns the inverse hyperbolic tangent of the specified argument.
|
static long |
bin2dec(java.util.BitSet bits)
Converts binary number to it's base 10 decimal equivalent.
|
static double |
cosh(double x)
Deprecated.
|
static double |
cubeRoot(double x)
Deprecated.
|
static void |
dec2bin(int decimal,
java.util.BitSet bits)
Converts a positive decimal number to it's binary equivalent.
|
static double |
epsilon(double x)
Returns the smallest roundoff in quantities of size x, EPSILON, such that
x + EPSILON > x . |
static float |
epsilon(float x)
Returns the smallest roundoff in quantities of size x, EPSILON, such that
x + EPSILON > x . |
static boolean |
even(long n)
Test to see if a given long integer is even.
|
static java.math.BigInteger |
factorial(int n)
Returns the factorial of n (usually written as n!) for any value of n.
|
static double |
frac(double x)
Returns the fractional part of a floating point number (removes the integer part).
|
static long |
greatestCommonDivisor(long xval,
long yval)
Calculates the greatest common divisor between two input integers.
|
static int |
intFactorial(int n)
Returns the factorial of n (usually written as n!) for values of n up to 12.
|
static boolean |
isApproxEqual(double a,
double b)
Returns true if the two supplied numbers are approximately equal to within machine
precision.
|
static boolean |
isApproxEqual(double a,
double b,
double tol)
Returns true if the two supplied numbers are approximately equal to within the
specified tolerance.
|
static boolean |
isApproxEqual(float a,
float b)
Returns true if the two supplied numbers are approximately equal to within machine
precision.
|
static boolean |
isApproxEqual(float a,
float b,
float tol)
Returns true if the two supplied numbers are approximately equal to within the
specified tolerance.
|
static boolean |
isApproxZero(double a)
Returns true if the supplied number is approximately zero to within machine
precision.
|
static boolean |
isApproxZero(double a,
double tol)
Returns true if the supplied number is approximately zero to within the specified
tolerance.
|
static boolean |
isApproxZero(float a)
Returns true if the supplied number is approximately zero to within machine
precision.
|
static boolean |
isApproxZero(float a,
float tol)
Returns true if the supplied number is approximately zero to within the specified
tolerance.
|
static double |
lineInterp(double x1,
double y1,
double x2,
double y2,
double x)
Straight linear 1D interpolation between two points.
|
static double |
lnGamma(double value)
Returns the natural log of the Gamma Function defined by the integral:
Gamma(z) = integral from 0 to infinity of t^(z-1)*e^-t dt . |
static double |
log10(double x)
Deprecated.
|
static double |
log2(double x)
Find the base 2 logarithm of the given double.
|
static void |
main(java.lang.String[] args)
Used to test out the methods in this class.
|
static boolean |
odd(long n)
Test to see if a given long integer is odd.
|
static long |
pow(long a,
long b)
Returns a number "a" raised to the power "b".
|
static double |
pow10(int x)
Raises 10 to the small integer power indicated (eg: 10^5 = 100000).
|
static long |
pow2(long x)
Raises 2 to the small integer power indicated (eg: 2^3 = 8).
|
static double |
roundDownToPlace(double value,
int place)
Rounds a floating point number down to the desired decimal place.
|
static double |
roundToPlace(double value,
int place)
Rounds a floating point number to the desired decimal place.
|
static double |
roundToSigFig(double value,
int sigFig)
Rounds a floating point number to the desired number of significant digits.
|
static double |
roundUpToPlace(double value,
int place)
Rounds a floating point number up to the desired decimal place.
|
static double |
sign(double a,
double b)
Returns the absolute value of "a" times the sign of "b".
|
static float |
sign(float a,
double b)
Returns the absolute value of "a" times the sign of "b".
|
static int |
sign(int a,
double b)
Returns the absolute value of "a" times the sign of "b".
|
static long |
sign(long a,
double b)
Returns the absolute value of "a" times the sign of "b".
|
static double |
sinh(double x)
Deprecated.
|
static double |
sqr(double x)
Calculates the square (x^2) of the argument.
|
static double |
tanh(double x)
Deprecated.
|
public static final double LOG10
public static final double LOG2
public static final double MAX_LOG
public static final double MIN_LOG
public static final double EPS
double
in the Java
environment. Machine epsilon gives an upper bound on the relative error due to
rounding in floating point arithmetic. Machine epsilon is the smallest number such
that (1.0 + EPS != 1.0).public static final double SQRT_EPS
double
.public static final float EPSF
float
in the Java
environment. Machine epsilon gives an upper bound on the relative error due to
rounding in floating point arithmetic. Machine epsilon is the smallest number such
that (1F + EPSF != 1F).public static final float SQRT_EPSF
float
.public static boolean even(long n)
n
- Integer number to be tested.public static boolean odd(long n)
n
- Integer number to be tested.public static double sqr(double x)
x
- Argument to be squared.public static double cubeRoot(double x)
x
- Argument for which the cube root is to be found.Math.cbrt(double)
public static long pow(long a, long b) throws java.lang.ArithmeticException
a
- Number to be raised to the power "b".b
- Power to raise number "a" to.java.lang.ArithmeticException
- if "b" is negative.public static long pow2(long x)
x
- Amount to raise 2 to the power of.public static double pow10(int x)
x
- Amount to raise 10 to the power of.public static double log10(double x)
x
- Value to find the base 10 logarithm of.Math.log10(double)
public static double log2(double x)
x
- Value to find the base 2 logarithm of.public static double roundToPlace(double value, int place)
value
- The value to be rounded.place
- Number of decimal places to round value to. A place of 1 rounds to
10's place, 2 to 100's place, -2 to 1/100th place, et cetera.roundToSigFig(double, int)
,
roundDownToPlace(double, int)
,
roundUpToPlace(double, int)
public static double roundUpToPlace(double value, int place)
value
- The value to be rounded up.place
- Number of decimal places to round value to. A place of 1 rounds to
10's place, 2 to 100's place, -2 to 1/100th place, et cetera.roundToPlace(double, int)
,
roundDownToPlace(double, int)
public static double roundDownToPlace(double value, int place)
value
- The value to be rounded down.place
- Number of decimal places to round value to. A place of 1 rounds to
10's place, 2 to 100's place, -2 to 1/100th place, et cetera.roundToPlace(double, int)
,
roundUpToPlace(double, int)
public static double roundToSigFig(double value, int sigFig)
value
- The value to be rounded.sigFig
- The number of significant digits/figures to retain.roundToPlace(double, int)
public static java.math.BigInteger factorial(int n)
n
- The number to calculate the factorial for.intFactorial(int)
public static int intFactorial(int n)
n
- The number to calculate the factorial for (0 ≤ n ≤ 12).factorial(int)
public static long greatestCommonDivisor(long xval, long yval)
xval
- First integeryval
- Second integerpublic static double frac(double x)
x
- Argument for which the fractional part is to be returned.public static double lineInterp(double x1, double y1, double x2, double y2, double x)
x1
- X-coordinate of the 1st point (the high point).y1
- Y-coordinate of the 1st point (the high point).x2
- X-coordinate of the 2nd point (the low point).y2
- Y-coordinate of the 2nd point (the low point).x
- The X coordinate of the point for which we want to interpolate to
determine a Y coordinate. Will extrapolate if X is outside of the bounds
of the point arguments.public static void dec2bin(int decimal, java.util.BitSet bits)
decimal
- The positive decimal number to be encoded in binary.bits
- The bitset to encode the number in.public static long bin2dec(java.util.BitSet bits)
bits
- The bitset that encodes the number to be converted.public static double cosh(double x)
x
- The number whose hyperbolic cosine is to be returned.Math.cosh(double)
public static double sinh(double x)
x
- The number whose hyperbolic sine is to be returned.Math.sinh(double)
public static double tanh(double x)
x
- The number whose hyperbolic tangent is to be returned.Math.tanh(double)
public static double acosh(double x)
acosh(x) = log(x + sqrt((x-1)*(x+1)))
x
- Value to return inverse hyperbolic cosine of.java.lang.IllegalArgumentException
- if x is less than 1.0.public static double asinh(double xx)
asinh(x) = log(x + sqrt(1 + x*x))
xx
- Value to return inverse hyperbolic cosine of.public static double atanh(double x)
atanh(x) = 0.5*log((1 + x)/(1 - x))
x
- Value to return inverse hyperbolic cosine of.java.lang.IllegalArgumentException
- if x is outside the range -1, to +1.public static double sign(double a, double b)
a
- The value for which the magnitude is returned.b
- The value for which the sign is returned.public static float sign(float a, double b)
a
- The value for which the magnitude is returned.b
- The value for which the sign is returned.public static long sign(long a, double b)
a
- The value for which the magnitude is returned.b
- The value for which the sign is returned.public static int sign(int a, double b)
a
- The value for which the magnitude is returned.b
- The value for which the sign is returned.public static double lnGamma(double value)
Gamma(z) = integral from 0 to infinity of t^(z-1)*e^-t dt
.
It is better to implement ln(Gamma(x)) rather than Gamma(x) since the latter will overflow most computer floating point representations at quite modest values of x.
value
- The value to evaluate the log of the gamma function for.public static double epsilon(double x)
x + EPSILON > x
. This is the Units in the Last Place (ULP) and is
different than the machine roundoff EPS (macheps).x
- The value to return the roundoff quantity for.EPS
,
Math.ulp(double)
public static float epsilon(float x)
x + EPSILON > x
. This is the Units in the Last Place (ULP) and is
different than the machine roundoff EPS (macheps).x
- The value to return the roundoff quantity for.EPSF
,
Math.ulp(float)
public static boolean isApproxEqual(double a, double b)
a
- The 1st value to compare for approximate equality.b
- The 2nd value to compare for approximate equality.public static boolean isApproxEqual(double a, double b, double tol)
a
- The 1st value to compare for approximate equality.b
- The 2nd value to compare for approximate equality.tol
- The tolerance for equality.public static boolean isApproxEqual(float a, float b)
a
- The 1st value to compare for approximate equality.b
- The 2nd value to compare for approximate equality.public static boolean isApproxEqual(float a, float b, float tol)
a
- The 1st value to compare for approximate equality.b
- The 2nd value to compare for approximate equality.tol
- The tolerance for equality.public static boolean isApproxZero(double a)
a
- The value to be compared with zero.public static boolean isApproxZero(double a, double tol)
a
- The value to be compared with zero.tol
- The tolerance for equality.public static boolean isApproxZero(float a)
a
- The value to be compared with zero.public static boolean isApproxZero(float a, float tol)
a
- The value to be compared with zero.tol
- The tolerance for equality.public static void main(java.lang.String[] args)
args
- Command line arguments (not used).