软件构造Lab1-Java Math类方法总结

本文综合参考了以下几篇文章,加上自己的理解和实践,总结如下:
https://blog.csdn.net/ZMYHH323/article/details/90112551
https://blog.csdn.net/qq_15128547/article/details/50588988
https://blog.csdn.net/xuexiangjys/article/details/79849888


        //常量
        Math.E;                        //e:2.718281828459045	
        Math.PI;                        //π:3.141592653589793


        //符号相关
		Math.abs(12.3);					//12.3 返回这个数的绝对值
		Math.abs(-12.3);				//12.3
		
		Math.copySign(1.23, -12.3);		//-1.23,返回第一个参数的量值和第二个参数的符号
		Math.copySign(-12.3, 1.23);		//12.3
		
		Math.signum(x);					//符号:如果x大于0则返回1.0,小于0则返回-1.0,等于0则返回0
		Math.signum(12.3);				//1.0
		Math.signum(-12.3);				//-1.0
		Math.signum(0);					//0.0

        Math.negateExact(100);          //-100:改变参数符号,结果溢出时抛出ArithmeticException
		
		
		//指数、幂函数
		Math.exp(x);					//e的x次幂
		Math.expm1(x);					//e的x次幂 - 1
		
        Math.pow(x, y);					//x的y次幂
		Math.pow(2.0, 3.0);					//即2³ 即返回:8.0

		Math.scalb(x, y);				//x*(2的y次幂)
		Math.scalb(12.3, 3);			//12.3*2³
      

        //开根号
		Math.sqrt(x);					//√(x) x的二次方根
		Math.sqrt(9);					//3.0  
		Math.sqrt(16);					//4.0
        
        //x和y平方和的二次方根
		Math.hypot(x, y);				//常用于求两点间距离:√(x²+y²)
        Math.hypot(3.0, 4.0);			//5.0
		
		//开三次方
		Math.cbrt(27.0);				//3 
		Math.cbrt(-125.0);				//-5
		
		//对数函数 (参数和返回值都只能是double型)
		Math.log(e);					//1 以e为底的对数
		Math.log10(100);				//10 以10为底的对数
		Math.log1p(x);					//ln(x + 1)


		//取整
		Math.ceil(12.3);				//上整:返回最近的且大于这个数的整数13.0
		Math.ceil(-12.3);				//-12.0
		
		Math.floor(12.3);				//下整:返回最近的且小于这个数的整数12.0
		Math.floor(-12.3);				//-13.0

        //返回最接近这个数的整数,如果.5,则取偶数  返回double
		Math.rint(12.3);				//12.0 
		Math.rint(-12.3);				//-12.0
		Math.rint(78.9);				//79.0
		Math.rint(-78.9);				//-79.0
		Math.rint(35.5);				//36.0
        Math.rint(34.5);				//34.0
		
        //四舍五入:参数是float时返回int,double时返回long
		Math.round(12.3);				//12
        Math.round(34.5);				//35

        // <注意:这里所有进位的方法的参数都要保证是float或double类型>
        Math.ceil(130/100);             //1.0  :错误的使用
        Math.ceil((float)130/100);      //2.0  :正确的使用
 
	
		//返回较大值和较小值
		Math.max(x, y);					//返回x、y中较大的那个数
		Math.min(x, y);					//返回x、y中较小的那个数
		
		//返回随机数(伪)
		Math.random();			        //随机返回[0,1)之间的无符号double值	
        Math.random() * 2;              //随机返回[0,2)之间的无符号double值
        Math.random() * 2 + 1;          //随机返回[1,3)之间的无符号double值

        //返回计算机中刚好比参数大一点的浮点数
        Math.nextUp(1.2);               //1.2000000000000002
        //返回计算机中刚好比参数小一点的浮点数
        Math.nextDown(1.2);             //1.1999999999999997
		

		//三角函数
		Math.sin(α);					//sin(α)的值
		Math.cos(α);					//cos(α)的值
		Math.tan(α);					//tan(α)的值
		
		//求角:反三角
		Math.asin(x/z);					//返回弧度值[-π/2,π/2]  arc sin(x/z)
		Math.acos(y/z);					//返回弧度值[0~π]   arc cos(y/z)
		Math.atan2(y-y0, x-x0); 		//返回弧度(-π, π]   返回点A(x0,y0)与B(x,y)连线AB与x轴正向夹角。
		
		Math.sinh(x);					//双曲正弦函数sinh(x)=(exp(x) - exp(-x)) / 2.0;
		Math.cosh(x);					//双曲余弦函数cosh(x)=(exp(x) + exp(-x)) / 2.0;
		Math.tanh(x);					//tanh(x) = sinh(x) / cosh(x);
		
		//角度弧度互换
		Math.toDegrees(angrad);			//弧度转换成角度,返回:angrad * 180d / PI
		
		Math.toRadians(angdeg);			//角度转换成弧度,返回:angdeg / 180d * PI


附:对于atan2函数的理解:

  • double atan2(double y, double x)
    返回点O(0,0)与B(x,y)连线OB与x轴正向夹角,注意函数atan2(y,x)中参数的顺序是倒置的。

  • double atan2(y - y0, x - x0);
    返回点A(x0,y0)与B(x,y)连线AB与x轴正向夹角。

单位是弧度,取值范围为(-π, π],(也就是说可以覆盖90°以及1~4象限)结果为正表示从 X 轴逆时针旋转的角度,结果为负表示从 X 轴顺时针旋转的角度(顺负逆正)。
常使用Math.toDegrees(Math.atan2(double y, double x));

Math.toDegrees(Math.atan2(1,1));           //45.0
Math.toDegrees(Math.atan2(1 - 0, 0 - 1));  //135.0
Math.toDegrees(Math.atan2(0-1,(-1)-0));    //-135.0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值