Math类常用API基础

Math类位于java.lang.Math
特点: 1.java.lang包不需要引入,系统会自动调用
2.Math方法都是静态方法 ----可以用Math.的方法直接调用

Math类常用API基础

一.常用常量

Math.PI;//返回值为double
Math.E;//返回值为double

二.三角函数

//60度的正弦 返回double
	System.out.println(Math.sin(Math.PI/3));
//60度的余弦 返回double
	System.out.println(Math.cos(Math.PI/3));
//60度的正切 返回double
	System.out.println(Math.tan(Math.PI/3));
//-1的反正弦函数 返回double
	System.out.println(Math.asin(-1));
//-1的反余弦函数 返回double
	System.out.println(Math.acos(-1));
//-1的反正切函数 返回double
	System.out.println(Math.atan(-1));
//0的双曲正弦 返回double
	System.out.println(Math.sinh(0));
//0的双曲余弦 返回double
	System.out.println(Math.cosh(0));
//0的双曲正切 返回double
	System.out.println(Math.tanh(0));

三.指数函数

//e的n方根 返回double
	System.out.println(Math.exp(2)); //e的平方
	System.out.println(Math.exp(3)); //e的三次方
//返回一个a的b次方 返回double
	System.out.println(Math.pow(3, 4)); //81.0
//返回a的平方根 返回double
	System.out.println(Math.sqrt(4)); //2.0
//返回a的立方根 返回double
	System.out.println(Math.cbrt(8)); //2.0

四.对数函数

//返回以e为底的 double对数值值。  返回double
	System.out.println(Math.log(Math.E*Math.E)); //2.0
//返回一个以10为底的double对数值。  返回double
	System.out.println(Math.log10(100)); //2.0

五.弧度制与角度值的转化

//将以度为单位转换为弧度制的近似等效角度(不准确)返回double
	System.out.println(Math.toRadians(90.0));
//将弧度制转换为以度为单位的近似等效角度(不准确) 返回double
	System.out.println(Math.toDegrees(Math.PI/3));

六.绝对值函数

//-4的绝对值,返回值与输入值的类型相同
	System.out.println(Math.abs(-4));

七.比较函数

//返回两个值中的较大值。 返回值与输入值的类型相同
	System.out.println(Math.max(50, 10.84)); //返回50.0 double
	
//返回两个值中的较小值。 返回值与输入值的类型相同
	System.out.println(Math.max(4.55, 5)); //返回5.0 double
	
/* 返回大于或等于参数的最小的double值。 特殊情况:  
如果参数为NaN或无穷大或正零或负零,则结果与参数相同。 
如果参数值小于零但大于-1.0,则结果为负零  -0.0。 */
	System.out.println(Math.ceil(-4.2)); //-4.0
	System.out.println(Math.ceil(-0.4)); //-0.0
	System.out.println(Math.ceil(+0.0)); //0.0
	System.out.println(Math.ceil(-0.0)); //-0.0
			
/* 返回小于或等于参数的最大double值
如果参数为NaN或无穷大或正零或负零,则结果与参数相同*/
	System.out.println(Math.floor(5.8));//5.0
	System.out.println(Math.floor(-4));//-4.0
	System.out.println(Math.floor(0.0));//0.0
	System.out.println(Math.floor(-0.0));//-0.0

八.两个数相加

//返回其参数的总和,如果结果溢出int(long),则抛出 int(long) 。
	System.out.println(Math.addExact(111111, 55888888));

九.四舍五入函数

Math.round(x)返回值为long!

本质Math.round(x) = (long) Math.floor(x + 0.5F)

	System.out.println(Math.round(15.5)); //16
	System.out.println(Math.round(-15.5));  //-15
	System.out.println(Math.round(-15.51));  //-16
	
	System.out.println(Math.floor(-15.5 + 0.5F)); //-15 >=  -15
	System.out.println(Math.floor(-15.51 + 0.5F)); //-15.01 >= -16
	
	
	double x =9.9984;
	int xx =(int) round(x);
//		int xx = round(x); 报错  因为Math.round()返回值为long型
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值