Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I would like to convert the Math.sin(x), where x in radians to a result which will give me as x in degrees not radians.

I have used the normal method and java built in method of conversion between degrees and radians, but any argument I pass to the Math.sin() method is being treated as radians, thereby causing my conversion to be a futile effort.

I want the output of a sin Input to be given as if the input is treated in degrees not radians like the Math.sin() method does.

like image 630
Omiye Jay Jay Avatar asked Jul 20 '13 17:07

Omiye Jay Jay


People also ask

Is math sin in radians or degrees Java?

The Math. sin() function returns the sine of a number in radians.

How do you calculate degrees in Java?

Java toDegrees() method with Examplelang. Math. toDegrees() is used to convert an angle measured in radians to an approximately equivalent angle measured in degrees. Note: The conversion from radians to degrees is generally inexact; users should not expect cos(toRadians(90.0)) to exactly equal 0.0.


1 Answers

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 ) ;
like image 106
Shafik Yaghmour Avatar answered Oct 21 '22 23:10

Shafik Yaghmour