Trigonometric functions: Difference between revisions
No edit summary |
m (Fixed lang tags.) |
||
Line 4: | Line 4: | ||
=={{header|Ada}}== |
=={{header|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). |
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> |
<lang ada>with Ada.Numerics.Elementary_Functions; |
||
with Ada.Numerics.Elementary_Functions; |
|||
use Ada.Numerics.Elementary_Functions; |
use Ada.Numerics.Elementary_Functions; |
||
with Ada.Float_Text_Io; use Ada.Float_Text_Io; |
with Ada.Float_Text_Io; use Ada.Float_Text_Io; |
||
Line 39: | Line 38: | ||
Put (Arccot (X => Cot (Angle_Degrees, Degrees_Cycle)), |
Put (Arccot (X => Cot (Angle_Degrees, Degrees_Cycle)), |
||
Arccot (X => Cot (Angle_Degrees, Degrees_Cycle))); |
Arccot (X => Cot (Angle_Degrees, Degrees_Cycle))); |
||
end Trig; |
end Trig;</lang> |
||
⚫ | |||
Output: |
Output: |
||
Line 61: | Line 59: | ||
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
||
<lang |
<lang algol68>main:( |
||
main:( |
|||
REAL pi = 4 * arc tan(1); |
REAL pi = 4 * arc tan(1); |
||
# Pi / 4 is 45 degrees. All answers should be the same. # |
# Pi / 4 is 45 degrees. All answers should be the same. # |
||
Line 83: | Line 80: | ||
temp := arc tan(tan(radians)); |
temp := arc tan(tan(radians)); |
||
print((temp, " ", temp * 180 / pi, new line)) |
print((temp, " ", temp * 180 / pi, new line)) |
||
⚫ | |||
) |
|||
⚫ | |||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 128: | Line 124: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
awk provides just these bare necessities for trigonometry: |
awk provides just these bare necessities for trigonometry: |
||
<lang awk>$ awk 'BEGIN{p4=3.14159/4;print cos(p4),sin(p4),atan2(1,1)}' |
|||
0.707107 0.707106 0.785398</lang> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
Line 152: | Line 148: | ||
=={{header|C}}== |
=={{header|C}}== |
||
<lang c> |
<lang c>#include <math.h> |
||
#include <math.h> |
|||
#include <stdio.h> |
#include <stdio.h> |
||
Line 179: | Line 174: | ||
return 0; |
return 0; |
||
⚫ | |||
} |
|||
⚫ | |||
Output: |
Output: |
||
Line 193: | Line 187: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
<lang cpp> |
<lang cpp>#include <iostream> |
||
#include <iostream> |
|||
#include <cmath> |
#include <cmath> |
||
Line 224: | Line 217: | ||
return 0; |
return 0; |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
Line 274: | Line 266: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<lang d> |
<lang d>import std.stdio, std.math ; |
||
import std.stdio, std.math ; |
|||
int main() { |
int main() { |
||
Line 300: | Line 291: | ||
return 0; |
return 0; |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|E}}== |
=={{header|E}}== |
||
Line 332: | Line 322: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<lang forth> |
<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] )</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Trigonometic functions expect arguments in radians so degrees require conversion |
Trigonometic functions expect arguments in radians so degrees require conversion |
||
<lang fortran> |
<lang fortran>PROGRAM Trig |
||
PROGRAM Trig |
|||
REAL pi, dtor, rtod, radians, degrees |
REAL pi, dtor, rtod, radians, degrees |
||
Line 361: | Line 350: | ||
WRITE(*,*) ATAN(TAN(radians)), ATAN(TAN(degrees*dtor))*rtod |
WRITE(*,*) ATAN(TAN(radians)), ATAN(TAN(degrees*dtor))*rtod |
||
END PROGRAM Trig |
END PROGRAM Trig</lang> |
||
</lang> |
|||
Output: |
Output: |
||
0.707107 0.707107 |
0.707107 0.707107 |
||
Line 371: | Line 359: | ||
0.785398 45.0000 |
0.785398 45.0000 |
||
The following trigonometric functions are also available |
The following trigonometric functions are also available |
||
<lang fortran> |
<lang fortran>ATAN2(y,x) ! Arctangent(y/x), ''-pi < result <= +pi'' |
||
SINH(x) ! Hyperbolic sine |
|||
COSH(x) ! Hyperbolic cosine |
|||
TANH(x) ! Hyperbolic tangent</lang> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Line 403: | Line 391: | ||
Trigonometric functions use radians; degrees require conversion. |
Trigonometric functions use radians; degrees require conversion. |
||
<lang haskell> |
<lang haskell>fromDegrees deg = deg * pi / 180 |
||
fromDegrees deg = deg * pi / 180 |
|||
toDegrees rad = rad * 180 / pi |
toDegrees rad = rad * 180 / pi |
||
Line 413: | Line 400: | ||
asin 0.5, toDegrees (asin 0.5), |
asin 0.5, toDegrees (asin 0.5), |
||
acos 0.5, toDegrees (acos 0.5), |
acos 0.5, toDegrees (acos 0.5), |
||
atan 0.5, toDegrees (atan 0.5)] |
atan 0.5, toDegrees (atan 0.5)]</lang> |
||
</lang> |
|||
=={{header|IDL}}== |
=={{header|IDL}}== |
||
⚫ | |||
<lang idl> |
|||
⚫ | |||
rad = !dtor*deg ; system variables !dtor and !radeg convert between rad and deg</lang> |
rad = !dtor*deg ; system variables !dtor and !radeg convert between rad and deg</lang> |
||
<lang idl>; the trig functions receive and emit radians: |
<lang idl>; the trig functions receive and emit radians: |
||
Line 468: | Line 453: | ||
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'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. |
||
<lang java> |
<lang java>public class Trig { |
||
public class Trig { |
|||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
//Pi / 4 is 45 degrees. All answers should be the same. |
//Pi / 4 is 45 degrees. All answers should be the same. |
||
Line 490: | Line 474: | ||
System.out.println(arctan + " " + Math.toDegrees(arctan)); |
System.out.println(arctan + " " + Math.toDegrees(arctan)); |
||
} |
} |
||
⚫ | |||
} |
|||
</lang> |
|||
Output: |
Output: |
||
Line 507: | Line 490: | ||
JavaScript'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. |
JavaScript'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. |
||
<lang javascript> |
<lang javascript>//Pi / 4 is 45 degrees. All answers should be the same. |
||
//Pi / 4 is 45 degrees. All answers should be the same. |
|||
var radians = Math.PI / 4; |
var radians = Math.PI / 4; |
||
var degrees = 45.0; |
var degrees = 45.0; |
||
Line 525: | Line 507: | ||
//arctangent |
//arctangent |
||
var arctan = Math.atan(Math.tan(radians)); |
var arctan = Math.atan(Math.tan(radians)); |
||
window.alert(arctan + " " + (arctan * 180 / Math.PI)); |
window.alert(arctan + " " + (arctan * 180 / Math.PI));</lang> |
||
</lang> |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
[[UCB Logo]] has sine, cosine, and arctangent; each having variants for degrees or radians. |
[[UCB Logo]] has sine, cosine, and arctangent; each having variants for degrees or radians. |
||
<lang logo> |
<lang logo>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</lang> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
Maxscript trigonometric functions accept degrees only. The built-ins degToRad and radToDeg allow easy conversion. |
Maxscript trigonometric functions accept degrees only. The built-ins degToRad and radToDeg allow easy conversion. |
||
< |
<lang maxscript>local radians = pi / 4 |
||
local degrees = 45.0 |
local degrees = 45.0 |
||
Line 560: | Line 541: | ||
--arctangent |
--arctangent |
||
print (atan (tan (radToDeg radians))) |
print (atan (tan (radToDeg radians))) |
||
print (atan (tan degrees))</ |
print (atan (tan degrees))</lang> |
||
=={{header|Metafont}}== |
=={{header|Metafont}}== |
||
Line 616: | Line 597: | ||
OCaml's preloaded <tt>Pervasives</tt> modules contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees. |
OCaml's preloaded <tt>Pervasives</tt> modules contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees. |
||
<lang ocaml> |
<lang ocaml>let pi = 4. *. atan 1. |
||
let pi = 4. *. atan 1. |
|||
let radians = pi /. 4. |
let radians = pi /. 4. |
||
Line 630: | Line 610: | ||
Printf.printf "%f %f\n" arccos (arccos *. 180. /. pi);; |
Printf.printf "%f %f\n" arccos (arccos *. 180. /. pi);; |
||
let arctan = atan (tan radians);; |
let arctan = atan (tan radians);; |
||
Printf.printf "%f %f\n" arctan (arctan *. 180. /. pi);; |
Printf.printf "%f %f\n" arctan (arctan *. 180. /. pi);;</lang> |
||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 758: | Line 737: | ||
Python's <tt>math</tt> module contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees. The <tt>math</tt> module also has <tt>degrees()</tt> and <tt>radians()</tt> functions for easy conversion. |
Python's <tt>math</tt> module contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees. The <tt>math</tt> module also has <tt>degrees()</tt> and <tt>radians()</tt> functions for easy conversion. |
||
<lang python> |
<lang python>import math |
||
import math |
|||
radians = math.pi / 4 |
radians = math.pi / 4 |
||
Line 778: | Line 756: | ||
#arctangent |
#arctangent |
||
arctan = math.atan(math.tan(radians)) |
arctan = math.atan(math.tan(radians)) |
||
print arctan, math.degrees(arctan) |
print arctan, math.degrees(arctan)</lang> |
||
</lang> |
|||
Output: |
Output: |
||
Line 820: | Line 797: | ||
Ruby's <tt>Math</tt> module contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees. |
Ruby's <tt>Math</tt> module contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees. |
||
<lang ruby> |
<lang ruby>radians = Math::PI / 4 |
||
radians = Math::PI / 4 |
|||
degrees = 45.0 |
degrees = 45.0 |
||
Line 846: | Line 822: | ||
#arctangent |
#arctangent |
||
arctan = Math.atan(Math.tan(radians)) |
arctan = Math.atan(Math.tan(radians)) |
||
puts "#{arctan} #{rad2deg(arctan)}" |
puts "#{arctan} #{rad2deg(arctan)}"</lang> |
||
</lang> |
|||
Output: |
Output: |
||
Line 861: | Line 836: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
<lang scheme> |
<lang scheme>(define pi (* 4 (atan 1))) |
||
(define pi (* 4 (atan 1))) |
|||
(define radians (/ pi 4)) |
(define radians (/ pi 4)) |
||
Line 898: | Line 872: | ||
(display " ") |
(display " ") |
||
(display (* arctan (/ 180 pi))) |
(display (* arctan (/ 180 pi))) |
||
(newline) |
(newline)</lang> |
||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
Revision as of 15:42, 22 November 2009
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
<lang algol68>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))
)</lang> 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
AutoHotkey
<lang AutoHotkey>pi := 4 * atan(1)
radians := pi / 4 degrees := 45.0 result .= "`n" . sin(radians) . " " . sin(degrees * pi / 180) result .= "`n" . cos(radians) . " " . cos(degrees * pi / 180) result .= "`n" . tan(radians) . " " . tan(degrees * pi / 180)
temp := asin(sin(radians)) result .= "`n" . temp . " " . temp * 180 / pi
temp := acos(cos(radians)) result .= "`n" . temp . " " . temp * 180 / pi
temp := atan(tan(radians)) result .= "`n" . temp . " " . temp * 180 / pi
msgbox % result /* output
trig.ahk
0.707107 0.707107 0.707107 0.707107 1.000000 1.000000 0.785398 45.000000 0.785398 45.000000 0.785398 45.000000
- /</lang>
AWK
awk provides just these bare necessities for trigonometry: <lang awk>$ awk 'BEGIN{p4=3.14159/4;print cos(p4),sin(p4),atan2(1,1)}' 0.707107 0.707106 0.785398</lang>
BASIC
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>#include <math.h>
- 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
C++
<lang cpp>#include <iostream>
- include <cmath>
- ifdef M_PI // defined by all POSIX systems and some non-POSIX ones
double const pi = M_PI;
- else
double const pi = 4*std::atan(1);
- endif
double const degree = pi/180;
int main() {
std::cout << "=== radians ===\n"; std::cout << "sin(pi/3) = " << std::sin(pi/3) << "\n"; std::cout << "cos(pi/3) = " << std::cos(pi/3) << "\n"; std::cout << "tan(pi/3) = " << std::tan(pi/3) << "\n"; std::cout << "arcsin(1/2) = " << std::asin(0.5) << "\n"; std::cout << "arccos(1/2) = " << std::acos(0.5) << "\n"; std::cout << "arctan(1/2) = " << std::atan(0.5) << "\n";
std::cout << "\n=== degrees ===\n"; std::cout << "sin(60°) = " << std::sin(60*degree) << "\n"; std::cout << "cos(60°) = " << std::cos(60*degree) << "\n"; std::cout << "tan(60°) = " << std::tan(60*degree) << "\n"; std::cout << "arcsin(1/2) = " << std::asin(0.5)/degree << "°\n"; std::cout << "arccos(1/2) = " << std::acos(0.5)/degree << "°\n"; std::cout << "arctan(1/2) = " << std::atan(0.5)/degree << "°\n";
return 0;
}</lang>
C#
<lang csharp>using System;
namespace RosettaCode {
class Program { static void Main(string[] args) { Console.WriteLine("=== radians ==="); Console.WriteLine("sin (pi/3) = {0}", Math.Sin(Math.PI / 3)); Console.WriteLine("cos (pi/3) = {0}", Math.Cos(Math.PI / 3)); Console.WriteLine("tan (pi/3) = {0}", Math.Tan(Math.PI / 3)); Console.WriteLine("arcsin (1/2) = {0}", Math.Asin(0.5)); Console.WriteLine("arccos (1/2) = {0}", Math.Acos(0.5)); Console.WriteLine("arctan (1/2) = {0}", Math.Atan(0.5)); Console.WriteLine(""); Console.WriteLine("=== degrees ==="); Console.WriteLine("sin (60) = {0}", Math.Sin(60 * Math.PI / 180)); Console.WriteLine("cos (60) = {0}", Math.Cos(60 * Math.PI / 180)); Console.WriteLine("tan (60) = {0}", Math.Tan(60 * Math.PI / 180)); Console.WriteLine("arcsin (1/2) = {0}", Math.Asin(0.5) * 180/ Math.PI); Console.WriteLine("arccos (1/2) = {0}", Math.Acos(0.5) * 180 / Math.PI); Console.WriteLine("arctan (1/2) = {0}", Math.Atan(0.5) * 180 / Math.PI);
Console.ReadLine(); } }
}</lang>
Common Lisp
<lang lisp>(defun deg->rad (x) (* x (/ pi 180))) (defun rad->deg (x) (* x (/ 180 pi)))
(mapc (lambda (x) (format t "~s => ~s~%" x (eval x)))
'((sin (/ pi 4)) (sin (deg->rad 45)) (cos (/ pi 6)) (cos (deg->rad 30)) (tan (/ pi 3)) (tan (deg->rad 60)) (asin 1) (rad->deg (asin 1)) (acos 1/2) (rad->deg (acos 1/2)) (atan 15) (rad->deg (atan 15))))</lang>
D
<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>
E
<lang e>def pi := (-1.0).acos()
def radians := pi / 4.0 def degrees := 45.0
def d2r := (pi/180).multiply def r2d := (180/pi).multiply
println(`$\ ${radians.sin()} ${d2r(degrees).sin()} ${radians.cos()} ${d2r(degrees).cos()} ${radians.tan()} ${d2r(degrees).tan()} ${def asin := radians.sin().asin()} ${r2d(asin)} ${def acos := radians.cos().acos()} ${r2d(acos)} ${def atan := radians.tan().atan()} ${r2d(atan)} `)</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
Forth
<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] )</lang>
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 <lang fortran>ATAN2(y,x) ! Arctangent(y/x), -pi < result <= +pi SINH(x) ! Hyperbolic sine COSH(x) ! Hyperbolic cosine TANH(x) ! Hyperbolic tangent</lang>
Groovy
Trig functions use radians, degrees must be converted to/from radians <lang groovy>def radians = Math.PI/4 def degrees = 45
def d2r = { it*Math.PI/180 } def r2d = { it*180/Math.PI }
println "sin(\u03C0/4) = ${Math.sin(radians)} == sin(45\u00B0) = ${Math.sin(d2r(degrees))}" println "cos(\u03C0/4) = ${Math.cos(radians)} == cos(45\u00B0) = ${Math.cos(d2r(degrees))}" println "tan(\u03C0/4) = ${Math.tan(radians)} == tan(45\u00B0) = ${Math.tan(d2r(degrees))}" println "asin(\u221A2/2) = ${Math.asin(2**(-0.5))} == asin(\u221A2/2)\u00B0 = ${r2d(Math.asin(2**(-0.5)))}\u00B0" println "acos(\u221A2/2) = ${Math.acos(2**(-0.5))} == acos(\u221A2/2)\u00B0 = ${r2d(Math.acos(2**(-0.5)))}\u00B0" println "atan(1) = ${Math.atan(1)} == atan(1)\u00B0 = ${r2d(Math.atan(1))}\u00B0"</lang>
Output:
sin(π/4) = 0.7071067811865475 == sin(45°) = 0.7071067811865475 cos(π/4) = 0.7071067811865476 == cos(45°) = 0.7071067811865476 tan(π/4) = 0.9999999999999999 == tan(45°) = 0.9999999999999999 asin(√2/2) = 0.7853981633974482 == asin(√2/2)° = 44.99999999999999° acos(√2/2) = 0.7853981633974484 == acos(√2/2)° = 45.00000000000001° atan(1) = 0.7853981633974483 == atan(1)° = 45.0°
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
<lang idl>deg = 35 ; arbitrary number of degrees rad = !dtor*deg ; system variables !dtor and !radeg convert between rad and deg</lang> <lang idl>; 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</lang>
<lang idl>; the hyperbolic versions exist and behave as expected: print, sinh(rad) ; etc
- outputs
- 0.649572</lang>
<lang idl>;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</lang>
<lang idl>; the trig functions behave as expected for complex arguments: x = complex(1,2) print,sin(x)
- outputs
- ( 3.16578, 1.95960)</lang>
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: <lang j> >,:(1&o. ; 2&o. ; 3&o.) (4%~o. 1), 180%~o. 45</lang>
0.707107 0.707107 0.707107 0.707107 1 1
Arcsine, arccosine, and arctangent of one-half, in radians and degrees: <lang j> >,:([ , 180p_1&*)&.> (_1&o. ; _2&o. ; _3&o.) 0.5</lang>
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>
Logo
UCB Logo has sine, cosine, and arctangent; each having variants for degrees or radians. <lang logo>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</lang>
MAXScript
Maxscript trigonometric functions accept degrees only. The built-ins degToRad and radToDeg allow easy conversion. <lang maxscript>local radians = pi / 4 local degrees = 45.0
--sine print (sin (radToDeg radians)) print (sin degrees) --cosine print (cos (radToDeg radians)) print (cos degrees) --tangent print (tan (radToDeg radians)) print (tan degrees) --arcsine print (asin (sin (radToDeg radians))) print (asin (sin degrees)) --arccosine print (acos (cos (radToDeg radians))) print (acos (cos degrees)) --arctangent print (atan (tan (radToDeg radians))) print (atan (tan degrees))</lang>
Metafont
Metafont has sind
and cosd
, which compute sine and cosine of an angle expressed in degree. We need to define the rest.
<lang metafont>Pi := 3.14159; vardef torad expr x = Pi*x/180 enddef; % conversions vardef todeg expr x = 180x/Pi enddef; vardef sin expr x = sind(todeg(x)) enddef; % radians version of sind vardef cos expr x = cosd(todeg(x)) enddef; % and cosd
vardef sign expr x = if x>=0: 1 else: -1 fi enddef; % commodity
vardef tand expr x = % tan with arg in degree
if cosd(x) = 0: infinity * sign(sind(x)) else: sind(x)/cosd(x) fi enddef;
vardef tan expr x = tand(todeg(x)) enddef; % arg in rad
% INVERSE
% the arc having x as tanget is that between x-axis and a line % from the center to the point (1, x); MF angle says this vardef atand expr x = angle(1,x) enddef; vardef atan expr x = torad(atand(x)) enddef; % rad version
% known formula to express asin and acos in function of % atan; a+-+b stays for sqrt(a^2 - b^2) (defined in plain MF) vardef asin expr x = 2atan(x/(1+(1+-+x))) enddef; vardef acos expr x = 2atan((1+-+x)/(1+x)) enddef;
vardef asind expr x = todeg(asin(x)) enddef; % degree versions vardef acosd expr x = todeg(acos(x)) enddef;
% commodity def outcompare(expr a, b) = message decimal a & " = " & decimal b enddef;
% output tests outcompare(torad(60), Pi/3); outcompare(todeg(Pi/6), 30);
outcompare(Pi/3, asin(sind(60))); outcompare(30, acosd(cos(Pi/6))); outcompare(45, atand(tand(45))); outcompare(Pi/4, atan(tand(45)));
outcompare(sin(Pi/3), sind(60)); outcompare(cos(Pi/4), cosd(45)); outcompare(tan(Pi/3), tand(60));
end</lang>
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
Octave
<lang octave>function d = degree(rad)
d = 180*rad/pi;
endfunction
r = pi/3; rd = degree(r);
funcs = { "sin", "cos", "tan", "sec", "cot", "csc" }; ifuncs = { "asin", "acos", "atan", "asec", "acot", "acsc" };
for i = 1 : numel(funcs)
v = arrayfun(funcs{i}, r); vd = arrayfun(strcat(funcs{i}, "d"), rd); iv = arrayfun(ifuncs{i}, v); ivd = arrayfun(strcat(ifuncs{i}, "d"), vd); printf("%s(%f) = %s(%f) = %f (%f)\n", funcs(i), r, strcat(funcs{i}, "d"), rd, v, vd); printf("%s(%f) = %f\n%s(%f) = %f\n", ifuncs{i}, v, iv, strcat(ifuncs{i}, "d"), vd, ivd);
endfor</lang>
Output:
sin(1.047198) = sind(60.000000) = 0.866025 (0.866025) asin(0.866025) = 1.047198 asind(0.866025) = 60.000000 cos(1.047198) = cosd(60.000000) = 0.500000 (0.500000) acos(0.500000) = 1.047198 acosd(0.500000) = 60.000000 tan(1.047198) = tand(60.000000) = 1.732051 (1.732051) atan(1.732051) = 1.047198 atand(1.732051) = 60.000000 sec(1.047198) = secd(60.000000) = 2.000000 (2.000000) asec(2.000000) = 1.047198 asecd(2.000000) = 60.000000 cot(1.047198) = cotd(60.000000) = 0.577350 (0.577350) acot(0.577350) = 1.047198 acotd(0.577350) = 60.000000 csc(1.047198) = cscd(60.000000) = 1.154701 (1.154701) acsc(1.154701) = 1.047198 acscd(1.154701) = 60.000000
(Lacking in this code but present in GNU Octave: sinh, cosh, tanh, coth and inverses)
Perl
<lang perl>use Math::Trig;
my $angle_degrees = 45; my $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"; my $asin = asin(sin($angle_radians)); print $asin, ' ', rad2deg($asin), "\n"; my $acos = acos(cos($angle_radians)); print $acos, ' ', rad2deg($acos), "\n"; my $atan = atan(tan($angle_radians)); print $atan, ' ', rad2deg($atan), "\n"; my $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
Perl 6
<lang perl6>say sin(pi/3), ' ', sin 60, 'd'; # 'g' (gradians) and 1 (circles) say cos(pi/4), ' ', cos 45, 'd'; # are also recognized. say tan(pi/6), ' ', tan 30, 'd';
say asin(sqrt(3)/2), ' ', asin sqrt(3)/2, 'd'; say acos(1/sqrt 2), ' ', acos 1/sqrt(2), 'd'; say atan(1/sqrt 3), ' ', atan 1/sqrt(3), 'd';</lang>
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.
<lang pop11>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) =></lang>
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
- sine
print math.sin(radians), math.sin(math.radians(degrees))
- cosine
print math.cos(radians), math.cos(math.radians(degrees))
- tangent
print math.tan(radians), math.tan(math.radians(degrees))
- arcsine
arcsin = math.asin(math.sin(radians)) print arcsin, math.degrees(arcsin)
- arccosine
arccos = math.acos(math.cos(radians)) print arccos, math.degrees(arccos)
- 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
R
<lang R>deg <- function(radians) 180*radians/pi rad <- function(degrees) degrees*pi/180 sind <- function(ang) sin(rad(ang)) cosd <- function(ang) cos(rad(ang)) tand <- function(ang) tan(rad(ang)) asind <- function(v) deg(asin(v)) acosd <- function(v) deg(acos(v)) atand <- function(v) deg(atan(v))
r <- pi/3 rd <- deg(r)
print( c( sin(r), sind(rd)) ) print( c( cos(r), cosd(rd)) ) print( c( tan(r), tand(rd)) )
S <- sin(pi/4) C <- cos(pi/3) T <- tan(pi/4)
print( c( asin(S), asind(S) ) ) print( c( acos(C), acosd(C) ) ) print( c( atan(T), atand(T) ) )</lang>
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
- sine
puts "#{Math.sin(radians)} #{Math.sin(deg2rad(degrees))}"
- cosine
puts "#{Math.cos(radians)} #{Math.cos(deg2rad(degrees))}"
- tangent
puts "#{Math.tan(radians)} #{Math.tan(deg2rad(degrees))}"
- arcsine
arcsin = Math.asin(Math.sin(radians)) puts "#{arcsin} #{rad2deg(arcsin)}"
- arccosine
arccos = Math.acos(Math.cos(radians)) puts "#{arccos} #{rad2deg(arccos)}"
- 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>
Tcl
The built-in functions only take radian arguments. <lang tcl>package require Tcl 8.5
proc PI {} {expr {4*atan(1)}} proc deg2rad d {expr {$d/180*[PI]}} proc rad2deg r {expr {$r*180/[PI]}}
namespace path ::tcl::mathfunc
proc trig degrees {
set radians [deg2rad $degrees] puts [sin $radians] puts [cos $radians] puts [tan $radians] set arcsin [asin [sin $radians]]; puts "$arcsin [rad2deg $arcsin]" set arccos [acos [cos $radians]]; puts "$arccos [rad2deg $arccos]" set arctan [atan [tan $radians]]; puts "$arctan [rad2deg $arctan]"
} trig 60.0</lang>
0.8660254037844386 0.5000000000000001 1.7320508075688767 1.0471975511965976 59.99999999999999 1.0471975511965976 59.99999999999999 1.0471975511965976 59.99999999999999