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


 106 
 107     /**
 108      * Don't let anyone instantiate this class.
 109      */
 110     private Math() {}
 111 
 112     /**
 113      * The {@code double} value that is closer than any other to
 114      * <i>e</i>, the base of the natural logarithms.
 115      */
 116     public static final double E = 2.7182818284590452354;
 117 
 118     /**
 119      * The {@code double} value that is closer than any other to
 120      * <i>pi</i>, the ratio of the circumference of a circle to its
 121      * diameter.
 122      */
 123     public static final double PI = 3.14159265358979323846;
 124 
 125     /**












 126      * Returns the trigonometric sine of an angle.  Special cases:
 127      * <ul><li>If the argument is NaN or an infinity, then the
 128      * result is NaN.
 129      * <li>If the argument is zero, then the result is a zero with the
 130      * same sign as the argument.</ul>
 131      *
 132      * <p>The computed result must be within 1 ulp of the exact result.
 133      * Results must be semi-monotonic.
 134      *
 135      * @param   a   an angle, in radians.
 136      * @return  the sine of the argument.
 137      */
 138     public static double sin(double a) {
 139         return StrictMath.sin(a); // default impl. delegates to StrictMath
 140     }
 141 
 142     /**
 143      * Returns the trigonometric cosine of an angle. Special cases:
 144      * <ul><li>If the argument is NaN or an infinity, then the
 145      * result is NaN.</ul>


 216      * Results must be semi-monotonic.
 217      *
 218      * @param   a   the value whose arc tangent is to be returned.
 219      * @return  the arc tangent of the argument.
 220      */
 221     public static double atan(double a) {
 222         return StrictMath.atan(a); // default impl. delegates to StrictMath
 223     }
 224 
 225     /**
 226      * Converts an angle measured in degrees to an approximately
 227      * equivalent angle measured in radians.  The conversion from
 228      * degrees to radians is generally inexact.
 229      *
 230      * @param   angdeg   an angle, in degrees
 231      * @return  the measurement of the angle {@code angdeg}
 232      *          in radians.
 233      * @since   1.2
 234      */
 235     public static double toRadians(double angdeg) {
 236         return angdeg / 180.0 * PI;
 237     }
 238 
 239     /**
 240      * Converts an angle measured in radians to an approximately
 241      * equivalent angle measured in degrees.  The conversion from
 242      * radians to degrees is generally inexact; users should
 243      * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
 244      * equal {@code 0.0}.
 245      *
 246      * @param   angrad   an angle, in radians
 247      * @return  the measurement of the angle {@code angrad}
 248      *          in degrees.
 249      * @since   1.2
 250      */
 251     public static double toDegrees(double angrad) {
 252         return angrad * 180.0 / PI;
 253     }
 254 
 255     /**
 256      * Returns Euler's number <i>e</i> raised to the power of a
 257      * {@code double} value.  Special cases:
 258      * <ul><li>If the argument is NaN, the result is NaN.
 259      * <li>If the argument is positive infinity, then the result is
 260      * positive infinity.
 261      * <li>If the argument is negative infinity, then the result is
 262      * positive zero.</ul>
 263      *
 264      * <p>The computed result must be within 1 ulp of the exact result.
 265      * Results must be semi-monotonic.
 266      *
 267      * @param   a   the exponent to raise <i>e</i> to.
 268      * @return  the value <i>e</i><sup>{@code a}</sup>,
 269      *          where <i>e</i> is the base of the natural logarithms.
 270      */
 271     public static double exp(double a) {
 272         return StrictMath.exp(a); // default impl. delegates to StrictMath




 106 
 107     /**
 108      * Don't let anyone instantiate this class.
 109      */
 110     private Math() {}
 111 
 112     /**
 113      * The {@code double} value that is closer than any other to
 114      * <i>e</i>, the base of the natural logarithms.
 115      */
 116     public static final double E = 2.7182818284590452354;
 117 
 118     /**
 119      * The {@code double} value that is closer than any other to
 120      * <i>pi</i>, the ratio of the circumference of a circle to its
 121      * diameter.
 122      */
 123     public static final double PI = 3.14159265358979323846;
 124 
 125     /**
 126      * Constant by which to multiply an angular value in degrees to obtain an
 127      * angular value in radians.
 128      */
 129     private static final double DEGREES_TO_RADIANS = 0.017453292519943295;
 130 
 131     /**
 132      * Constant by which to multiply an angular value in radians to obtain an
 133      * angular value in degrees.
 134      */
 135     private static final double RADIANS_TO_DEGREES = 57.29577951308232;
 136 
 137     /**
 138      * Returns the trigonometric sine of an angle.  Special cases:
 139      * <ul><li>If the argument is NaN or an infinity, then the
 140      * result is NaN.
 141      * <li>If the argument is zero, then the result is a zero with the
 142      * same sign as the argument.</ul>
 143      *
 144      * <p>The computed result must be within 1 ulp of the exact result.
 145      * Results must be semi-monotonic.
 146      *
 147      * @param   a   an angle, in radians.
 148      * @return  the sine of the argument.
 149      */
 150     public static double sin(double a) {
 151         return StrictMath.sin(a); // default impl. delegates to StrictMath
 152     }
 153 
 154     /**
 155      * Returns the trigonometric cosine of an angle. Special cases:
 156      * <ul><li>If the argument is NaN or an infinity, then the
 157      * result is NaN.</ul>


 228      * Results must be semi-monotonic.
 229      *
 230      * @param   a   the value whose arc tangent is to be returned.
 231      * @return  the arc tangent of the argument.
 232      */
 233     public static double atan(double a) {
 234         return StrictMath.atan(a); // default impl. delegates to StrictMath
 235     }
 236 
 237     /**
 238      * Converts an angle measured in degrees to an approximately
 239      * equivalent angle measured in radians.  The conversion from
 240      * degrees to radians is generally inexact.
 241      *
 242      * @param   angdeg   an angle, in degrees
 243      * @return  the measurement of the angle {@code angdeg}
 244      *          in radians.
 245      * @since   1.2
 246      */
 247     public static double toRadians(double angdeg) {
 248         return angdeg * DEGREES_TO_RADIANS;
 249     }
 250 
 251     /**
 252      * Converts an angle measured in radians to an approximately
 253      * equivalent angle measured in degrees.  The conversion from
 254      * radians to degrees is generally inexact; users should
 255      * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
 256      * equal {@code 0.0}.
 257      *
 258      * @param   angrad   an angle, in radians
 259      * @return  the measurement of the angle {@code angrad}
 260      *          in degrees.
 261      * @since   1.2
 262      */
 263     public static double toDegrees(double angrad) {
 264         return angrad * RADIANS_TO_DEGREES;
 265     }
 266 
 267     /**
 268      * Returns Euler's number <i>e</i> raised to the power of a
 269      * {@code double} value.  Special cases:
 270      * <ul><li>If the argument is NaN, the result is NaN.
 271      * <li>If the argument is positive infinity, then the result is
 272      * positive infinity.
 273      * <li>If the argument is negative infinity, then the result is
 274      * positive zero.</ul>
 275      *
 276      * <p>The computed result must be within 1 ulp of the exact result.
 277      * Results must be semi-monotonic.
 278      *
 279      * @param   a   the exponent to raise <i>e</i> to.
 280      * @return  the value <i>e</i><sup>{@code a}</sup>,
 281      *          where <i>e</i> is the base of the natural logarithms.
 282      */
 283     public static double exp(double a) {
 284         return StrictMath.exp(a); // default impl. delegates to StrictMath