4477961: java.lang.Math.toDegrees(double) could be optimized

Change toDegrees() and toRadians() to multiplication by a compile-time constant.

Reviewed-by: mduigou, shade
This commit is contained in:
Brian Burkhalter 2014-09-22 16:59:23 -07:00
parent 42e13628a3
commit a5187396f5
2 changed files with 29 additions and 4 deletions

View File

@ -122,6 +122,18 @@ public final class Math {
*/
public static final double PI = 3.14159265358979323846;
/**
* Constant by which to multiply an angular value in degrees to obtain an
* angular value in radians.
*/
private static final double DEGREES_TO_RADIANS = 0.017453292519943295;
/**
* Constant by which to multiply an angular value in radians to obtain an
* angular value in degrees.
*/
private static final double RADIANS_TO_DEGREES = 57.29577951308232;
/**
* Returns the trigonometric sine of an angle. Special cases:
* <ul><li>If the argument is NaN or an infinity, then the
@ -233,7 +245,7 @@ public final class Math {
* @since 1.2
*/
public static double toRadians(double angdeg) {
return angdeg / 180.0 * PI;
return angdeg * DEGREES_TO_RADIANS;
}
/**
@ -249,7 +261,7 @@ public final class Math {
* @since 1.2
*/
public static double toDegrees(double angrad) {
return angrad * 180.0 / PI;
return angrad * RADIANS_TO_DEGREES;
}
/**

View File

@ -97,6 +97,19 @@ public final class StrictMath {
*/
public static final double PI = 3.14159265358979323846;
/**
* Constant by which to multiply an angular value in degrees to obtain an
* angular value in radians.
*/
private static final double DEGREES_TO_RADIANS = 0.017453292519943295;
/**
* Constant by which to multiply an angular value in radians to obtain an
* angular value in degrees.
*/
private static final double RADIANS_TO_DEGREES = 57.29577951308232;
/**
* Returns the trigonometric sine of an angle. Special cases:
* <ul><li>If the argument is NaN or an infinity, then the
@ -179,7 +192,7 @@ public final class StrictMath {
public static strictfp double toRadians(double angdeg) {
// Do not delegate to Math.toRadians(angdeg) because
// this method has the strictfp modifier.
return angdeg / 180.0 * PI;
return angdeg * DEGREES_TO_RADIANS;
}
/**
@ -196,7 +209,7 @@ public final class StrictMath {
public static strictfp double toDegrees(double angrad) {
// Do not delegate to Math.toDegrees(angrad) because
// this method has the strictfp modifier.
return angrad * 180.0 / PI;
return angrad * RADIANS_TO_DEGREES;
}
/**