1. 程式人生 > >JAVA基礎知識之Math類

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;
    }