java 如何解决java中的cos 90问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5263653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to solve cos 90 problem in java?
提问by David King
I have some problems with calculating cosinus 90 in Java using Math.cos function :
我在使用 Math.cos 函数在 Java 中计算余弦 90 时遇到了一些问题:
public class calc{
private double x;
private double y;
public calc(double x,double y){
this.x=x;
this.y=y;
}
public void print(double theta){
x = x*Math.cos(theta);
y = y*Math.sin(theta);
System.out.println("cos 90 : "+x);
System.out.println("sin 90 : "+y);
}
public static void main(String[]args){
calc p = new calc(3,4);
p.print(Math.toRadians(90));
}
}
}
When I calculate cos90 or cos270, it gives me absurb values. It should be 0. I tested with 91 or 271, gives a near 0 which is correct.
当我计算 cos90 或 cos270 时,它给了我荒谬的值。它应该是 0。我用 91 或 271 进行了测试,结果接近 0,这是正确的。
what should I do to make the output of cos 90 = 0? so, it makes the output x = 0 and y = 4.
我该怎么做才能使 cos 90 = 0 的输出?因此,它使输出 x = 0 和 y = 4。
Thankful for advice
感谢您的建议
采纳答案by Ignacio Vazquez-Abrams
What you're getting is most likely very, very small numbers, which are being displayed in exponential notation. The reason you're getting them is because pi/2 is not exactly representable in IEEE 754 notation, so there's no way to get the exact cosine of 90/270 degrees.
您得到的很可能是非常非常小的数字,它们以指数表示法显示。你得到它们的原因是因为 pi/2 不能完全用 IEEE 754 表示法表示,所以没有办法得到 90/270 度的精确余弦。
回答by MicSim
Just run your source and it returns:
只需运行您的源代码,它就会返回:
cos 90 : 1.8369701987210297E-16
sin 90 : 4.0
That's absolutely correct. The first value is nearly 0. The second is 4 as expected.
这是完全正确的。第一个值接近于 0。第二个是 4,正如预期的那样。
3 * cos(90°) = 3 * 0 = 0
3 * cos(90°) = 3 * 0 = 0
Here you have to read the Math.toRadians()documentation which says:
在这里,您必须阅读Math.toRadians()文档,其中说:
Converts an angle measured in degrees to an approximately equivalent angle measured in radians. The conversion from degrees to radians is generally inexact.
将以度为单位的角度转换为以弧度为单位的近似等效的角度。从度数到弧度的转换通常是不准确的。
Update:You can use for example the MathUtils.round()method from the Apache Commons repository and round the output to say 8 decimals, like this:
更新:例如,您可以使用Apache Commons 存储库中的MathUtils.round()方法并将输出四舍五入为 8 位小数,如下所示:
System.out.println("cos 90 : " + MathUtils.round(x, 8));
That will give you:
这会给你:
cos 90 : 0.0
sin 90 : 4.0
回答by Eng.Fouad
Try this:
试试这个:
public class calc
{
private double x;
private double y;
public calc(double x,double y)
{
this.x=x;
this.y=y;
}
public void print(double theta)
{
if( ((Math.toDegrees(theta) / 90) % 2) == 1)
{
x = x*0;
y = y*Math.sin(theta);
}
else if( ((Math.toDegrees(theta) / 90) % 2) == 0)
{
x = x*Math.cos(theta);
y = y*0;
}
else
{
x = x*Math.cos(theta);
y = y*Math.sin(theta);
}
System.out.println("cos 90 : "+x);
System.out.println("sin 90 : "+y);
}
public static void main(String[]args)
{
calc p = new calc(3,4);
p.print(Math.toRadians(90));
}
}