计算缺乏精确性 sin cos & tan (java)



当我运行这段代码时

System.out.println( Math.cos(8.8485279795) ); // should be : 0.988098452127
System.out.println( Math.sin(23.437101628) ); // should be :0.397742095267
System.out.println( Math.tan(52.2166666666667) ); // should be : 1.289966871548
System.out.println( Math.cos(23.437101628) ); // should be :0.917497261932
System.out.println( Math.sin(8.8485279795) ); //should be : 0.153822784089

输出是这样的;

-0.8385118249144639
-0.9922171619691422
-2.500855651260892
-0.12451949041776937
0.5448833998926885

我该如何解决这个问题?

这不是缺乏精度,而是因为您将度数传递给需要弧度的函数。使用Math.toRadians(double)进行修复。喜欢

System.out.println(Math.cos(Math.toRadians(8.8485279795)));
System.out.println(Math.sin(Math.toRadians(23.437101628)));
System.out.println(Math.tan(Math.toRadians(52.2166666666667)));
System.out.println(Math.cos(Math.toRadians(23.437101628)));
System.out.println(Math.sin(Math.toRadians(8.8485279795)));

我得到(如你所料(

0.9880984521266581
0.39774209526711635
1.2899668715484742
0.9174972619318948
0.1538227840890365

最新更新