net.jscience.math.waba
Class MathFP

java.lang.Object
  |
  +--net.jscience.math.waba.MathFP

public abstract class MathFP
extends java.lang.Object

This class implements 32 bit fixed-point integer math functions. The intend is to support floating point like calculations on the WABA platform. For more details on MathFP see the documentation of net.jscience.math.kvm.MathFP

Example:

int n = MathFP.toFP(12);
int m = MathFP.toFP("14.5");
int a = n + m;
a = a - MathFP.toFP("1.5");
a = MathFP.div(a,n);
a = MathFP.sqrt(a);
System.out.println(MathFP.toString(a));

Author: Onno Hommes
Version: 2.0.6


Field Summary
static int E
          The fixed-point representation of the natural number (2.7183).
static int MAX_VALUE
          The maximum fixed-point representation for the current precision.
static int MIN_VALUE
          The minimum fixed-point representation for the current precision.
static int PI
          The fixed-point representation of pi (3.1416)
 
Constructor Summary
MathFP()
           
 
Method Summary
static int abs(int n)
          Returns the absolute value of the fixed-point integer.
static int acos(int r)
          Returns the arc cosine of the fixed-point integer.
static int add(int n, int m)
          Adds two fixed-point integers.
static int asin(int x)
          Returns the arc sine of the fixed-point integer.
static int atan(int r)
          Returns the arc tangent of the radian fixed-point integer.
static int atan2(int y, int x)
          Returns the pricipal value of the arc tangent of y/x
static int convert(int l, int o)
          Changes precision of a fixed-point integer.
static int cos(int r)
          Returns the cosine of the radian fixed-point integer.
static int cot(int r)
          Returns the cotangent of the radian fixed-point integer.
static int div(int n, int m)
          Divides two fixed-point integers.
static int exp(int x)
          The natural number raised to the power of a fixed-point integer.
static int getPrecision()
          Get the current runtime precision for MathFP.
static int log(int s)
          Returns the natural logarithm of a fixed-point integer.
static int max(int n, int m)
          Returns the biggest number of the two fixed-point integers.
static int min(int n, int m)
          Returns the smallest number of the two fixed-point integers.
static int mul(int n, int m)
          Multiplies two fixed-point integers.
static int pow(int b, int e)
          Returns a fixed-point integer raised to a fixed-point integer
static int round(int x, int d)
          rounds a fixed-point integer.
static int setPrecision(int p)
          Set the runtime precision for MathFP.
static int sin(int r)
          Returns the sine of the radian fixed-point integer.
static int sqrt(int n)
          Returns the square root of the the fixed-point integer.
static int sqrt(int n, int r)
          Returns the square root of the the fixed-point integer.
static int stat()
          Returns the internal overflow value if any
static int sub(int n, int m)
          Subtracts two fixed-point integers.
static int tan(int r)
          Returns the tangent of the radian fixed-point integer.
static int toFP(int l)
          Converts a normal int to a fixed-point integer.
static int toFP(java.lang.String s)
          Converts a string input to a fixed-point integer.
static int toInt(int x)
          Converts a fixed-point integer to a int.
static java.lang.String toString(int l)
          Converts a fixed-point integer to a string.
static java.lang.String toString(int x, int d)
          Converts a fixed-point integer to a string with rounding.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PI

public static int PI
The fixed-point representation of pi (3.1416)

E

public static int E
The fixed-point representation of the natural number (2.7183).

MAX_VALUE

public static final int MAX_VALUE
The maximum fixed-point representation for the current precision.

MIN_VALUE

public static final int MIN_VALUE
The minimum fixed-point representation for the current precision.
Constructor Detail

MathFP

public MathFP()
Method Detail

setPrecision

public static int setPrecision(int p)
Set the runtime precision for MathFP.

The precision can be anywhere from 12 to 0 bits. However using 0 bits for the precision makes no sense for this library. The lowest setting would usually be 3. The default precision is 12 bits for the fraction.

Parameters:
p - the number of fractional bits.
Returns:
the number of visual digits behind the decimal point.
Since:
MathFP 2.0.3

getPrecision

public static int getPrecision()
Get the current runtime precision for MathFP.

The current precision is expressed in the number of bits used for the fractional part.

Returns:
the size of the fraction expressed in the number of bits.
Since:
MathFP 2.0.3

toInt

public static int toInt(int x)
Converts a fixed-point integer to a int.

This method will round the fixed point integer before converting it to a normal int. To avoid rounding you can shift the value precision bits to the right.

Parameters:
x - the fixed-point integer to be converted.
Returns:
a int in a normal integer representation.
Since:
MathFP 1.0

toFP

public static int toFP(int l)
Converts a normal int to a fixed-point integer.

The input int will be shifted the number of fraction bits to the left.

Parameters:
l - the int to be converted to a fixed-point integer
Returns:
a fixed-point integer with runtime precision bits fraction.
Since:
MathFP 1.0

convert

public static int convert(int l,
                          int o)
Changes precision of a fixed-point integer.

See net.jscience.math.MathFP for more details.

Parameters:
l - the fixed-point to change the precision of.
o - the current precision of the fixed-point integer.
Returns:
a fixed-point integer with the current runtime precision.
Since:
MathFP 1.0

toFP

public static int toFP(java.lang.String s)
Converts a string input to a fixed-point integer.

See net.jscience.math.MathFP for more details.

Parameters:
s - the String to be converted to a fixed-point integer.
Returns:
a fixed-point integer with precision bits fraction.
Throws:
java.lang.NumberFormatException - if the input is invalid.
Since:
MathFP 1.0

toString

public static java.lang.String toString(int l)
Converts a fixed-point integer to a string.

See net.jscience.math.MathFP for more details.

Parameters:
x - the fixed-point integer to be converted.
Returns:
a string representing the fixed-point integer value.
Since:
MathFP 2.0.3

toString

public static java.lang.String toString(int x,
                                        int d)
Converts a fixed-point integer to a string with rounding.

Possible values for d are 0 through 8 depending on the current runtime precision. See net.jscience.math.MathFP for more details.

Parameters:
x - the fixed-point integer to be converted.
d - the number of digits to show of the fraction.
Returns:
a string representing the rounded fixed-point integer.
Since:
MathFP 1.0.2

max

public static int max(int n,
                      int m)
Returns the biggest number of the two fixed-point integers.

See net.jscience.math.MathFP for more details.

Parameters:
n - a fixed-point integer.
m - a fixed-point integer.
Returns:
the maximum of the two fixed-point integers n,m.
Since:
MathFP 1.0.2

min

public static int min(int n,
                      int m)
Returns the smallest number of the two fixed-point integers.

See net.jscience.math.MathFP for more details.

Parameters:
n - a fixed-point integer.
m - a fixed-point integer.
Returns:
the smallest number of the two fixed-point integers n,m.
Since:
MathFP 1.0.2

round

public static int round(int x,
                        int d)
rounds a fixed-point integer.

The maximum number of digits to round to is 4 but can be lower depending on the current runtime precision. See net.jscience.math.MathFP for more details.

Parameters:
x - the fixed-point integer to be rounded
d - the number of precision digits to round to.
Returns:
a rounded fixed-point integer.
Since:
MathFP 1.0.2

mul

public static int mul(int n,
                      int m)
Multiplies two fixed-point integers.

See net.jscience.math.MathFP for more details.

Parameters:
n - the fixed-point integer to be multiplied.
m - the fixed-point integer multiplier
Returns:
an new fixed point integer representing n multiplied by m.
Throws:
Nothing - but sets err_no flag
Since:
MathFP 1.0.0

div

public static int div(int n,
                      int m)
Divides two fixed-point integers.

See net.jscience.math.MathFP for more details.

Parameters:
n - the dividend fixed-point integer.
m - the divider fixed-point integer.
Returns:
an new fixed point integer representing n divided by m.
Since:
MathFP 1.0.0

add

public static int add(int n,
                      int m)
Adds two fixed-point integers.

See net.jscience.math.MathFP for more details.

Parameters:
n - the fixed-point integer to add to.
m - the fixed-point integer to be added.
Returns:
an new fixed point integer representing the addition of n and m.
Since:
MathFP 1.0.0

sub

public static int sub(int n,
                      int m)
Subtracts two fixed-point integers.

See net.jscience.math.MathFP for more details.

Parameters:
n - the fixed-point integer to subtract from.
m - the fixed-point integer to be subtracted.
Returns:
an new fixed point integer representing the subtraction of n and m.
Since:
MathFP 1.0.0

abs

public static int abs(int n)
Returns the absolute value of the fixed-point integer.

See net.jscience.math.MathFP for more details.

Parameters:
n - the fixed-point integer to get the absolute value of.
Returns:
an new fixed point integer representing the absolute value of n.
Since:
KVM-DR4.1

sqrt

public static int sqrt(int n,
                       int r)
Returns the square root of the the fixed-point integer.

See net.jscience.math.MathFP for more details. However the value for r would normally not have to be bigger then 16.

This method uses the Newton method to extract the root.

Parameters:
n - the fixed-point integer to extract the root from.
r - an integer that specifies the number of iterations.
Returns:
the fixed-point integer sqrt of n.
Throws:
Nothing - but sets err_no flag
Since:
MathFP 1.1.0

sqrt

public static int sqrt(int n)
Returns the square root of the the fixed-point integer.

See net.jscience.math.MathFP for more details.

Parameters:
n - the fixed-point integer to extract the root from.
Returns:
the fixed-point integer sqrt of n.
Throws:
Nothing - but sets err_no flag
Since:
MathFP 1.0.1

sin

public static int sin(int r)
Returns the sine of the radian fixed-point integer.

See net.jscience.math.MathFP for more details.

Parameters:
r - the radian fixed-point integer to get the sine value of.
Returns:
the sine for the radian n as a fixed-point integer.
Since:
MathFP 1.0.2

asin

public static int asin(int x)
Returns the arc sine of the fixed-point integer.

See net.jscience.math.MathFP for more details.

Parameters:
s - a fixed-point integer to get the arc sine of.
Returns:
the arc sine of the fixed-point integer s.
Throws:
ArithmeticException - if the input is < -1 and > 1.
Since:
MathFP 1.0.3

cos

public static int cos(int r)
Returns the cosine of the radian fixed-point integer.

See net.jscience.math.MathFP for more details.

Parameters:
r - the radian fixed-point integer to get the cosine value of.
Returns:
the cosine for the radian r as a fixed-point integer.
Since:
MathFP 1.0.2

acos

public static int acos(int r)
Returns the arc cosine of the fixed-point integer.

See net.jscience.math.MathFP for more details.

Parameters:
s - a fixed-point integer to get the arc sine of.
Returns:
the arc sine of the fixed-point integer s.
Throws:
ArithmeticException - if the input is < -1 and > 1.
Since:
MathFP 1.0.2

tan

public static int tan(int r)
Returns the tangent of the radian fixed-point integer.

See net.jscience.math.MathFP for more details.

Parameters:
r - the radian fixed-point integer to get the tangent value of.
Returns:
the tangent of the radian r as a fixed-point integer.
Throws:
ArithmeticException - if the input is PI/2 or any multplication of it.
Since:
MathFP 1.0.2

cot

public static int cot(int r)
Returns the cotangent of the radian fixed-point integer.

See net.jscience.math.MathFP for more details.

Parameters:
r - the radian fixed-point integer to get the tangent value of.
Returns:
the cotangent of the radian r as a fixed-point integer.
Throws:
ArithmeticException - if the input is 0, PI or any multplication of it.
Since:
MathFP 1.1.2

atan

public static int atan(int r)
Returns the arc tangent of the radian fixed-point integer.

See net.jscience.math.MathFP for more details.

Parameters:
r - the radian fixed-point integer to get the tangent value of.
Returns:
the arc tangent of the radian r as a fixed-point integer.
Throws:
ArithmeticException. -  
Since:
MathFP 2.0.0

exp

public static int exp(int x)
The natural number raised to the power of a fixed-point integer.

See net.jscience.math.MathFP for more details.

Parameters:
x - the fixed-point integer exponent.
Returns:
the natural number e raised to the power of x.
Throws:
ArithmeticException - if the function overflows
Since:
MathFP 1.2.0

log

public static int log(int s)
Returns the natural logarithm of a fixed-point integer.

See net.jscience.math.MathFP for more details.

Parameters:
s - the fixed-point integer to get the logarithm of.
Returns:
the logarithm of s.
Throws:
ArithmeticException - if the input is <= 0.
Since:
MathFP 1.2.0

pow

public static int pow(int b,
                      int e)
Returns a fixed-point integer raised to a fixed-point integer

See net.jscience.math.MathFP for more details.

Parameters:
b - the fixed-point integer base.
e - the fixed-point integer exponent.
Returns:
the natural number s raised to the power of e.
Throws:
ArithmeticException - if the function overflows.
Since:
MathFP 1.2.0

stat

public static int stat()
Returns the internal overflow value if any

An err_no equal to 0 means no bad input or overflow. A value of 1 means bad input argument A value of 2 means overflow detected

Returns:
the internal overflow or bad input detector.
Since:
MathFP 1.2.0

atan2

public static int atan2(int y,
                        int x)
Returns the pricipal value of the arc tangent of y/x

Compute the principal value of the arc tangent of y/x, using the signs of both parameters to determine the quadrant of the return value or inother words computes elementwise the angle in radian between the positive part of the x-axis and the line with origin in (0,0) which contains the point (x, y).

Parameters:
y - the fixed-point integer
x - the fixed-point integer
Returns:
the pricipal value of the arctangent y/x
Since:
MathFP2.0.5