在计算中缺乏精度sin cos&tan(java)

时间:2018-10-07 16:44:51

标签: 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

我该如何解决?

1 个答案:

答案 0 :(得分:1)

这不是缺乏精度,这是因为您正在将度数传递给需要弧度的函数。使用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