【问题标题】:Angle help in a JavaJava中的角度帮助
【发布时间】:2013-03-29 18:57:47
【问题描述】:

我正在尝试制作一个角度程序来计算输入角度的 sin、cos 和 tan。这是我遇到问题的代码部分:

c.println ("You have entered an angle of " + angle + " degrees.");
radians = Math.toRadians (angle); //convert angle (which is in degrees) to radians(radians = (pi / 180)*degrees)
c.println ("The angle in radians is " + radians); //diplay angle in radians

//caluclating sine, cosine & tangent

double sinx = Math.sin (Math.toRadians (angle));
c.println ("The sine of the angle is " + sinx+ " radians."); //convert sin of angle (in deg) to radians
double cosx = Math.cos (Math.toRadians (angle));
c.println ("The cosine of the angle is " + cosx+ " radians.");//convert cos of angle (in deg) to radians
double tanx = Math.tan (Math.toRadians (angle));
c.println ("The tangent of the angle is " + tanx+ " radians.");//convert tan of angle (in deg) to radians

如果我输入一个 90 度的角度,余弦输出是 6.12^-17.... 而它应该只是 = 0。所以某处有计算错误?

【问题讨论】:

  • There's an error in the code 是什么意思?
  • 你遇到了什么错误?
  • 什么是样本输入和输出?另外,请注意弧度是从 0-6.28...,而不是 0-2。
  • "The ... of the angle is ... radians." - 不确定打印信息是否不正确,或者您的理解是否正确,但角度的sin/cos/tan 不会返回以弧度为单位的数字. sincos[-1,1]tan[-Inf, Inf]。见this
  • 我试过并用我的计算器检查了它,它会输出所有应该的东西。我没有看到任何错误。

标签: java trigonometry angle


【解决方案1】:

这只是一个舍入错误。这个数字非常小,因此如果将其四舍五入到小数点后 14 位或仅输出前 15 位,它将等于 0。从度数到弧度的转换不准确,因此它给出的值非常接近 0,但不完全。由于显而易见的原因,您实际上无法存储 PI 的确切值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多