edu.neu.ccs.util
Class MathUtilities

java.lang.Object
  extended byedu.neu.ccs.util.MathUtilities

public class MathUtilities
extends Object

The class MathUtilities collects several useful static mathematical functions. The class may be viewed as an extension of the tools found in java.lang.Math.

Since:
1.2

Constructor Summary
MathUtilities()
           
 
Method Summary
static double acosdeg(double value)
          Return the arc cosine in degrees of the specified input value in the range -1 to +1.
static double asindeg(double value)
          Return the arc sine in degrees of the specified input value in the range -1 to +1.
static double atan2deg(double y, double x)
          Return the polar angle in degrees of the point (x, y) where the y-coordinate is given first and the x-coordinate second.
static double atandeg(double value)
          Return the arc tangent in degrees of the specified input value.
static double cosdeg(double degrees)
          Return the cosine of the given angle specified in degrees.
static int GCD(int a, int b)
          Returns the greatest common divisor of the int inputs a and b.
static long GCD(long a, long b)
          Returns the greatest common divisor of the long inputs a and b.
static int LCM(int a, int b)
          Returns the least common multiple of the int inputs a and b.
static long LCM(long a, long b)
          Returns the least common multiple of the long inputs a and b.
static int modulus(int number, int base)
          Returns the modulus of int number relative to int base.
static long modulus(long number, long base)
          Returns the modulus of long number relative to long base.
static double power(double x, int n)
          Returns the n-th power of x, xn, using the given integer exponent n.
static double randomDouble(double x)
          Return a random double r in the range between 0 and x.
static double randomDouble(double x, double y)
          Return a random double r in the range between x and y.
static int randomInt(int a)
          Return a random int r in the range between 0 and a inclusive.
static int randomInt(int a, int b)
          Return a random int r in the range between a and b inclusive.
static double root(double x, double n)
          Returns the n-th root of x.
static double sindeg(double degrees)
          Return the sine of the given angle specified in degrees.
static double tandeg(double degrees)
          Return the tangent of the given angle specified in degrees.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MathUtilities

public MathUtilities()
Method Detail

randomInt

public static int randomInt(int a)
Return a random int r in the range between 0 and a inclusive.

Parameters:
a - an endpoint of a range

randomInt

public static int randomInt(int a,
                            int b)
Return a random int r in the range between a and b inclusive.

Parameters:
a - endpoint 1 of a range
b - endpoint 2 of a range

randomDouble

public static double randomDouble(double x)
Return a random double r in the range between 0 and x.

Parameters:
x - an endpoint of a range

randomDouble

public static double randomDouble(double x,
                                  double y)
Return a random double r in the range between x and y.

Parameters:
x - endpoint 1 of a range
y - endpoint 2 of a range

sindeg

public static double sindeg(double degrees)
Return the sine of the given angle specified in degrees.


cosdeg

public static double cosdeg(double degrees)
Return the cosine of the given angle specified in degrees.


tandeg

public static double tandeg(double degrees)
Return the tangent of the given angle specified in degrees.


asindeg

public static double asindeg(double value)
Return the arc sine in degrees of the specified input value in the range -1 to +1.


acosdeg

public static double acosdeg(double value)
Return the arc cosine in degrees of the specified input value in the range -1 to +1.


atandeg

public static double atandeg(double value)
Return the arc tangent in degrees of the specified input value.


atan2deg

public static double atan2deg(double y,
                              double x)
Return the polar angle in degrees of the point (x, y) where the y-coordinate is given first and the x-coordinate second.

Parameters:
y - the y-coordinate of the point
x - the x-coordinate of the point

GCD

public static int GCD(int a,
                      int b)

Returns the greatest common divisor of the int inputs a and b.

The return value will be positive or zero and will be zero only if both a and b are zero.


GCD

public static long GCD(long a,
                       long b)

Returns the greatest common divisor of the long inputs a and b.

The return value will be positive or zero and will be zero only if both a and b are zero.


LCM

public static int LCM(int a,
                      int b)

Returns the least common multiple of the int inputs a and b.

The return value will be positive or zero and will be zero only if both a and b are zero.


LCM

public static long LCM(long a,
                       long b)

Returns the least common multiple of the long inputs a and b.

The return value will be positive or zero and will be zero only if both a and b are zero.


modulus

public static int modulus(int number,
                          int base)

Returns the modulus of int number relative to int base.

Returns n such that n is congruent to number modulo base and 0 <= n < Math.abs(base).

Throws ArithmeticException if base is zero.


modulus

public static long modulus(long number,
                           long base)

Returns the modulus of long number relative to long base.

Returns n such that n is congruent to number modulo base and 0 <= n < Math.abs(base).

Throws ArithmeticException if base is zero.


power

public static double power(double x,
                           int n)

Returns the n-th power of x, xn, using the given integer exponent n.

Computes using ordinary arithmetic operations.

Returns 1.0 regardless of x if n equals 0.

Returns Double.NaN if n is negative and x is zero.

Note: Departs from the convention of the Java Math class by calling the method power rather than simply pow.

Note added in 2.5.0: This method uses only multiplication and division and does not use exp or log. Recents experiments showed that this method may be less accurate than calling Math.pow. Hence the use of this method is no longer recommended. It is retained in JPT to avoid breaking old code.

Parameters:
x - the base
n - the exponent

root

public static double root(double x,
                          double n)

Returns the n-th root of x.

If n is 0, returns NaN.

If x is 0 and n > 0, returns 0; otherwise returns NaN.

If x > 0, returns Math.pow(x,1.0/n).

If x < 0 and n is an odd integer, then, with z = -x, returns -Math.pow(z,1.0/n); otherwise, returns NaN.

Parameters:
x - the base
n - the inverse of the exponent