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
--- a/jdk/src/java.base/share/classes/java/lang/Math.java Thu Sep 18 11:34:14 2014 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/Math.java Mon Sep 22 16:59:23 2014 -0700
@@ -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:
* <ul><li>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;
}
/**
--- a/jdk/src/java.base/share/classes/java/lang/StrictMath.java Thu Sep 18 11:34:14 2014 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/StrictMath.java Mon Sep 22 16:59:23 2014 -0700
@@ -98,6 +98,19 @@
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
* result is NaN.
@@ -179,7 +192,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 +209,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;
}
/**