3.4常用类(java学习笔记)Math和Random

一、Math

这个类包含执行指数、对数、平方根法、三角函数等基本的数字运算。

Math中还包含一些静态常量供我们调用。

如PI圆周率,E。

1.abs(),返回该数的绝对值。

public class Test {
    public static void main(String[] args) {
        System.out.println(Math.abs(1.0));
        System.out.println(Math.abs(-1));
        System.out.println(Math.abs(-0));
    }
}
运行结果:
1.0
1
0

正数的绝对值就是本事,负数的绝对值就是它的相反数,0的绝对值就是0;

Math类中有根据传入的参数类型不同,设置了多个abs函数用于接收不同的类型的参数,并返回对应类型的绝对值。

简而言之就是传递进去的参数是什么类型的,就返回对应类型的绝对值。

2.三角函数(sin,cos,tan)

使用三角函数我们需要角度和弧度之间转换。

三角函数无法直接传递角度进行计算,而是通过传递弧度进行计算。

可以直接自己直接计算,也可以通过调用toRadians()将角度转换为弧度。

源码中对toRadians()的描述:将以度数衡量的角度,转换为近似相等的以弧度衡量的角。

角度与弧度的转换通常是不精确的。

其实自己手动转换通常也是不精确的,在计算机的世界只有相对的准确,没有绝对的精准。

public class Test {
    public static void main(String[] args) {
        System.out.println(Math.sin(30 * (Math.PI / 180.0)));//1度 = PI / 180弧度, 
        System.out.println(Math.sin(Math.toRadians(30))); //其实toRadians内部也是这样转换的,
    }                                                     //传进去的参数作为角度,然后乘以PI / 180
}
运行结果:
0.49999999999999994 0.49999999999999994

sin30度应该是0.5,计算机算的有一定的误差。

剩下的cos 和tan类似。

3.反三角函数(asin(),acos() , atan())。

即三角函数的逆运算,已知值求代表该值的角度。例如sin30度 = 0.5

反三角即arcsin(0.5) = 30度 , arccos,arctan类似。

三角函数返回的值并不是角度,而是弧度所以还需要进行一次角度与弧度直接的转换。

这个也可以自己转换或调用toDegrees()将弧度转化为角度,这个转换也是不精准的。

public class Test {
    public static void main(String[] args) {
        System.out.println(Math.asin(0.5) / (Math.PI / 180.0));//将返回的弧度,转化为角度。
        System.out.println(Math.toDegrees(Math.asin(0.5)));   //通过toDgrees转换
    }
}
30.000000000000004
30.000000000000004

4.sqrt() 开平方

public class Test {
    public static void main(String[] args) {
        System.out.println(Math.sqrt(2));  // √2
        System.out.println(Math.sqrt(3));  // √3
        System.out.println(Math.sqrt(4));
    }
}
1.4142135623730951
1.7320508075688772
2.0

这个没什么好说的了,有兴趣的人可以了解下开平方的算法(一般用牛顿迭代法)。

还有一个有趣的代码,雷神之锤开平方代码中的神秘常数。

5.pow(double a, doubel b),求a^b

public class Test {
    public static void main(String[] args) {
        System.out.println(Math.pow(1, 100)); // 1^100
        System.out.println(Math.pow(2, 3));
        System.out.println(Math.pow(5, 2.5)); //5^2.5
    }
}
运行结果:
1.0 8.0 55.90169943749474

6.ceil()

我们来看下JDK源码中的解释:

返回一个最小值(括号部分没有翻译),它大于或等于(传递进去的)参数

而且它是一个整数。

看着可能有些绕口,我也觉得很绕口...

简而言之就是向上取整。向数值较大的整数靠齐。

也可以说返回在数轴正无穷方向上与参数距离最近的整数。

如果参数是整数,则返回本身。

public class Test {
    public static void main(String[] args) {
        System.out.println(Math.ceil(-1.2));//往数值较大的整数靠齐,-1 比 -1.2大
        System.out.println(Math.ceil(-0.5));//0比-0.5大。
        System.out.println(Math.ceil(1));
        System.out.println(Math.ceil(1.2));
        System.out.println(Math.ceil(1.5));
    }
}
运行结果:
-1.0
-0.0
1.0
2.0
2.0

7.floor()

这个刚好和ceil()相反,往数值较小的整数靠齐。

也可以说返回在数轴负无穷方向上与参数距离最近的整数。

如果参数为整数则返回本身。

public class Test {
    public static void main(String[] args) {
           System.out.println(Math.floor(-1.2));//往数值较小的整数靠齐,-2比 -1.2小
           System.out.println(Math.floor(-0.5));//0比-0.5大。
           System.out.println(Math.floor(1));
           System.out.println(Math.floor(1.2));
           System.out.println(Math.floor(1.5));    
    }
}
运行结果:
-2.0
-1.0
1.0
1.0
1.0

 8.round()

简单的说对于正数是四舍五入。

但对于负数是五舍六入,更准确的说应该是对于负数,先去掉负号当做正数,然后五舍六入,最后加上负号。

public class Test {
    public static void main(String[] args) {
        System.out.println(Math.round(-2.8)); //-3
        System.out.println(Math.round(-2.6)); //-3
        System.out.println(Math.round(-2.5)); //-2
        System.out.println(Math.round(-1.5)); //-1
        System.out.println(Math.round(-1.4)); //-1
        System.out.println(Math.round(-0.4)); // 0
        System.out.println(Math.round(0.4));  // 0
        System.out.println(Math.round(1.4));  //1
        System.out.println(Math.round(1.5));  //2
        System.out.println(Math.round(2.5));  //3
        System.out.println(Math.round(2.6));  //3
        System.out.println(Math.round(2.8));  //3
    }
}
运行结果:
-3
-3
-2
-1
-1
0
0
1
2
3
3
3

 还有一些方法就不一 一解释了,有兴趣的可以查阅文档或者看源码及其注释。

二、Random

可以产生随机数,其实计算机中也没有真的随机数。

1. nextInt()和nextInt(int bound)

nextInt()是直接生产int范围内的伪随机数,有2^32种可能。

nextInt(int bound) 生成 [0,boudn) 范围之间的伪随机数,包含0,不包含bound。

public class Test {
    public static void main(String[] args) {
           Random r = new Random();
           for(int i = 0; i < 3; i++){
               System.out.println(r.nextInt());//生成随机数
           }
           for(int i = 0; i < 3; i++){
               System.out.println(r.nextInt(10));//生成[0,10)之间的随机数,包含0,不包含10.
           }
    }
}
运行结果:
442673013
19803501
-388344272
0
2
7

2.nextfloat()与nextdouble()类似,只是返回值类型不同。

nextFloat()返回0.0f~1.0f之间的随机数。

nextDouble()返回0.0 ~ 1.0之间的随机数。(小数默认是double精度)

nextfloat()与nextdouble()源码里面两个方法的注释只写了取0.0~1.0之间的随机数,没有说两端的情况。

netxDouble()和nextFlaot()注释,0.0~1.0之间(between)

而对于一端包含一端不包含的情况nextInt(int boudn)有说明:

包含(inclusive)0 ,不包含(exclusive)指定值(bound) 

对于一端包含一端不包含的情况,注释应该会说明。所以注释没有明确说明的我认为是不包含的。

结上所述我认为(0.0,1.0)之间应该是不包含两端。

我在网上查了下,有说包含0.0不包含1.0的,对这种说法我持保留态度。

如果有了解的人士,烦请解惑,不胜感谢!

原文地址:https://www.cnblogs.com/huang-changfan/p/9519704.html