猿问

我怎样才能正确舍入这个数字?

我应该得到 PI 和 PI/2 的余弦和正弦以及角度 0。除了 PI/2 的余弦之外,我的所有数字都是正确的。


预期输出:


Radians: (cos, sin)

0.0: 1.0, 0.0

1.5707963267948966: 0.0, 1.0

3.141592653589793: -1.0, 0.0

我的输出:


Radians: (cos, sin)

0.0: 1.0, 0.0

1.5707963267948966: 1.0, 1.0

3.141592653589793: -1.0, 0.0

public class UnitCircle 

{

    public static void main(String[] args)

    {

        System.out.println("Radians: (cos, sin)");


        double angle = 0.0;

        double piDivideTwo = Math.PI/2.0;

        double pi = Math.PI;


        System.out.println(angle + ": " + Math.cos(angle) + ", " + Math.sin(angle) );



        double cosine = Math.cos(piDivideTwo); 

        cosine = Math.round(cosine * 100) / 100.0;


        System.out.println(piDivideTwo + ": " + Math.cos(cosine) + ", " + Math.sin(piDivideTwo) );


        double sin = Math.sin(pi);

        sin = Math.round(sin *100) / 100.0;


        System.out.println(pi + ": " + Math.cos(pi) + ", " + Math.sin(sin) );

    }

}


繁星coding
浏览 83回答 1
1回答

海绵宝宝撒

为什么它似乎可以使用两次,cos或者sin只是因为它的价值0您只需要计算一次cos,sin,然后按格式打印,因为 PI/2 和 PI 不可能是完美值static void cosSin(double angle) {    double cos = Math.cos(angle);    double sin = Math.sin(angle);    System.out.printf("%.4f : %.1f %.1f \n", angle, cos, sin);}public static void main(String[] args) {    System.out.println("Radians: (cos, sin)");    double angle = 0.0;    double piDivideTwo = Math.PI / 2.0;    double pi = Math.PI;    cosSin(angle);        // 0,0000 :  1,0 0,0    cosSin(piDivideTwo);  // 1,5708 :  0,0 1,0    cosSin(pi);           // 3,1416 : -1,0 0,0}
随时随地看视频慕课网APP

相关分类

Java
我要回答