0

我正在尝试制作一个角度程序来计算输入角度的 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。所以某处有计算错误?

4

1 回答 1

-1

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

于 2013-05-20T19:25:54.123 回答