我是Java新手。我想打印出每15度間隔的sin
,cos
和tan
值。Java:使用`Math.sin()時可能會失去精度`
public class W3Aufgabe5 {
public static void main(String[] args) {
System.out.printf("%6s%6s%6s%6s%n", "Angle", "Sin", "Cos", "Tan");
System.out.println("------------------------");
for (float angle = 0; angle <= 180; angle += 15) {
float sin = Math.sin(Math.toRadians(angle));
float cos = Math.cos(Math.toRadians(angle));
float tan = Math.tan(Math.toRadians(angle));
System.out.printf("%6d%6d%6d%6d%n", angle, sin, cos, tan);
}
}
}
每Math.x()
線,編譯器打印
error: possible loss of precision
required: float
found: double
我真的不明白。爲什麼需要double
,即使我一直在使用float
?
你爲什麼會犯罪?除非你有實際的理由使用float,否則只需使用double。圖像處理或圖形。 – matt