extra.util
Class Maths

java.lang.Object
  |
  +--extra.util.Maths

public class Maths
extends Object

The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. One big change is that (in general) all methods use and return floats instead of doubles!

Math provides approximately 5 decimal place accuracy versions of the methods provided by java.math.Math. The approximations are based on those published in "Approximations for Digital Computers" by Cecil Hastings, Jr., Princeton University Press, 1955.

I chose not to use the implementations noted below from netlib (not at netlib.att.com!) as they provide more accuracy (and would be slower) than seemed needed for a hand-held device. I have left in the reference for completeness. I realized after a review of Float that one can get/set the bits for a floating value using intBitsToFloat and floatToIntBits. Therefore, it would be possible to implement the fdlibm versions of the algorithms where more accuracy and robustness is needed.

To help ensure portability of Java programs, the definitions of many of the numeric functions in this package require that they produce the same results as certain published algorithms. These algorithms are available from the well-known network library netlib as the package "Freely Distributable Math Library" (fdlibm). These algorithms, which are written in the C programming language, are then to be understood as executed with all floating-point operations following the rules of Java floating-point arithmetic.

The network library may be found on the World Wide Web at

then perform a keyword search for "fdlibm".

The Java math library is defined with respect to the version of fdlibm dated January 4, 1995. Where fdlibm provides more than one definition for a function (such as acos), use the "IEEE 754 core function" version (residing in a file whose name begins with the letter e).


Field Summary
static float E
          The double value that is closer than any other to e, the base of the natural logarithms.
static float 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 float abs(float a)
          Returns the absolute value of a float value.
static int abs(int a)
          Returns the absolute value of a int value.
static float acos(float x)
          Returns the arc cosine of an angle, in the range of 0.0 through pi.
static float asin(float x)
          Returns the arc sine of an angle, in the range of -pi/2 through pi/2.
static float atan(float a)
          Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.
static float atan2(float y, float x)
          Converts rectangular coordinates (ba) to polar (r, theta).
static float cos(float radians)
          Returns the trigonometric cosine of an angle.
static float exp(float x)
          Returns the exponential number e (i.e., 2.718...) raised to the power of a double value.
static float log(float x)
          Returns the natural logarithm (base e) of a float value.
static void main(String[] args)
           
static float max(float a, float b)
          Returns the maximum of the two given float values.
static int max(int a, int b)
          Returns the maximum of the two given int values.
static float min(float a, float b)
          Returns the minimum of the two given float values.
static int min(int a, int b)
          Returns the minimum of the two given int values.
static float pow(float a, float b)
          Returns of value of the first argument raised to the power of the second argument.
static int round(float x)
          Rounds the given float to the nearest whole number.
static float sin(float radians)
          Returns the trigonometric sine of an angle.
static float sqrt(float a)
          Returns the square root of a float value.
static float tan(float x)
          Returns the trigonometric tangent of an angle.
 
Methods inherited from class java.lang.Object
equals, hashCode, toString
 

Field Detail

E

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

PI

public static final float PI
The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.
Since:
JDK1.0
Method Detail

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.
Parameters:
a - a float value.
Returns:
the absolute value of the argument.

abs

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

round

public static int round(float x)
Rounds the given float to the nearest whole number.
Parameters:
x - the number to round

min

public static int min(int a,
                      int b)
Returns the minimum of the two given int values.

max

public static int max(int a,
                      int b)
Returns the maximum of the two given int values.

min

public static float min(float a,
                        float b)
Returns the minimum of the two given float values.

max

public static float max(float a,
                        float b)
Returns the maximum of the two given float values.

asin

public static float asin(float x)
Returns the arc sine of an angle, in the range of -pi/2 through pi/2.
Parameters:
a - an angle, in radians.
Returns:
the arc sine of the argument.

acos

public static float acos(float x)
Returns the arc cosine of an angle, in the range of 0.0 through pi.
Parameters:
a - an angle, in radians.
Returns:
the arc cosine of the argument.

tan

public static float tan(float x)
Returns the trigonometric tangent of an angle.
Parameters:
a - an angle, in radians.
Returns:
the tangent of the argument.

atan

public static float atan(float a)
Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.
Parameters:
a - an angle, in radians.
Returns:
the arc tangent of the argument.

atan2

public static float atan2(float y,
                          float x)
Converts rectangular coordinates (ba) to polar (r, theta). This method computes the phase theta by computing an arc tangent of b/a in the range of -pi to pi.
Parameters:
a - a float value.
b - a float value.
Returns:
the theta component of the point (rtheta) in polar coordinates that corresponds to the point (ba) in Cartesian coordinates.

sqrt

public static float sqrt(float a)
Returns the square root of a float value.
Parameters:
a - a float value.
Returns:
the square root of a. If the argument is NaN or less than zero, the result is NaN.
Since:
JDK1.0

exp

public static float exp(float x)
Returns the exponential number e (i.e., 2.718...) raised to the power of a double value.
Parameters:
a - a double value.
Returns:
the value ea, where e is the base of the natural logarithms.

pow

public static float pow(float a,
                        float b)
Returns of value of the first argument raised to the power of the second argument.

Parameters:
a - a float value.
b - a float value.
Returns:
the value ab.

log

public static float log(float x)
Returns the natural logarithm (base e) of a float value.
Parameters:
a - a number greater than 0.0.
Returns:
the value ln a, the natural logarithm of a.

sin

public static float sin(float radians)
Returns the trigonometric sine of an angle.
Parameters:
a - an angle, in radians.
Returns:
the sine of the argument.

cos

public static float cos(float radians)
Returns the trigonometric cosine of an angle.
Parameters:
a - an angle, in radians.
Returns:
the cosine of the argument.

main

public static void main(String[] args)