[英]Possible loss of precision
我試圖在java中運行此代碼並獲得以下錯誤,需要int found double。
public class UseMath{
public static void main(String[]args){
int x = Math.pow (2,4);
System.out.println ("2 to the power of 4 is" + x);
}
}
如果您看一下文檔 ,它說, Math.pow()
需要兩個doubles
,並返回一個double
。 當你將ints傳遞給這個函數時,它意味着沒有壞處,因為將int
轉換(轉換)為double
意味着沒有損失。 但是當你將值賦給int
,就意味着它會失去精度。
只需這樣做:
int x = (int)Math.pow (2,4);
要么
double x = Math.pow (2,4);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.