Java Math toDegrees()

Overview
An angle measured in radians can be converted to an approximately equivalent angle in degrees using the toDegrees() method defined in the Math class.
- toDegrees() method returns NaN if the argument is NaN(Not a Number).
- toDegrees() method returns zero with the same sign as the argument if the argument is zero.
- toDegrees() method returns Infinity with the same sign as the input if the argument is Infinity.
Syntax of Math toDegrees()in Java
The syntax of the toDegrees() method is:
Parameters of Math toDegrees() in Java
The toDegrees() method takes only one parameter.
- angle: The angle in radians that needs to be changed to degrees.
Return Value of Math toDegrees() in Java
- Returns the angle measured in degrees
- Return type is double
Exceptions of Math toDegrees() in Java
Math.toDegrees() does NOT throw any exception.
Example
Note: To convert an angle in radians to degrees we simply multiply it with 180/π.
In this example we are going to see how to use/call Math.toDegrees() method in java.
Output:
Explanation:
- In above code we have simply converted angle in radian to angle in degrees using Math.toDegrees() method of the Math class.
- As we know, 3.14, which is roughly equal to π, is very close to 180 degrees.
What is Math toDegrees() in Java?
An angle measured in radians is converted to a roughly comparable angle measured in degrees using the Math.toDegrees() method.
- This method will return NaN if the argument is NaN
- This method will return zero with the same sign as the argument if the argument is zero.
- This method will return Infinity with the same sign as the input if the argument is Infinity.
More Examples
Example 1
In this example, we are going to see how to convert radians to degrees using toDegrees() method in Java:
Output:
Explanation:
- In the above Example, we have converted the angle in radians to degrees using toDegrees() method in Java.
- We have initialized x and y as 1.5708 and -0.0, and converted them into degrees.
Example 2
In this example, we are going to use toDegrees() method in java with NaN.
Output:
Explanation:
- As we have discussed above that toDegrees() will return NaN when a NaN is passed as argument.
Note: Square of a negative number is NaN in Java.
Example-3
In this example, we are going to see how to use Math.toDegrees() in java with Infinity.
Output:
Explanation:
- As we discussed above that this method will return Infinity with the same sign as the input if the argument is Infinity.
- So in the above code Infinity is printed when POSITIVE_INFINITY is passed and -Infinity is printed when NEGATIVE_INFINITY is passed.
Conclusion
- An angle measured in radians is converted to a roughly comparable angle measured in degrees using the toDegrees() method defined in Math class.
- This method will return NaN if the argument is NaN.
- This method will return zero with the same sign as the argument if the argument is zero.
- This method will return Infinity with the same sign as the argument if the argument is Infinity.