FileDocCategorySizeDatePackage
MathSupport.javaAPI DocphoneME MR2 API (J2ME)12362Wed May 02 18:00:34 BST 2007com.sun.perseus.platform

MathSupport

public final class MathSupport extends Object
This class is used to provide URL resolution.
version
$Id: MathSupport.java,v 1.5 2006/04/21 06:34:50 st125089 Exp $

Fields Summary
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.
Constructors Summary
Methods Summary
public static intabs(int a)
Returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

param
a the argument whose absolute value is to be determined
return
the absolute value of the argument.
see
java.lang.Integer#MIN_VALUE

        return (a < 0) ? -a : a;
    
public static floatabs(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. Special cases:
  • If the argument is positive zero or negative zero, the result is positive zero.
  • If the argument is infinite, the result is positive infinity.
  • If the argument is NaN, the result is NaN.
In other words, the result is the same as the value of the expression:

Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))

param
a the argument whose absolute value is to be determined
return
the absolute value of the argument.

        return (a <= 0.0F) ? 0.0F - a : a;
    
public static floatatan(float a)
Returns the arc tangent of an angle, in the range of -pi/2 through pi/2. Special cases:
  • If the argument is NaN, then the result is NaN.
  • If the argument is zero, then the result is a zero with the same sign as the argument.

A result must be within 1 ulp of the correctly rounded result. Results must be semi-monotonic.

param
a the value whose arc tangent is to be returned.
return
the arc tangent of the argument.

	//atan(x) = x/(1+ 0.28*x^2) (|x|<=1)
	//atan(x) = pi/2 - x/(x^2 + 0.28) (|x| >=1)

	if (MathSupport.abs(a) <= 1.0f) {
	    return (a /(1 + 0.28f*(a*a)));
	} else {
	    float retval = (((float)Math.PI)/2.0f) - (a/((a*a) + 0.28f)); 
	    if (a < (-1.0f)){
		return (retval - (float)Math.PI);
	    } else {
		//if a > 1.0f
		return retval;
	    }
	}
    
public static floatatan2(float y, float x)
Converts rectangular coordinates (xy) to polar (r, theta). This method computes the phase theta by computing an arc tangent of y/x in the range of -pi to pi. Special cases:
  • If either argument is NaN, then the result is NaN.
  • If the first argument is positive zero and the second argument is positive, or the first argument is positive and finite and the second argument is positive infinity, then the result is positive zero.
  • If the first argument is negative zero and the second argument is positive, or the first argument is negative and finite and the second argument is positive infinity, then the result is negative zero.
  • If the first argument is positive zero and the second argument is negative, or the first argument is positive and finite and the second argument is negative infinity, then the result is the float value closest to pi.
  • If the first argument is negative zero and the second argument is negative, or the first argument is negative and finite and the second argument is negative infinity, then the result is the float value closest to -pi.
  • If the first argument is positive and the second argument is positive zero or negative zero, or the first argument is positive infinity and the second argument is finite, then the result is the float value closest to pi/2.
  • If the first argument is negative and the second argument is positive zero or negative zero, or the first argument is negative infinity and the second argument is finite, then the result is the float value closest to -pi/2.
  • If both arguments are positive infinity, then the result is the float value closest to pi/4.
  • If the first argument is positive infinity and the second argument is negative infinity, then the result is the float value closest to 3*pi/4.
  • If the first argument is negative infinity and the second argument is positive infinity, then the result is the float value closest to -pi/4.
  • If both arguments are negative infinity, then the result is the float value closest to -3*pi/4.

A result must be within 2 ulps of the correctly rounded result. Results must be semi-monotonic.

param
y the ordinate coordinate
param
x the abscissa coordinate
return
the theta component of the point (rtheta) in polar coordinates that corresponds to the point (xy) in Cartesian coordinates.

	// if x=y=0
	if((y == 0.0f) && (x == 0.0f)) {
	    return 0.0f;
	}

	// if x>0 atan(y/x)
	if(x > 0.0f) {
	    return atan(y/x);
	}

	// if x<0 sign(y)*(pi - atan(|y/x|))
	if(x < 0.0f) {
	    if(y < 0.0f) {
		return (float)(-(Math.PI - MathSupport.atan(y/x)));
	    } else {
		return (float)(Math.PI - MathSupport.atan(-y/x));
	    }
	}

	// if x=0 y!=0 sign(y)*pi/2
	if(y<0.0f) {
	    return (float)(-(Math.PI/2.0f));
	} else {
	    return (float)(Math.PI/2.0f);
	}

    
public static floatcos(float a)
Returns the trigonometric cosine of an angle. Special cases:
  • If the argument is NaN or an infinity, then the result is NaN.

A result must be within 1 ulp of the correctly rounded result. Results must be semi-monotonic.

param
a an angle, in radians.
return
the cosine of the argument.


                                                                   
         
        return (float) Math.cos(a);
    
public static intround(float a)
Returns the closest int to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type int. In other words, the result is equal to the value of the expression:

(int)Math.floor(a + 0.5f)

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.

param
a a floating-point value to be rounded to an integer.
return
the value of the argument rounded to the nearest int value.
see
java.lang.Integer#MAX_VALUE
see
java.lang.Integer#MIN_VALUE

        return (int) Math.floor(a + 0.5f);
    
public static floatsin(float a)
Returns the trigonometric sine of an angle. Special cases:
  • If the argument is NaN or an infinity, then the result is NaN.
  • If the argument is zero, then the result is a zero with the same sign as the argument.

A result must be within 1 ulp of the correctly rounded result. Results must be semi-monotonic.

param
a an angle, in radians.
return
the sine of the argument.

        return (float) Math.sin(a);
    
public static floatsqrt(float a)
Returns the correctly rounded positive square root of a float value. Special cases:
  • If the argument is NaN or less than zero, then the result is NaN.
  • If the argument is positive infinity, then the result is positive infinity.
  • If the argument is positive zero or negative zero, then the result is the same as the argument.
Otherwise, the result is the float value closest to the true mathematical square root of the argument value.

param
a a value.
return
the positive square root of a. If the argument is NaN or less than zero, the result is NaN.

        return (float) Math.sqrt(a);
    
public static floattan(float a)
Returns the trigonometric tangent of an angle. Special cases:
  • If the argument is NaN or an infinity, then the result is NaN.
  • If the argument is zero, then the result is a zero with the same sign as the argument.

A result must be within 1 ulp of the correctly rounded result. Results must be semi-monotonic.

param
a an angle, in radians.
return
the tangent of the argument.

        return (float) Math.tan(a);
    
public static floattoDegrees(float angrad)
Converts an angle measured in radians to an approximately equivalent angle measured in degrees. The conversion from radians to degrees is generally inexact; users should not expect cos(toRadians(90.0)) to exactly equal 0.0.

param
angrad an angle, in radians
return
the measurement of the angle angrad in degrees.
since
1.2

        return angrad * 180.0f / PI;
    
public static floattoRadians(float angdeg)
Converts an angle measured in degrees to an approximately equivalent angle measured in radians. The conversion from degrees to radians is generally inexact.

param
angdeg an angle, in degrees
return
the measurement of the angle angdeg in radians.
since
1.2

        return angdeg / 180.0f * PI;