Math
class,
* but are not in in J2ME's.
* * The following methods are still missing from the implementation: *
public static double exp (double a)
* public static double log (double a)
* public static double pow (double a, double b)
* public static double random ()
* public static double rint()
* atan
calculation.
*/
private static final double ATAN_CONSTANT = 1.732050807569;
/**
* Returns the arc cosine of an angle, in the range of 0.0 through Math.PI
.
* Special case:
* NaN
or its absolute value is greater than 1,
* then the result is NaN
.
* -Math.PI/2
through
* Math.PI/2
. Special cases:
* NaN
or its absolute value is greater than 1,
* then the result is NaN
.
* -Math.PI/2
* through Math.PI/2
. Special cases:
* NaN
, then the result is NaN
.
* * 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. */ public static double atan (double a) { // Special cases. if ( Double.isNaN(a) ) { return Double.NaN; } if ( a == 0.0 ) { return a; } // Compute the arc tangent. boolean negative = false; boolean greaterThanOne = false; int i = 0; if ( a < 0.0 ) { a = -a; negative = true; } if ( a > 1.0 ) { a = 1.0 / a; greaterThanOne = true; } double t; for ( ; a > PIover12; a *= t ) { i++; t = a + ATAN_CONSTANT; t = 1.0 / t; a *= ATAN_CONSTANT; a--; } double aSquared = a * a; double arcTangent = aSquared + 1.4087812; arcTangent = 0.55913709 / arcTangent; arcTangent += 0.60310578999999997; arcTangent -= 0.051604539999999997 * aSquared; arcTangent *= a; for ( ; i > 0; i-- ) { arcTangent += PIover6; } if ( greaterThanOne ) { arcTangent = PIover2 - arcTangent; } if ( negative ) { arcTangent = -arcTangent; } return arcTangent; } /** * Converts rectangular coordinates (x, y) 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: *
NaN
, then the result is NaN
.
* double
value
* closest to pi.
* double
value
* closest to -pi.
* double
value
* closest to pi/2.
* double
value
* closest to -pi/2.
*
* 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 (r, theta) in polar
* coordinates that corresponds to the point (x, y) in Cartesian coordinates.
*/
public static double atan2 (double y, double x)
{
// Special cases.
if ( Double.isNaN(y) || Double.isNaN(x) )
{
return Double.NaN;
}
else if ( Double.isInfinite(y) )
{
if ( y > 0.0 ) // Positive infinity
{
if ( Double.isInfinite(x) )
{
if ( x > 0.0 )
{
return PIover4;
}
else
{
return 3.0 * PIover4;
}
}
else if ( x != 0.0 )
{
return PIover2;
}
}
else // Negative infinity
{
if ( Double.isInfinite(x) )
{
if ( x > 0.0 )
{
return -PIover4;
}
else
{
return -3.0 * PIover4;
}
}
else if ( x != 0.0 )
{
return -PIover2;
}
}
}
else if ( y == 0.0 )
{
if ( x > 0.0 )
{
return y;
}
else if ( x < 0.0 )
{
return Math.PI;
}
}
else if ( Double.isInfinite(x) )
{
if ( x > 0.0 ) // Positive infinity
{
if ( y > 0.0 )
{
return 0.0;
}
else if ( y < 0.0 )
{
return -0.0;
}
}
else // Negative infinity
{
if ( y > 0.0 )
{
return Math.PI;
}
else if ( y < 0.0 )
{
return -Math.PI;
}
}
}
else if ( x == 0.0 )
{
if ( y > 0.0 )
{
return PIover2;
}
else if ( y < 0.0 )
{
return -PIover2;
}
}
// Implementation a simple version ported from a PASCAL implementation:
// http://everything2.com/index.pl?node_id=1008481
double arcTangent;
// Use arctan() avoiding division by zero.
if ( Math.abs(x) > Math.abs(y) )
{
arcTangent = atan(y / x);
}
else
{
arcTangent = atan(x / y); // -PI/4 <= a <= PI/4
if ( arcTangent < 0 )
{
arcTangent = -PIover2 - arcTangent; // a is negative, so we're adding
}
else
{
arcTangent = PIover2 - arcTangent;
}
}
// Adjust result to be from [-PI, PI]
if ( x < 0 )
{
if ( y < 0 )
{
arcTangent = arcTangent - Math.PI;
}
else
{
arcTangent = arcTangent + Math.PI;
}
}
return arcTangent;
}
/**
* 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: *
Integer.MIN_VALUE
, the result is
* equal to the value of Integer.MIN_VALUE
.
* Integer.MAX_VALUE
, the result is
* equal to the value of Integer.MAX_VALUE
.
* int
value.
*/
public static int round (float a)
{
return (int)Math.floor( a + 0.5f );
}
/**
* Returns the closest long
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 long
. In other
* words, the result is equal to the value of the expression:
* *
(long)Math.floor(a + 0.5d)*
* Special cases: *
Long.MIN_VALUE
, the result is
* equal to the value of Long.MIN_VALUE
.
* Long.MAX_VALUE
, the result is
* equal to the value of Long.MAX_VALUE
.
* long
.
* @return the value of the argument rounded to the nearest long
value.
*/
public static long round (double a)
{
return (long)Math.floor( a + 0.5 );
}
}