java中的Math类

Math类

1、次方根方法

计算平方根 :Math.sqrt()
计算立方根 :Math.cbrt()
计算 (x的平方+y的平方)的平方根:Math.hypot(x,y)

public class Math_test {
    public static void main(String[] args) {
        int a=4,b=8,c=3;
        System.out.println(Math.sqrt(a));//a的平方根
        System.out.println(Math.cbrt(b));//b的立方根
        System.out.println(Math.hypot(a,c));//(a^2+c^2)的平方根
    }
}

2.0
2.0
5.0

2、次方方法

计算a的b次方 :Math.pow(a,b)
计算e^x的值:Math.exp(x)

public class Math_test {
    public static void main(String[] args) {
        int a=4,b=8,c=4,d=2;
        System.out.println(Math.pow(a,d));//a的d次方
        System.out.println(Math.exp(d));//e的d次方
    }
}

16.0
7.38905609893065

3、最大值最小值

计算最大值 :Math.max()
计算最小值:Math.min()

public class Math_test {
    public static void main(String[] args) {
        int a=4,b=8,c=4,d=2;
        System.out.println(Math.max(a,b));
        System.out.println(Math.min(c,d));
    }
}

8
2

4、求绝对值:Math.abs()
public class Math_test {
    public static void main(String[] args) {
        System.out.println(Math.abs(-4));//-4的绝对值
        System.out.println(Math.abs(4));
    }
}

4
4

5、返回大于等于( >= )给定参数的的最小整数,类型为双精度浮点型:Math.ceil
public class Math_test {
    public static void main(String[] args) {
        System.out.println(Math.ceil(11.5));
        System.out.println(Math.ceil(9.3));
        System.out.println(Math.ceil(-0.5));
        System.out.println(Math.ceil(-1.3));
    }
}

12.0
10.0
-0.0
-1.0

6、返回小于等于(<=)给定参数的最大整数 :
public class Math_test {
    public static void main(String[] args) {
        System.out.println(Math.floor(11.5));
        System.out.println(Math.floor(9.3));
        System.out.println(Math.floor(-0.5));
        System.out.println(Math.floor(-1.3));
    }
}

11.0
9.0
-1.0
-2.0

7、四舍五入:round(),rint()

rint:返回与参数最接近的整数。返回类型为double。

public class Math_test {
    public static void main(String[] args) {
        System.out.println(Math.rint(11.5));
        System.out.println(Math.rint(9.3));
        System.out.println(Math.rint(-0.5));
        System.out.println(Math.rint(-1.3));
    }
}

round:四舍五入,返回值为int

public class Math_test {
    public static void main(String[] args) {
        System.out.println(Math.round(11.5));
        System.out.println(Math.round(9.3));
        System.out.println(Math.round(-0.5));
        System.out.println(Math.round(-1.3));
    }
}

12
9
0
-1

【注】需要注意round和ceil、floor的区别,ceil是向上取整,floor是向下取整,round是原来的数字加上 0.5 后再向下取整,即Math.floor(x+0.5)

8、返回一个随机数:Math.random()

Math.random():0~1之间的随机数
Math.random()*100L:0~100之间的随机数

public class Math_test {
    public static void main(String[] args) {
        System.out.println(Math.random());
        System.out.println(Math.random()*10);
        System.out.println(Math.random()*100);
    }
}

0.4339010631732211
7.885143694519387
26.670248337989044

9、三角函数与常量π(PI)

sin()
cos()
tan()
。。。。

public class Math_test {
    public static void main (String []args)
    {
        System.out.println("90 度的正弦值:" + Math.sin(Math.PI/2));
        System.out.println("0度的余弦值:" + Math.cos(0));
        System.out.println("60度的正切值:" + Math.tan(Math.PI/3));
        System.out.println("1的反正切值: " + Math.atan(1));
        System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI/2));
        System.out.println(Math.PI);
    }
}

90 度的正弦值:1.0
0度的余弦值:1.0
60度的正切值:1.7320508075688767
1的反正切值: 0.7853981633974483
π/2的角度值:90.0
3.141592653589793

10、Math.nextUp()、Math.nextDown()、Math.nextAfter()

返回比a大一点点的浮点数:Math.nextUp(a)
返回比a小一点点的浮点数:Math.nextDown(a)
返回(a,b)或(b,a)间与a相邻的浮点数 b可以比a小:Math.nextAfter(a,b)

public class Math_test {
    public static void main(String[] args) {
        System.out.println(Math.nextUp(11.5));
        System.out.println(Math.nextDown(9.3));
        System.out.println(Math.nextAfter(-0.5,12));
        System.out.println(Math.nextAfter(5,1));
    }
}

11.500000000000002
9.299999999999999
-0.49999999999999994
4.9999995

Math类常用方法大全
Java Number & Math 类

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值