

PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 
java.lang.Object net.rim.device.api.math.Fixed32
public final class Fixed32
The class Fixed32
is a collection of fixedpoint math
routines. Fixed32
uses the 16.16 convention to fix the
decimal point.
There are no add
or sub
methods because
they can be performed with regular integer +
and

operators. The goal is to keep this library to an
absolute minimum in size.
The methods of class Fixed32
are all static, and deal
with 32bit int
s.
The 16.16 convention uses the 16 mostsignificant bits of a
32bit int
to represent the whole part, and the 16
leastsignificant bits to represent the fractional part.
For example, 0x00010000
is the fixedpoint
representation of 1, 0x00018000
is the fixedpoint
representation of 1.5, and 0x00000001
is the
fixedpoint representation of 1/65536.
The numbers are signed, so negative numbers can be represented. The largest positive number in 16.16 format is just over 32767.9999, and the closest to negative infinity is 32768.0. The smallest increment between consecutive numbers is 1/65536 (which works out to about 0.00001526).
Fixed32
provides 2 methods for converting to
fixedpoint representation. The method toFP
converts an integer to fixedpoint:
int n = Fixed32.toFP(7); // 7.0The method
tenThouToFP
converts a quantity in
tenthousandths to fixedpoint:
int m = Fixed32.tenThouToFP(70625); // 7.0625Also, there are 2 methods for converting back from fixedpoint. The method
toInt
truncates any fractional part:
System.out.println(Fixed32.toInt(m)); // prints 7The method
toIntTenThou
returns a value in units
of tenthousandths:
System.out.println(Fixed32.toIntTenThou(m)); // prints 70625Fixedpoint numbers can be added, subtracted, negated, and compared using regular integer operators:
int result = m  n; // 7.0625  7.0 = 0.0625 result = result; // 0.0625 result = n; // 0.0625  7.0 = 7.0625 boolean b = (result == m); // true boolean bb = (m < n); // falseThe increment and decrement operators will not give expected results:
result = Fixed32.toFP(2); ++result; // WRONG! result will NOT be 3 result = Fixed32.toFP(2); result += Fixed32.toFP(1); // Correct: result will be 3If you are multiplying or dividing by an integer scalar, you may use the regular integer * and / operators. Otherwise you must use the
mul
or div
methods.
You must use mul
to multiply 2 fixedpoint
numbers, and you must use div
to divide 2
fixedpoint numbers!
m = Fixed32.tenThouToFP(12500); // 1.25 m *= 3; // OK: 1.25 * 3 = 3.75 m /= 2; // OK: 3.75 / 2 = 1.875 m = Fixed32.mul(m, Fixed32.tenThouToFP(15000)); // 1.875 * 1.5000 = 2.8125 m = Fixed32.div(m, m); // 2.8125 / 2.8125 = 1.0The
Fixed32
methods mul
, div
,
sqrt
, sind
, cosd
,
tand
, and atand2
all deal with
16.16 fixedpoint numbers:
m = Fixed32.tenThouToFP(172500); // 17.2500 n = Fixed32.sqrt(m); // sqrt(17.25) n = Fixed32.sind(m); // sine of 17.25 degrees n = Fixed32.cosd(m); // cosine of 17.25 degrees result = Fixed32.atand2(m, m); // returns 135.0 degrees in fixedpoint
Field Summary  

static int 
E
A fixedpoint representation of e, good to the fourth decimal place (2.71828...). 

static int 
FP090
Fixedpoint representation of 90. 

static int 
FP180
Fixedpoint representation of 180. 

static int 
FP270
Fixedpoint representation of 270. 

static int 
FP360
Fixedpoint representation of 360. 

static int 
HALF


static int 
MAX_VALUE
The maximum fixedpoint representation (32767.9999847412109375). 

static int 
MIN_VALUE
The minimum fixedpoint representation (32768). 

static short 
NUM_FRACTION_BITS
The number of fraction bits for 16.16 fixedpoint representation (16). 

static int 
ONE


static int 
PI
A fixedpoint representation of pi, good to the fourth decimal place (3.14159...). 

static int 
PI_OVER_2


static int 
QUARTER


static int 
RAD2DEG
A fixedpoint representation of the conversion factor from radians to degrees, good to the fourth decimal place (57.29578...). 

static int 
TWOPI

Method Summary  

static int 
ArcTan(int f)
Computes ArcTan(f) in radians, f is a fixed point number. 

static int 
Cos(int f)
Computes COS(f), f is a fixed point number in radians. 

static int 
Sin(int f)
Computes SIN(f), f is a fixed point number in radians. 

static int 
Tan(int f)
Computes Tan(f), f is a fixed point number in radians. 

static int 
abs(int a)
Returns the absolute value of the given parameter 

static int 
atand2(int y,
int x)
Returns the arctangent of y/x in degrees
for the point (x,y)
with consideration to all 4 quadrants. 

static int 
cosd(int ang)
Returns the cosine of a fixedpoint angle in degrees. 

static int 
div(int n,
int m)
Returns the quotient of two fixedpoint numbers. 

static int 
divtoInt(int n,
int m)
Divide and convert back to regular int. 

static int 
mul(int n,
int m)
Returns the product of two fixedpoint numbers. 

static int 
parseFixed32(String value)
Parses a Fixed32 value from a String. 

static int 
round(int n)
Round a fixedpoint value to the nearest fixedpoint value representing an integer. 

static int 
sind(int ang)
Returns the sine of a fixedpoint angle in degrees. 

static int 
sqrt(int n)
Returns the square root of a fixedpoint number. 

static int 
tand(int ang)
Returns the tangent of a fixedpoint angle in degrees. 

static int 
tenThouToFP(int tenThou)
Converts an integer in tenthousandths to a fixedpoint number. 

static int 
toFP(int i)
Converts a normal integer to a fixedpoint number. 

static int 
toInt(int fp)
Converts a fixedpoint number to a normal integer. 

static int 
toIntTenThou(int fp)
Converts a fixedpoint number to an integer in tenthousandths. 

static int 
toRoundedInt(int value)
Converts a fixedpoint number to a normal integer. 
Methods inherited from class java.lang.Object 

equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait 
Field Detail 

public static final int MAX_VALUE
public static final int MIN_VALUE
public static final int PI
public static final int E
public static final short NUM_FRACTION_BITS
public static final int FP090
public static final int FP180
public static final int FP270
public static final int FP360
public static final int RAD2DEG
public static final int ONE
public static final int HALF
public static final int QUARTER
public static final int TWOPI
public static final int PI_OVER_2
Method Detail 

public static int abs(int a)
a
 The value to find the absolute value ofpublic static int mul(int n, int m)
n
 the fixedpoint number to be multiplied.m
 the fixedpoint multiplier.
n
and m
.public static int div(int n, int m)
n
 the fixedpoint dividend.m
 the fixedpoint divisor.
n
divided by m
.public static int toInt(int fp)
Fixed32.toIntTenThou(int)
if you need
to keep the fractional part.
fp
 the fixedpoint number to be truncated and converted.
public static int toRoundedInt(int value)
value
 the fixedpoint number to convert.
public static int toIntTenThou(int fp)
fp
 the fixedpoint number to be converted.
public static int toFP(int i)
Fixed32.tenThouToFP(int)
if you need to convert a number
with a fractional part. The given integer must be within
32768 to 32767 (inclusive) or the return value will not
be representative.
i
 the integer to be converted to a fixedpoint number.
public static int tenThouToFP(int tenThou)
tenThou
 the integer in tenthousandths to be converted.
public static int sqrt(int n)
n
 the fixedpoint number to extract the root from.
n
.
IllegalArgumentException
 if n
is negative.public static int sind(int ang)
ang
 the fixedpoint angle in degrees.
ang
.public static int cosd(int ang)
ang
 the fixedpoint angle in degrees.
ang
.public static int tand(int ang)
ang
 the fixedpoint angle in degrees.
ang
.public static int atand2(int y, int x)
y/x
in degrees
for the point (x,y)
with consideration to all 4 quadrants.
This function is analogous to the standard atan2(y,x)
function, except that it returns an angle in degrees.
If x
and y
are both zero, the result
is undefined and a division by zero will occur.
y
 fixedpoint numerator for arctangent.x
 fixedpoint denominator for arctangent.
y/x
. Will be
between 180.0 and 180.0 degrees.public static int round(int n)
n
 Fixedpoint value to round.
public static int Sin(int f)
public static int Cos(int f)
public static int Tan(int f)
public static int ArcTan(int f)
public static int parseFixed32(String value) throws NumberFormatException
NumberFormatException
public static int divtoInt(int n, int m)
n
 the fixedpoint dividend.m
 the fixedpoint divisor.
n
divided by m
.


PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 
Copyright 19992011 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved.
Java is a trademark of Oracle America Inc. in the US and other countries.
Legal