org.kalmeo.util
Class MathFP

java.lang.Object
  extended by org.kalmeo.util.MathFP

public abstract class MathFP
extends java.lang.Object

J2ME Fixed-Point Math Library

Provided under the artistic license: http://www.opensource.org/licenses/artistic-license.html

Basically it means you can use this library for free, even for commercial purposes.

References:

Author:
Dan Carter (orbisstudios.com), bbeaulant (Refactor and bug fix)

Field Summary
static int DEFAULT_PRECISION
           
static int E
           
static int HALF
           
static int INFINITY
          largest possible number
static int ONE
           
static int PI
           
static int PI_HALF
           
static int PI_TWO
           
static int TWO
           
 
Constructor Summary
MathFP()
           
 
Method Summary
static int acos(int fp)
          Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.
static int asin(int fp)
          Returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2.
static int atan(int fp)
          Returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2.
static int atan2(int fpX, int fpY)
          Returns the angle theta from the conversion of rectangular coordinates (fpXfpY) to polar coordinates (r, theta).
static int ceil(int fp)
          Returns the smallest (closest to negative infinity) fixed-point value that is greater than or equal to the argument and is equal to a mathematical integer.
static int convert(int fp, int precision)
          Converts a fixed-point value to the current set precision.
static int cos(int fp)
          Returns the trigonometric cosine of an angle.
static int div(int fp1, int fp2)
          Divides two fixed-point values.
static int exp(int fp)
          Returns Euler's number e raised to the power of a fixed-point value.
static int floor(int fp)
          Returns the largest (closest to positive infinity) fixed-point value value that is less than or equal to the argument and is equal to a mathematical integer.
static int frac(int fp)
          Returns the fractional part of a fixed-point value.
static int fracAsInt(int fp)
          Converts a fixed-point integer to an int with only the decimal value.
static int getPrecision()
           
static int log(int x)
          Returns the natural logarithm (base e) of a fixed-point value.
static int log(int fp, int base)
          Returns the logarithm (base base) of a fixed-point value.
static int mul(int fp1, int fp2)
          Multiplies two fixed-point values.
static int pow(int fp1, int fp2)
          Returns the value of the first argument raised to the power of the second argument
static int round(int fp)
          Returns the closest integer to the argument.
static void setPrecision(int precision)
          Sets the precision for all fixed-point operations.
static int sin(int fp)
          Returns the trigonometric sine of an angle.
static int sqrt(int fp)
          Returns the correctly rounded positive square root of a fixed-point value.
static int tan(int fp)
          Returns the trigonometric tangent of an angle.
static int toDegrees(int fp)
          Converts an angle measured in radians to an approximately equivalent angle measured in degrees.
static int toFP(int i)
          Converts an int to a fixed-point int.
static int toFP(java.lang.String s)
          Converts a string to a fixed-point value.
static int toInt(int fp)
          Converts a fixed-point value to an int.
static int toRadians(int fp)
          Converts an angle measured in degrees to an approximately equivalent angle measured in radians.
static java.lang.String toString(int fp)
          Converts a fixed-point value to a string.
static int trunc(int fp)
          Removes the fractional part of a fixed-point value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PRECISION

public static final int DEFAULT_PRECISION
See Also:
Constant Field Values

ONE

public static int ONE

HALF

public static int HALF

TWO

public static int TWO

E

public static int E

PI

public static int PI

PI_HALF

public static int PI_HALF

PI_TWO

public static int PI_TWO

INFINITY

public static final int INFINITY
largest possible number

See Also:
Constant Field Values
Constructor Detail

MathFP

public MathFP()
Method Detail

getPrecision

public static int getPrecision()
Returns:
the precision

setPrecision

public static void setPrecision(int precision)
Sets the precision for all fixed-point operations.
The maximum precision is 31 bits.

Parameters:
precision - the desired precision in number of bits

convert

public static int convert(int fp,
                          int precision)
Converts a fixed-point value to the current set precision.

Parameters:
fp - the fixed-point value to convert.
precision - the precision of the fixed-point value passed in.
Returns:
a fixed-point value of the current precision

toFP

public static int toFP(int i)
Converts an int to a fixed-point int.

Parameters:
i - int to convert.
Returns:
the converted fixed-point value.

toFP

public static int toFP(java.lang.String s)
Converts a string to a fixed-point value.
The string should trimmed of any whitespace before-hand.
A few examples of valid strings:
 .01
 0.01
 10
 130.0
 -30000.12345
 

Parameters:
s - the string to convert.
Returns:
the fixed-point value.

toInt

public static int toInt(int fp)
Converts a fixed-point value to an int.

Parameters:
fp - fixed-point value to convert
Returns:
the converted int value.

toString

public static java.lang.String toString(int fp)
Converts a fixed-point value to a string. Same as toString(x, 0, max_possible_digits)

Parameters:
fp - the fixed-point value to convert.
Returns:
a string representing the fixed-point value with a minimum of decimals in the string.

ceil

public static int ceil(int fp)
Returns the smallest (closest to negative infinity) fixed-point value that is greater than or equal to the argument and is equal to a mathematical integer.

Parameters:
fp - a fixed-point value.
Returns:
the smallest (closest to negative infinity) fixed-point value that is greater than or equal to the argument and is equal to a mathematical integer.

floor

public static int floor(int fp)
Returns the largest (closest to positive infinity) fixed-point value value that is less than or equal to the argument and is equal to a mathematical integer.

Parameters:
fp - a fixed-point value.
Returns:
the largest (closest to positive infinity) fixed-point value that less than or equal to the argument and is equal to a mathematical integer.

trunc

public static int trunc(int fp)
Removes the fractional part of a fixed-point value.

Parameters:
fp - the fixed-point value to truncate.
Returns:
a truncated fixed-point value.

frac

public static int frac(int fp)
Returns the fractional part of a fixed-point value.

Parameters:
fp - a fixed-point value to get fractional part of.
Returns:
positive fractional fixed-point value if input is positive, negative fractional otherwise.

fracAsInt

public static int fracAsInt(int fp)
Converts a fixed-point integer to an int with only the decimal value.

For example, if fp represents 12.34 the method returns 34

Parameters:
fp - the fixed-point integer to be converted
Returns:
a int in a normal integer representation

round

public static int round(int fp)
Returns the closest integer to the argument.

Parameters:
fp - the fixed-point value to round
Returns:
the value of the argument rounded to the nearest integer value.

mul

public static int mul(int fp1,
                      int fp2)
Multiplies two fixed-point values.

Parameters:
fp1 - first fixed-point value.
fp2 - second fixed-point value.
Returns:
the result of the multiplication.

div

public static int div(int fp1,
                      int fp2)
Divides two fixed-point values.

Parameters:
fp1 - mumerator fixed-point value.
fp2 - denominator fixed-point value.
Returns:
the result of the division.

sqrt

public static int sqrt(int fp)
Returns the correctly rounded positive square root of a fixed-point value.

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

sin

public static int sin(int fp)
Returns the trigonometric sine of an angle.

Parameters:
fp - the angle in radians
Returns:
the sine of the argument.

cos

public static int cos(int fp)
Returns the trigonometric cosine of an angle.

Parameters:
fp - the angle in radians
Returns:
the cosine of the argument.

tan

public static int tan(int fp)
Returns the trigonometric tangent of an angle.

Parameters:
fp - the angle in radians
Returns:
the tangent of the argument.

asin

public static int asin(int fp)
Returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2.

Parameters:
fp - the fixed-point value whose arc sine is to be returned.
Returns:
the arc sine of the argument.

acos

public static int acos(int fp)
Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.

Parameters:
fp - the fixed-point value whose arc cosine is to be returned.
Returns:
the arc cosine of the argument.

atan

public static int atan(int fp)
Returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2.

Parameters:
fp - the fiexed-point value whose arc tangent is to be returned.
Returns:
the arc tangent of the argument.

atan2

public static int atan2(int fpX,
                        int fpY)
Returns the angle theta from the conversion of rectangular coordinates (fpXfpY) to polar coordinates (r, theta).

Parameters:
fpX - the ordinate coordinate
fpY - the abscissa coordinate
Returns:
the theta component of the point (rtheta) in polar coordinates that corresponds to the point (fpXfpY) in Cartesian coordinates.

exp

public static int exp(int fp)
Returns Euler's number e raised to the power of a fixed-point value.

Parameters:
fp - the exponent to raise e to.
Returns:
the value efp, where e is the base of the natural logarithms.

log

public static int log(int x)
Returns the natural logarithm (base e) of a fixed-point value.

Parameters:
fp - a fixed-point value
Returns:
the value ln a, the natural logarithm of fp.

log

public static int log(int fp,
                      int base)
Returns the logarithm (base base) of a fixed-point value.

Parameters:
fp - a fixed-point value
base -
Returns:
the value log a, the logarithm of fp

pow

public static int pow(int fp1,
                      int fp2)
Returns the value of the first argument raised to the power of the second argument

Parameters:
fp1 - the base
fp2 - the exponent
Returns:
the value ab.

toRadians

public static int toRadians(int fp)
Converts an angle measured in degrees to an approximately equivalent angle measured in radians.

Parameters:
fp - a fixed-point angle in degrees
Returns:
the measurement of the angle angrad in radians.

toDegrees

public static int toDegrees(int fp)
Converts an angle measured in radians to an approximately equivalent angle measured in degrees.

Parameters:
fp - a fixed-point angle in radians
Returns:
the measurement of the angle angrad in degrees.


Copyright © 2007-2008 Kalmeo. All Rights Reserved.