math.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 889 行 · 第 1/3 页

JAVA
889
字号
/* * @(#)Math.java	1.57 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License version   * 2 only, as published by the Free Software Foundation.    *    * This program is distributed in the hope that it will be useful, but   * WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * General Public License version 2 for more details (a copy is   * included at /legal/license.txt).    *    * You should have received a copy of the GNU General Public License   * version 2 along with this work; if not, write to the Free Software   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA   * 02110-1301 USA    *    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * */package java.lang;import java.util.Random;/** * The class <code>Math</code> contains methods for performing basic * numeric operations such as the elementary exponential, logarithm, * square root, and trigonometric functions. * <p> * Unlike some of the numeric methods of class * <code>StrictMath</code>, all implementations of the equivalent * functions of class <code>Math</code> are not defined to return the * bit-for-bit same results.  This relaxation permits * better-performing implementations where strict reproducibility is * not required. * <p> * By default many of the <code>Math</code> methods simply call * the equivalent method in <code>StrictMath</code> for their * implementation.  Code generators are encouraged to use * platform-specific native libraries or microprocessor instructions, * where available, to provide higher-performance implementations of * <code>Math</code> methods.  Such higher-performance * implementations still must conform to the specification for * <code>Math</code>. * <p> * The quality of implementation specifications concern two * properties, accuracy of the returned result and monotonicity of the * method.  Accuracy of the floating-point <code>Math</code> methods * is measured in terms of <i>ulps</i>, units in the last place.  For * a given floating-point format, an ulp of a specific real number * value is the difference between the two floating-point values * closest to that numerical value.  When discussing the accuracy of a * method as a whole rather than at a specific argument, the number of * ulps cited is for the worst-case error at any argument.  If a * method always has an error less than 0.5 ulps, the method always * returns the floating-point number nearest the exact result; such a * method is <i>correctly rounded</i>.  A correctly rounded method is * generally the best a floating-point approximation can be; however, * it is impractical for many floating-point methods to be correctly * rounded.  Instead, for the <code>Math</code> class, a larger error * bound of 1 or 2 ulps is allowed for certain methods.  Informally, * with a 1 ulp error bound, when the exact result is a representable * number the exact result should be returned; otherwise, either of * the two floating-point numbers closest to the exact result may be * returned.  Besides accuracy at individual arguments, maintaining * proper relations between the method at different arguments is also * important.  Therefore, methods with more than 0.5 ulp errors are * required to be <i>semi-monotonic</i>: whenever the mathematical * function is non-decreasing, so is the floating-point approximation, * likewise, whenever the mathematical function is non-increasing, so * is the floating-point approximation.  Not all approximations that * have 1 ulp accuracy will automatically meet the monotonicity * requirements. *  * @author  unascribed * @version 1.50, 02/02/00 * @since   JDK1.0 */public final strictfp class Math {    /**     * Don't let anyone instantiate this class.     */    private Math() {}    /**     * The <code>double</code> value that is closer than any other to     * <i>e</i>, the base of the natural logarithms.     */    public static final double E = 2.7182818284590452354;    /**     * The <code>double</code> value that is closer than any other to     * <i>pi</i>, the ratio of the circumference of a circle to its     * diameter.     */    public static final double PI = 3.14159265358979323846;    /**     * 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>     * <p>     * A result must be within 1 ulp of the correctly rounded result.  Results     * must be semi-monotonic.     *     * @param   a   an angle, in radians.     * @return  the sine of the argument.     */    public static double sin(double a) {	return StrictMath.sin(a); // default impl. delegates to StrictMath    }        /**     * Returns the trigonometric cosine of an angle. Special cases:     * <ul><li>If the argument is NaN or an infinity, then the      * result is NaN.</ul>     * <p>     * A result must be within 1 ulp of the correctly rounded result.  Results     * must be semi-monotonic.     *     * @param   a   an angle, in radians.     * @return  the cosine of the argument.     */    public static double cos(double a) {	return StrictMath.cos(a); // default impl. delegates to StrictMath    }       /**     * Returns the trigonometric tangent 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>     * <p>     * A result must be within 1 ulp of the correctly rounded result.  Results     * must be semi-monotonic.     *     * @param   a   an angle, in radians.     * @return  the tangent of the argument.     */    public static double tan(double a) {	return StrictMath.tan(a); // default impl. delegates to StrictMath    }    /**     * Returns the arc sine of an angle, in the range of -<i>pi</i>/2 through     * <i>pi</i>/2. Special cases:      * <ul><li>If the argument is NaN or its absolute value is greater      * than 1, 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>     * <p>     * A result must be within 1 ulp of the correctly rounded result.  Results     * must be semi-monotonic.     *     * @param   a   the value whose arc sine is to be returned.     * @return  the arc sine of the argument.     */    public static double asin(double a) {	return StrictMath.asin(a); // default impl. delegates to StrictMath    }    /**     * Returns the arc cosine of an angle, in the range of 0.0 through     * <i>pi</i>.  Special case:     * <ul><li>If the argument is NaN or its absolute value is greater      * than 1, then the result is NaN.</ul>     * <p>     * A result must be within 1 ulp of the correctly rounded result.  Results      * must be semi-monotonic.     *     * @param   a   the value whose arc cosine is to be returned.     * @return  the arc cosine of the argument.     */    public static double acos(double a) {	return StrictMath.acos(a); // default impl. delegates to StrictMath    }    /**     * Returns the arc tangent of an angle, in the range of -<i>pi</i>/2     * through <i>pi</i>/2.  Special cases:      * <ul><li>If the argument is NaN, 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>     * <p>     * A result must be within 1 ulp of the correctly rounded result.  Results     * must be semi-monotonic.     *     * @param   a   the value whose arc tangent is to be returned.     * @return  the arc tangent of the argument.     */    public static double atan(double a) {	return StrictMath.atan(a); // default impl. delegates to StrictMath    }    /**     * Converts an angle measured in degrees to an approximately     * equivalent angle measured in radians.  The conversion from     * degrees to radians is generally inexact.     *     * @param   angdeg   an angle, in degrees     * @return  the measurement of the angle <code>angdeg</code>     *          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     * radians to degrees is generally inexact; users should     * <i>not</i> expect <code>cos(toRadians(90.0))</code> to exactly     * equal <code>0.0</code>.     *     * @param   angrad   an angle, in radians     * @return  the measurement of the angle <code>angrad</code>     *          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</code> value.  Special cases:     * <ul><li>If the argument is NaN, the result is NaN.     * <li>If the argument is positive infinity, then the result is      * positive infinity.     * <li>If the argument is negative infinity, then the result is      * positive zero.</ul>     * <p>     * A result must be within 1 ulp of the correctly rounded result.  Results     * must be semi-monotonic.     *     * @param   a   the exponent to raise <i>e</i> to.     * @return  the value <i>e</i><sup><code>a</code></sup>,      *          where <i>e</i> is the base of the natural logarithms.     */    public static double exp(double a) {	return StrictMath.exp(a); // default impl. delegates to StrictMath    }    /**     * Returns the natural logarithm (base <i>e</i>) of a <code>double</code>     * value.  Special cases:     * <ul><li>If the argument is NaN or less than zero, then the result      * is NaN.     * <li>If the argument is positive infinity, then the result is      * positive infinity.     * <li>If the argument is positive zero or negative zero, then the      * result is negative infinity.</ul>     * <p>     * A result must be within 1 ulp of the correctly rounded result.  Results     * must be semi-monotonic.     *     * @param   a   a number greater than <code>0.0</code>.     * @return  the value ln&nbsp;<code>a</code>, the natural logarithm of     *          <code>a</code>.     */    public static double log(double a) {	return StrictMath.log(a); // default impl. delegates to StrictMath    }    /**     * Returns the correctly rounded positive square root of a      * <code>double</code> value.     * Special cases:     * <ul><li>If the argument is NaN or less than zero, then the result      * is NaN.      * <li>If the argument is positive infinity, then the result is positive      * infinity.      * <li>If the argument is positive zero or negative zero, then the      * result is the same as the argument.</ul>     * Otherwise, the result is the <code>double</code> value closest to      * the true mathematical square root of the argument value.     *      * @param   a   a value.     * <!--@return  the value of &radic;&nbsp;<code>a</code>.-->     * @return  the positive square root of <code>a</code>.     *          If the argument is NaN or less than zero, the result is NaN.     */    public static double sqrt(double a) {	return StrictMath.sqrt(a); // default impl. delegates to StrictMath

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?