我正在使用Math.pow()方法,並且得到的結果與我從計算器獲得的結果不同。Java math.pow()返回錯誤答案
我使用的代碼:
public static int getSum(int a, int b, int c, int d){
return (int)(d*Math.pow(16.0,6.0)+c*Math.pow(16.0,4.0)+b*Math.pow(16.0,2.0)+a);
}
印刷
getSum(0,0,128,191)
打印出2147483647 雖然谷歌計算器顯示3212836864.
是什麼原因造成的?
哦,我忘了提及,2147483647是一個大屁股素數。這是人類直到1867年才知道的最大素數 – Anton