完整名java.lang.Math
java.lang时 Java 环境默认导入的包,所以Math工具类可以直接使用。
Math包含了用于基本数值运算的,例如基本指数、对数、平方根和三角函数。
Math提供给了两个double常量字段:自然对数的底数E和圆周率PI的近似值。
public static final double E = 2.7182818284590452354;
public static final double PI = 3.14159265358979323846;
接下来时Math为基本数值运算提供的工具方法(默认情况下,有关角度的方法均采用弧度制计算,即输入参数或者返回值都被默认看作是弧度值):
-
sin()public static double sin(double a) {}返回角度
a的正弦值。 -
cos()public static double cos(double a) {}返回角度
a的余弦值。 -
tan()public static double tan(double a) {}返回角度
a的正切值。 -
asin()public static double asin(double a) {}返回数值
a的反正弦函数值,返回的角度范围从 -PI/2 到 PI/2。如果a绝对值大于 1 或者为NaN值,那么返回值为NaN值。 -
acos()public static double acos(double a) {}返回数值
a的反余弦函数值,返回的角度范围从 0.0 到 PI。如果a绝对值大于 1 或者为NaN值,那么返回值为NaN值。 -
atan()public static double atan(double a) {}返回数值
a的反正切函数值,返回的角度范围从 -PI/2 到 PI/2。如果a为NaN值,那么返回值也为NaN值。 -
toRadians()public static double toRadians(double angdeg) {}将以度为单位角度值
angdeg转换为以弧度为单位的角度(近似)值。 -
toDegrees()public static double toDegrees(double angrad) {}将以弧度为单位角度值
angrad转换为以度为单位的角度(近似)值。 -
exp()public static double exp(double a) {}计算欧拉数
e的指数函数值,即e的a次幂。 -
log()public static double log(double a) {}计算
a的自然对数(以欧拉数e为底)值。如果a为负数或者NaN,则返回NaN。如果a为正零或负零,则返回NEGATIVE_INFINITY(负无穷)。 -
log10()public static double log10(double a) {}计算
a的以 10 为底的对数值。参数a为非正常范围内的情况时,返回值同log()。 -
sqrt()public static double sqrt(double a) {}计算
a的开平方的正数值。如果a为负数或者Nan,则返回值为NaN。 -
cbrt()public static double cbrt(double a) {}计算
a的立方根值。如果a为Nan,则返回值为NaN。 -
IEEEremainder()public static double IEEEremainder(double f1, double f2) {}根据 IEEE 754 标准计算参数
f1的f2的取余运算值。 -
ceil()public static double ceil(double a) {}返回
a的向上取整值。注意返回值仍然是double类型的。 -
floor()public static double floor(double a) {}返回
a的向下取整值。注意返回值仍然是double类型的。 -
rint()public static double rint(double a) {}返回与
a最接近的整数值。注意返回值仍然是double类型的。 -
atan2()public static double atan2(double y, double x) {}返回直角坐标 (x, y) 对应的极坐标 (r, theta) 中的 theta 值。相当于计算
atan(y/x)。 -
pow()public static double pow(double a, double b) {}计算
a的b次幂。 -
round()public static int round(float a) {} public static long round(double a) {}计算与
float/double类型参数a的最接近的整数值,返回值为int/long型。 -
random()public static double random() {}返回一个 0.0 到 1.0 (不包含 1.0)的
double型随机值。 -
addExact()public static int addExact(int x, int y) {} public static long addExact(long x, long y) {}返回
int/long型参数x与y的和。如果结果溢出,则抛出ArithmeticException类型的异常。 -
subtractExact()public static int subtractExact(int x, int y) {} public static long subtractExact(long x, long y) {}返回
int/long型参数x和y的差值x-y。结果溢出则抛出ArithmeticException类型的异常。 -
multiplyExact()public static int multiplyExact(int x, int y) {} public static long multiplyExact(long x, int y) {} // 从 Java 9 开始加入 public static long multiplyExact(long x, long y) {}返回
x和y的乘积。结果溢出则抛出ArithmeticException类型的异常。 -
incrementExact()public static int incrementExact(int a) {} public static long incrementExact(long a) {}将
int/long型参数a增加 1 并返回。结果溢出则抛出ArithmeticException类型的异常。 -
decrementExact()public static int decrementExact(int a) {} public static long decrementExact(long a) {}将
int/long型参数a减去 1 并返回。结果溢出则抛出ArithmeticException类型的异常。 -
negateExact()public static int negateExact(int a) {} public static long negateExact(long a) {}将
int/long型参数a乘以 -1 并返回。结果溢出则抛出ArithmeticException类型的异常。 -
toIntExact()public static int toIntExact(long value) {}将
long型参数value转换为int型并返回。结果溢出则抛出ArithmeticException类型的异常。 -
floorDiv()public static int floorDiv(int x, int y) {} public static long floorDiv(long x, int y) {} // 从 Java 9 开始加入 public static long floorDiv(long x, long y) {}计算
x/y并向下取整,就算结果为负数,也同样返回小于或等于结果的最大值,而不是返回接近零的整数。特殊情况下,如果被除数是Integer.MIN_VALUE且除数为 -1,则会发生溢出,那么结果返是Integer.MIN_VALUE。 -
floorMod()public static int floorMod(int x, int y) {} public static int floorMod(long x, int y) {} // 从 Java 9 开始加入 public static long floorMod(long x, long y) {}返回
x对y的向下取整的取模值。返回的值等于x-(floorDiv(x, y)*y,它和y符号相同,并且在-abs(y)到abs(y)的范围内。floorDiv和floorMod之间的关系为:floorDiv(x,y)+floorMod(x,y)=x。 -
abs()public static int abs(int a) {} public static long abs(long a) {} public static float abs(float a) {} public static double abs(double a) {}返回
a的绝对值。如果a的值为Integer.MIN_VALUE/Long.MIN_VALUE,返回结果将保持不变。 -
max()public static int max(int a, int b) {} public static long max(long a, long b) {}返回
a和b中较大的整数值。public static float max(float a, float b) {} public static double max(double a, double b) {}返回
a和b中较大的浮点数值。当任意一个值为NaN时,返回结果为NaN。该方法还考虑到了正零和负零的比较,当一个参数为正零而另一个为负零时,返回正零。 -
min()public static int min(int a, int b) {} public static long min(long a, long b) {}返回
a和b中较小的整数值。public static float min(float a, float b) {} public static double min(double a, double b) {}返回
a和b中较大小的浮点数值。当任意一个值为NaN时,返回结果为NaN。该方法还考虑到了正零和负零的比较,当一个参数为正零而另一个为负零时,返回负零。 -
ulp()public static double ulp(double d) {} public static float ulp(float f) {}返回参数的最后位置的单位大小(unit in the last place, ulp)。对于一个
double/float值来说,就是该浮点数跟比它大的下一个double/float值之间的距离。 -
signum()public static double signum(double d) {} public static float signum(float f) {}返回所给参数的符号数。即是说,若参数为负数则返回 -1,参数为 0 则返回 0,参数为正数则返回 1。
-
sinh()public static double sinh(double x) {}返回
x的双曲正弦函数值。双曲正弦函数形式为(e^x - e^(-x))/2。 -
cosh()public static double cosh(double x) {}返回
x双曲余弦函数值。双曲正弦函数形式为(e^x + e^(-x))/2。 -
tanh()public static double tanh(double x) {}返回
x双曲正切函数值。双曲正弦函数形式为(e^x - e^(-x))、(e^x + e^(-x))。 -
hypot()public static double hypot(double x, double y) {}返回
sqrt(x^2+y^2),没有中间上溢或下溢。 -
expm1()public static double expm1(double x) {}返回
e^x-1。注意:对于接近 0 附近的x,expm1(x)+1的值比exp(x)更加接近e^x的真实值。 -
log1p()public static double log1p(double x) {}返回
x+1的自然对数。注意:对于很小的x值,log1p(x)的计算结果比log(x+1)的计算结果更加接近于真实的ln(x+1)。 -
copySign()public static double copySign(double magnitude, double sign) {} public static float copySign(float magnitude, float sign) {}返回一个浮点数值,该值与第一个参数
magnitude的数值相等,与第二个参数sign的符号相同。 -
getExponent()public static int getExponent(float f) {} public static int getExponent(double d) {}返回参数的无偏值数值。返回值
exp与输入参数x之间的关系为2^exp<=x。 -
nextAfter()public static double nextAfter(double start, double direction) {} public static float nextAfter(float start, double direction) {}返回第二个参数
direction方向上第一个参数start紧邻的数值。如果两个参数相等,那么直接返回第二个参数。 -
nextUp()public static double nextUp(double d) {} public static float nextUp(float f) {}返回正方向上与所给参数紧邻的数值。此方法等效于
nextAfter(d, Double.POSITIVE_INFINITY)/nextAfter(f, Float.POSITIVE_INFINITY),但是nextUp()的实现可能比其等效的nextAfter()运行更快。 -
nextDown()public static double nextDown(double d) {} public static float nextDown(float f) {}返回负方向上与所给参数紧邻的数值。此方法等效于
nextAfter(d, Double.NEGATIVE_INFINITY)/nextAfter(f, Float.NEGATIVE_INFINITY),但是nextUp()的实现可能比其等效的nextAfter()运行更快。 -
scalb()public static double scalb(double d, int scaleFactor) {} public static float scalb(float f, int scaleFactor) {}返回
d*2^(scaleFactor)。
以下是从 Java 9 开始加入的方法:
-
multiplyFull()public static long multiplyFull(int x, int y) {}计算两个
int型整数的乘积正确值(不考虑结果溢出),返回long类型的结果。 -
multiplyHigh()public static long multiplyHigh(long x, long y) {}计算两个 64 位的
long型参数的 128 位乘积,并返回结果的高 64 位。 -
fma()public static double fma(double a, double b, double c) {} public static float fma(float a, float b, float c) {}返回三个参数的融合乘法加法(fused multiply add),即
a*b+c。