Java 中的 Math 方法

1. 常量

        // 自然數 e
        System.out.println("自然對數: " + Math.E);
        // 圓周率 PI
        System.out.println("圓周率: " + Math.PI);

在這裏插入圖片描述

2. 隨機數

        // 隨機數
        System.out.println("0 到 1 的隨機數" + Math.random());

在這裏插入圖片描述

3. 最值

        // 取兩個數的最大值
        System.out.println("最大值: " + Math.max(1, 2));
        // 取兩個數的最最小值
        System.out.println("最小值: " + Math.min(1, 2));

在這裏插入圖片描述

4. 和三角函數有關

        //  轉化爲角度
        System.out.println("圓周率轉化爲角度: " + Math.toDegrees(Math.PI));
        //  轉化爲弧度
        System.out.println("180 度轉化爲弧度 " + Math.toRadians(180));

        // 求 sin 值
        System.out.println("sin 0 = " + Math.sin(0));
        // 求 雙曲正弦 值
        System.out.println("sinh 0 = " + Math.sinh(0));
        // 求 asin 值 (單位爲弧度)
        System.out.println("asin 1 = " + Math.toDegrees(Math.asin(1)));
        // 求 cos 值
        System.out.println("cos 0 = " + Math.cos(0));
        // 求 雙曲餘弦 值
        System.out.println("cosh 0 = " + Math.cosh(0));
        // 求 acos 值 (單位爲弧度)
        System.out.println("acos 0 = " + Math.toDegrees(Math.acos(0)));
        // 求 tan 值
        System.out.println("tan PI/2 = " + Math.tan(Math.PI / 2));
        // 求 雙曲正切 值
        System.out.println("tanh 0 = " + Math.tanh(0));
        // 求 atan 值 (單位爲弧度)
        System.out.println("atan 1 = " + Math.toDegrees(Math.atan(1)));
        // 正切值爲兩個指定數字的商的角度
        System.out.println("atan2(e, e) = " + Math.toDegrees(Math.atan2(Math.E, Math.E)));

在這裏插入圖片描述

5. 和對數函數有關

        // 自然對數
        System.out.println("ln e = " + Math.log(Math.E));
        // 自然對數 中 (x + 1)
        System.out.println("ln 0 = " + Math.log1p(0));
        // 以 10 爲底對數
        System.out.println("log 10 = " + Math.log10(10));

在這裏插入圖片描述

6. 和指數以及冪函數有關

        // e 爲底的次方
        System.out.println("e 的 1 次方: " + Math.exp(1));
        // e 爲底的次方減去 1
        System.out.println("e 的 1 次方 - 1: " + Math.expm1(0));
        // 返回 float 或 double 的無偏指數
        System.out.println(Math.getExponent(1.0));
        // (第一個數) 的 (第二個數) 次方
        System.out.println("2 的 3 次方: " + Math.pow(2, 3));
        // 兩個數的平方和的平方根, 沒有中間溢出或下溢
        System.out.println("(3 * 3 + 4 * 4) 的平方根: " + Math.hypot(3, 4));
        // 返回 第一個數乘以 2 的第二個數的次方
        System.out.println("4 * 2 的 3 次方: " + Math.scalb(4, 3));
        
        // 平方根
        System.out.println("4 的平方根: " + Math.sqrt(4));
        // 立方根
        System.out.println("27 的立方根: " + Math.cbrt(27));

在這裏插入圖片描述

7. 與有等於有關

        // 四捨五入
        System.out.println("2.5 四捨五入: " + Math.round(2.5));
        // 四捨五入, 如果有 2 個數同樣接近,則返回偶數的那個
        System.out.println("2.5 不同於數學的四捨五入: " + Math.rint(2.5));

        // 返回相鄰的第一個參數的浮點數 (朝着第二個參數的方向)
        System.out.println("向 3 方向臨近於 1 的數: " + Math.nextAfter(1, 3));
        // 負方向基本上無限接近於該數
        System.out.println("負方向接近於 3: " + Math.nextDown(3));
        // 正方向基本上無限接近於該數
        System.out.println("正方向接近於 3: " + Math.nextUp(3));

        // 向上取整
        System.out.println("1.1 向上取整: " + Math.ceil(1.1));
        // 向下取整
        System.out.println("1.7 向下取整: " + Math.floor(1.7));
        // 第一個數除以第二個數結果向下取整
        System.out.println("3 / 2 向下取整: " + Math.floorDiv(3, 2));
        // 模運算 相當於 x - (floorDiv(x, y) * y)
        System.out.println("3 - (1 * 2) = " + Math.floorMod(3, 2));
        

在這裏插入圖片描述

8. 符號有關

        // 絕對值
        System.out.println("-11.5 得絕對值: " + Math.abs(-11.5));
        // 相反數
        System.out.println("1 的相反數: " + Math.negateExact(1));
        // 參數的符號函數; 零返回零; 參數大於零返回 1.0; 參數小於零返回 -1.0
        System.out.println("3 的符號: " + Math.signum(3));

在這裏插入圖片描述

9. 加減法有關

        // 兩個值相加
        System.out.println("2 + 5 = " + Math.addExact(2, 5));
        // 兩個數相減
        System.out.println("3 - 1 =" + Math.subtractExact(3, 1));
        // 返回的參數值減 1
        System.out.println("2 - 1 = " + Math.decrementExact(2));
        // 返回值參數值加 1
        System.out.println("0 + 1 = " + Math.incrementExact(0));
        // 返回來兩個數相乘
        System.out.println("1 * 3 = " + Math.multiplyExact(1, 3));
        // 返回兩個參數的確切乘積
        System.out.println("1 * 3 = " + Math.multiplyFull(1, 3));
        // 返回長度是兩個 64 位參數的 128 位乘積的最高有效 64 位
        System.out.println(Math.multiplyHigh(100000, 300000));

        // 符號拷貝 第一數的符號跟隨於第二數的符號
        System.out.println("1 的符號變成 -2 的符號: " + Math.copySign(1, -2));
        // 第一個數乘以第二個數加上第三個數
        System.out.println("3 * 5 + 5 = " + Math.fma(3, 5, 5));
        // IEEE 754 標準的兩個參數的餘數運算, dividend - (divisor * Math.Round(dividend / divisor))
        System.out.println("5 - (3 * 2) = " + Math.IEEEremainder(5, 3));

在這裏插入圖片描述

10. 轉型有關

        // 轉化爲 int 值返回
        System.out.println(Math.toIntExact(3));
        // 要返回其 ulp 的浮點值
        System.out.println(Math.ulp(3));

在這裏插入圖片描述

詳細過程可以參考: http://www.yiidian.com/ 或者官方.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章