学习Java的第十五天——数学运算

学习内容:数学运算

1.三角函数运算

代码实现:

public class 三角函数运算 {

public static void main(String[] args) {
// TODO 自动生成的方法存根
double a1=Math.sin(Math.PI/2);//求正弦值
double a2=Math.cos(0);//求余弦值
double a3=Math.tan(Math.PI/3);//求正切值
double b1=Math.asin(Math.sqrt(2)/2);//反正弦值
double b2=Math.acos(Math.sqrt(2)/2);//反余弦值
double b3=Math.atan(1);//反正切值
double c1=Math.toDegrees(Math.PI/2);//弧度转角度
double c2=Math.toRadians(120.0);//角度转弧度
System.out.println("90度的正弦值为:"+a1);
System.out.println("0度的余弦值为:"+a2);
System.out.println("60度的正切值为:"+a3);
System.out.println("Math.sqrt(2)/2的反正弦值为:"+b1);
System.out.println("Math.sqrt(2)/2的反余弦值为:"+b2);
System.out.println("1的反正切值为:"+b3);
System.out.println("PI/2的角度值为:"+c1);
System.out.println("120度的弧度值为:"+c2);
}

}

运算结果:

90度的正弦值为:1.0
0度的余弦值为:1.0
60度的正切值为:1.7320508075688767
Math.sqrt(2)/2的反正弦值为:0.7853981633974484
Math.sqrt(2)/2的反余弦值为:0.7853981633974483
1的反正切值为:0.7853981633974483
PI/2的角度值为:90.0
120度的弧度值为:2.0943951023931953

2.指数运算

代码实现:

public class 指数运算 {

public static void main(String[] args) {
// TODO 自动生成的方法存根
double a1=Math.exp(2);
double a2=Math.log(2);
double a3=Math.log10(2);
double a4=Math.sqrt(4);
double a5=Math.cbrt(8);
double a6=Math.pow(2, 3);
System.out.println("e的平方为:"+a1);
System.out.println("以e为底2的对数为:"+a2);
System.out.println("以10为底2的对数为:"+a3);
System.out.println("4的平方根为:"+a4);
System.out.println("8的立方根为:"+a5);
System.out.println("2的3次方为:"+a6);
}

}

运算结果:

e的平方为:7.38905609893065
以e为底2的对数为:0.6931471805599453
以10为底2的对数为:0.3010299956639812
4的平方根为:2.0
8的立方根为:2.0
2的3次方为:8.0

3.取整函数

代码实现:

public class 取整函数运算 {

public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.println("5.2使用ceil()方法取整:"+Math.ceil(5.2));//返回第一个大于参数的整数
System.out.println("5.2使用floor()方法取整:"+Math.floor(5.2));//返回第一个小于参数的整数
System.out.println("2.7使用rint()方法取整:"+Math.rint(2.7));//返回与参数最接近的整数
System.out.println("2.2使用rint()方法取整:"+Math.rint(2.2));
System.out.println("2.5使用rint()方法取整:"+Math.rint(2.5));
System.out.println("3.4使用round()方法取整:"+Math.round(3.4f));//将参数加上0.5返回最接近的整数
System.out.println("2.5使用round()方法取整:"+Math.round(2.5));//将参数加上0.5返回最接近的整数,并将结果强制转换为长整型
}

}

运算结果:

5.2使用ceil()方法取整:6.0
5.2使用floor()方法取整:5.0
2.7使用rint()方法取整:3.0
2.2使用rint()方法取整:2.0
2.5使用rint()方法取整:2.0
3.4使用round()方法取整:3
2.5使用round()方法取整:3

4.取最值和绝对值

代码实现:

public class 取最值与绝对值 {

public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.println("4和8较大的是:"+Math.max(4, 8));
System.out.println("4.4和4较小的是:"+Math.min(4.4, 4));
System.out.println("-7的绝对值是:"+Math.abs(-7));
}

}

运算结果:

4和8较大的是:8
4.4和4较小的是:4.0
-7的绝对值是:7

明天任务:随机数

posted @ 2020-07-20 18:22  CherriesOvO  阅读(204)  评论(0编辑  收藏  举报