为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

繁星coding 2023-09-27 21:18:37
我应该得到 PI 和 PI/2 的余弦和正弦以及角度 0。除了 PI/2 的余弦之外,我的所有数字都是正确的。预期输出:Radians: (cos, sin)0.0: 1.0, 0.01.5707963267948966: 0.0, 1.03.141592653589793: -1.0, 0.0我的输出:Radians: (cos, sin)0.0: 1.0, 0.01.5707963267948966: 1.0, 1.03.141592653589793: -1.0, 0.0public 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) );    }}
查看完整描述

1 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

为什么它似乎可以使用两次,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

}


查看完整回答
反对 回复 2023-09-27
  • 1 回答
  • 0 关注
  • 55 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信