[英]Possible loss of precision error
我有一個問題,我正在制作一個三角形類,並在一行代碼中說int [] y coord,我得到了這個編譯器錯誤,該錯誤表明可能會損失精度,因此int找到了double,但我試圖添加平方根(3 )/ 2到我的get()y,所以這不是兩倍嗎?
感謝幫助。
import java.awt.*;
public class Triangle extends Shape {
private int leng;
public Triangle(int x, int y, Color color, int leng) {
super(x, y, color);
this.leng=leng;
}
public void draw(Graphics g) {
int[]Xcoord={getX(),getX()+leng,getX()+leng/2};
int[]Ycoord={getY(),getY(),getY()+Math.sqrt(3)/(2.0)};
g.drawPolygon(Xcoord,Ycoord,3);
}
public int getHeight() {
return leng;
}
public int getWidth() {
return leng;
}
}
3的平方根不能表示為整數,因為它不是整數。 使數組成為double[]
:
double[] Xcoord = { getX(), getX() + leng, getX() + leng / 2.0};
double[] Ycoord = { getY(), getY(), getY() + Math.sqrt(3) / (2.0)};
順便說一下,正如我在給出的代碼中所修改的那樣,建議使用空格來提高可讀性。
您的公式也不正確。 單獨使用double不能解決問題:
double [] Xcoord = { getX(), (getX()+leng), getX()+leng/2 );
double [] Ycoord = { getY(), getY(), getY()*(1.0+Math.sqrt(3)/(2.0)) };
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.