Converting result of Math.sin(x) into a result for degrees in java

泪湿孤枕 提交于 2019-12-01 04:11:36

Java's Math library gives you methods to convert between degrees and radians: toRadians and toDegrees:

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 ) ;

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.

You can convert radians to degrees like this:

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

And the reverse to convert degrees to radians (multiply by pi/180). You can't change the "input method" for Math.sin (you can't tell the function to use degrees instead of radians), you can only change what you pass it as a parameter. If you want the rest of the program to work with degrees, you have to convert it to radians especially for Math.sin(). In other words, multiply the degrees value by pi and divide by 180. To use this with Math.sin(), just convert it like so:

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

Just using the conversion by itself doesn't change anything, you have to pass the converted value to the sin function.

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!