src/java.base/share/classes/java/lang/StrictMath.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


  81 
  82     /**
  83      * Don't let anyone instantiate this class.
  84      */
  85     private StrictMath() {}
  86 
  87     /**
  88      * The {@code double} value that is closer than any other to
  89      * <i>e</i>, the base of the natural logarithms.
  90      */
  91     public static final double E = 2.7182818284590452354;
  92 
  93     /**
  94      * The {@code double} value that is closer than any other to
  95      * <i>pi</i>, the ratio of the circumference of a circle to its
  96      * diameter.
  97      */
  98     public static final double PI = 3.14159265358979323846;
  99 
 100     /**












 101      * Returns the trigonometric sine of an angle. Special cases:
 102      * <ul><li>If the argument is NaN or an infinity, then the
 103      * result is NaN.
 104      * <li>If the argument is zero, then the result is a zero with the
 105      * same sign as the argument.</ul>
 106      *
 107      * @param   a   an angle, in radians.
 108      * @return  the sine of the argument.
 109      */
 110     public static native double sin(double a);
 111 
 112     /**
 113      * Returns the trigonometric cosine of an angle. Special cases:
 114      * <ul><li>If the argument is NaN or an infinity, then the
 115      * result is NaN.</ul>
 116      *
 117      * @param   a   an angle, in radians.
 118      * @return  the cosine of the argument.
 119      */
 120     public static native double cos(double a);


 162      * <li>If the argument is zero, then the result is a zero with the
 163      * same sign as the argument.</ul>
 164      *
 165      * @param   a   the value whose arc tangent is to be returned.
 166      * @return  the arc tangent of the argument.
 167      */
 168     public static native double atan(double a);
 169 
 170     /**
 171      * Converts an angle measured in degrees to an approximately
 172      * equivalent angle measured in radians.  The conversion from
 173      * degrees to radians is generally inexact.
 174      *
 175      * @param   angdeg   an angle, in degrees
 176      * @return  the measurement of the angle {@code angdeg}
 177      *          in radians.
 178      */
 179     public static strictfp double toRadians(double angdeg) {
 180         // Do not delegate to Math.toRadians(angdeg) because
 181         // this method has the strictfp modifier.
 182         return angdeg / 180.0 * PI;
 183     }
 184 
 185     /**
 186      * Converts an angle measured in radians to an approximately
 187      * equivalent angle measured in degrees.  The conversion from
 188      * radians to degrees is generally inexact; users should
 189      * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
 190      * equal {@code 0.0}.
 191      *
 192      * @param   angrad   an angle, in radians
 193      * @return  the measurement of the angle {@code angrad}
 194      *          in degrees.
 195      */
 196     public static strictfp double toDegrees(double angrad) {
 197         // Do not delegate to Math.toDegrees(angrad) because
 198         // this method has the strictfp modifier.
 199         return angrad * 180.0 / PI;
 200     }
 201 
 202     /**
 203      * Returns Euler's number <i>e</i> raised to the power of a
 204      * {@code double} value. Special cases:
 205      * <ul><li>If the argument is NaN, the result is NaN.
 206      * <li>If the argument is positive infinity, then the result is
 207      * positive infinity.
 208      * <li>If the argument is negative infinity, then the result is
 209      * positive zero.</ul>
 210      *
 211      * @param   a   the exponent to raise <i>e</i> to.
 212      * @return  the value <i>e</i><sup>{@code a}</sup>,
 213      *          where <i>e</i> is the base of the natural logarithms.
 214      */
 215     public static native double exp(double a);
 216 
 217     /**
 218      * Returns the natural logarithm (base <i>e</i>) of a {@code double}
 219      * value. Special cases:




  81 
  82     /**
  83      * Don't let anyone instantiate this class.
  84      */
  85     private StrictMath() {}
  86 
  87     /**
  88      * The {@code double} value that is closer than any other to
  89      * <i>e</i>, the base of the natural logarithms.
  90      */
  91     public static final double E = 2.7182818284590452354;
  92 
  93     /**
  94      * The {@code double} value that is closer than any other to
  95      * <i>pi</i>, the ratio of the circumference of a circle to its
  96      * diameter.
  97      */
  98     public static final double PI = 3.14159265358979323846;
  99 
 100     /**
 101      * Constant by which to multiply an angular value in degrees to obtain an
 102      * angular value in radians.
 103      */
 104     private static final double DEGREES_TO_RADIANS = 0.017453292519943295;
 105 
 106     /**
 107      * Constant by which to multiply an angular value in radians to obtain an
 108      * angular value in degrees.
 109      */
 110 
 111     private static final double RADIANS_TO_DEGREES = 57.29577951308232;
 112     /**
 113      * Returns the trigonometric sine of an angle. Special cases:
 114      * <ul><li>If the argument is NaN or an infinity, then the
 115      * result is NaN.
 116      * <li>If the argument is zero, then the result is a zero with the
 117      * same sign as the argument.</ul>
 118      *
 119      * @param   a   an angle, in radians.
 120      * @return  the sine of the argument.
 121      */
 122     public static native double sin(double a);
 123 
 124     /**
 125      * Returns the trigonometric cosine of an angle. Special cases:
 126      * <ul><li>If the argument is NaN or an infinity, then the
 127      * result is NaN.</ul>
 128      *
 129      * @param   a   an angle, in radians.
 130      * @return  the cosine of the argument.
 131      */
 132     public static native double cos(double a);


 174      * <li>If the argument is zero, then the result is a zero with the
 175      * same sign as the argument.</ul>
 176      *
 177      * @param   a   the value whose arc tangent is to be returned.
 178      * @return  the arc tangent of the argument.
 179      */
 180     public static native double atan(double a);
 181 
 182     /**
 183      * Converts an angle measured in degrees to an approximately
 184      * equivalent angle measured in radians.  The conversion from
 185      * degrees to radians is generally inexact.
 186      *
 187      * @param   angdeg   an angle, in degrees
 188      * @return  the measurement of the angle {@code angdeg}
 189      *          in radians.
 190      */
 191     public static strictfp double toRadians(double angdeg) {
 192         // Do not delegate to Math.toRadians(angdeg) because
 193         // this method has the strictfp modifier.
 194         return angdeg * DEGREES_TO_RADIANS;
 195     }
 196 
 197     /**
 198      * Converts an angle measured in radians to an approximately
 199      * equivalent angle measured in degrees.  The conversion from
 200      * radians to degrees is generally inexact; users should
 201      * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
 202      * equal {@code 0.0}.
 203      *
 204      * @param   angrad   an angle, in radians
 205      * @return  the measurement of the angle {@code angrad}
 206      *          in degrees.
 207      */
 208     public static strictfp double toDegrees(double angrad) {
 209         // Do not delegate to Math.toDegrees(angrad) because
 210         // this method has the strictfp modifier.
 211         return angrad * RADIANS_TO_DEGREES;
 212     }
 213 
 214     /**
 215      * Returns Euler's number <i>e</i> raised to the power of a
 216      * {@code double} value. Special cases:
 217      * <ul><li>If the argument is NaN, the result is NaN.
 218      * <li>If the argument is positive infinity, then the result is
 219      * positive infinity.
 220      * <li>If the argument is negative infinity, then the result is
 221      * positive zero.</ul>
 222      *
 223      * @param   a   the exponent to raise <i>e</i> to.
 224      * @return  the value <i>e</i><sup>{@code a}</sup>,
 225      *          where <i>e</i> is the base of the natural logarithms.
 226      */
 227     public static native double exp(double a);
 228 
 229     /**
 230      * Returns the natural logarithm (base <i>e</i>) of a {@code double}
 231      * value. Special cases: