# HG changeset patch # User bpb # Date 1411424137 25200 # Mon Sep 22 15:15:37 2014 -0700 # Node ID ba0874b62d54136b2d9566dbd91045b17b20f059 # Parent d245c240f0d7cafe4978605ef3a52c35b9e95d85 4477961: java.lang.Math.toDegrees(double) could be optimized Summary: Change toDegrees() and toRadians() to multiplication by a compile-time constant. Reviewed-by: mduigou, shade diff --git a/src/java.base/share/classes/java/lang/Math.java b/src/java.base/share/classes/java/lang/Math.java --- a/src/java.base/share/classes/java/lang/Math.java +++ b/src/java.base/share/classes/java/lang/Math.java @@ -123,6 +123,18 @@ 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: *
• If the argument is NaN or an infinity, then the * result is NaN. @@ -233,7 +245,7 @@ * @since 1.2 */ public static double toRadians(double angdeg) { - return angdeg / 180.0 * PI; + return angdeg * DEGREES_TO_RADIANS; } /** @@ -249,7 +261,7 @@ * @since 1.2 */ public static double toDegrees(double angrad) { - return angrad * 180.0 / PI; + return angrad * RADIANS_TO_DEGREES; } /** diff --git a/src/java.base/share/classes/java/lang/StrictMath.java b/src/java.base/share/classes/java/lang/StrictMath.java --- a/src/java.base/share/classes/java/lang/StrictMath.java +++ b/src/java.base/share/classes/java/lang/StrictMath.java @@ -98,6 +98,18 @@ 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: *
• If the argument is NaN or an infinity, then the * result is NaN. @@ -179,7 +191,7 @@ 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 +208,7 @@ 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; } /**