Java中Math函数的用法

相信大家在Java的学习过程中,在敲一下代码的时候,总会用到和数学有关的函数,它就是Math函数。

下面我总结了一些Math函数的用法。

本文借鉴了:https://blog.csdn.net/xuexiangjys/article/details/79849888

算数的计算

Math.sqrt()  计算平方根

Math.abs()  取绝对值

Math.cbrt()  计算立方根

Math.pow(a, b)  计算a的b次方

Math.max( , )  计算最大值

Math.min( , )  计算最小值

System.out.println(Math.sqrt(16)); // 4.0
System.out.println(Math.cbrt(8)); // 2.0
System.out.println(Math.pow(3, 2)); // 9.0
System.out.println(Math.max(2.3, 4.5));// 4.5
System.out.println(Math.min(2.3, 4.5));// 2.3

/**
 * abs求绝对值
 */
System.out.println(Math.abs(-10.4)); // 10.4
System.out.println(Math.abs(10.1)); // 10.1

进位:

Math.ceil()  天花板的意思,就是逢余进一

Math.floor()   地板的意思,就是逢余舍一

Math.rint()  四舍五入,返回double值。注意.5的时候会取偶数

Math.round()  四舍五入,float时返回int值,double时返回long值

/**
* ceil天花板的意思,就是逢余进一
 */
System.out.println(Math.ceil(-10.1)); // -10.0
System.out.println(Math.ceil(10.7)); // 11.0
System.out.println(Math.ceil(-0.7)); // -0.0
System.out.println(Math.ceil(0.0)); // 0.0
System.out.println(Math.ceil(-0.0)); // -0.0
System.out.println(Math.ceil(-1.7)); // -1.0

System.out.println("-------------------");

/**
 * floor地板的意思,就是逢余舍一
 */
System.out.println(Math.floor(-10.1)); // -11.0
System.out.println(Math.floor(10.7)); // 10.0
System.out.println(Math.floor(-0.7)); // -1.0
System.out.println(Math.floor(0.0)); // 0.0
System.out.println(Math.floor(-0.0)); // -0.0

System.out.println("-------------------");

/**
 * rint 四舍五入,返回double值 注意.5的时候会取偶数 异常的尴尬=。=
 */
System.out.println(Math.rint(10.1)); // 10.0
System.out.println(Math.rint(10.7)); // 11.0
System.out.println(Math.rint(11.5)); // 12.0
System.out.println(Math.rint(10.5)); // 10.0
System.out.println(Math.rint(10.51)); // 11.0
System.out.println(Math.rint(-10.5)); // -10.0
System.out.println(Math.rint(-11.5)); // -12.0
System.out.println(Math.rint(-10.51)); // -11.0
System.out.println(Math.rint(-10.6)); // -11.0
System.out.println(Math.rint(-10.2)); // -10.0

System.out.println("-------------------");
/**
 * round 四舍五入,float时返回int值,double时返回long值
 */
System.out.println(Math.round(10)); // 10
System.out.println(Math.round(10.1)); // 10
System.out.println(Math.round(10.7)); // 11
System.out.println(Math.round(10.5)); // 11
System.out.println(Math.round(10.51)); // 11
System.out.println(Math.round(-10.5)); // -10
System.out.println(Math.round(-10.51)); // -11
System.out.println(Math.round(-10.6)); // -11
System.out.println(Math.round(-10.2)); // -10

【注意】这里有一个非常需要注意的一点是:这里所有进位的方法的入参都要保证是float或者double类型,否则进位方法将毫无意义。例如如下我们经常犯的错误:

int a = 1300, b = 1000;
System.out.println(Math.ceil(a / b));  // 1  表达式A(错误使用)
System.out.println(Math.ceil(a / (float)b));  // 2 表达式B(正确使用)

看上去表达式A和表达式B没有什么区别,可仔细分析可知:a / b = 1 ,而 a / (float)b = 1.3, 实际上表达式A的Math.ceil()根本起不了任何作用。

随机数:

Math.random()  取得一个[0, 1)范围内的随机数

System.out.println(Math.random()); // [0, 1)的double类型的数
System.out.println(Math.random() * 2);//[0, 2)的double类型的数
System.out.println(Math.random() * 2 + 1);// [1, 3)的double类型的数

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值