JAVA-Math类吐血大整理

1.常量e和PI.
e.g.
System.out.println(Math.E); //2.718281828459045
System.out.println(Math.PI); //3.141592653589793

2.Math.abs(); 计算绝对值
e.g.
System.out.println(Math.abs(-3)); //3

3.三角函数与反三角函数:(变量均为弧度制)
cos()求余弦
sin()求正弦
tan()求正切

acos()求反余弦
asin()求反正弦
atan()求反正切

Math.atan2(y,x)求向量(x,y)与x轴夹角
Math.toDegrees()将弧度转换角度
Math.toRadians()将角度转换为弧度(这条变量为角度值)

cosh()计算双曲余弦
sinh()计算双曲正弦
tanh()计算双曲正切

e.g.
System.out.println(Math.acos(1));
System.out.println(Math.acos(-1));
e.g.
System.out.println(Math.toDegrees(1.57));//89.95437383553926
System.out.println(Math.toRadians(90));//1.5707963267948966

4.Math.sqrt() 开平方根
Math.cbrt() 开立方根
Math.hypot(x,y) 求sqrt(xx+yy),求勾股定理的公式

e.g.
System.out.println(Math.sqrt(4.0));//2.0
e.g.
System.out.println(Math.cbrt(-1));//-1.0
System.out.println(Math.cbrt(1));//1.0
System.out.println(Math.cbrt(0.5));//0.7937005259840998
System.out.println(Math.cbrt(5));//1.709975946676697
e.g.
System.out.println(Math.hypot(3.0, 4.0));//5.0

5.Math.ceil() 天花板,向上取整,返回大的值
Math.floor() 地板,向下取整,返回小的值
Math.floorDiv(a,b) 第一个参数除以第二参数,然后针对结果执行floor操作

Math.Random() 能够返回带正号的double值,该值属于[0.0,1.0).
这个函数用的频繁,自己总结了个小技巧:求a到b范围内的随机数,公式是a+Math.Random()*(b-a+1).

Math.rint(x): x取整为它最接近的整数,如果x与两个整数的距离,则返回其 中为偶数的那一个.
Math.round(x): 返回"四舍五入"值。

e.g.
System.out.println(Math.ceil(7.2));//8.0
System.out.println(Math.ceil(7.5));//8.0
System.out.println(Math.ceil(7.6));//8.0
System.out.println(Math.ceil(-7.2));//-7.0
System.out.println(Math.ceil(-7.5));//-7.0
System.out.println(Math.ceil(-7.6));//-7.0
e.g.
System.out.println(Math.floor(7.2));//7.0
System.out.println(Math.floor(7.5));//7.0
System.out.println(Math.floor(7.6));//7.0
System.out.println(Math.floor(-7.2));//-8.0
System.out.println(Math.floor(-7.5));//-8.0
System.out.println(Math.floor(-7.6));//-8.0
e.g.
System.out.println(Math.floorDiv(7, 3));//2
System.out.println(Math.floorDiv(-7, 3));//-3
e.g.
System.out.println(Math.random());//[0.0,1.0)随机值
e.g.
System.out.println(Math.rint(3.5));//4.0
System.out.println(Math.rint(4.5));//4.0
System.out.println(Math.rint(3.1));//3.0
System.out.println(Math.rint(4.1));//4.0
System.out.println(Math.rint(3.7));//4.0
System.out.println(Math.rint(4.7));//5.0
e.g.
System.out.println(Math.round(3.5));//4
System.out.println(Math.round(4.5));//5
System.out.println(Math.round(3.1));//3
System.out.println(Math.round(4.7));//5

6.Math.cosh() 返回 double值的双曲线余弦.x的双曲线余弦的定义是(ex + e-x)/2.其中e是欧拉数.
e.g.
System.out.println(Math.cosh(1));//1.543080634815244
System.out.println(Math.cosh(0));//1.0

7.Math.exp(x) 返回e^x的值
Math.expm1(x) 返回e^x - 1的值
(x只允许double类型,因为e是一个小数)
e.g.
System.out.println(Math.exp(2));//7.38905609893065
System.out.println(Math.expm1(2));//6.38905609893065

8.Math.pow(x,y) 返回x^y的值
e.g.
System.out.println("Math.pow(2.0, 3.0));//8.0

9.对数
Math.log(a) a的自然对数(底数是e)
Math.log10(a) a的底数为10的对数
Math.log1p(a) 计算的是以e为底,a+1的对数
(对数运算的函数只能传double型数据并返回double型数据)

e.g.
System.out.println(Math.log(Math.E));//1.0
System.out.println(Math.log10(10));//1.0
System.out.println(Math.log1p(Math.E - 1.0));//1.0

10.Math.max(a,b) 求最大值
Math.min(a,b) 求最小值
e.g.
System.out.println(Math.max(1, 2));//2
System.out.println(Math.min(1, -2));//-2

11.Math.nextAfter(x,y) 返回朝向第二个参数方向的与第一个参数非常非常接近的浮点数。
Math.nextUp(x) 返回朝向正无穷大方向的与x非常非常接近的浮点值。
Math.nextDown(x) 返回朝向负无穷大方向的与x非常非常接近的浮点值

e.g.
System.out.println(Math.nextAfter(-1, 2));//-0.99999994
System.out.println(Math.nextAfter(1, 2));//1.0000001
e.g.
System.out.println(Math.nextUp(1));//1.0000001
System.out.println(Math.nextUp(-1));//-0.99999994
e.g.
System.out.println(Math.nextDown(1));//0.99999994 System.out.println(Math.nextDown(-1));//-1.0000001
12. Math.signum(): 数值=0返回0.0,数值>0返回1.0,数值<0返回-1.0。
Math.floorMod():
1.如果参数的符号相同,则floorMod和%运算符的结果是相同的。
2.如果参数的符号不同,则结果与%运算符不同。

e.g.
System.out.println(Math.signum(10));//1.0
System.out.println(Math.signum(-10));//-1.0
System.out.println(Math.signum(0));//0.0

e.g.
如果参数的符号相同,floorMod和%运算符的结果是相同的
System.out.println( Math.floorMod(4, 3));//1
System.out.println( (4 % 3));//1

System.out.println(Math.floorMod(-4, -3));//-1
System.out.println((-4 % -3));//-1

如果参数的符号不同,则结果与%运算符不同。
System.out.println(Math.floorMod(4, -3));//-2
System.out.println((4 % -3));//1

System.out.println(Math.floorMod(-4, 3));//2
System.out.println((-4 % 3));//-1

13.Math.toIntExact(); long类型转int类型
e.g.
System.out.println(Math.toIntExact(1));

14.暂时看不懂的函数们
Math.scalb(double d, int scaleFactor);

Math.scalb(float f, int scaleFactor);

Math.ulp();

( 如果要理解什么是ulp(unit in the last place or unit of least precision (ULP)),先要了解在计算机中保存的数和我们在数学上认为的数是不一样的; 比方说2.0和3.0之间有多少个数,在数学中是无限的,但是在计算机中是有限的,因为计算机需要用一堆字节来表示double或者float。但是因为计算机表示不了无限的数(因为没有无限内存)所以就有了ulp,假设在float 2.0和3.0之间有8,388,609个数,那么在2.0和3.0之间的数的ulp就是8,388,609/1.0约等于0.0000001. 你如果想知道某一个具体的double或float的先一个或者上一个数字是什么可以使用函数)

Math.addExact();求和,结果溢出时抛出ArithmeticException。

Math.substractExact(); 返回两个参数之差,结果溢出时抛出ArithmeticException

Math.incrementExact() ; 返回参数值加一,结果溢出时抛出ArithmeticException

Math.decrementExact(); 返回参数值减一,结果溢出时抛出ArithmeticException

Math.multiplyExact(); 返回两个参数之积,结果溢出时抛出ArithmeticException

Math.negateExact() ; 改变参数符号,结果溢出时抛出ArithmeticException

参考的原博客:https://blog.csdn.net/ZMYHH323/article/details/90112551?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.p

我把他的整理了一下,作为一名大学狗的学习资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值