Methods Summary |
---|
public static double | acosh(double d)inverse hyperbolic cosine
return Math.log(Math.sqrt(Math.pow(d, 2) - 1) + d);
|
public static double | asinh(double d)inverse hyperbolic sine
double d2 = d*d;
return Math.log(Math.sqrt(d*d + 1) + d);
|
public static double | atanh(double d)inverse hyperbolic tangent
return Math.log((1 + d)/(1 - d)) / 2;
|
public static double | average(double[] values)average of all values
double ave = 0;
double sum = 0;
for (int i=0, iSize=values.length; i<iSize; i++) {
sum += values[i];
}
ave = sum / values.length;
return ave;
|
public static double | ceiling(double n, double s)Note: this function is different from java.lang.Math.ceil(..).
When n and s are "valid" arguments, the returned value is: Math.ceiling(n/s) * s;
n and s are invalid if any of following conditions are true:
- s is zero
- n is negative and s is positive
- n is positive and s is negative
In all such cases, Double.NaN is returned.
double c;
if ((n<0 && s>0) || (n>0 && s<0)) {
c = Double.NaN;
}
else {
c = (n == 0 || s == 0) ? 0 : Math.ceil(n/s) * s;
}
return c;
|
public static double | cosh(double d)hyperbolic cosine
double ePowX = Math.pow(Math.E, d);
double ePowNegX = Math.pow(Math.E, -d);
d = (ePowX + ePowNegX) / 2;
return d;
|
public static double | factorial(int n) for all n >= 1; factorial n = n * (n-1) * (n-2) * ... * 1
else if n == 0; factorial n = 1
else if n < 0; factorial n = Double.NaN
Loss of precision can occur if n is large enough.
If n is large so that the resulting value would be greater
than Double.MAX_VALUE; Double.POSITIVE_INFINITY is returned.
If n < 0, Double.NaN is returned.
double d = 1;
if (n >= 0) {
if (n <= 170) {
for (int i=1; i<=n; i++) {
d *= i;
}
}
else {
d = Double.POSITIVE_INFINITY;
}
}
else {
d = Double.NaN;
}
return d;
|
public static double | floor(double n, double s)Note: this function is different from java.lang.Math.floor(..).
When n and s are "valid" arguments, the returned value is: Math.floor(n/s) * s;
n and s are invalid if any of following conditions are true:
- s is zero
- n is negative and s is positive
- n is positive and s is negative
In all such cases, Double.NaN is returned.
double f;
if ((n<0 && s>0) || (n>0 && s<0) || (s==0 && n!=0)) {
f = Double.NaN;
}
else {
f = (n==0 || s==0) ? 0 : Math.floor(n/s) * s;
}
return f;
|
public static double | max(double[] values)min of all values. If supplied array is zero length,
Double.NEGATIVE_INFINITY is returned.
double max = Double.NEGATIVE_INFINITY;
for (int i=0, iSize=values.length; i<iSize; i++) {
max = Math.max(max, values[i]);
}
return max;
|
public static double | min(double[] values)min of all values. If supplied array is zero length,
Double.POSITIVE_INFINITY is returned.
double min = Double.POSITIVE_INFINITY;
for (int i=0, iSize=values.length; i<iSize; i++) {
min = Math.min(min, values[i]);
}
return min;
|
public static double | mod(double n, double d)returns the remainder resulting from operation:
n / d.
The result has the sign of the divisor.
Examples:
- mod(3.4, 2) = 1.4
- mod(-3.4, 2) = 0.6
- mod(-3.4, -2) = -1.4
- mod(3.4, -2) = -0.6
If d == 0, result is NaN
double result = 0;
if (d == 0) {
result = Double.NaN;
}
else if (sign(n) == sign(d)) {
double t = Math.abs(n / d);
t = t - (long) t;
result = sign(d) * Math.abs(t * d);
}
else {
double t = Math.abs(n / d);
t = t - (long) t;
t = Math.ceil(t) - t;
result = sign(d) * Math.abs(t * d);
}
return result;
|
public static double | nChooseK(int n, int k)returns the total number of combinations possible when
k items are chosen out of total of n items. If the number
is too large, loss of precision may occur (since returned
value is double). If the returned value is larger than
Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned.
If either of the parameters is negative, Double.NaN is returned.
double d = 1;
if (n<0 || k<0 || n<k) {
d= Double.NaN;
}
else {
int minnk = Math.min(n-k, k);
int maxnk = Math.max(n-k, k);
for (int i=maxnk; i<n; i++) {
d *= i+1;
}
d /= factorial(minnk);
}
return d;
|
public static double | product(double[] values)product of all values
double product = 0;
if (values!=null && values.length > 0) {
product = 1;
for (int i=0, iSize=values.length; i<iSize; i++) {
product *= values[i];
}
}
return product;
|
public static double | round(double n, int p)Returns a value rounded to p digits after decimal.
If p is negative, then the number is rounded to
places to the left of the decimal point. eg.
10.23 rounded to -1 will give: 10. If p is zero,
the returned value is rounded to the nearest integral
value.
If n is negative, the resulting value is obtained
as the round value of absolute value of n multiplied
by the sign value of n (@see MathX.sign(double d)).
Thus, -0.6666666 rounded to p=0 will give -1 not 0.
If n is NaN, returned value is NaN.
double retval;
if (Double.isNaN(n) || Double.isInfinite(n)) {
retval = Double.NaN;
}
else {
if (p != 0) {
double temp = Math.pow(10, p);
retval = Math.round(n*temp)/temp;
}
else {
retval = Math.round(n);
}
}
return retval;
|
public static double | roundDown(double n, int p)Returns a value rounded to p digits after decimal.
If p is negative, then the number is rounded to
places to the left of the decimal point. eg.
10.23 rounded to -1 will give: 10. If p is zero,
the returned value is rounded to the nearest integral
value.
If n is negative, the resulting value is obtained
as the round-up value of absolute value of n multiplied
by the sign value of n (@see MathX.sign(double d)).
Thus, -0.8 rounded-down to p=0 will give 0 not -1.
If n is NaN, returned value is NaN.
double retval;
if (Double.isNaN(n) || Double.isInfinite(n)) {
retval = Double.NaN;
}
else {
if (p != 0) {
double temp = Math.pow(10, p);
retval = sign(n) * Math.round((Math.abs(n)*temp) - 0.5)/temp;
}
else {
retval = (long) n;
}
}
return retval;
|
public static double | roundUp(double n, int p)Returns a value rounded-up to p digits after decimal.
If p is negative, then the number is rounded to
places to the left of the decimal point. eg.
10.23 rounded to -1 will give: 20. If p is zero,
the returned value is rounded to the nearest integral
value.
If n is negative, the resulting value is obtained
as the round-up value of absolute value of n multiplied
by the sign value of n (@see MathX.sign(double d)).
Thus, -0.2 rounded-up to p=0 will give -1 not 0.
If n is NaN, returned value is NaN.
double retval;
if (Double.isNaN(n) || Double.isInfinite(n)) {
retval = Double.NaN;
}
else {
if (p != 0) {
double temp = Math.pow(10, p);
double nat = Math.abs(n*temp);
retval = sign(n) *
((nat == (long) nat)
? nat / temp
: Math.round(nat + 0.5) / temp);
}
else {
double na = Math.abs(n);
retval = sign(n) *
((na == (long) na)
? na
: (long) na + 1);
}
}
return retval;
|
public static short | sign(double d)If d < 0, returns short -1
If d > 0, returns short 1
If d == 0, returns short 0
If d is NaN, then 1 will be returned. It is the responsibility
of caller to check for d isNaN if some other value is desired.
return (short) ((d == 0)
? 0
: (d < 0)
? -1
: 1);
|
public static double | sinh(double d)hyperbolic sine
double ePowX = Math.pow(Math.E, d);
double ePowNegX = Math.pow(Math.E, -d);
d = (ePowX - ePowNegX) / 2;
return d;
|
public static double | sum(double[] values)sum of all values
double sum = 0;
for (int i=0, iSize=values.length; i<iSize; i++) {
sum += values[i];
}
return sum;
|
public static double | sumproduct(double[][] arrays)returns the sum of product of corresponding double value in each
subarray. It is the responsibility of the caller to ensure that
all the subarrays are of equal length. If the subarrays are
not of equal length, the return value can be unpredictable.
double d = 0;
try {
int narr = arrays.length;
int arrlen = arrays[0].length;
for (int j=0; j<arrlen; j++) {
double t = 1;
for (int i=0; i<narr; i++) {
t *= arrays[i][j];
}
d += t;
}
}
catch (ArrayIndexOutOfBoundsException ae) {
d = Double.NaN;
}
return d;
|
public static double | sumsq(double[] values)sum of squares of all values
double sumsq = 0;
for (int i=0, iSize=values.length; i<iSize; i++) {
sumsq += values[i]*values[i];
}
return sumsq;
|
public static double | sumx2my2(double[] xarr, double[] yarr)returns the sum of difference of squares of corresponding double
value in each subarray: ie. sigma (xarr[i]^2-yarr[i]^2)
It is the responsibility of the caller
to ensure that the two subarrays are of equal length. If the
subarrays are not of equal length, the return value can be
unpredictable.
double d = 0;
try {
for (int i=0, iSize=xarr.length; i<iSize; i++) {
d += (xarr[i] + yarr[i]) * (xarr[i] - yarr[i]);
}
}
catch (ArrayIndexOutOfBoundsException ae) {
d = Double.NaN;
}
return d;
|
public static double | sumx2py2(double[] xarr, double[] yarr)returns the sum of sum of squares of corresponding double
value in each subarray: ie. sigma (xarr[i]^2 + yarr[i]^2)
It is the responsibility of the caller
to ensure that the two subarrays are of equal length. If the
subarrays are not of equal length, the return value can be
unpredictable.
double d = 0;
try {
for (int i=0, iSize=xarr.length; i<iSize; i++) {
d += (xarr[i] * xarr[i]) + (yarr[i] * yarr[i]);
}
}
catch (ArrayIndexOutOfBoundsException ae) {
d = Double.NaN;
}
return d;
|
public static double | sumxmy2(double[] xarr, double[] yarr)returns the sum of squares of difference of corresponding double
value in each subarray: ie. sigma ( (xarr[i]-yarr[i])^2 )
It is the responsibility of the caller
to ensure that the two subarrays are of equal length. If the
subarrays are not of equal length, the return value can be
unpredictable.
double d = 0;
try {
for (int i=0, iSize=xarr.length; i<iSize; i++) {
double t = (xarr[i] - yarr[i]);
d += t * t;
}
}
catch (ArrayIndexOutOfBoundsException ae) {
d = Double.NaN;
}
return d;
|
public static double | tanh(double d)hyperbolic tangent
double ePowX = Math.pow(Math.E, d);
double ePowNegX = Math.pow(Math.E, -d);
d = (ePowX - ePowNegX) / (ePowX + ePowNegX);
return d;
|