jcontrol.lang
Class Math

java.lang.Object
  extended by jcontrol.lang.Math

public class Math
extends Object

Provides some simple math functions.

JControl devices are limited in computation rate and precision, so some useful mathematical functions are approximated with tables.


Field Summary
static double E
          The double value that is closer than any other to e, the base of the natural logarithms.
static double PI
          The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.
 
Method Summary
static double abs(double a)
          Returns the absolute value of a double value.
static float abs(float a)
          Returns the absolute value of a float value.
static int abs(int a)
          Returns the absolute value of an int value.
static long abs(long a)
          Returns the absolute value of a long value.
static double ceil(double a)
          Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer.
static float ceil(float a)
           
static int cos(int op)
          Look-up table cosine function.
static double floor(double a)
          Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer.
static float floor(float a)
           
static double max(double a, double b)
          Returns the greater of two double values.
static float max(float a, float b)
          Returns the greater of two float values.
static int max(int a, int b)
          Returns the greater of two int values.
static long max(long a, long b)
          Returns the greater of two long values.
static double min(double a, double b)
          Returns the smaller of two double values.
static float min(float a, float b)
          Returns the smaller of two float values.
static int min(int a, int b)
          Returns the smaller of two int values.
static long min(long a, long b)
          Returns the smaller of two long values.
static double pow(double a, double b)
          Returns the value of the first argument raised to the power of the second argument.
static int pow(int op, int exp)
          op raised to the power of exp
static int rnd(int mod)
          Returns a pseudo random number.
static long round(double a)
          Returns the closest long to the argument.
static int round(float a)
          Returns the closest int to the argument.
static int scale(int value, int source, int dest)
          Rule of proportion.
static int sin(int op)
          Look-up table sine function.
static double sqrt(double a)
          Returns the correctly rounded positive square root of a double value.
static int sqrt(int op)
          Look-up table square root.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, notifyAll, toString, wait
 

Field Detail

E

public static final double E
The double value that is closer than any other to e, the base of the natural logarithms.

See Also:
Constant Field Values

PI

public static final double PI
The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.

See Also:
Constant Field Values
Method Detail

rnd

public static int rnd(int mod)
Returns a pseudo random number.

Parameters:
mod - the range for the random number (1...mod-1), 0 for full 15 bit range
Returns:
the random number

sin

public static int sin(int op)
Look-up table sine function. The result is scaled to full integer range.

Parameters:
op - function argument in degrees (range 0 ... 360)
Returns:
scaled sine (-32767 ... 32767)

cos

public static int cos(int op)
Look-up table cosine function. The result is scaled to full integer range.

Parameters:
op - function argument in degrees (range 0 ... 360)
Returns:
scaled cosine (-32767 ... 32767)

pow

public static int pow(int op,
                      int exp)
op raised to the power of exp


scale

public static int scale(int value,
                        int source,
                        int dest)
Rule of proportion. As some JControl implementations are only able to calculate in 16 bit domain, this function could help scaling e.g. coordinates or control variables. The intermediate result of the multiplication will have 32 bit, the division takes these 32 bit and returns 16 bit. y=scale(x,a,b) is equal to y=x*b/a.

Parameters:
value - the value to scale (range -32768 ... 32767)
source - the divisor (range -32768 ... 32767)
dest - the multiplicant (range -32768 ... 32767)
Returns:
scaled value (range -32768 ... 32767)

sqrt

public static int sqrt(int op)
Look-up table square root.

Parameters:
op - value to extract the root from (range: 0 ... 32767)
Returns:
floor of square root (range 0 ... 181)

max

public static int max(int a,
                      int b)
Returns the greater of two int values. That is, the result is the argument closer to the value of Integer.MAX_VALUE. If the arguments have the same value, the result is that same value.

Parameters:
a - an int value.
b - an int value.
Returns:
the larger of a and b.
See Also:
Long.MAX_VALUE

min

public static int min(int a,
                      int b)
Returns the smaller of two int values. That is, the result the argument closer to the value of Integer.MIN_VALUE. If the arguments have the same value, the result is that same value.

Parameters:
a - an int value.
b - an int value.
Returns:
the smaller of a and b.
See Also:
Long.MIN_VALUE

abs

public static int abs(int a)
Returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

Parameters:
a - - the argument whose absolute value is to be determined
Returns:
the absolute value of the argument.
See Also:
Integer.MIN_VALUE

sqrt

public static double sqrt(double a)
Returns the correctly rounded positive square root of a double value. Special cases: Otherwise, the result is the double value closest to the true mathematical square root of the argument value.

Parameters:
a - a value.
Returns:
the positive square root of a. If the argument is NaN or less than zero, the result is NaN.

ceil

public static double ceil(double a)
Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer. Special cases: Note that the value of Math.ceil(x) is exactly the value of -Math.floor(-x).

Parameters:
a - a value.
Returns:
the smallest (closest to negative infinity) floating-point value that is not less than the argument and is equal to a mathematical integer.

ceil

public static float ceil(float a)
Parameters:
a -
Returns:
See Also:
ceil(double)

floor

public static double floor(double a)
Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer. Special cases:

Parameters:
a - a value.
Returns:
the largest (closest to positive infinity) floating-point value that is not greater than the argument and is equal to a mathematical integer.

floor

public static float floor(float a)
Parameters:
a -
Returns:
See Also:
floor(double)

pow

public static double pow(double a,
                         double b)
Returns the value of the first argument raised to the power of the second argument. Special cases:

(In the foregoing descriptions, a floating-point value is considered to be an integer if and only if it is finite and a fixed point of the method ceil or, equivalently, a fixed point of the method floor. A value is a fixed point of a one-argument method if and only if the result of applying the method to the value is equal to the value.)

A result must be within 1 ulp of the correctly rounded result. Results must be semi-monotonic.

Parameters:
a - the base.
b - the exponent.
Returns:
the value ab.

round

public static int round(float a)
Returns the closest int to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type int. In other words, the result is equal to the value of the expression:

(int)Math.floor(a + 0.5f)

Special cases:

Parameters:
a - a floating-point value to be rounded to an integer.
Returns:
the value of the argument rounded to the nearest int value.
See Also:
Integer.MAX_VALUE, Integer.MIN_VALUE

round

public static long round(double a)
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:

(long)Math.floor(a + 0.5d)

Special cases:

Parameters:
a - a floating-point value to be rounded to a long.
Returns:
the value of the argument rounded to the nearest long value.
See Also:
Long.MAX_VALUE, Long.MIN_VALUE

abs

public static long abs(long a)
Returns the absolute value of a long value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

Note that if the argument is equal to the value of Long.MIN_VALUE, the most negative representable long value, the result is that same value, which is negative.

Parameters:
a - the argument whose absolute value is to be determined
Returns:
the absolute value of the argument.
See Also:
Long.MIN_VALUE

abs

public static float abs(float a)
Returns the absolute value of a float value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned. Special cases: In other words, the result is the same as the value of the expression:

Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))

Parameters:
a - the argument whose absolute value is to be determined
Returns:
the absolute value of the argument.

abs

public static double abs(double a)
Returns the absolute value of a double value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned. Special cases: In other words, the result is the same as the value of the expression:

Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)

Parameters:
a - the argument whose absolute value is to be determined
Returns:
the absolute value of the argument.

max

public static long max(long a,
                       long b)
Returns the greater of two long values. That is, the result is the argument closer to the value of Long.MAX_VALUE. If the arguments have the same value, the result is that same value.

Parameters:
a - an argument.
b - another argument.
Returns:
the larger of a and b.
See Also:
Long.MAX_VALUE

max

public static float max(float a,
                        float b)
Returns the greater of two float values. That is, the result is the argument closer to positive infinity. If the arguments have the same value, the result is that same value. If either value is NaN, then the result is NaN. Unlike the the numerical comparison operators, this method considers negative zero to be strictly smaller than positive zero. If one argument is positive zero and the other negative zero, the result is positive zero.

Parameters:
a - an argument.
b - another argument.
Returns:
the larger of a and b.

max

public static double max(double a,
                         double b)
Returns the greater of two double values. That is, the result is the argument closer to positive infinity. If the arguments have the same value, the result is that same value. If either value is NaN, then the result is NaN. Unlike the the numerical comparison operators, this method considers negative zero to be strictly smaller than positive zero. If one argument is positive zero and the other negative zero, the result is positive zero.

Parameters:
a - an argument.
b - another argument.
Returns:
the larger of a and b.

min

public static long min(long a,
                       long b)
Returns the smaller of two long values. That is, the result is the argument closer to the value of Long.MIN_VALUE. If the arguments have the same value, the result is that same value.

Parameters:
a - an argument.
b - another argument.
Returns:
the smaller of a and b.
See Also:
Long.MIN_VALUE

min

public static float min(float a,
                        float b)
Returns the smaller of two float values. That is, the result is the value closer to negative infinity. If the arguments have the same value, the result is that same value. If either value is NaN, then the result is NaN. Unlike the the numerical comparison operators, this method considers negative zero to be strictly smaller than positive zero. If one argument is positive zero and the other is negative zero, the result is negative zero.

Parameters:
a - an argument.
b - another argument.
Returns:
the smaller of a and b.

min

public static double min(double a,
                         double b)
Returns the smaller of two double values. That is, the result is the value closer to negative infinity. If the arguments have the same value, the result is that same value. If either value is NaN, then the result is NaN. Unlike the the numerical comparison operators, this method considers negative zero to be strictly smaller than positive zero. If one argument is positive zero and the other is negative zero, the result is negative zero.

Parameters:
a - an argument.
b - another argument.
Returns:
the smaller of a and b.