Anonymous user
Trigonometric functions: Difference between revisions
m
→{{header|Java}}: Changed arc functions to use better arguments.
(→{{header|J}}: Note conversion from degrees.) |
m (→{{header|Java}}: Changed arc functions to use better arguments.) |
||
Line 121:
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.
public class Trig {
public static void main(String[] args) {
Line 133 ⟶ 134:
System.out.println(Math.tan(radians) + " " + Math.tan(degrees * Math.PI / 180));
//arcsine
double arcsin = Math.asin(Math.sin(radians));
System.out.println(arcsin + " " + arcsin * 180 / Math.PI);
//arccosine
double arccos = Math.acos(Math.cos(radians));
System.out.println(arccos + " " + arccos * 180 / Math.PI);
//arctangent
double arctan = Math.atan(Math.tan(radians));
System.out.println(arctan + " " + arctan * 180 / Math.PI);
}
Line 149 ⟶ 150:
0.7071067811865476 0.7071067811865476
0.9999999999999999 0.9999999999999999
0.7853981633974482 44.99999999999999
0.7853981633974483 45.0
0.7853981633974483 45.0
</pre>
|