根據數學鎢: COS(50)=0.6427876096865394;Math.cos()給出錯誤的結果
但這個代碼在Java中:
System.out.println(Math.cos(50));
給0.9649660284921133。
java.lang.Math
有什麼問題?
根據數學鎢: COS(50)=0.6427876096865394;Math.cos()給出錯誤的結果
但這個代碼在Java中:
System.out.println(Math.cos(50));
給0.9649660284921133。
java.lang.Math
有什麼問題?
Math.cos()
期望參數以弧度表示。這將返回的結果,你需要:
Math.cos(Math.toRadians(50));
Math.cos()
使用弧度,所以讓你期望的結果,你需要做的
System.out.println(Math.cos(Math.toRadians(50)));
大部分Java三角函數都需要參數爲弧度。可以,請使用Math.toRadians()轉換:
System.out.println(Math.cos(Math.toRadians(50)));
我...
System.out.println(Math.cos(50));
System.out.println(Math.cos(new Double(50)));
System.out.println(Math.cos(Math.toRadians(50)));
System.out.println(Math.cos(Math.toRadians(new Double(50))));
回報
0.9649660284921133
0.9649660284921133
0.6427876096865394
0.6427876096865394
http://www.wolframalpha.com/input/?i=cos%2850deg%29
cos(50deg)
得到相同的結果。cos(50)
...所以鎢是默認的程度。
+1。僅供參考,因爲OP在談論wolfram http://www.wolframalpha.com/input/?i=cos%2850rad%29 –
Java中有Math.cos()與Degrees一起使用的版本嗎?我們經常需要記住手動將度數轉換爲弧度? –