## src/java.base/share/classes/java/lang/Math.java

Print this page
rev 10702 : 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

*** 121,130 ****
--- 121,142 ----
* diameter.
*/
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.
* <li>If the argument is zero, then the result is a zero with the
* same sign as the argument.</ul>

*** 231,241 ****
* @return the measurement of the angle {@code angdeg}
* in radians.
* @since 1.2
*/
public static double toRadians(double angdeg) {
! return angdeg / 180.0 * PI;
}
/**
* Converts an angle measured in radians to an approximately
* equivalent angle measured in degrees. The conversion from
--- 243,253 ----
* @return the measurement of the angle {@code angdeg}
* in radians.
* @since 1.2
*/
public static double toRadians(double angdeg) {
! return angdeg * DEGREES_TO_RADIANS;
}
/**
* Converts an angle measured in radians to an approximately
* equivalent angle measured in degrees. The conversion from

*** 247,257 ****
* @return the measurement of the angle {@code angrad}
* in degrees.
* @since 1.2
*/
public static double toDegrees(double angrad) {
! return angrad * 180.0 / PI;
}
/**
* Returns Euler's number <i>e</i> raised to the power of a
* {@code double} value. Special cases:
--- 259,269 ----
* @return the measurement of the angle {@code angrad}
* in degrees.
* @since 1.2
*/
public static double toDegrees(double angrad) {
! return angrad * RADIANS_TO_DEGREES;
}
/**
* Returns Euler's number <i>e</i> raised to the power of a
* {@code double} value. Special cases: