Java Math方法记录

Java 提供 java.lang.Math 类,很方便的进行数学运算。

Math 类是基于浮点数的运算,可能导致精度损失,不适用于高精度计算。

记录如下,

方法 说明
Math.PI 常量,圆周率 π
Math.E 常量,自然对数的底数 e
exp(double a) 求自然对数的底数 e 的 a 次方
max(int a, int b) 取 a 和 b 的最大值。有重载方法,支持 double 、float 、long
min(int a, int b) 取 a 和 b 的最小值。有重载方法,支持 double 、float 、long
abs(int a) 取 a 的绝对值。有重载方法,支持 double 、float 、long
pow(double a, double b) 求 a 的 b 次方
sqrt(double a) 求 a 的平方根
ceil(double a) a 向上取整
floor(double a) a 向下取整
round(double a) 对 a 进行四舍五入
rint(double a) 返回最接近 a 的整数,如果有2个则取偶数那个
random() 取一个 0.0 到 1.0 之间的 double 型随机数
toRadians(double angdeg) 把角度转换为弧度
toDegrees(double angrad) 把弧度转换为角度
sin(double a) 求角度的正弦函数值,非法输入则返回 NaN
asin(double a) 求角度的余割函数值 (余割函数和正弦函数互为倒数),非法输入则返回 NaN
cos(double a) 求角度的余弦函数值,非法输入则返回 NaN
acos(double a) 求角度的正割函数值(正割函数和余弦函数互为倒数),非法输入则返回 NaN
tan(double a) 求角度的正切函数值 ,非法输入则返回 NaN
atan(double a) 求角度余切函数值 (余切函数和正切函数互为倒数),非法输入则返回 NaN
log(double a) 求以 e 底 a 的对数
log10(double a) 求以10底 a 的对数

常量

提供了两个常量,

  • Math.PI :圆周率π
  • Math.E :自然对数的底数 e
System.out.println("圆周率 π  : " + Math.PI);
System.out.println("自然对数的底数 e : " + Math.E);
圆周率 π  : 3.141592653589793
自然对数的底数 e : 2.718281828459045

取值(最大值、最小值、绝对值、取反)

  • Math.max(int a, int b) :取 a 、b 中的最大值。有重载方法,还支持 double 、float 、long 型参数
  • Math.min(int a, int b) :取 a 、b 中的最小值。有重载方法,还支持 double 、float 、long 型参数
  • Math.abs(int a) :取 a 的绝对值。有重载方法,还支持 double 、float 、long 型参数
  • Math.negateExact(int a) :对 a 取反。有重载方法,还支持 long 型参数
System.out.println("取最大值,示例 Math.max(100,200) : " + Math.max(100,200));
System.out.println("取最小值,示例 Math.min(100,200) : " + Math.min(100,200));
System.out.println("取绝对值,示例 Math.abs(-100) : " + Math.abs(-100));
System.out.println("取反,示例 Math.negateExact(100) : " + Math.negateExact(100));
取最大值,示例 Math.max(100,200) : 200
取最小值,示例 Math.min(100,200) : 100
取绝对值,示例 Math.abs(-100) : 100
取反,示例 Math.negateExact(100) : -100

四舍五入

Math.round(double a) :对 a 四舍五入。有重载方法,还支持 float 型参数

System.out.println("2023.4 四舍五入 : " + Math.round(2023.4));
System.out.println("2023.5 四舍五入 : " + Math.round(2023.5));
2023.4 四舍五入 : 2023
2023.5 四舍五入 : 2024

取整

  • Math.rint(double a) : 返回最接近 a 的整数,如果有2个这样的整数,就返回其中的偶数
  • Math.ceil(double a) :向上取整。
  • Math.floor(double a) :向下取整。
		System.out.println("取最接近的整数 Math.rint(-2.5) : " + Math.rint(-2.1));
        System.out.println("取最接近的整数 Math.rint(-2.9) : " + Math.rint(-2.9));
        System.out.println("取最接近的整数 Math.rint(3.2) : " + Math.rint(3.2));
        System.out.println("取最接近的整数 Math.rint(4.7) : " + Math.rint(4.7));
        System.out.println("取最接近的整数 Math.rint(5.5) : " + Math.rint(5.5));
        System.out.println("99.5 向上取整 Math.ceil(99.5) : " + Math.ceil(99.5));
        System.out.println("99.5 向上取整 Math.floor(99.5) : " + Math.floor(99.5));
取最接近的整数 Math.rint(-2.5) : -2.0
取最接近的整数 Math.rint(-2.9) : -3.0
取最接近的整数 Math.rint(3.2) : 3.0
取最接近的整数 Math.rint(4.7) : 5.0
取最接近的整数 Math.rint(5.5) : 6.0
99.5 向上取整 Math.ceil(99.5) : 100.0
99.5 向上取整 Math.floor(99.5) : 99.0

加减乘除

取模、取余

Math.IEEEremainder(double f1, double f2) : 采用 IEEE 754 标准 ,f1 对 f2 取模 。

随机数

Math.random() :生成一个大于等于 0.0 且小于 1.0 的 double 型随机数。

幂次运算

  • Math.pow(double a, double b) :求 a 的 b 次方。
  • Math.exp(double a) :求自然对数的底数 e 的 a 次方。
System.out.println("求2的3次方 Math.pow(2,3): " + Math.pow(2,3));
System.out.println("求自然对数的底数 e 的2次方  : " + Math.exp(2));
求2的3次方 Math.pow(2,3): 8.0
求自然对数的底数 e 的2次方  : 7.38905609893065

根号运算

  • Math.sqrt(double a) :求 a 的平方根。
  • Math.cbrt(double a) :求 a 的立方根。
System.out.println("求25的平方根 Math.sqrt(25): " + Math.sqrt(25));
System.out.println("求27的立方根 Math.cbrt(27): " + Math.cbrt(27));
求25的平方根 Math.sqrt(25): 5.0
求27的立方根 Math.cbrt(27): 3.0

对数运算

  • Math.log(double a) :求以 e 底 a 的对数。
  • Math.log10(double a) :求以 10 底 a 的对数。
  • 如果要计算以 a 为底 b 的对数,通过数学运算实现 : Math.log(b)/Math.log(a)
System.out.println("求以e底e²的对数 Math.log(Math.E * Math.E) : " + Math.log(Math.E * Math.E));
System.out.println("求以10底100的对数 Math.log10(100) : " + Math.log10(100));
System.out.println("求以3为底81的对数 Math.log3(81) : " + Math.log(81)/Math.log(3));
求以e底e²的对数 Math.log(Math.E * Math.E) : 2.0
求以10底100的对数 Math.log10(100) : 2.0
求以3为底81的对数 Math.log3(81) : 4.0

弧度、角度运算

三角函数

  • Math.sin(double a) :求角度的正弦函数值,非法输入则返回 NaN
  • Math.asin(double a) :求角度的余割函数值 (余割函数和正弦函数互为倒数),非法输入则返回 NaN
  • Math.cos(double a) :求角度的余弦函数值,非法输入则返回 NaN
  • Math.acos(double a) :求角度的正割函数值(正割函数和余弦函数互为倒数),非法输入则返回 NaN
  • Math.tan(double a) :求角度的正切函数值 ,非法输入则返回 NaN
  • Math.atan(double a) :求角度余切函数值 (余切函数和正切函数互为倒数),非法输入则返回 NaN
System.out.println("正弦 sin 90° ,Math.sin(90) : " + Math.sin(Math.toRadians(90)));
System.out.println("余割 csc 90° ,Math.asin(90) : " + Math.asin(Math.toRadians(90)));
System.out.println("余弦 cos 180° ,Math.cos(180) : " + Math.cos(Math.toRadians(180)));
System.out.println("正割 sec 180° ,Math.acos(180) : " + Math.acos(Math.toRadians(180)));
System.out.println("正切 tan 45° ,Math.tan(45) : " + Math.tan(Math.toRadians(45)));
System.out.println("余切 cot 45° ,Math.atan(45) : " + Math.atan(Math.toRadians(45)));
正弦 sin 90° ,Math.sin(90) : 1.0
余割 csc 90° ,Math.asin(90) : NaN
余弦 cos 180° ,Math.cos(180) : -1.0
正割 sec 180° ,Math.acos(180) : NaN
正切 tan 45° ,Math.tan(45) : 0.9999999999999999
余切 cot 45° ,Math.atan(45) : 0.6657737500283538

都知道 tan 45° = 1 ,输出的值是 0.9999999999999999 ,说明精度不准确。

你可能感兴趣的:(Java,java,Math)