每天記錄學習的新知識:Math總結

說明:

代碼中,常用到計算,總結一篇運算方法。

概括:

1. 向上取整,Math.ceil()

2. 向下取整,Math.floor()

3. 四捨五入,Math.round()

    @Test
    public void test() {
    	System.out.println("向上取整,Math.ceil()");
        System.out.println(Math.ceil(1));
        System.out.println(Math.ceil(1.4));
        System.out.println(Math.ceil(1.5));
        System.out.println(Math.ceil(-1.4));
        System.out.println(Math.ceil(-1.5));
        System.out.println(Math.ceil(1.444444));
        System.out.println(Math.ceil(-1.55555));
        1.0
		2.0
		2.0
		-1.0
		-1.0
		2.0
		-1.0
        System.out.println("*******************************************");
        System.out.println("向下取整,Math.floor()");
        System.out.println(Math.floor(1));
        System.out.println(Math.floor(1.4));
        System.out.println(Math.floor(1.5));
        System.out.println(Math.floor(-1.4));
        System.out.println(Math.floor(-1.5));
        System.out.println(Math.floor(1.444444));
        System.out.println(Math.floor(-1.55555));
        1.0
		1.0
		1.0
		-2.0
		-2.0
		1.0
		-2.0
        System.out.println("*******************************************");
        System.out.println("四捨五入,Math.round()");
        System.out.println(Math.round(1));
        System.out.println(Math.round(1.4));
        System.out.println(Math.round(1.5));
        System.out.println(Math.round(-1.4));
        System.out.println(Math.round(-1.5));
        System.out.println(Math.round(1.444444));
        System.out.println(Math.round(-1.55555));
        1
		1
		2
		-1
		-1
		1
		-2
		}

4. 圓周率,Math.PI

System.out.println(Math.PI);

3.141592653589793

5. e的常量,Math.E

System.out.println( Math.E);

2.718281828459045

6. 絕對值,Math.abs

在這裏插入圖片描述

        System.out.println( Math.abs(1));//int
        System.out.println( Math.abs(new Float(1.1)));
        System.out.println( Math.abs(1.1));//double
        System.out.println( Math.abs(new Long(821141242)));
        
        System.out.println( Math.abs(-1));//int
        System.out.println( Math.abs(new Float(-1.1)));
        System.out.println( Math.abs(-1.1));//double
        System.out.println( Math.abs(new Long(-821141242)));
        
        1
		1.1
		1.1
		821141242
		
		1
		1.1
		1.1
		821141242

7. 正弦函數,Math.sin

8. 反正弦函數, Math.asin

9. 餘弦函數,Math.cos

10. 反餘弦函數,Math.acos

11. 正切函數,Math.tan

12. 反正切函數, Math.atan

13. 商的反正切函數,Math.atan2

以上方法,參數是弧度制,如sin30° = Math.Sin(Math.PI*30.0/180.0);

正弦和餘弦,正切:

		函數sin和cos的值的範圍爲[-1,1]

        System.out.println(Math.sin(Math.PI * 30.0 / 180.0));
        System.out.println(Math.cos(Math.PI * 30.0 / 180.0));
		System.out.println(Math.tan(Math.PI * 30.0 / 180.0));
		
		0.49999999999999994 = 1/2
		0.8660254037844387 = 根號3/2
		0.5773502691896257 = 正弦/餘弦(0.49999999999999994/0.8660254037844387

0.8660254037844387 = 3/2\sqrt{3}/2 ,代碼中不會這麼寫,見諒。
0.5773502691896257 = 3/3\sqrt{3}/3

反正弦和反餘弦:

		略略略...

        System.out.println(Math.asin(Math.PI * 30.0 / 180.0));
        System.out.println(Math.acos(Math.PI * 30.0 / 180.0));
		0.5510695830994463
		1.0197267436954502

反正切和商的反正切:

		略略略...
        
        System.out.println(Math.atan(Math.PI * 30.0 / 180.0));
        System.out.println(Math.atan2(Math.PI * 30.0 / 180.0, Math.PI * 30.0 / 180.0));

		0.48234790710102493
		0.7853981633974483

14. 弧度轉化爲角度 ,Math.toDegrees

15. 角度轉化爲弧度,Math.toRadians

    @Test
    public void test() {
        System.out.println(Math.toDegrees(Math.PI * 30.0 / 180.0));
        System.out.println(Math.toRadians(30));
    }
	
	29.999999999999996  等於30°角
	0.5235987755982988	等於30*π/180,意思是30°的弧度。

16. 求餘,Math.IEEEremainder

IEEERemainder 函數遵守電氣和電子工程師協會於 1985 年制定的 IEEE 二進制浮點數算術標準、ANSI/IEEE 標準 754-1985 第 5.1 節中定義的餘數運算。(官方解釋)
<font color="#ff2233">官方給的介紹如下:x爲被除數,y爲除數。返回值等於 x - ( y Q),其中 Q 是 x / y 的商的最接近整數(如果 x / y 在兩個整數中間,則返回偶數)。</font>

如果 x - ( y Q) 爲零,則在 x 爲正時返回值 +0,而在 x 爲負時返回 -0。
如果 y = 0,則返回 NaN(非數字)。
因此這個函數計算的餘數肯能和我們平時理解的餘數有差異。
如果你要計算數學上的餘數的話,最好還是用%運算符。

[以上轉載自:CSDN論壇](https://bbs.csdn.net/topics/392037065)
  
        System.out.println(Math.IEEEremainder(5,0));
        System.out.println(Math.IEEEremainder(5,1));
        System.out.println(Math.IEEEremainder(5,2));
        System.out.println(Math.IEEEremainder(5,3));
        System.out.println(Math.IEEEremainder(5,4));
        System.out.println(Math.IEEEremainder(5,5));

		NaN
		0.0
		1.0
		-1.0
		1.0
		0.0

        System.out.println("**************************************");
        
        System.out.println(Math.IEEEremainder(5,6));
        System.out.println(Math.IEEEremainder(5,7));
        System.out.println(Math.IEEEremainder(5,8));
        System.out.println(Math.IEEEremainder(5,9));
        System.out.println(Math.IEEEremainder(5,10));
        System.out.println(Math.IEEEremainder(5,11));
        System.out.println(Math.IEEEremainder(5,12));
    
		-1.0
		-2.0
		-3.0
		-4.0
		5.0
		5.0
		5.0

17. 求餘,“%”

		System.out.println(new StringBuffer().append(5 % 0)); 錯誤,不可以除以0

		System.out.println(new StringBuffer().append(5 % 1));
        System.out.println(new StringBuffer().append(5 % 2));
        System.out.println(new StringBuffer().append(5 % 3));
        System.out.println(new StringBuffer().append(5 % 4));
        System.out.println(new StringBuffer().append(5 % 5));
        System.out.println(new StringBuffer().append(5 % 6));
        0
		1
		2
		1
		0
		5	
        System.out.println("*****************");
        
        System.out.println(new StringBuffer().append(5.0 % 1));
        System.out.println(new StringBuffer().append(5.0 % 2));
        System.out.println(new StringBuffer().append(5.0 % 3));
        System.out.println(new StringBuffer().append(5.0 % 4));
        System.out.println(new StringBuffer().append(5.0 % 5));
        System.out.println(new StringBuffer().append(5.0 % 6));
        0.0
		1.0
		2.0
		1.0
		0.0
		5.0
        System.out.println("*****************");
        
        System.out.println(new StringBuffer().append(5.1 % 1));
        System.out.println(new StringBuffer().append(5.1 % 2));
        System.out.println(new StringBuffer().append(5.1 % 3));
        System.out.println(new StringBuffer().append(5.1 % 4));
		
		0.099999999999999640.1
		1.09999999999999961.1
		2.09999999999999962.1
		1.09999999999999961.1	

18. 求兩數中最大,Math.max

19. 求兩數中最小,Math.min

在這裏插入圖片描述
在這裏插入圖片描述

        System.out.println(Math.max(1,2));
        System.out.println(Math.min(1,2));
        System.out.println(Math.max(1.0,2));
        2
		1
		2.0

20. 平方根,Math.sqrt,參數類型 double

21. 冪,Math.pow,參數類型 double

        System.out.println(Math.sqrt(2));
        System.out.println(Math.sqrt(1));
        System.out.println(Math.pow(2,2));
        1.4142135623730951
		1.0
		4.0

22. 求e的任意次方 ,Math.exp

        System.out.println(Math.exp(2));
        System.out.println(Math.pow(Math.E,2));

		7.38905609893065
		7.3890560989306495

23. 求e的任意次方減1( Ex - 1),Math.expm1

        System.out.println(Math.exp(2));
        System.out.println(Math.expm1(2));
        7.38905609893065
		6.38905609893065
        System.out.println(Math.exp(3));
        System.out.println(Math.expm1(3));
		20.085536923187668
		19.085536923187668

24. 以10爲底的對數,Math.log10

25. 自然對數,Math.log

[對數是什麼?](https://baike.baidu.com/item/%E5%AF%B9%E6%95%B0/91326)

如果a的x次方等於N(a>0,且a不等於1),那麼數x叫做以a爲底N的對數(logarithm),記作x=logaN。其中,a叫做對數的底數,N叫做真數。
        System.out.println(Math.log(100));
        System.out.println(Math.log10(100));
        4.605170185988092
		2.0
		
        System.out.println(Math.log(8)/Math.log(2));
        System.out.println(Math.log(16)/Math.log(2));
		3.0
		4.0

26. 返回最接近參數的整數,如果有2個數同樣接近,則返回偶數的那個,Math.rint

        System.out.println(Math.rint(1.5));
        System.out.println(Math.rint(2.5));
        System.out.println(Math.rint(3.5));
        System.out.println(Math.rint(4.5));
        System.out.println(Math.rint(5));
		2.0
		2.0
		4.0
		4.0
		5.0

        System.out.println(Math.round(1.5));
        System.out.println(Math.round(2.5));
        System.out.println(Math.round(3.5));
        System.out.println(Math.round(4.5));
        System.out.println(Math.round(5));
        2
		3
		4
		5
		5

27. 返回0,1之間的一個隨機數,Math.random

        System.out.println(Math.random());
        System.out.println(Math.random());
        System.out.println(Math.random());
		0.14288622780377047
		0.8350387473995642
		0.6085233355951529
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章