我正在尝试做一个角度程序,计算输入角度的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的时候。所以在某个地方有计算错误?
发布于 2013-05-21 03:25:54
这只是一个舍入误差。这个数字真的很小,所以如果你把它四舍五入到小数点后14位,或者只输出前15位,它就等于0。从度数到弧度的转换并不精确,因此它给出的值非常接近于0,但不是很精确。由于显而易见的原因,您实际上不能存储PI的确切值。
https://stackoverflow.com/questions/15709701
复制相似问题