Trigonometric functions: Difference between revisions

→‎{{header|Java}}: Now uses Math functions to convert to and from degrees and radians, syntax highlighting
m (More works with's)
(→‎{{header|Java}}: Now uses Math functions to convert to and from degrees and radians, syntax highlighting)
Line 107:
Java's <tt>Math</tt> class contains all six functions and is automatically included as part of the language. The functions all accept radians only, so conversion is necessary when dealing with degrees. The <tt>Math</tt> class also has a <tt>PI</tt> constant for easy conversion.
 
<java>public class Trig {
public static void main(String[] args) {
//Pi / 4 is 45 degrees. All answers should be the same.
double radians = Math.PI / 4;
double degrees = 45.0;
//sine
System.out.println(Math.sin(radians) + " " + Math.sin(degrees * Math.PI / 180toRadians(degrees)));
//cosine
System.out.println(Math.cos(radians) + " " + Math.cos(degrees * Math.PI / 180toRadians(degrees)));
//tangent
System.out.println(Math.tan(radians) + " " + Math.tan(degrees * Math.PI / 180toRadians(degrees)));
//arcsine
double arcsin = Math.asin(Math.sin(radians));
System.out.println(arcsin + " " + arcsin * 180 / Math.PItoDegrees(arcsin));
//arccosine
double arccos = Math.acos(Math.cos(radians));
System.out.println(arccos + " " + arccos * 180 / Math.PItoDegrees(arccos));
//arctangent
double arctan = Math.atan(Math.tan(radians));
System.out.println(arctan + " " + arctan * 180 / Math.PItoDegrees(arctan));
}
}</java>
}
 
Output:
Anonymous user