net.rim.device.api.util
Class MathUtilities

java.lang.Object
  |
  +--net.rim.device.api.util.MathUtilities

public final class MathUtilities
extends Object

Provides some basic numeric operations.


Method Summary
static double acos(double x)
          Return the arc cosine of the value x.
static double asin(double x)
          Return the arc sine of the value x.
static double atan(double x)
          Return the arc tangent of the value x.
static double atan2(double y, double x)
          Converts rectangular coordinates (x,y) to polar coordinates (r,theta).
static int clamp(int low, int value, int high)
          Clamps provided value between a lower and upper bound.
static double exp(double x)
          Return the exponential (base e) of x.
static double ldexp(double x, int exp)
          Returns the result of multiplying x (the significand) by 2 raised to the power of exp (the exponent).
static double log(double x)
          Return the natural logarithm (base e) of x.
static int log2(int value)
          Returns the log base 2 of the unsigned value rounded down.
static int log2(long value)
          Returns the log base 2 of the unsigned value rounded down.
static double pow(double x, double y)
          Return x raised to the power of y.
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 wrap(int low, int value, int high)
          Wraps provided value around a lower and upper bound.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

acos

public static double acos(double x)
Return the arc cosine of the value x.
Parameters:
x - the value.
Returns:
the arc cosine of the argument.
Since:
JDE 4.6.0

asin

public static double asin(double x)
Return the arc sine of the value x.
Parameters:
x - the value.
Returns:
the arc sine of the argument.
Since:
JDE 4.6.0

atan

public static double atan(double x)
Return the arc tangent of the value x.
Parameters:
x - the value.
Returns:
the arc tangent of the argument.
Since:
JDE 4.6.0

atan2

public static double atan2(double y,
                           double x)
Converts rectangular coordinates (x,y) to polar coordinates (r,theta). This method computes the phase theta by computing the arc tangent of y/x in the range of -pi to pi.
Parameters:
y - the abscissa coordinate.
x - the ordinate coordinate.
Returns:
the theta component of the point (r,theta) in polar coordinates that corresponds to the point (x,y) in Cartesian coordinates.
Since:
JDE 4.6.0

clamp

public static int clamp(int low,
                        int value,
                        int high)
Clamps provided value between a lower and upper bound.

Note: This method only produces sensible results if the lower bound is less than, or equal to, the higher bound.

Parameters:
low - Lower bound value.
value - Value to clamp.
high - Upper bound value.
Returns:
If the value is lower than the lower bound, this method returns the lower bound; if the value is higher than the higher bound, this method returns the higher bound; otherwise, this method returns the value itself.

exp

public static double exp(double x)
Return the exponential (base e) of x. Interesting cases:
  • If the argument is negative infinity, the result is zero.
  • If the argument is positive infinity, the result is positive infinity.
  • If the argument is NaN, the result is NaN.
  • If the argument is greater than 7.09e+02, the result is positive infinity.
  • If the argument is less than -7.45e+02, the result is zero.
Parameters:
x - the power to raise e to.
Returns:
e raised to the power x.
Since:
JDE 4.2.0

ldexp

public static double ldexp(double x,
                           int exp)
Returns the result of multiplying x (the significand) by 2 raised to the power of exp (the exponent).
Parameters:
x - the significand.
exp - the exponent.
Returns:
x * 2exp.
Since:
JDE 4.6.0

log

public static double log(double x)
Return the natural logarithm (base e) of x. Interesting cases:
  • If the argument is less than zero (including negative infinity), the result is NaN.
  • If the argument is zero, the result is negative infinity.
  • If the argument is positive infinity, the result is positive infinity.
  • If the argument is NaN, the result is NaN.
Parameters:
x - a number greater than zero.
Returns:
the natural logarithm (base e) of the argument.
Since:
JDE 4.2.0

log2

public static int log2(int value)
Returns the log base 2 of the unsigned value rounded down.

The value zero and one both return zero.

Parameters:
value - The unsigned value to calculate log2 on.
Returns:
The calculation of log2(value) (range: 0-31).
Since:
JDE 4.2.0

log2

public static int log2(long value)
Returns the log base 2 of the unsigned value rounded down.

The value zero and one both return a log2 result of zero.

Parameters:
value - The unsigned value to calculate log2 on.
Returns:
The calculation of log2(value) (range: 0-63).
Since:
JDE 4.2.0

pow

public static double pow(double x,
                         double y)
Return x raised to the power of y.
Parameters:
x - the base value.
y - the exponent.
Returns:
xy.
Since:
JDE 4.6.0

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 if positive or subtracting 1/2 if negative, taking the floor of the result, and casting the result to type long.

Special cases:

  • If the argument is NaN, the result is 0.
  • If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE.
  • If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.
  • If the argument is an exact negative half (-x.5), the value is rounded to the closest value to zero.
Parameters:
a - a floating-point value to be rounded to a long.
Returns:
the value of the argument rounded to the nearest long value.
Since:
JDE 4.6.0
See Also:
Long.MAX_VALUE, Long.MIN_VALUE

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 if positive or subtracting 1/2 if negative, taking the floor of the result, and casting the result to type int.

Special cases:

  • If the argument is NaN, the result is 0.
  • If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.
  • If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.
  • If the argument is an exact negative half (-x.5), the value is rounded to the closes value to zero.
Parameters:
a - a floating-point value to be rounded to an integer.
Returns:
the value of the argument rounded to the nearest int value.
Since:
JDE 4.6.0
See Also:
Integer.MAX_VALUE, Integer.MIN_VALUE

wrap

public static int wrap(int low,
                       int value,
                       int high)
Wraps provided value around a lower and upper bound.

Note: This method only produces sensible results if the lower bound is less than, or equal to, the higher bound.

Parameters:
low - Lower bound value.
value - Value to wrap.
high - Upper bound value.
Returns:
If the value is lower than the lower bound, this method returns the higher bound; if the value is higher than the higher bound, this method returns the lower bound; otherwise, this method returns the value itself. This is the opposite of clamp()
Since:
JDE 4.0.0



Copyright 1999-2009 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved.
Copyright 1993-2003 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
Copyright 2002-2003 Nokia Corporation All Rights Reserved.
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.