盘点Math类中取整函数、三角函数和指数函数方法

Java进阶学习交流

共 3510字,需浏览 8分钟

 ·

2021-05-12 16:42

点击上方“Java进阶学习交流”,进行关注

后台回复“Java”即可获赠Java学习资料

一日不见兮,思之如狂。

大家好,我是Java进阶者,今天小编带大家一起来学习Java技术基础!

一、Math类取整函数方法

1.Math类取整函数方法,如下所示:

public static double ceil(double a)方法:返回double类值的最小值,这个值大于或等于。简单来说是向上取整;

public static double floor(double a)方法:返回double类值的最大值,这个值小于或等于。简单来说是向下取整;

public static double rint(double a)方法:返回最接近的参数a的值,并且它的值是double类型的值;

public static int round(float a)方法:返回最接近的参数加上0.5将结果转换为int类型,也就是四舍五入取整;

public static long round(double a)方法:返回最接近的参数加上0.5将结果转换为long类型,也就是四舍五入取整;

2.Math类取整函数方法例子:

public class p71 {public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("ceil()方法 :"+Math.ceil(2.1));        System.out.println("ceil()方法 :"+Math.ceil(2.5));        System.out.println("ceil()方法 :"+Math.ceil(2.8));        System.out.println("floor()方法 :"+Math.floor(1.1));        System.out.println("floor()方法 :"+Math.floor(1.5));        System.out.println("floor()方法 :"+Math.floor(1.8));        System.out.println("rint()方法 :"+Math.rint(3.1));        System.out.println("rint()方法 :"+Math.rint(3.5));        System.out.println("rint()方法 :"+Math.rint(3.8));        System.out.println("round()方法 :"+Math.round(5.1));        System.out.println("round()方法 :"+Math.round(5.5));        System.out.println("round()方法 :"+Math.round(5.8));  }}

运行的结果是:


二、Math类三角函数方法

1.Math类三角函数方法,如下所示:

public static double sin(double a)方法:返回参数的正弦值,a是以弧度表示角度;

public static double cos(double a)方法:返回参数的余弦值,a是以弧度表示角度;

public static double tan(double a)方法:返回参数的正切值,a是以弧度表示角度;

public static double asin(double a)方法:返回参数的反正弦值;

public static double acos(double a)方法:返回参数的反余弦值;

public static double atan(double a)方法:返回参数的反正切值;

public static  double toRadians(double a) : 把角度转换为弧度;

public static  doueble toDegrees(double a) : 把弧度转化为角度;

2.Math类三角函数方法例子

public class p72 {public static void main(String[] args) {        // TODO Auto-generated method stub        double p=Math.PI;        System.out.println("30度的正弦值:"+Math.sin(p/6));        System.out.println("90度的正弦值:"+Math.sin(p/2));        System.out.println("0度的余弦值:"+Math.cos(0));        System.out.println("30度的余弦值:"+Math.cos(p/6));        System.out.println("1的反正切值:"+Math.atan(1));        System.out.println("60度的弧度值:" + Math.toRadians(60.0));    }}

运行的结果是:


三、Math类指数函数方法

1.Math类指数函数方法,如下所示:

public static double sqrt(double a ):用来取a的平方根(a²);

public static double cbrt(double a ):用来取a的立方根(a³);

public static double log(double a ):相当于lna;

public static double log10(double a ):以10为底的对数,也就是log10a;

public static double exp(double a ):用来获取e的a次方;

public static double pow(double a,double b):a表示底数,b表示指数,用来求a的b次方;

2.Math类指数函数方法例子:

public class p73 {public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("e的二次方"+Math.exp(2));        System.out.println("2的立方值:"+Math.pow(2, 3));        System.out.println("9的平方根:"+Math.sqrt(9));        System.out.println("10为底 10的对数:"+Math.log10(10));  }}

运行的结果是:


四、总结

本文主要介绍了Math类取整函数方法、三角函数方法、指数函数方法。

Math类取整函数方法有ceil、floor、rint、round,这些方法通过例子了解它的用法。Math类三角函数方法有sin、cos、tan、toRadians、toDegrees等,这些方法通过例子了解它的用法。

Math类指数函数方法有sqrt、cbrt、log、log10等,这些方法通过例子了解它的用法。希望大家通过本文的学习,对你有所帮助!

我是Java进阶者,希望大家通过本文的学习,对你有所帮助!欢迎大家加我微信,有问题可以随时帮大家解决噢,交个朋友也好哇~

------------------- End -------------------

往期精彩文章推荐:

欢迎大家点赞,留言,转发,转载,感谢大家的相伴与支持

想加入Java学习群请在后台回复【入群

万水千山总是情,点个【在看】行不行

浏览 78
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报