Trigonometric functions: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Java}}: Changed the arc functions to print the answer in degrees and radians rather than use two different angles.)
(→‎{{header|J}}: Note conversion from degrees.)
Line 91: Line 91:


=={{header|J}}==
=={{header|J}}==
The [http://www.jsoftware.com/help/dictionary/dodot.htm circle functions] in J include trigonometric functions. Here the same program is presented twice, first using only primaries, then using named definitions for components.
The [http://www.jsoftware.com/help/dictionary/dodot.htm circle functions] in J include trigonometric functions. They accept radians, so conversion from degrees is required.

Here the same program is presented twice, first using only primaries, then using named definitions for components.
>,:(1&o. ; 2&o. ; 3&o. ; _1&o. ; _2&o. ; _3&o.) (4%~o. 1), 180 %~ o. 45
>,:(1&o. ; 2&o. ; 3&o. ; _1&o. ; _2&o. ; _3&o.) (4%~o. 1), 180 %~ o. 45

Revision as of 05:06, 9 January 2008

Task
Trigonometric functions
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

BASIC

Compiler: QuickBasic 4.5

QuickBasic 4.5 does not have arcsin and arccos built in. They are defined by identities found here.

pi = 3.141592653589793#
radians = pi / 4 'a.k.a. 45 degrees
degrees = 45 * pi / 180 'convert 45 degrees to radians once
PRINT SIN(radians) + " " + SIN(degrees) 'sine
PRINT COS(radians) + " " + COS(degrees) 'cosine
PRINT TAN(radians) + " " + TAN (degrees) 'tangent
'arcsin
arcsin = 2 * ATN * radians / (1 + SQR(1 - radians ^ 2))
PRINT arcsin + " " + arcsin * 180 / pi
'arccos
arccos = 2 * ATN * SQR(1 - radians ^ 2) / (1 + radians)
PRINT arccos + " " + arccos * 180 / pi
PRINT ATN(radians) + " " + ATN (radians) * 180 / pi 'arctan

Forth

45e  pi f* 180e f/	\ radians

cr fdup  fsin f.	\ also available: fsincos ( r -- sin cos )
cr fdup  fcos f.
cr fdup  ftan f.
cr fdup fasin f.
cr fdup facos f.
cr      fatan f.	\ also available: fatan2 ( r1 r2 -- atan[r1/r2] )

J

The circle functions in J include trigonometric functions. They accept radians, so conversion from degrees is required.

Here the same program is presented twice, first using only primaries, then using named definitions for components.

   >,:(1&o. ; 2&o. ; 3&o. ; _1&o. ; _2&o. ; _3&o.) (4%~o. 1), 180 %~ o. 45

   NB. ------------- program above is equivalent to program below -------------
   sine    =: 1&o.
   cosine  =: 2&o.
   tangent =: 3&o.
   arcsin  =: _1&o.
   arccos  =: _2&o.
   arctan  =: _3&o.

   columns=: >@,:     NB. Arrange output to look like other examples
   rfd=: 180 %~ o.    NB. Radians from degrees
   AD=: 45            NB. Angle, in degrees
   AR=: (o. 1)%4      NB. Same angle, in radians (pi divided by four)

   columns (sine; cosine; tangent; arcsin; arccos; arctan) AR, rfd AD

Output (from either version of the program):

0.707107 0.707107
0.707107 0.707107
       1        1
0.903339 0.903339
0.667457 0.667457
0.665774 0.665774

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
		double arcsin = Math.asin(radians);
		System.out.println(arcsin + " " + arcsin * 180 / Math.PI);
		//arccosine
		double arccos = Math.acos(radians);
		System.out.println(arccos + " " + arccos * 180 / Math.PI);
		//arctangent
		double arctan = Math.atan(radians);
		System.out.println(arctan + " " + arctan * 180 / Math.PI);
	}
}

Output:

0.7071067811865475 0.7071067811865475
0.7071067811865476 0.7071067811865476
0.9999999999999999 0.9999999999999999
0.9033391107665127 0.9033391107665127
0.6674572160283838 0.6674572160283838
0.6657737500283538 0.6657737500283538

Perl

Interpreter: Perl 5.8.8

use Math::Trig;

$angle_degrees = 45;
$angle_radians = pi / 4;

print sin($angle_radians).' '.sin(deg2rad($angle_degrees))."\n";
print cos($angle_radians).' '.cos(deg2rad($angle_degrees))."\n";
print tan($angle_radians).' '.tan(deg2rad($angle_degrees))."\n";
print cot($angle_radians).' '.cot(deg2rad($angle_degrees))."\n";
print asin($angle_radians).' '.asin(deg2rad($angle_degrees))."\n";
print acos($angle_radians).' '.acos(deg2rad($angle_degrees))."\n";
print atan($angle_radians).' '.atan(deg2rad($angle_degrees))."\n";
print acot($angle_radians).' '.acot(deg2rad($angle_degrees))."\n";

Output:

0.707106781186547 0.707106781186547
0.707106781186548 0.707106781186548
1 1
1 1
0.903339110766513 0.903339110766513
0.667457216028384 0.667457216028384
0.665773750028354 0.665773750028354
0.905022576766543 0.905022576766543