Trigonometric functions: Difference between revisions
(Ada example) |
(→{{header|Ada}}: Add output.) |
||
Line 48: | Line 48: | ||
New_Line; |
New_Line; |
||
end Trig; |
end Trig; |
||
Output: |
|||
<pre> |
|||
0.70711 0.70711 |
|||
0.70711 0.70711 |
|||
1.00000 1.00000 |
|||
1.00000 1.00000 |
|||
45.00000 0.78540 |
|||
45.00000 0.78540 |
|||
45.00000 0.78540 |
|||
45.00000 0.78540 |
|||
</pre> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
Revision as of 11:34, 6 January 2008
You are encouraged to solve this task according to the task description, using any language you may know.
If your language has a library or built-in functions for trigonometry, show examples of sine, cosine, tangent, and their inverses using the same angle in radians and degrees. If your language does not have trigonometric functions available or only has some available, write functions to calculate the functions based on any known approximation or identity.
Ada
Ada provides library trig functions which default to radians along with corresponding library functions for which the cycle can be specified. The examples below specify the cycle for degrees and for radians. The output of the inverse trig functions is in units of the specified cycle (degrees or radians).
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; with Ada.Float_Text_Io; use Ada.Float_Text_Io; with Ada.Text_Io; use Ada.Text_Io; procedure Trig is Degrees_Cycle : constant Float := 360.0; Radians_Cycle : constant Float := 2.0 * Ada.Numerics.Pi; Angle_Degrees : constant Float := 45.0; Angle_Radians : constant Float := Ada.Numerics.Pi / 4.0; begin Put(Item => Sin(Angle_Degrees, Degrees_Cycle), Aft => 5, Exp => 0); Put(" "); Put(Item => Sin(Angle_Radians, Radians_Cycle), Aft => 5, Exp => 0); New_Line; Put(Item => Cos(Angle_Degrees, Degrees_Cycle), Aft => 5, Exp => 0); Put(" "); Put(Item => Cos(Angle_Radians, Radians_Cycle), Aft => 5, Exp => 0); New_Line; Put(Item => Tan(Angle_Degrees, Degrees_Cycle), Aft => 5, Exp => 0); Put(" "); Put(Item => Tan(Angle_Radians, Radians_Cycle), Aft => 5, Exp => 0); New_Line; Put(Item => Cot(Angle_Degrees, Degrees_Cycle), Aft => 5, Exp => 0); Put(" "); Put(Item => Cot(Angle_Radians, Radians_Cycle), Aft => 5, Exp => 0); New_Line; Put(Item => ArcSin(Sin(Angle_Degrees, Degrees_Cycle), Degrees_Cycle), Aft => 5, Exp => 0); Put(" "); Put(Item => ArcSin(Sin(Angle_Radians, Radians_Cycle), Radians_Cycle), Aft => 5, Exp => 0); New_Line; Put(Item => Arccos(Cos(Angle_Degrees, Degrees_Cycle), Degrees_Cycle), Aft => 5, Exp => 0); Put(" "); Put(Item => Arccos(Cos(Angle_Radians, Radians_Cycle), Radians_Cycle), Aft => 5, Exp => 0); New_Line; Put(Item => Arctan(Y => Tan(Angle_Degrees, Degrees_Cycle), Cycle => Degrees_Cycle), Aft => 5, Exp => 0); Put(" "); Put(Item => Arctan(Y => Tan(Angle_Radians, Radians_Cycle), Cycle => Radians_Cycle), Aft => 5, Exp => 0); New_Line; Put(Item => Arccot(X => Cot(Angle_Degrees, Degrees_Cycle), Cycle => Degrees_Cycle), Aft => 5, Exp => 0); Put(" "); Put(Item => Arccot(X => Cot(Angle_Degrees, Degrees_Cycle), Cycle => Radians_Cycle), Aft => 5, Exp => 0); New_Line; end Trig;
Output:
0.70711 0.70711 0.70711 0.70711 1.00000 1.00000 1.00000 1.00000 45.00000 0.78540 45.00000 0.78540 45.00000 0.78540 45.00000 0.78540
Java
Java's Math 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 Math class also has a PI constant for easy conversion.
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 / 180)); //cosine System.out.println(Math.cos(radians) + " " + Math.cos(degrees * Math.PI / 180)); //tangent System.out.println(Math.tan(radians) + " " + Math.tan(degrees * Math.PI / 180)); //arcsine System.out.println(Math.asin(radians) + " " + Math.asin(degrees * Math.PI / 180)); //arccosine System.out.println(Math.acos(radians) + " " + Math.acos(degrees * Math.PI / 180)); //arctangent System.out.println(Math.atan(radians) + " " + Math.atan(degrees * Math.PI / 180)); } }