JAVA基础知识之Math类

一、JAVA工具类

        为了专门解决项目中的某些实际需求,JAVA的开发者们会提前定义了一些类用于给使用者们使用,这部分可以称为工具类

二、Math类

        JAVA中的Math类顾名思义是用于解决数学方面的问题的工具类

三、Math类定义

        使用final修饰代表该类不能被继承,没有子类不存在多态现象,所有该类声明变量都是指向该类

public final class Test2 
{
}

四、Math类构造方法

      使用private定义构造方法代表不能在其它类中通过new来实例化对象

private Test2() {}

五、Math类属性

            Math类中没有设置实例域,都是设置的静态常量,因此可以通过Math.常量名类获取常量

    public static final double E = 2.7182818284590452354;

    public static final double PI = 3.14159265358979323846;

    private static long negativeZeroFloatBits = Float.floatToIntBits(-0.0f);
    
    private static long negativeZeroDoubleBits = Double.doubleToLongBits(-0.0d);
    
    private static Random randomNumberGenerator;

六、Math类中的方法

        1.Math类中的方法都是静态方法,都可以通过Math.方法名来调用方法   

        2.结合此点考虑之所以设置构造器是private的目的应该是统一使用形式为类.方法和类.变量,而不是既有对象.方法又有类.方法的情况存在

  
    public static double sin(double a)
    {
        return StrictMath.sin(a); // default impl. delegates to StrictMath
    }

    public static double cos(double a)
    {
        return StrictMath.cos(a); // default impl. delegates to StrictMath
    }

    public static double tan(double a)
    {
        return StrictMath.tan(a); // default impl. delegates to StrictMath
    }

六、Math类常用的方法

1)向上取整--返回离参数最近的比参数大的整数,如传入0.2,返回1.0, 传入0.8,返回1.0,传入-1.2,返回-1.0

  public static double ceil(double a)
    {
        return StrictMath.ceil(a);
}

2)向下取整--返回离参数最近的比参数小的整数,如传入0.2,返回0.0,传入0.8返回1.0,传入-1.2,返回-2.0

    public static double floor(double a)
    {
        return StrictMath.floor(a);
    }

3)四舍五入制--等于是参数+0.5在进行向下取整 ,如传入0.2,返回0.0,传入0.8返回1.0,传入-1.2,返回-1.0

 public static int round(float a)
    {
        if (a != 0x1.fffffep-2f)
            return (int) floor(a + 0.5f);
        else
            return 0;
    }

    public static long round(double a)
    {
        if (a != 0x1.fffffffffffffp-2)
            return (long) floor(a + 0.5d);
        else
            return 0;
    }

4)取随机数--随机生成0-1之间的double型的值

public static double random()
    {
        Random rnd = randomNumberGenerator;
        if (rnd == null)
            rnd = initRNG();
        return rnd.nextDouble();
    }

5)求一个数的n次方--即求a的b次方

 public static double pow(double a, double b)
    {
        return StrictMath.pow(a, b);
    }

6)返回两数之间的最大者或者最小者

   public static int max(int a, int b)
    {
        return (a >= b) ? a : b;
    }

    public static long max(long a, long b)
    {
        return (a >= b) ? a : b;
    }

    public static float max(float a, float b)
    {
        if (a != a)
            return a; // a is NaN
        if ((a == 0.0f) && (b == 0.0f)
                && (Float.floatToIntBits(a) == negativeZeroFloatBits))
        {
            return b;
        }
        return (a >= b) ? a : b;
    }

    public static double max(double a, double b)
    {
        if (a != a)
            return a;
        if ((a == 0.0d) && (b == 0.0d)
                && (Double.doubleToLongBits(a) == negativeZeroDoubleBits))
        {
            return b;
        }
        return (a >= b) ? a : b;
    }

    public static int min(int a, int b)
    {
        return (a <= b) ? a : b;
    }

    public static long min(long a, long b)
    {
        return (a <= b) ? a : b;
    }

    public static float min(float a, float b)
    {
        if (a != a)
            return a;
        if ((a == 0.0f) && (b == 0.0f)
                && (Float.floatToIntBits(b) == negativeZeroFloatBits))
        {
            return b;
        }
        return (a <= b) ? a : b;
    }

    public static double min(double a, double b)
    {
        if (a != a)
            return a; // a is NaN
        if ((a == 0.0d) && (b == 0.0d)
                && (Double.doubleToLongBits(b) == negativeZeroDoubleBits))
        {
            return b;
        }
        return (a <= b) ? a : b;
    }



        

猜你喜欢

转载自blog.csdn.net/ai_bao_zi/article/details/80988747
今日推荐