10

我想将Math.sin(x), where xin 弧度转换为一个结果,它会给我x以度而不是弧度。

我使用了普通方法和 java 内置的角度和弧度之间的转换方法,但是我传递给该Math.sin()方法的任何参数都被视为弧度,从而导致我的转换是徒劳的。

我希望给定 sin Input 的输出,就好像输入是以度数而不是像Math.sin()方法那样的弧度来处理的。

4

4 回答 4

21

Java 的Math库为您提供了在度数和弧度之间进行转换的方法:toRadianstoDegrees

public class examples
{
    public static void main(String[] args)
    {
         System.out.println( Math.toRadians( 180 ) ) ;
         System.out.println( Math.toDegrees( Math.PI ) ) ;
    }
}

If your input is in degrees, you need to convert the number going in to sin to radians:

double angle = 90 ;
double result  = Math.sin( Math.toRadians( angle ) ) ;
System.out.println( result ) ;
于 2013-07-20T17:07:14.337 回答
1

if your radian value is a, then multiply the radian value with (22/7)/180.

The code would be like this for the above situation:-

double rad = 45            // value in radians.
double deg ;
deg = rad * Math.PI/180;   // value of rad in degrees.
于 2013-07-20T17:18:13.487 回答
0

您可以像这样将弧度转换为度数:

double rad = 3.14159;
double deg = rad*180/Math.PI;

反之则将度数转换为弧度(乘以 pi/180)。您不能更改 Math.sin 的“输入法”(您不能告诉函数使用度数而不是弧度),您只能更改作为参数传递的内容。如果您希望程序的其余部分使用度数,则必须将其转换为弧度,尤其是对于 Math.sin()。换句话说,将度数乘以 pi 并除以 180。要将其与 Math.sin() 一起使用,只需将其转换为:

double angle = 90;    //90 degrees
double result = Math.sin(angle*Math.PI/180);

仅使用转换本身不会改变任何内容,您必须将转换后的值传递给 sin 函数。

于 2013-07-20T17:03:31.153 回答
-1

If you want to print sin(90) degree value, you can use this code:

double value = 90.0;
double radians = Math.toRadians(value);

System.out.format("The sine of %.1f degrees is %.4f%n", value, Math.sin(radians));
于 2017-06-07T13:07:31.040 回答