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 ) ;
Answer from Shafik Yaghmour on Stack OverflowJava'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.
Videos
First of all, you should read the javadoc. sin(double) takes a double in parameter which is the angle in radians like said in the documentation. You'll also find on the linked page that sqrt takes a double as well.
Then, you should know that java can perform non-destructive conversion automatically. So if a method takes a double and you have a long, it will be no problem, since there's no loss in the conversion long -> double. The reverse is false, so Java refuse ton compile.
For the radians conversion, you'll find a toRadians method in the Math class.
- If you have value in degrees, just do
degrees * Math.PI / 180. Or better use function suggested by coobird in the comment (didn't know of it). - Yes, you can pass any double value in it. (Any number, for that matter.)
- No, both functions take
doubleparameter. Check the docs.