Math.cos() gives wrong result

試著忘記壹切 提交于 2019-11-27 14:34:14

Math.cos() expects the parameter to be in radians. This will return the result you need:

Math.cos(Math.toRadians(50));

Math.cos() uses radians, so to get your expected result you need to do

System.out.println(Math.cos(Math.toRadians(50)));

Most Java trigonometric functions expects parameters to be in radians. You can use Math.toRadians() to convert:

System.out.println(Math.cos(Math.toRadians(50)));

Degrees <> radians...........

For me...

System.out.println(Math.cos(50));
System.out.println(Math.cos(new Double(50)));
System.out.println(Math.cos(Math.toRadians(50)));
System.out.println(Math.cos(Math.toRadians(new Double(50))));

returns

0.9649660284921133
0.9649660284921133
0.6427876096865394
0.6427876096865394



http://www.wolframalpha.com/input/?i=cos%2850deg%29

cos(50deg) give same result as cos(50)... so Wolfram is degree by default.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!