Java中的Math函数如何进行数字运算?
Java中的Math类是一个内置的数学库,提供了一系列数字运算的方法,包括最基本的加、减、乘、除,以及各种科学计算、三角函数、指数函数、对数函数等。在Java程序开发中,尤其在数学计算方面,Math类是非常重要的。
一、基础四则运算
Math类提供了基础的四则运算,分别是加(add)、减(subtract)、乘(multiply)和除(divide)。
1.1 加(add)
两个数相加,得到的结果直接返回。Math类提供了两种方法:
public static int addExact(int x, int y)
public static long addExact(long x, long y)
两个方法的作用相同,只是参数类型不同。方法名中的Exact表示在计算中,如果发生溢出,会报ArithmeticException异常。例如:
int a = Math.addExact(Integer.MAX_VALUE, 1); // 报ArithmeticException异常
1.2 减(subtract)
两个数相减,得到的结果直接返回。Math类提供了两种方法:
public static int subtractExact(int x, int y)
public static long subtractExact(long x, long y)
两个方法的作用相同,只是参数类型不同。方法名中的Exact表示在计算中,如果发生溢出,会报ArithmeticException异常。例如:
int a = Math.subtractExact(Integer.MIN_VALUE, 1); // 报ArithmeticException异常
1.3 乘(multiply)
两个数相乘,得到的结果直接返回。Math类提供了两种方法:
public static int multiplyExact(int x, int y)
public static long multiplyExact(long x, long y)
两个方法的作用相同,只是参数类型不同。方法名中的Exact表示在计算中,如果发生溢出,会报ArithmeticException异常。例如:
int a = Math.multiplyExact(Integer.MAX_VALUE, 2); // 报ArithmeticException异常
1.4 除(divide)
两个数相除,得到的结果直接返回。Math类提供了两种方法:
public static int divideExact(int x, int y)
public static long divideExact(long x, long y)
两个方法的作用相同,只是参数类型不同。方法名中的Exact表示在计算中,如果发生越界或y为0,会报ArithmeticException异常;如果发生溢出,会返回最大或最小值。例如:
int a = Math.divideExact(Integer.MAX_VALUE, 0); // 报ArithmeticException异常
int b = Math.divideExact(Integer.MIN_VALUE, -1); // 报ArithmeticException异常
int c = Math.divideExact(Integer.MAX_VALUE, 2); // 返回1073741823(二进制为0111111111111111111111111111111)
二、一些常用的数学函数
除了基础的四则运算,Math类还提供了一些常用的数学函数,如下所示:
2.1 绝对值
public static int abs(int a)
public static long abs(long a)
public static float abs(float a)
public static double abs(double a)
取得参数的绝对值,直接返回。例如:
int a = Math.abs(-10); // 返回10
2.2 幂和指数
public static double pow(double a, double b)
求a的b次幂,直接返回。例如:
double a = Math.pow(2, 3); // 返回8
2.3 根号
public static double sqrt(double a)
求a的平方根,直接返回。例如:
double a = Math.sqrt(16); // 返回4
2.4 自然对数
public static double log(double a)
求a的自然对数,即ln(a),直接返回。例如:
double a = Math.log(2.718); // 返回1.0
2.5 以e为底的指数
public static double exp(double a)
求e的a次幂,即e的a次方。例如:
double a = Math.exp(1); // 返回2.718281828459045
2.6 取整
public static int round(float a)
public static long round(double a)
对a进行四舍五入,取整之后返回。例如:
int a = Math.round(1.5f); // 返回2
2.7 取最大值和最小值
public static int max(int a, int b)
public static long max(long a, long b)
public static float max(float a, float b)
public static double max(double a, double b)
返回a和b之间的最大值。Math类也提供了相应的min方法,返回a和b之间的最小值。例如:
int a = Math.max(2, 5); // 返回5
三、三角函数
Math类也提供了各种三角函数,如下所示:
3.1 正弦函数
public static double sin(double a)
求a的正弦值,即sin(a),直接返回。例如:
double a = Math.sin(Math.PI / 2); // 返回1.0
3.2 余弦函数
public static double cos(double a)
求a的余弦值,即cos(a),直接返回。例如:
double a = Math.cos(Math.PI / 2); // 返回0.0
3.3 正切函数
public static double tan(double a)
求a的正切值,即tan(a),直接返回。例如:
double a = Math.tan(Math.PI / 4); // 返回1.0
3.4 反正弦函数
public static double asin(double a)
求a的反正弦值,即arcsin(a),直接返回。例如:
double a = Math.asin(1); // 返回1.5707963267948966
3.5 反余弦函数
public static double acos(double a)
求a的反余弦值,即arccos(a),直接返回。例如:
double a = Math.acos(0); // 返回1.5707963267948966
3.6 反正切函数
public static double atan(double a)
求a的反正切值,即arctan(a),直接返回。例如:
double a = Math.atan(1); // 返回0.7853981633974483
以上三角函数都是以弧度制为参数的。Java中还提供了以角度制为参数的三角函数,如下所示:
3.7 角度转弧度
public static double toRadians(double angdeg)
将以角度表示的角转化为以弧度表示的角,直接返回。例如:
double a = Math.toRadians(180); // 返回Math.PI
3.8 弧度转角度
public static double toDegrees(double angrad)
将以弧度表示的角转化为以角度表示的角,直接返回。例如:
double a = Math.toDegrees(Math.PI / 2); // 返回90.0
四、高级数学运算
Math类还提供了各种高级数学运算,如下所示:
4.1 取余数
public static int floorMod(int x, int y)
求x对y的余数。例如:
int a = Math.floorMod(-17, 5); // 返回3
4.2 模运算
public static double IEEEremainder(double f1, double f2)
求f1对于f2的模运算结果,即f1 - n * f2,其中n为整数。例如:
double a = Math.IEEEremainder(10, 3); // 返回1.0
4.3 随机数
public static double random()
返回一个介于0.0和1.0之间的随机数。例如:
double a = Math.random(); // 返回0.xxxxxx
4.4 最大公约数
public static int gcd(int a, int b)
求a和b的最大公约数,直接返回。例如:
int a = Math.gcd(12, 18); // 返回6
4.5 最小公倍数
public static int lcm(int a, int b)
求a和b的最小公倍数,直接返回。例如:
int a = Math.lcm(12, 18); // 返回36
4.6 双曲函数
public static double sinh(double x)
public static double cosh(double x)
public static double tanh(double x)
Math类还提供了各种双曲函数,如sinh、cosh、
