📄 math.java
字号:
/* java.lang.Math -- common mathematical functions, native allowed Copyright (C) 1998, 2001, 2002 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.lang;import java.util.Random;import gnu.classpath.Configuration;/** * Helper class containing useful mathematical functions and constants. * <P> * * Note that angles are specified in radians. Conversion functions are * provided for your convenience. * * @author Paul Fisher * @author John Keiser * @author Eric Blake <ebb9@email.byu.edu> * @since 1.0 */public final class Math{ /** * Math is non-instantiable */ private Math() { } static { if (Configuration.INIT_LOAD_LIBRARY) { System.loadLibrary("javalang"); } } /** * A random number generator, initialized on first use. */ private static Random rand; /** * The most accurate approximation to the mathematical constant <em>e</em>: * <code>2.718281828459045</code>. Used in natural log and exp. * * @see #log(double) * @see #exp(double) */ public static final double E = 2.718281828459045; /** * The most accurate approximation to the mathematical constant <em>pi</em>: * <code>3.141592653589793</code>. This is the ratio of a circle's diameter * to its circumference. */ public static final double PI = 3.141592653589793; /** * Take the absolute value of the argument. * (Absolute value means make it positive.) * <P> * * Note that the the largest negative value (Integer.MIN_VALUE) cannot * be made positive. In this case, because of the rules of negation in * a computer, MIN_VALUE is what will be returned. * This is a <em>negative</em> value. You have been warned. * * @param i the number to take the absolute value of * @return the absolute value * @see Integer#MIN_VALUE */ public static int abs(int i) { return (i < 0) ? -i : i; } /** * Take the absolute value of the argument. * (Absolute value means make it positive.) * <P> * * Note that the the largest negative value (Long.MIN_VALUE) cannot * be made positive. In this case, because of the rules of negation in * a computer, MIN_VALUE is what will be returned. * This is a <em>negative</em> value. You have been warned. * * @param l the number to take the absolute value of * @return the absolute value * @see Long#MIN_VALUE */ public static long abs(long l) { return (l < 0) ? -l : l; } /** * Take the absolute value of the argument. * (Absolute value means make it positive.) * <P> * * This is equivalent, but faster than, calling * <code>Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))</code>. * * @param f the number to take the absolute value of * @return the absolute value */ public static float abs(float f) { return (f <= 0) ? 0 - f : f; } /** * Take the absolute value of the argument. * (Absolute value means make it positive.) * * This is equivalent, but faster than, calling * <code>Double.longBitsToDouble(Double.doubleToLongBits(a) * << 1) >>> 1);</code>. * * @param d the number to take the absolute value of * @return the absolute value */ public static double abs(double d) { return (d <= 0) ? 0 - d : d; } /** * Return whichever argument is smaller. * * @param a the first number * @param b a second number * @return the smaller of the two numbers */ public static int min(int a, int b) { return (a < b) ? a : b; } /** * Return whichever argument is smaller. * * @param a the first number * @param b a second number * @return the smaller of the two numbers */ public static long min(long a, long b) { return (a < b) ? a : b; } /** * Return whichever argument is smaller. If either argument is NaN, the * result is NaN, and when comparing 0 and -0, -0 is always smaller. * * @param a the first number * @param b a second number * @return the smaller of the two numbers */ public static float min(float a, float b) { // this check for NaN, from JLS 15.21.1, saves a method call if (a != a) return a; // no need to check if b is NaN; < will work correctly // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special if (a == 0 && b == 0) return -(-a - b); return (a < b) ? a : b; } /** * Return whichever argument is smaller. If either argument is NaN, the * result is NaN, and when comparing 0 and -0, -0 is always smaller. * * @param a the first number * @param b a second number * @return the smaller of the two numbers */ public static double min(double a, double b) { // this check for NaN, from JLS 15.21.1, saves a method call if (a != a) return a; // no need to check if b is NaN; < will work correctly // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special if (a == 0 && b == 0) return -(-a - b); return (a < b) ? a : b; } /** * Return whichever argument is larger. * * @param a the first number * @param b a second number * @return the larger of the two numbers */ public static int max(int a, int b) { return (a > b) ? a : b; } /** * Return whichever argument is larger. * * @param a the first number * @param b a second number * @return the larger of the two numbers */ public static long max(long a, long b) { return (a > b) ? a : b; } /** * Return whichever argument is larger. If either argument is NaN, the * result is NaN, and when comparing 0 and -0, 0 is always larger. * * @param a the first number * @param b a second number * @return the larger of the two numbers */ public static float max(float a, float b) { // this check for NaN, from JLS 15.21.1, saves a method call if (a != a) return a; // no need to check if b is NaN; > will work correctly // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special if (a == 0 && b == 0) return a - -b; return (a > b) ? a : b; } /** * Return whichever argument is larger. If either argument is NaN, the * result is NaN, and when comparing 0 and -0, 0 is always larger. * * @param a the first number * @param b a second number * @return the larger of the two numbers */ public static double max(double a, double b) { // this check for NaN, from JLS 15.21.1, saves a method call if (a != a) return a; // no need to check if b is NaN; > will work correctly // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special if (a == 0 && b == 0) return a - -b; return (a > b) ? a : b; } /** * The trigonometric function <em>sin</em>. The sine of NaN or infinity is * NaN, and the sine of 0 retains its sign. This is accurate within 1 ulp, * and is semi-monotonic. * * @param a the angle (in radians) * @return sin(a) */ public native static double sin(double a); /** * The trigonometric function <em>cos</em>. The cosine of NaN or infinity is * NaN. This is accurate within 1 ulp, and is semi-monotonic. * * @param a the angle (in radians) * @return cos(a) */ public native static double cos(double a); /** * The trigonometric function <em>tan</em>. The tangent of NaN or infinity * is NaN, and the tangent of 0 retains its sign. This is accurate within 1 * ulp, and is semi-monotonic. * * @param a the angle (in radians) * @return tan(a) */ public native static double tan(double a); /** * The trigonometric function <em>arcsin</em>. The range of angles returned
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -