Trigonometric functions: Difference between revisions

From Rosetta Code
Content added Content deleted
m (<lang>)
Line 52: Line 52:
45.00000 0.78540
45.00000 0.78540
45.00000 0.78540
45.00000 0.78540
</pre>
=={{header|ALGOL 68}}==
{{trans|C}}

{{works with|ALGOL 68|Standard - no extensions to language used}}
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<pre>
main:(
REAL pi = 4 * arc tan(1);
# Pi / 4 is 45 degrees. All answers should be the same. #
REAL radians = pi / 4;
REAL degrees = 45.0;
REAL temp;
# sine #
print((sin(radians), " ", sin(degrees * pi / 180), new line));
# cosine #
print((cos(radians), " ", cos(degrees * pi / 180), new line));
# tangent #
print((tan(radians), " ", tan(degrees * pi / 180), new line));
# arcsine #
temp := arc sin(sin(radians));
print((temp, " ", temp * 180 / pi, new line));
# arccosine #
temp := arc cos(cos(radians));
print((temp, " ", temp * 180 / pi, new line));
# arctangent #
temp := arc tan(tan(radians));
print((temp, " ", temp * 180 / pi, new line))
)
</pre>
Output:
<pre>
+.707106781186548e +0 +.707106781186548e +0
+.707106781186548e +0 +.707106781186548e +0
+.100000000000000e +1 +.100000000000000e +1
+.785398163397448e +0 +.450000000000000e +2
+.785398163397448e +0 +.450000000000000e +2
+.785398163397448e +0 +.450000000000000e +2
</pre>
</pre>



Revision as of 09:32, 6 February 2009

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. For the non-inverse functions, each radian/degree pair should use arguments that evaluate to the same angle (that is, it's not necessary to use the same angle for all three regular functions as long as the two sine calls use the same angle). For the inverse functions, use the same number and convert its answer to 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). <lang ada> 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;
  procedure Put (V1, V2 : Float) is
  begin
     Put (V1, Aft => 5, Exp => 0);
     Put (" ");
     Put (V2, Aft => 5, Exp => 0);
     New_Line;
  end Put;

begin

  Put (Sin (Angle_Degrees, Degrees_Cycle), 
       Sin (Angle_Radians, Radians_Cycle));
  Put (Cos (Angle_Degrees, Degrees_Cycle), 
       Cos (Angle_Radians, Radians_Cycle));
  Put (Tan (Angle_Degrees, Degrees_Cycle), 
       Tan (Angle_Radians, Radians_Cycle));
  Put (Cot (Angle_Degrees, Degrees_Cycle), 
       Cot (Angle_Radians, Radians_Cycle));
  Put (ArcSin (Sin (Angle_Degrees, Degrees_Cycle), Degrees_Cycle), 
       ArcSin (Sin (Angle_Radians, Radians_Cycle), Radians_Cycle));
  Put (Arccos (Cos (Angle_Degrees, Degrees_Cycle), Degrees_Cycle), 
       Arccos (Cos (Angle_Radians, Radians_Cycle), Radians_Cycle));
  Put (Arctan (Y => Tan (Angle_Degrees, Degrees_Cycle)),
       Arctan (Y => Tan (Angle_Radians, Radians_Cycle)));
  Put (Arccot (X => Cot (Angle_Degrees, Degrees_Cycle)), 
       Arccot (X => Cot (Angle_Degrees, Degrees_Cycle)));

end Trig; </lang>

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

ALGOL 68

Translation of: C
Works with: ALGOL 68 version Standard - no extensions to language used
Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386
main:(
  REAL pi = 4 * arc tan(1);
  # Pi / 4 is 45 degrees. All answers should be the same. #
  REAL radians = pi / 4;
  REAL degrees = 45.0;
  REAL temp;
  # sine #
  print((sin(radians), " ", sin(degrees * pi / 180), new line));
  # cosine #
  print((cos(radians), " ", cos(degrees * pi / 180), new line));
  # tangent #
  print((tan(radians), " ", tan(degrees * pi / 180), new line));
  # arcsine #
  temp := arc sin(sin(radians));
  print((temp, " ", temp * 180 / pi, new line));
  # arccosine #
  temp := arc cos(cos(radians));
  print((temp, " ", temp * 180 / pi, new line));
  # arctangent #
  temp := arc tan(tan(radians));
  print((temp, " ", temp * 180 / pi, new line))
)

Output:

+.707106781186548e +0  +.707106781186548e +0
+.707106781186548e +0  +.707106781186548e +0
+.100000000000000e +1  +.100000000000000e +1
+.785398163397448e +0  +.450000000000000e +2
+.785398163397448e +0  +.450000000000000e +2
+.785398163397448e +0  +.450000000000000e +2

BASIC

Works with: QuickBasic version 4.5

QuickBasic 4.5 does not have arcsin and arccos built in. They are defined by identities found here. <lang qbasic> 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(SIN(radians)) * radians / (1 + SQR(1 - radians ^ 2)) PRINT arcsin + " " + arcsin * 180 / pi 'arccos arccos = 2 * ATN(COS(radians)) * SQR(1 - radians ^ 2) / (1 + radians) PRINT arccos + " " + arccos * 180 / pi PRINT ATN(TAN(radians)) + " " + ATN(TAN(radians)) * 180 / pi 'arctan </lang>

C

<lang c>

  1. include <math.h>
  2. include <stdio.h>

int main() {

 double pi = 4 * atan(1);
 /*Pi / 4 is 45 degrees. All answers should be the same.*/
 double radians = pi / 4;
 double degrees = 45.0;
 double temp;
 /*sine*/
 printf("%f %f\n", sin(radians), sin(degrees * pi / 180));
 /*cosine*/
 printf("%f %f\n", cos(radians), cos(degrees * pi / 180));
 /*tangent*/
 printf("%f %f\n", tan(radians), tan(degrees * pi / 180));
 /*arcsine*/
 temp = asin(sin(radians));
 printf("%f %f\n", temp, temp * 180 / pi);
 /*arccosine*/
 temp = acos(cos(radians));
 printf("%f %f\n", temp, temp * 180 / pi);
 /*arctangent*/
 temp = atan(tan(radians));
 printf("%f %f\n", temp, temp * 180 / pi);
 return 0;

} </lang>

Output:

0.707107 0.707107
0.707107 0.707107
1.000000 1.000000
0.785398 45.000000
0.785398 45.000000
0.785398 45.000000

D

Translation of: C

<lang d> import std.stdio, std.math ;

int main() {

 double Pi = 4.0 * atan(1.0); // in D math module, PI is a _real_ constant of p
 /*Pi / 4 is 45 degrees. All answers should be the same.*/
 double radians = Pi / 4.0;
 double degrees = 45.0;
 double temp;
 /*sine*/
 writef("%f %f\n", sin(radians), sin(degrees * Pi / 180.0));
 /*cosine*/
 writef("%f %f\n", cos(radians), cos(degrees * Pi / 180.0));
 /*tangent*/
 writef("%f %f\n", tan(radians), tan(degrees * Pi / 180.0));
 /*arcsine*/
 temp = asin(sin(radians));
 writef("%f %f\n", temp, temp * 180.0 / Pi);
 /*arccosine*/
 temp = acos(cos(radians));
 writef("%f %f\n", temp, temp * 180.0 / Pi);
 /*arctangent*/
 temp = atan(tan(radians));
 writef("%f %f\n", temp, temp * 180.0 / Pi);

 return 0;

} </lang>

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] )

Fortran

Trigonometic functions expect arguments in radians so degrees require conversion <lang fortran> PROGRAM Trig

 REAL pi, dtor, rtod, radians, degrees

 pi = 4.0 * ATAN(1.0)
 dtor = pi / 180.0
 rtod = 180.0 / pi
 radians = pi / 4.0
 degrees = 45.0 

 WRITE(*,*) SIN(radians), SIN(degrees*dtor)
 WRITE(*,*) COS(radians), COS(degrees*dtor)
 WRITE(*,*) TAN(radians), TAN(degrees*dtor)
 WRITE(*,*) ASIN(SIN(radians)), ASIN(SIN(degrees*dtor))*rtod
 WRITE(*,*) ACOS(COS(radians)), ACOS(COS(degrees*dtor))*rtod
 WRITE(*,*) ATAN(TAN(radians)), ATAN(TAN(degrees*dtor))*rtod

END PROGRAM Trig </lang> Output:

 0.707107   0.707107
 0.707107   0.707107
  1.00000    1.00000
 0.785398    45.0000
 0.785398    45.0000
 0.785398    45.0000

The following trigonometric functions are also available

ATAN2(y,x) ! Arctangent(y/x), -pi < result <= +pi 
SINH(x)    ! Hyperbolic sine
COSH(x)    ! Hyperbolic cosine
TANH(x)    ! Hyperbolic tangent

Haskell

Trigonometric functions use radians; degrees require conversion.

<lang haskell> fromDegrees deg = deg * pi / 180 toDegrees rad = rad * 180 / pi

example = [

 sin (pi / 6), sin (fromDegrees 30), 
 cos (pi / 6), cos (fromDegrees 30), 
 tan (pi / 6), tan (fromDegrees 30), 
 asin 0.5, toDegrees (asin 0.5),
 acos 0.5, toDegrees (acos 0.5),
 atan 0.5, toDegrees (atan 0.5)]

</lang>

IDL

deg = 35         ; arbitrary number of degrees
rad = !dtor*deg  ; system variables !dtor and !radeg convert between rad and deg
the trig functions receive and emit radians

print, rad, sin(rad), asin(sin(rad)) print, cos(rad), acos(cos(rad)) print, tan(rad), atan(tan(rad))  ; etc

prints the following
0.610865 0.573576 0.610865
0.819152 0.610865
0.700208 0.610865
the hyperbolic versions exist and behave as expected

print, sinh(rad)  ; etc

outputs
0.649572
If the input is an array, the output has the same dimensions etc as the input

x = !dpi/[[2,3],[4,5],[6,7]] ; !dpi is a read-only sysvar = 3.1415... print,sin(x)

outputs
1.0000000 0.86602540
0.70710678 0.58778525
0.50000000 0.43388374
the trig functions behave as expected for complex arguments

x = complex(1,2) print,sin(x)

outputs
( 3.16578, 1.95960)


J

The circle functions in J include trigonometric functions. Native operation is in radians, so values in degrees involve conversion.

Sine, cosine, and tangent of a single angle, indicated as pi-over-four radians and as 45 degrees:

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

Arcsine, arccosine, and arctangent of one-half, in radians and degrees:

   >,:([ , 180p_1&*)&.> (_1&o. ; _2&o. ; _3&o.) 0.5
0.523599      30
  1.0472      60
0.463648 26.5651

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.

<lang 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(Math.toRadians(degrees)));
               //cosine
               System.out.println(Math.cos(radians) + " " + Math.cos(Math.toRadians(degrees)));
               //tangent
               System.out.println(Math.tan(radians) + " " + Math.tan(Math.toRadians(degrees)));
               //arcsine
               double arcsin = Math.asin(Math.sin(radians));
               System.out.println(arcsin + " " + Math.toDegrees(arcsin));
               //arccosine
               double arccos = Math.acos(Math.cos(radians));
               System.out.println(arccos + " " + Math.toDegrees(arccos));
               //arctangent
               double arctan = Math.atan(Math.tan(radians));
               System.out.println(arctan + " " + Math.toDegrees(arctan));
       }

} </lang>

Output:

0.7071067811865475 0.7071067811865475
0.7071067811865476 0.7071067811865476
0.9999999999999999 0.9999999999999999
0.7853981633974482 44.99999999999999
0.7853981633974483 45.0
0.7853981633974483 45.0

JavaScript

JavaScript'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.

<lang javascript> //Pi / 4 is 45 degrees. All answers should be the same. var radians = Math.PI / 4; var degrees = 45.0; //sine window.alert(Math.sin(radians) + " " + Math.sin(degrees * Math.PI / 180)); //cosine window.alert(Math.cos(radians) + " " + Math.cos(degrees * Math.PI / 180)); //tangent window.alert(Math.tan(radians) + " " + Math.tan(degrees * Math.PI / 180)); //arcsine var arcsin = Math.asin(Math.sin(radians)); window.alert(arcsin + " " + (arcsin * 180 / Math.PI)); //arccosine var arccos = Math.acos(Math.cos(radians)); window.alert(arccos + " " + (arccos * 180 / Math.PI)); //arctangent var arctan = Math.atan(Math.tan(radians)); window.alert(arctan + " " + (arctan * 180 / Math.PI)); </lang>

UCB Logo has sine, cosine, and arctangent; each having variants for degrees or radians.

print sin 45
print cos 45
print arctan 1
make "pi (radarctan 0 1) * 2 ; based on quadrant if uses two parameters
print radsin :pi / 4
print radcos :pi / 4
print 4 * radarctan 1

OCaml

OCaml's preloaded Pervasives modules contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees. <lang ocaml> let pi = 4. *. atan 1.

let radians = pi /. 4. let degrees = 45.;;

Printf.printf "%f %f\n" (sin radians) (sin (degrees *. pi /. 180.));; Printf.printf "%f %f\n" (cos radians) (cos (degrees *. pi /. 180.));; Printf.printf "%f %f\n" (tan radians) (tan (degrees *. pi /. 180.));; let arcsin = asin (sin radians);; Printf.printf "%f %f\n" arcsin (arcsin *. 180. /. pi);; let arccos = acos (cos radians);; Printf.printf "%f %f\n" arccos (arccos *. 180. /. pi);; let arctan = atan (tan radians);; Printf.printf "%f %f\n" arctan (arctan *. 180. /. pi);; </lang> Output:

0.707107 0.707107
0.707107 0.707107
1.000000 1.000000
0.785398 45.000000
0.785398 45.000000
0.785398 45.000000

Perl

Works with: Perl version 5.8.8

<lang perl> 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"; $asin = asin(sin($angle_radians)); print $asin.' '.rad2deg($asin)."\n"; $acos = acos(cos($angle_radians)); print $acos.' '.rad2deg($acos)."\n"; $atan = atan(tan($angle_radians)); print $atan.' '.rad2deg($atan)."\n"; $acot = acot(cot($angle_radians)); print $acot.' '.rad2deg($acot)."\n"; </lang>

Output:

0.707106781186547 0.707106781186547
0.707106781186548 0.707106781186548
1 1
1 1
0.785398163397448 45
0.785398163397448 45
0.785398163397448 45
0.785398163397448 45

Pop11

Pop11 trigonometric functions accept both degrees and radians. In default mode argument is in degrees, after setting 'popradians' flag to 'true' arguments are in radians.

sin(30) =>
cos(45) =>
tan(45) =>
arcsin(0.7) =>
arccos(0.7) =>
arctan(0.7) =>
;;; switch to radians
true -> popradians;

sin(pi*30/180) =>
cos(pi*45/180) =>
tan(pi*45/180) =>
arcsin(0.7) =>
arccos(0.7) =>
arctan(0.7) =>

Python

Python's math module contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees. The math module also has degrees() and radians() functions for easy conversion.

<lang python> import math

radians = math.pi / 4 degrees = 45.0

  1. sine

print math.sin(radians), math.sin(math.radians(degrees))

  1. cosine

print math.cos(radians), math.cos(math.radians(degrees))

  1. tangent

print math.tan(radians), math.tan(math.radians(degrees))

  1. arcsine

arcsin = math.asin(math.sin(radians)) print arcsin, math.degrees(arcsin)

  1. arccosine

arccos = math.acos(math.cos(radians)) print arccos, math.degrees(arccos)

  1. arctangent

arctan = math.atan(math.tan(radians)) print arctan, math.degrees(arctan) </lang>

Output:

0.707106781187 0.707106781187
0.707106781187 0.707106781187
1.0 1.0
0.785398163397 45.0
0.785398163397 45.0
0.785398163397 45.0

Ruby

Ruby's Math module contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees.

<lang ruby> radians = Math::PI / 4 degrees = 45.0

def deg2rad(d)

 d * Math::PI / 180

end

def rad2deg(r)

 r * 180 / Math::PI

end

  1. sine

puts "#{Math.sin(radians)} #{Math.sin(deg2rad(degrees))}"

  1. cosine

puts "#{Math.cos(radians)} #{Math.cos(deg2rad(degrees))}"

  1. tangent

puts "#{Math.tan(radians)} #{Math.tan(deg2rad(degrees))}"

  1. arcsine

arcsin = Math.asin(Math.sin(radians)) puts "#{arcsin} #{rad2deg(arcsin)}"

  1. arccosine

arccos = Math.acos(Math.cos(radians)) puts "#{arccos} #{rad2deg(arccos)}"

  1. arctangent

arctan = Math.atan(Math.tan(radians)) puts "#{arctan} #{rad2deg(arctan)}" </lang>

Output:

0.707106781187 0.707106781187
0.707106781187 0.707106781187
1.0 1.0
0.785398163397 45.0
0.785398163397 45.0
0.785398163397 45.0

Scheme

<lang scheme> (define pi (* 4 (atan 1)))

(define radians (/ pi 4)) (define degrees 45)

(display (sin radians)) (display " ") (display (sin (* degrees (/ pi 180)))) (newline)

(display (cos radians)) (display " ") (display (cos (* degrees (/ pi 180)))) (newline)

(display (tan radians)) (display " ") (display (tan (* degrees (/ pi 180)))) (newline)

(define arcsin (asin (sin radians))) (display arcsin) (display " ") (display (* arcsin (/ 180 pi))) (newline)

(define arccos (acos (cos radians))) (display arccos) (display " ") (display (* arccos (/ 180 pi))) (newline)

(define arctan (atan (tan radians))) (display arctan) (display " ") (display (* arctan (/ 180 pi))) (newline) </lang>