一,常用的方法
方法 | 描述 |
---|---|
Math.sqrt(x) | 计算x的平方根,返回值为 double |
Math.cbrt(x) | 计算x的立方根,返回值为 double |
Math.abs(x) | 返回参数x的绝对值,不改变x的数据类型 |
Math.floor(x) | 返回小于x的最大整数,返回类型为 double |
Math.ceil(x) | 返回大于等于x的的最小整数,返回类型为 double |
Math.round(x) | 将x四舍五入取整,返回值为 long |
Math.pow(x,y) | 计算x的y次方,返回类型为 double |
Math.max(x,y) | 计算x和y的最大值,返回类型为 double |
Math.min(x,y) | 计算x和y的最小值,返回类型为 double |
Math.toDegrees(x) | 将x转化为角度 |
Math.toRadians(x) | 将x转换为弧度 |
Math.sin(x) | 求x的正弦值 |
Math.cos(x) | 求x的余弦值 |
Math.tan(x) | 求x的正切值 |
Math.asin(x) | 求x的反正弦值 |
Math.acos(x) | 求x的反余弦值 |
二,方法使用
1. 平方根
// Math.sqrt(x)
int number = 64;
double sqrt = Math.sqrt();
System.out.println(sqrt);
执行结果:
8.0
Process finished with exit code 0
2. 立方根
// Math.cbrt(x)
int x = 64;
double cbrt = Math.cbrt(x);
System.out.println(cbrt);
执行结果:
4.0
Process finished with exit code 0
3. 绝对值
// Math.abs(x)
double x = -4.36;
double abs = Math.abs(x);
System.out.println(abs);
执行结果:
4.36
Process finished with exit code 0
4. 返回小于x的最大整数
// Math.floor(x)
double x = 7.96;
double floor = Math.floor(x);
System.out.println(floor);
执行结果:
7.0
Process finished with exit code 0
5. 返回大于x的最大整数
// Math.ceil (x)
double x = 7.96;
double ceil = Math.ceil(x);
System.out.println(ceil);
执行结果:
8.0
Process finished with exit code 0
6. 四舍五入
// Math.round(x)
double x = 14.965712;
long round = Math.round(x);
System.out.println(round);//默认取整long
执行结果:
15
Process finished with exit code 0
7. 计算x的y次方
//Math.pow(x,y);
double x = 4;
double y = 2;
double pow = Math.pow(x, y);
System.out.println(pow);
执行结果:
16.0
Process finished with exit code 0
8. 计算x的y的最大值
//Math.max(x,y);
double x = 2.79;
double y = 9.35;
double max = Math.max(x, y);
System.out.println(max);
执行结果:
9.35
Process finished with exit code 0
9. 计算x的y的最小值
//Math.min(x,y);
double x = 2.79;
double y = 9.35;
double min = Math.min(x, y);
System.out.println(min);
执行结果:
2.79
Process finished with exit code 0
10. 将x转化为角度
//Math.toDegrees(x);
double x = 30;
double toDegrees= Math.toDegrees(x);
System.out.println(toDegrees);
执行结果:
1718.8733853924698
Process finished with exit code 0
11. 将x转换为弧度
//Math.toRadians(x);
double x = 30;
double toRadians= Math.toRadians(x);
System.out.println(toRadians);
执行结果:
0.5235987755982988
Process finished with exit code 0
12. 正弦值
//Math.sin(x);
double x =Math.toRadians(90) ; //将数字90转换成弧度制
double sin = Math.sin(x); //返回值在(-1,1)之间
System.out.println(sin);
执行结果:
1
Process finished with exit code 0
13. 余弦值
//Math.cos(x);
double x =Math.toRadians(180) ; //将数字180转换成弧度制
double cos = Math.cos(x);//返回值在(-1,1)之间
System.out.println(cos);
执行结果:
-1
Process finished with exit code 0
14. 正切值
//Math.tan(x);
double x =Math.toRadians(45) ; //将数字45转换成弧度制
double tan = Math.tan(x);
System.out.println(tan);
执行结果:
0.9999999999999999
Process finished with exit code 0
15. 反正弦值
//Math.asin(x);
double degrees = 45.0;
double radians = Math.toRadians(degrees); //转弧度
System.out.format("π 的值为 %.2f%n", Math.PI);
System.out.format("%.2f 的反正弦值为 %.0f 度 %n", Math.sin(radians), Math.toDegrees(Math.asin(Math.sin(radians))));
执行结果:
π 的值为 3.14
0.71 的反正弦值为 45 度
Process finished with exit code 0
16. 反余弦值
//Math.asin(x);
double degrees = 45.0;
double radians = Math.toRadians(degrees);
System.out.format("π 的值为 %.2f%n", Math.PI);
System.out.format("%.2f 的反余弦值为 %.0f 度 %n", Math.cos(radians), Math.toDegrees(Math.acos(Math.sin(radians))));
执行结果:
π的值为 3.14
0.71 的反余弦值为 45 度
Process finished with exit code 0
本文部分引用链接: https://www.runoob.com/.