Java常用类(一)Math类和Random类

一、Math类

 Math类中有一些常用的数学函数,比较简单,不进行详细解释,仅举例说明:

1.绝对值和取整

import java.lang.Math;
public class Mat {
    public static void main(String []args)
    {
        //abs()返回绝对值
        System.out.println(Math.abs(-2.5));

        //ceil()返回较大的整数
        System.out.println(Math.ceil(-2.6));
        System.out.println(Math.ceil(2.3));

        //floor()返回较大的整数
        System.out.println(Math.floor(-2.6));
        System.out.println(Math.floor(2.8));

    }

}

几个要素:Math类在java.lang包中;Math类中基本都是静态方法,不可声明类对象。

结果:

2.5
-2.0
3.0
-3.0
2.0

abs返回同参数类型,floor和ceil参数和返回值均为double类型。

2.四舍五入

//        //round()返回四舍五入值 等价floor(x+0.5)
        System.out.println(Math.round(-11.5));
        System.out.println(Math.round(-11.501));
        System.out.println(Math.round(11.499));
        System.out.println(Math.round(11.5));

        //rint()四舍五入 0.5返回偶数
        System.out.println(Math.rint(3.5));
        System.out.println(Math.round(3.5));
        System.out.println(Math.rint(4.5));
        System.out.println(Math.round(4.5));

-11
-12
11
12
4.0
4
4.0
5

rint()参数是double型,返回的也是double型;round()参数为float返回int型,参数为double型返回long型

3.随机数

        //random()返回[0.0,1.0)之间的随机数
        System.out.println(Math.random());
        //random()返回[0.0,100.0)之间的随机数
        System.out.println(Math.random()*100);
        //random()返回[2.0,100.0)之间的随机数
        System.out.println(2+(Math.random()*98));
        //random()返回[2.0,100.0)之间的整数
        System.out.println((int)(2+(Math.random()*98)));

 

4.最大最小值

        //max()返回两个数中较大的,min()返回较小的数字
        System.out.println(Math.max(2,3));
        System.out.println(Math.min(-2,-3));
        //max和min不仅适用于int,还适用float,double,long
        System.out.println(Math.max(2.5,2.55));
        System.out.println(Math.min(-2.3,-3));
        //实现三个数
        System.out.println(Math.max(2.5,Math.max(-3,2.6)));

5.三角函数

        System.out.println(Math.sin(Math.PI));
        System.out.println(Math.cos(Math.PI));
        System.out.println(Math.tan(Math.PI/4));

可以看出结果并不准确,这是因为PI和E在Math中的定义是:

    /**
     * The {@code double} value that is closer than any other to
     * <i>e</i>, the base of the natural logarithms.
     */
    public static final double E = 2.7182818284590452354;

    /**
     * The {@code double} value that is closer than any other to
     * <i>pi</i>, the ratio of the circumference of a circle to its
     * diameter.
     */
    public static final double PI = 3.14159265358979323846;

此外还有反三角函数asin(),acos(),atan()以及双曲三角函数sinh,cosh,tanh; toDegrees弧度转角度,toRadians角度转弧度。

6.指数对数

        //指数 exp(x) - e^x
        System.out.println(Math.exp(1)-Math.E);
        //指数 expm1(x) - e^x-1
        System.out.println(Math.expm1(2)+1-Math.exp(2));
        //指数 pow(x,y) - e^x-1
        System.out.println(Math.pow(2,3));
        //对数 log(x) - ln(x)
        System.out.println(Math.log(Math.E));
        //对数 log10(x) - lg(x)
        System.out.println(Math.log10(100));
        //对数 log1p(x) - ln(x+1)
        System.out.println(Math.log1p(0));
        //指数 scalb(x,y) - x*2^y
        System.out.println(Math.scalb(12,2));

scalb第二个参数只能是整数,第一个是float和double类型

7.开平方立方

        // sqrt(x) 开平方
        System.out.println(Math.sqrt(16.1));

        //cbrt(x) 开立方
        System.out.println(Math.cbrt(64.2));

        //hypot(x,y) - x^2+y^2
        System.out.println(Math.hypot(3,4));

 

8.符号相关

        //signum(x) x>0返回1.0;x<0返回-1.0;x=0返回0.0
                System.out.println(Math.signum(0));
        System.out.println(Math.signum(-0));
        System.out.println(Math.signum(-0.0));
        System.out.println(Math.signum(-0.1));

        //copySign(x,y)将x的绝对值配上y的符号
        System.out.println(Math.copySign(-12.63,0));
        System.out.println(Math.copySign(12.63,-0));
        System.out.println(Math.copySign(12.63,-0.0));

经过我自己的实验,可以看出,在java中0和-0均视为0整数处置,而0.0和-0.0却有正负差异。

 二、Random类

除了Math类可以生成随机数之外,还有更加专业的Random类可以使用,Random有两种构造方法,分别是无参构造函数,以long为参数的构造方法。即:

Random r1=new Random();
Random r2=new Random(long seed);

 等下我们在介绍两种构造函数的区别,在此之前要先介绍产生随机数的方法,想必还记得包装类,除了Character、Short、Byte之外,所有类型都可以使用Random类产生:

nextInt()      //产生一个随机整数
nextInt(int n) //产生一个大于等于0小于n的整数
nextFloat()    //产生一个浮点数
nextDouble()   //
nextLong()     //产生一个长整数
nextBoolean()  //产生一个布尔值
    public static void main(String []args)
    {
        Random r= new Random();
        for (int i=0;i<10;i++)
        System.out.println(r.nextInt(100)); //参数为负抛出IllegalArgumentException

    }

第一次运行结果:   第二次运行结果:

 

                                  

也即没有种子作为构造参数,那么每次产生数都不尽相同。

重新试一下有种子的Random类构造参数:

每次产生的数据就一样了。

那么产生随机负数怎么办,加上负号就好了啊。(咦,怎么绿了?)

 

posted @ 2019-06-21 23:10  LeftBody  阅读(850)  评论(0编辑  收藏  举报