问题
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.
回答1:
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 ) ;
回答2:
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.
回答3:
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.
回答4:
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));
来源:https://stackoverflow.com/questions/17764525/converting-result-of-math-sinx-into-a-result-for-degrees-in-java