Trigonometric functions: Difference between revisions
Added Easylang
m (→{{header|REXX}}: optimized a function.) |
(Added Easylang) |
||
(40 intermediate revisions by 29 users not shown) | |||
Line 16:
If your language does not have trigonometric functions available or only has some available, write functions to calculate the functions based on any [[wp:List of trigonometric identities|known approximation or identity]].
<br><br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">V rad = math:pi / 4
V deg = 45.0
print(‘Sine: ’sin(rad)‘ ’sin(radians(deg)))
print(‘Cosine: ’cos(rad)‘ ’cos(radians(deg)))
print(‘Tangent: ’tan(rad)‘ ’tan(radians(deg)))
V arcsine = asin(sin(rad))
print(‘Arcsine: ’arcsine‘ ’degrees(arcsine))
V arccosine = acos(cos(rad))
print(‘Arccosine: ’arccosine‘ ’degrees(arccosine))
V arctangent = atan(tan(rad))
print(‘Arctangent: ’arctangent‘ ’degrees(arctangent))</syntaxhighlight>
{{out}}
<pre>
Sine: 0.707106781 0.707106781
Cosine: 0.707106781 0.707106781
Tangent: 1 1
Arcsine: 0.785398163 45
Arccosine: 0.785398163 45
Arctangent: 0.785398163 45
</pre>
=={{header|ACL2}}==
Line 21 ⟶ 46:
(This doesn't have the inverse functions; the Taylor series for those take too long to converge.)
<
(if (zp n)
1
Line 99 ⟶ 124:
(cw "~%tangent of pi / 4 radians: ")
(cw (as-decimal-str (tangent (/ *pi-approx* 4)) 20))
(cw "~%")))</
<pre>sine of pi / 4 radians: 0.70710678118654752440
Line 108 ⟶ 133:
=={{header|ActionScript}}==
Actionscript supports basic trigonometric and inverse trigonometric functions via the Math class, including the atan2 function, but not the hyperbolic functions.
<
trace("sin(Pi/4) = ", Math.sin(Math.PI/4));
trace("cos(Pi/4) = ", Math.cos(Math.PI/4));
Line 124 ⟶ 149:
trace("arctan(0.5) = ", Math.atan(0.5)*180/Math.PI);
trace("arctan2(-1,-2) = ", Math.atan2(-1,-2)*180/Math.PI);
</syntaxhighlight>
=={{header|Ada}}==
Line 130 ⟶ 155:
The examples below specify the cycle for degrees and for radians. <br>
The output of the inverse trig functions is in units of the specified cycle (degrees or radians).
<
use Ada.Numerics.Elementary_Functions;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
Line 164 ⟶ 189:
Put (Arccot (X => Cot (Angle_Degrees, Degrees_Cycle)),
Arccot (X => Cot (Angle_Degrees, Degrees_Cycle)));
end Trig;</
{{out}}
Line 186 ⟶ 211:
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<
REAL pi = 4 * arc tan(1);
# Pi / 4 is 45 degrees. All answers should be the same. #
Line 207 ⟶ 232:
temp := arc tan(tan(radians));
print((temp, " ", temp * 180 / pi, new line))
)</
{{out}}
<pre>
Line 219 ⟶ 244:
=={{header|ALGOL W}}==
<
% Algol W only supplies sin, cos and arctan as standard. We can define %
% arcsin, arccos and tan functions using these. The standard functions %
Line 280 ⟶ 305:
end
end.</
{{out}}
<pre>
Line 303 ⟶ 328:
arctan( tan( 60 degrees ) ): 60.00000
</pre>
=={{header|Arturo}}==
{{trans|C}}
<syntaxhighlight lang="rebol">pi: 4*atan 1.0
radians: pi/4
degrees: 45.0
print "sine"
print [sin radians, sin degrees*pi/180]
print "cosine"
print [cos radians, cos degrees*pi/180]
print "tangent"
print [tan radians, tan degrees*pi/180]
print "arcsine"
print [asin sin radians, (asin sin radians)*180/pi]
print "arccosine"
print [acos cos radians, (acos cos radians)*180/pi]
print "arctangent"
print [atan tan radians, (atan tan radians)*180/pi]</syntaxhighlight>
{{out}}
<pre>sine
0.7071067811865475 0.7071067811865475
cosine
0.7071067811865476 0.7071067811865476
tangent
0.9999999999999999 0.9999999999999999
arcsine
0.7853981633974482 44.99999999999999
arccosine
0.7853981633974483 45.0
arctangent
0.7853981633974483 45.0</pre>
=={{header|Asymptote}}==
<syntaxhighlight lang="asymptote">real pi = 4 * atan(1);
real radian = pi / 4.0;
real angulo = 45.0 * pi / 180;
write("Radians : ", radian);
write("Degrees : ", angulo / pi * 180);
write();
write("Sine : ", sin(radian), sin(angulo));
write("Cosine : ", cos(radian), cos(angulo));
write("Tangent : ", tan(radian), tan(angulo));
write();
real temp = asin(sin(radian));
write("Arc Sine : ", temp, temp * 180 / pi);
temp = acos(cos(radian));
write("Arc Cosine : ", temp, temp * 180 / pi);
temp = atan(tan(radian));
write("Arc Tangent : ", temp, temp * 180 / pi);</syntaxhighlight>
{{out}}
<pre>Radians : 0.785398163397448
Degrees : 45
Sine : 0.707106781186547 0.707106781186547
Cosine : 0.707106781186548 0.707106781186548
Tangent : 1 1
Arc Sine : 0.785398163397448 45
Arc Cosine : 0.785398163397448 45
Arc Tangent : 0.785398163397448 45</pre>
=={{header|AutoHotkey}}==
{{trans|C}}
<
radians := pi / 4
degrees := 45.0
Line 333 ⟶ 431:
0.785398 45.000000
0.785398 45.000000
*/</
=={{header|Autolisp}}==
Autolisp provides <b>(sin x) (cos x) (tan x)</b> and <b>(atan x)</b>.
Function arguments are expressed in radians.
<syntaxhighlight lang="autolisp">
(defun rad_to_deg (rad)(* 180.0 (/ rad PI)))
(defun deg_to_rad (deg)(* PI (/ deg 180.0)))
Line 364 ⟶ 462:
(list "atan pi/12" (atan (/ pi 12)) "atan 15 deg" (rad_to_deg(atan(deg_to_rad 15))))
)
</syntaxhighlight>
{{out}}
<pre>
Line 392 ⟶ 490:
<tt>atan2(y, x)</tt> actually computes the angle of the point ''(x, y)'', in the range ''[-pi, pi]''. When x > 0, this angle is the principle arctangent of ''y/x'', in the range ''(-pi/2, pi/2)''. The calculations for arcsine and arccosine use points on the unit circle at ''x<sup>2</sup> + y<sup>2</sup> = 1''. To calculate arcsine in the range ''[-pi/2, pi/2]'', we take the angle of points on the half-circle ''x = sqrt(1 - y<sup>2</sup>)''. To calculate arccosine in the range ''[0, pi]'', we take the angle of points on the half-circle ''y = sqrt(1 - x<sup>2</sup>)''.
<
function tan(x) {
return sin(x) / cos(x)
Line 431 ⟶ 529:
print " acos(-sqrt(2) / 2) =", acos(-sqrt(2) / 2) / degrees
print " atan(sqrt(3)) =", atan(sqrt(3)) / degrees
}</
{{out}}
Line 454 ⟶ 552:
The inverse tangent takes dX and dY parameters, rather than a single argument. This is because it is most often used to calculate angles.
<
Disp cos(43)▶Dec,i
Disp tan⁻¹(10,10)▶Dec,i</
{{out}}
Line 474 ⟶ 572:
=={{header|BaCon}}==
<
' The RAD() function converts from degrees to radians
Line 489 ⟶ 587:
PRINT "Arc Tangent: ", TAN(r), " is ", DEG(ATN(TAN(r))), " degrees (or ", ATN(TAN(r)), " radians)"
PRINT
NEXT</
{{out}}
Line 540 ⟶ 638:
{{works with|QuickBasic|4.5}}
QuickBasic 4.5 does not have arcsin and arccos built in. They are defined by identities found [[wp:Arctan#Relationships_among_the_inverse_trigonometric_functions|here]].
<
radians = pi / 4 'a.k.a. 45 degrees
degrees = 45 * pi / 180 'convert 45 degrees to radians once
Line 554 ⟶ 652:
arccos = 2 * ATN(SQR(1 - thecos ^ 2) / (1 + thecos))
PRINT arccos + " " + arccos * 180 / pi
PRINT ATN(TAN(radians)) + " " + ATN(TAN(radians)) * 180 / pi 'arctan</
==={{header|Applesoft BASIC}}===
The arcsine and arccoscine functions, while not intrinsic to Applesoft BASIC, are
calculated using the existing BASIC functions and implemented as FN ASN and FN ACS
using the DEF FN function.
<syntaxhighlight lang="gwbasic"> 100 TAU = 8 * ATN (1)
110 RAD = TAU / 8
120 DEG = 45.0
130 DEF FN RAD(DEG) = DEG * TAU / 360
140 DEF FN DEG(RAD) = RAD / TAU * 360
150 DEF FN ASN(RAD) = ATN (RAD / SQR ( - RAD * RAD + 1))
160 DEF FN ACS(RAD) = - ATN (RAD / SQR ( - RAD * RAD + 1)) + TAU / 4
170 PRINT " SINE: " SIN (RAD);: HTAB (25): PRINT SIN ( FN RAD(DEG))
180 PRINT " COSINE: " COS (RAD);: HTAB (25): PRINT COS ( FN RAD(DEG))
190 PRINT " TANGENT: " TAN (RAD);: HTAB (25): PRINT TAN ( FN RAD(DEG))
200 ARC = FN ASN( SIN (RAD))
210 PRINT " ARCSINE: "ARC;: HTAB (25): PRINT FN DEG(ARC)
220 ARC = FN ACS( COS (RAD))
230 PRINT " ARCCOSINE: "ARC;: HTAB (25): PRINT FN DEG(ARC)
240 ARC = ATN ( TAN (RAD))
250 PRINT " ARCTANGENT: "ARC;: HTAB (25): PRINT FN DEG(ARC);</syntaxhighlight>
{{out}}
<pre>
SINE: .707106781 .707106781
COSINE: .707106781 .707106781
TANGENT: 1 1
ARCSINE: .785398163 45
ARCCOSINE: .785398164 45.0000001
ARCTANGENT: .785398163 45
</pre>
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">radian = pi / 4
angulo = 45.0 * pi / 180
print "Radians : "; radians(angulo); " ";
print "Degrees : "; degrees(radian)
print
print "Sine : "; sin(radian); " "; sin(angulo)
print "Cosine : "; cos(radian); " "; cos(angulo)
print "Tangent : "; tan(radian); " "; tan(angulo)
print
#temp = asin(sin(radians(angulo)))
temp = asin(sin(radian))
print "Arc Sine : "; temp; " "; degrees(temp)
temp = acos(cos(radian))
print "Arc Cosine : "; temp; " "; degrees(temp)
temp = atan(tan(radian))
print "Arc Tangent : "; temp; " "; degrees(temp)
end</syntaxhighlight>
==={{header|BBC BASIC}}===
<
angle_radians = PI/5
Line 570 ⟶ 717:
PRINT ASN(number), DEG(ASN(number))
PRINT ACS(number), DEG(ACS(number))
PRINT ATN(number), DEG(ATN(number))</
==={{header|IS-BASIC}}===
<
110 OPTION ANGLE DEGREES
120 PRINT SIN(DG)
Line 588 ⟶ 735:
230 PRINT ASIN(SIN(RD))
240 PRINT ACOS(COS(RD))
250 PRINT ATN(TAN(RD))</
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">radians = pi / 4
degrees = 45.0 * pi / 180
tab$ = chr$(09)
print "Radians : ", radians, " ",
print "Degrees : ", degrees / pi * 180
print
print "Sine : ", sin(radians), tab$, sin(degrees)
print "Cosine : ", cos(radians), tab$, cos(degrees)
print "Tangent : ", tan(radians), tab$, tan(degrees)
print
temp = asin(sin(radians))
print "Arc Sine : ", temp, tab$, temp * 180 / pi
temp = acos(cos(radians))
print "Arc Cosine : ", temp, tab$, temp * 180 / pi
temp = atan(tan(radians))
print "Arc Tangent : ", temp, tab$, temp * 180 / pi
end</syntaxhighlight>
=={{header|bc}}==
{{libheader|bc -l}}
{{trans|AWK}}
<
define t(x) {
return s(x) / c(x)
Line 647 ⟶ 814:
" atan(sqrt(3)) = "; a(sqrt(3)) / d
quit</
{{out}}
Line 664 ⟶ 831:
acos(-sqrt(2) / 2) = 135.00000000000000000000000000000000000000000000005500
atan(sqrt(3)) = 60.00000000000000000000000000000000000000000000002463</pre>
=={{header|BQN}}==
BQN has a system value <code>•math</code> which contains trigonometry functions. Inputs are given in radians. These functions can also be used with BQN's Inverse modifier (<code>⁼</code>) to get their respective defined inverses.
Some results may be inaccurate due to floating point issues.
The following is done in the BQN REPL:
<syntaxhighlight lang="bqn"> ⟨sin, cos, tan⟩ ← •math
Sin 0
0
Sin π÷2
1
Cos 0
1
Cos π÷2
6.123233995736766e¯17
Tan 0
0
Tan π÷2
16331239353195370
Sin⁼ 0
0
Sin⁼ 1
1.5707963267948966
Cos⁼ 1
0
Cos⁼ 0
1.5707963267948966
Tan⁼ 0
0
Tan⁼ ∞
1.5707963267948966</syntaxhighlight>
=={{header|C}}==
<
#include <stdio.h>
Line 693 ⟶ 894:
return 0;
}</
{{out}}
Line 704 ⟶ 905:
0.785398 45.000000
</pre>
=={{header|C sharp|C#}}==
<syntaxhighlight 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();
}
}
}</syntaxhighlight>
=={{header|C++}}==
<
#include <cmath>
Line 736 ⟶ 964:
return 0;
}</
=={{header|Clojure}}==
{{trans|fortran}}
<
(:require [clojure.contrib.generic.math-functions :as generic]))
Line 783 ⟶ 985:
(println (str (asin (sin radians) ) " " (* (asin (sin (* degrees dtor))) rtod)))
(println (str (acos (cos radians) ) " " (* (acos (cos (* degrees dtor))) rtod)))
(println (str (atan (tan radians) ) " " (* (atan (tan (* degrees dtor))) rtod)))</
{{out}} (matches that of Java)
Line 795 ⟶ 997:
=={{header|COBOL}}==
<
PROGRAM-ID. Trig.
Line 833 ⟶ 1,035:
GOBACK
.</
{{out}}
Line 854 ⟶ 1,056:
=={{header|Common Lisp}}==
<
(defun rad->deg (x) (* x (/ 180 pi)))
Line 869 ⟶ 1,071:
(rad->deg (acos 1/2))
(atan 15)
(rad->deg (atan 15))))</
=={{header|D}}==
{{trans|C}}
<
import std.stdio, std.math;
Line 893 ⟶ 1,095:
immutable real t3 = PI_4.tan.atan;
writefln("Arctangent: %.20f %.20f", t3, t3 * 180.0L / PI);
}</
{{out}}
<pre>Reference: 0.7071067811865475244008
Line 904 ⟶ 1,106:
Arccosine: 0.78539816339744830961 45.00000000000000000000
Arctangent: 0.78539816339744830961 45.00000000000000000000</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
<syntaxhighlight lang="Delphi">
procedure ShowTrigFunctions(Memo: TMemo);
const AngleDeg = 45.0;
var AngleRad,ArcSine,ArcCosine,ArcTangent: double;
begin
AngleRad:=DegToRad(AngleDeg);
Memo.Lines.Add(Format('Angle: Degrees: %3.5f Radians: %3.6f',[AngleDeg,AngleRad]));
Memo.Lines.Add('-------------------------------------------------');
Memo.Lines.Add(Format('Sine: Degrees: %3.6f Radians: %3.6f',[sin(DegToRad(AngleDeg)),sin(AngleRad)]));
Memo.Lines.Add(Format('Cosine: Degrees: %3.6f Radians: %3.6f',[cos(DegToRad(AngleDeg)),cos(AngleRad)]));
Memo.Lines.Add(Format('Tangent: Degrees: %3.6f Radians: %3.6f',[tan(DegToRad(AngleDeg)),tan(AngleRad)]));
ArcSine:=ArcSin(Sin(AngleRad));
Memo.Lines.Add(Format('Arcsine: Degrees: %3.6f Radians: %3.6f',[DegToRad(ArcSine),ArcSine]));
ArcCosine:=ArcCos(cos(AngleRad));
Memo.Lines.Add(Format('Arccosine: Degrees: %3.6f Radians: %3.6f',[DegToRad(ArcCosine),ArcCosine]));
ArcTangent:=ArcTan(tan(AngleRad));
Memo.Lines.Add(Format('Arctangent: Degrees: %3.6f Radians: %3.6f',[DegToRad(ArcTangent),ArcTangent]));
end;
</syntaxhighlight>
{{out}}
<pre>
Angle: Degrees: 45.00000 Radians: 0.785398
-------------------------------------------------
Sine: Degrees: 0.707107 Radians: 0.707107
Cosine: Degrees: 0.707107 Radians: 0.707107
Tangent: Degrees: 1.000000 Radians: 1.000000
Arcsine: Degrees: 0.013708 Radians: 0.785398
Arccosine: Degrees: 0.013708 Radians: 0.785398
Arctangent: Degrees: 0.013708 Radians: 0.785398
Elapsed Time: 9.118 ms.
</pre>
=={{header|E}}==
{{trans|ALGOL 68}}
<
def radians := pi / 4.0
Line 923 ⟶ 1,170:
${def acos := radians.cos().acos()} ${r2d(acos)}
${def atan := radians.tan().atan()} ${r2d(atan)}
`)</
{{out}}
Line 932 ⟶ 1,179:
0.7853981633974483 45.0
0.7853981633974483 45.0
=={{header|EasyLang}}==
<syntaxhighlight>
r = pi / 4
d = 45
#
func r2d r .
return r / pi * 180
.
func d2r d .
return d * pi / 180
.
#
numfmt 4 0
print sin d & " " & sin r2d r
print cos d & " " & cos r2d r
print tan d & " " & tan r2d r
print ""
h = asin sin d
print h & " " & d2r h
h = acos cos d
print h & " " & d2r h
h = atan tan d
print h & " " & d2r h
</syntaxhighlight>
{{out}}
<pre>
0.7071 0.7071
0.7071 0.7071
1.0000 1.0000
45.0000 0.7854
45 0.7854
45 0.7854
</pre>
=={{header|Elena}}==
{{trans|C++}}
ELENA
<
import extensions
public program()
{
console
console
console
console
console
console
console
console
console
console
console
console
console
console
console
console
}</syntaxhighlight>
=={{header|Elixir}}==
{{trans|Erlang}}
<
45
iex(62)> rad = :math.pi / 4
Line 980 ⟶ 1,263:
0.7853981633974483
iex(69)> temp * 180 / :math.pi == deg
true</
=={{header|Erlang}}==
{{trans|C}}
<
Deg=45.
Rad=math:pi()/4.
math:sin(Deg * math:pi() / 180)==math:sin(Rad).
</syntaxhighlight>
{{out}}
true
<
math:cos(Deg * math:pi() / 180)==math:cos(Rad).
</syntaxhighlight>
{{out}}
true
<
math:tan(Deg * math:pi() / 180)==math:tan(Rad).
</syntaxhighlight>
{{out}}
true
<
Temp = math:acos(math:cos(Rad)).
Temp * 180 / math:pi()==Deg.
</syntaxhighlight>
{{out}}
true
<
Temp = math:atan(math:tan(Rad)).
Temp * 180 / math:pi()==Deg.
</syntaxhighlight>
{{out}}
true
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">open NUnit.Framework
open FsUnit
// radian
[<Test>]
let ``Verify that sin pi returns 0`` () =
let x = System.Math.Sin System.Math.PI
System.Math.Round(x,5) |> should equal 0
[<Test>]
let ``Verify that cos pi returns -1`` () =
let x = System.Math.Cos System.Math.PI
System.Math.Round(x,5) |> should equal -1
[<Test>]
let ``Verify that tan pi returns 0`` () =
let x = System.Math.Tan System.Math.PI
System.Math.Round(x,5) |> should equal 0
[<Test>]
let ``Verify that sin pi/2 returns 1`` () =
let x = System.Math.Sin (System.Math.PI / 2.0)
System.Math.Round(x,5) |> should equal 1
[<Test>]
let ``Verify that cos pi/2 returns -1`` () =
let x = System.Math.Cos (System.Math.PI / 2.0)
System.Math.Round(x,5) |> should equal 0
[<Test>]
let ``Verify that sin pi/3 returns sqrt 3/2`` () =
let actual = System.Math.Sin (System.Math.PI / 3.0)
let expected = System.Math.Round((System.Math.Sqrt 3.0) / 2.0, 5)
System.Math.Round(actual,5) |> should equal expected
[<Test>]
let ``Verify that cos pi/3 returns -1`` () =
let x = System.Math.Cos (System.Math.PI / 3.0)
System.Math.Round(x,5) |> should equal 0.5
[<Test>]
let ``Verify that cos and sin of pi/4 return same value`` () =
let c = System.Math.Cos (System.Math.PI / 4.0)
let s = System.Math.Sin (System.Math.PI / 4.0)
System.Math.Round(c,5) = System.Math.Round(s,5) |> should be True
[<Test>]
let ``Verify that acos pi/3 returns 1/2`` () =
let actual = System.Math.Acos 0.5
let expected = System.Math.Round((System.Math.PI / 3.0),5)
System.Math.Round(actual,5) |> should equal expected
[<Test>]
let ``Verify that asin 1 returns pi/2`` () =
let actual = System.Math.Asin 1.0
let expected = System.Math.Round((System.Math.PI / 2.0),5)
System.Math.Round(actual,5) |> should equal expected
[<Test>]
let ``Verify that atan 0 returns 0`` () =
let actual = System.Math.Atan 0.0
let expected = System.Math.Round(0.0,5)
System.Math.Round(actual,5) |> should equal expected
// degree
let toRadians d = d * System.Math.PI / 180.0
[<Test>]
let ``Verify that pi is 180 degrees`` () =
toRadians 180.0 |> should equal System.Math.PI
[<Test>]
let ``Verify that pi/2 is 90 degrees`` () =
toRadians 90.0 |> should equal (System.Math.PI / 2.0)
[<Test>]
let ``Verify that pi/3 is 60 degrees`` () =
toRadians 60.0 |> should equal (System.Math.PI / 3.0)
[<Test>]
let ``Verify that sin 180 returns 0`` () =
let x = System.Math.Sin (toRadians 180.0)
System.Math.Round(x,5) |> should equal 0
[<Test>]
let ``Verify that cos 180 returns -1`` () =
let x = System.Math.Cos (toRadians 180.0)
System.Math.Round(x,5) |> should equal -1
[<Test>]
let ``Verify that tan 180 returns 0`` () =
let x = System.Math.Tan (toRadians 180.0)
System.Math.Round(x,5) |> should equal 0
[<Test>]
let ``Verify that sin 90 returns 1`` () =
let x = System.Math.Sin (toRadians 90.0)
System.Math.Round(x,5) |> should equal 1
[<Test>]
let ``Verify that cos 90 returns -1`` () =
let x = System.Math.Cos (toRadians 90.0)
System.Math.Round(x,5) |> should equal 0
[<Test>]
let ``Verify that sin 60 returns sqrt 3/2`` () =
let actual = System.Math.Sin (toRadians 60.0)
let expected = System.Math.Round((System.Math.Sqrt 3.0) / 2.0, 5)
System.Math.Round(actual,5) |> should equal expected
[<Test>]
let ``Verify that cos 60 returns -1`` () =
let x = System.Math.Cos (toRadians 60.0)
System.Math.Round(x,5) |> should equal 0.5
[<Test>]
let ``Verify that cos and sin of 45 return same value`` () =
let c = System.Math.Cos (toRadians 45.0)
let s = System.Math.Sin (toRadians 45.0)
System.Math.Round(c,5) = System.Math.Round(s,5) |> should be True</syntaxhighlight>
=={{header|Factor}}==
<
prettyprint ;
Line 1,031 ⟶ 1,438:
[ [ . ] compose dup compose ] tri@ 2tri
.5 [ asin ] [ acos ] [ atan ] tri [ dup rad>deg [ . ] bi@ ] tri@</
=={{header|Fantom}}==
Line 1,039 ⟶ 1,446:
Methods are provided to convert: toDegrees and toRadians.
<
class Main
{
Line 1,060 ⟶ 1,467:
}
}
</syntaxhighlight>
=={{header|Forth}}==
<
cr fdup fsin f. \ also available: fsincos ( r -- sin cos )
Line 1,070 ⟶ 1,477:
cr fdup fasin f.
cr fdup facos f.
cr fatan f. \ also available: fatan2 ( r1 r2 -- atan[r1/r2] )</
=={{header|Fortran}}==
Trigonometic functions expect arguments in radians so degrees require conversion
<
REAL pi, dtor, rtod, radians, degrees
Line 1,091 ⟶ 1,498:
WRITE(*,*) ATAN(TAN(radians)), ATAN(TAN(degrees*dtor))*rtod
END PROGRAM Trig</
{{out}}
0.707107 0.707107
Line 1,100 ⟶ 1,507:
0.785398 45.0000
The following trigonometric functions are also available
<
SINH(x) ! Hyperbolic sine
COSH(x) ! Hyperbolic cosine
TANH(x) ! Hyperbolic tangent</
But, for those with access to fatter Fortran function libraries, trigonometrical functions working in degrees are also available.
<syntaxhighlight lang="fortran">
Calculate various trigonometric functions from the Fortran library.
INTEGER BIT(32),B,IP !Stuff for bit fiddling.
Line 1,170 ⟶ 1,577:
WRITE (6,*) " = 11.00100100001111110110101010001000100001..." !But actually...
END !So much for that.
</syntaxhighlight>
Output:
Deg. Sin(Deg) Sin(Rad) Rad - Deg ArcSinD ArcSinR Diff
Line 1,235 ⟶ 1,642:
=={{header|FreeBASIC}}==
{{trans|C}}
<
Const pi As Double = 4 * Atn(1)
Line 1,255 ⟶ 1,662:
temp = Atn(Tan(radians))
Print "Arc Tangent : "; temp, temp * 180 / pi
Sleep</
{{out}}
Line 1,270 ⟶ 1,677:
</pre>
=={{header|
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
void local fn DoIt
double degrees = 45.0
double radians = degrees * M_PI / 180
NSLog(@"%f, %f",sin(radians),sin(degrees * M_PI / 180))
NSLog(@"%f, %f",cos(radians),cos(degrees * M_PI / 180))
NSLog(@"%f, %f",tan(radians),tan(degrees * M_PI / 180))
NSLog(@"%f, %f",asin(sin(radians)),asin(sin(radians)) * 180 / M_PI)
NSLog(@"%f, %f",acos(cos(radians)),acos(cos(radians)) * 180 / M_PI)
NSLog(@"%f, %f",atan(tan(radians)),atan(tan(radians)) * 180 / M_PI)
end fn
fn DoIt
HandleEvents
</syntaxhighlight>
{{out}}
<pre>
0.707107, 0.707107
0.707107, 0.707107
1.000000, 1.000000
0.785398, 45.000000
0.785398, 45.000000
0.785398, 45.000000
</pre>
=={{header|GAP}}==
<
Pi := Acos(-1.0);
Line 1,412 ⟶ 1,727:
Cos(Deg(d)); Acos(last);
Tan(r); Atan(last);
Tan(Deg(d)); Atan(last);</
=={{header|Go}}==
The Go math package provides the constant pi and the six trigonometric functions called for by the task. The functions all use the float64 type and work in radians. It also provides a [http://golang.org/pkg/math/#Sincos Sincos] function.
<
import (
Line 1,443 ⟶ 1,758:
fmt.Printf("atan(%f) = %9.6f deg\n", t, math.Atan(t)*180/math.Pi)
fmt.Printf("atan(%f) = %9.6f rad\n", t, math.Atan(t))
}</
{{out}}
<pre>
Line 1,462 ⟶ 1,777:
=={{header|Groovy}}==
Trig functions use radians, degrees must be converted to/from radians
<
def degrees = 45
Line 1,473 ⟶ 1,788:
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"</
{{out}}
Line 1,487 ⟶ 1,802:
Trigonometric functions use radians; degrees require conversion.
<
fromDegrees deg = deg * pi / 180
Line 1,509 ⟶ 1,824:
, atan 0.5
, toDegrees (atan 0.5)
]</
{{Out}}
<pre>0.49999999999999994
Line 1,526 ⟶ 1,841:
=={{header|HicEst}}==
Translated from Fortran:
<
dtor = pi / 180.0
rtod = 180.0 / pi
Line 1,537 ⟶ 1,852:
WRITE(ClipBoard) ASIN(SIN(radians)), ASIN(SIN(degrees*dtor))*rtod
WRITE(ClipBoard) ACOS(COS(radians)), ACOS(COS(degrees*dtor))*rtod
WRITE(ClipBoard) ATAN(TAN(radians)), ATAN(TAN(degrees*dtor))*rtod</
<
0.7071067812 0.7071067812
1 1
0.7853981634 45
0.7853981634 45
0.7853981634 45</
SINH, COSH, TANH, and inverses are available as well.
=={{header|IDL}}==
<
rad = !dtor*deg ; system variables !dtor and !radeg convert between rad and deg</
<
print, rad, sin(rad), asin(sin(rad))
print, cos(rad), acos(cos(rad))
Line 1,558 ⟶ 1,873:
; 0.610865 0.573576 0.610865
; 0.819152 0.610865
; 0.700208 0.610865</
<
print, sinh(rad) ; etc
; outputs
; 0.649572</
<
x = !dpi/[[2,3],[4,5],[6,7]] ; !dpi is a read-only sysvar = 3.1415...
print,sin(x)
Line 1,571 ⟶ 1,886:
; 1.0000000 0.86602540
; 0.70710678 0.58778525
; 0.50000000 0.43388374</
<
x = complex(1,2)
print,sin(x)
; outputs
; ( 3.16578, 1.95960)</
== Icon and Unicon ==
Icon and Unicon trig functions 'sin', 'cos', 'tan', 'asin', 'acos', and 'atan' operate on angles expressed in radians. Conversion functions 'dtor' and 'rtod' convert between the two systems. The example below uses string invocation to construct and call the functions:
==={{header|Icon}}===
<
procedure main()
Line 1,589 ⟶ 1,904:
every write(f := !["sin","cos","tan"],"(",r,")=",y := f(r)," ",fi := "a" || f,"(",y,")=",x := fi(y)," rad = ",rtod(x)," deg")
end</
{{out}}
<pre>sin(0.5235987755982988)=0.4999999999999999 asin(0.4999999999999999)=0.5235987755982988 rad = 30.0 deg
Line 1,602 ⟶ 1,917:
Sine, cosine, and tangent of a single angle, indicated as pi-over-four radians and as 45 degrees:
<
0.707107 0.707107
0.707107 0.707107
1 1</
Arcsine, arccosine, and arctangent of one-half, in radians and degrees:
<
0.523599 30
1.0472 60
0.463648 26.5651</
The <code>trig</code> script adds cover functions for the trigonometric operations as well as verbs for converting degrees from radians (<code>dfr</code>) and radians from degrees (<code>rfd</code>)
<
(sin , cos ,: tan) (1p1 % 4), rfd 45
0.707107 0.707107
Line 1,622 ⟶ 1,937:
0.523599 30
1.0472 60
0.463648 26.5651</
=={{header|Java}}==
Line 1,628 ⟶ 1,943:
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 static void main(String[] args) {
//Pi / 4 is 45 degrees. All answers should be the same.
Line 1,649 ⟶ 1,964:
System.out.println(arctan + " " + Math.toDegrees(arctan));
}
}</
{{out}}
Line 1,665 ⟶ 1,980:
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.
<
radians = Math.PI / 4, // Pi / 4 is 45 degrees. All answers should be the same.
degrees = 45.0,
Line 1,686 ⟶ 2,001:
window.alert(arccos + " " + (arccos * 180 / Math.PI));
// arctangent
window.alert(arctan + " " + (arctan * 180 / Math.PI));</
=={{header|jq}}==
Line 1,692 ⟶ 2,007:
jq includes the standard C-library trigonometric functions (sin, cos, tan, asin, acos, atan), but they are provided as filters as illustrated in the definition of <tt>radians</tt> below.
The trigonometric filters only accept radians, so conversion is necessary when dealing with degrees. The constant <tt>π</tt> can be defined as also shown in the following definition of <tt>radians</tt>:<
# degrees to radians
def radians:
Line 1,718 ⟶ 2,033:
task
</syntaxhighlight>
{{out}}
<
sin(-pi / 6) = -0.49999999999999994
cos(3 * pi / 4) = -0.7071067811865475
Line 1,733 ⟶ 2,048:
asin(-1 / 2) = -29.999999999999996
acos(-sqrt(2)/2) = 135
atan(sqrt(3)) = 60.00000000000001</
=={{header|Jsish}}==
Like many programming languages that handle trig, Jsish also includes the ''atan2'' function, which was originally added to Fortran to allow disambiguous results when converting from cartesian to polar coordinates, due to the mirror image nature of normal arctan.
To find what methods are supported, ''jsish'' supports help for the Math module.
<pre>help Math
Math.method(...)
Commands performing math operations on numbers
Methods: abs acos asin atan atan2 ceil cos exp floor log max min pow random round sin sqrt tan</pre>
Angles passed to the trigonometric functions expect arguments in ''radians'' (Pi by 4 radians being 45 degrees). Degree to radian conversion is shown by multiplying radians by Pi over 180.
''Note the inexact nature of floating point approximations.''
<syntaxhighlight lang="javascript">/* Trig in Jsish */
var x;
;x = Math.PI / 4;
;Math.sin(x);
;Math.cos(x);
;Math.tan(x);
;Math.asin(Math.sin(x)) * 4;
;Math.acos(Math.cos(x)) * 4;
;Math.atan(Math.tan(x));
;Math.atan2(Math.tan(x), 1.0);
;Math.atan2(Math.tan(x), -1.0);
;x = 45.0;
;Math.sin(x * Math.PI / 180);
;Math.cos(x * Math.PI / 180);
;Math.tan(x * Math.PI / 180);
/*
=!EXPECTSTART!=
x = Math.PI / 4 ==> 0.7853981633974483
Math.sin(x) ==> 0.7071067811865475
Math.cos(x) ==> 0.7071067811865476
Math.tan(x) ==> 0.9999999999999999
Math.asin(Math.sin(x)) * 4 ==> 3.141592653589793
Math.acos(Math.cos(x)) * 4 ==> 3.141592653589793
Math.atan(Math.tan(x)) ==> 0.7853981633974483
Math.atan2(Math.tan(x), 1.0) ==> 0.7853981633974483
Math.atan2(Math.tan(x), -1.0) ==> 2.356194490192345
x = 45.0 ==> 45
Math.sin(x * Math.PI / 180) ==> 0.7071067811865475
Math.cos(x * Math.PI / 180) ==> 0.7071067811865476
Math.tan(x * Math.PI / 180) ==> 0.9999999999999999
=!EXPECTEND!=
*/</syntaxhighlight>
{{out}}
<pre>prompt$ jsish --U trigonometric.jsi
x = Math.PI / 4 ==> 0.7853981633974483
Math.sin(x) ==> 0.7071067811865475
Math.cos(x) ==> 0.7071067811865476
Math.tan(x) ==> 0.9999999999999999
Math.asin(Math.sin(x)) * 4 ==> 3.141592653589793
Math.acos(Math.cos(x)) * 4 ==> 3.141592653589793
Math.atan(Math.tan(x)) ==> 0.7853981633974483
Math.atan2(Math.tan(x), 1.0) ==> 0.7853981633974483
Math.atan2(Math.tan(x), -1.0) ==> 2.356194490192345
x = 45.0 ==> 45
Math.sin(x * Math.PI / 180) ==> 0.7071067811865475
Math.cos(x * Math.PI / 180) ==> 0.7071067811865476
Math.tan(x * Math.PI / 180) ==> 0.9999999999999999
prompt$ jsish -u trigonometric.jsi
[PASS] trigonometric.jsi</pre>
=={{header|Julia}}==
<
rad = π / 4
Line 1,747 ⟶ 2,131:
@show asin(sin(rad)) asin(sin(rad)) |> rad2deg
@show acos(cos(rad)) acos(cos(rad)) |> rad2deg
@show atan(tan(rad)) atan(tan(rad)) |> rad2deg</
{{out}}
Line 1,766 ⟶ 2,150:
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">import kotlin.math.*
fun main() {
fun Double.toDegrees() = this * 180 / PI
val angle = PI / 4
println("angle = $angle rad =
val sine = sin(angle)
println("
val cosine = cos(angle)
println("
println("
println()
println("
val acos = acos(cosine)
println("acos(cos(angle)) = $acos rad = ${acos.toDegrees()}°")
val atan = atan(tangent)
println("atan(tan(angle)) = $atan rad = ${atan.toDegrees()}°")
}</syntaxhighlight>
{{out}}
<pre>
angle = 0.7853981633974483 rad = 45.0°
sin(angle) = 0.7071067811865475
cos(angle) = 0.7071067811865476
tan(angle) = 0.9999999999999999
asin(sin(angle)) = 0.7853981633974482 rad = 44.99999999999999°
acos(cos(angle)) = 0.7853981633974483 rad = 45.0°
atan(tan(angle)) = 0.7853981633974483 rad = 45.0°
</pre>
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def deg2rad {lambda {:d} {* {/ {PI} 180} :d}}}
-> deg2rad
{def rad2deg {lambda {:r} {* {/ 180 {PI}} :r}}}
-> rad2deg
{deg2rad 180}
-> 3.141592653589793 = PI
{rad2deg {PI}}°
-> 180°
{sin {deg2rad 45}}
-> 0.7071067811865475 = PI/4
{cos {deg2rad 45}}
-> 0.7071067811865476 = PI/4
{tan {deg2rad 45}}
-> 0.9999999999999999 = 1
{rad2deg {asin 0.5}}° -> 30.000000000000004°
{rad2deg {acos 0.5}}° -> 60.00000000000001°
{rad2deg {atan 1}}° -> 45°
</syntaxhighlight>
=={{header|Liberty BASIC}}==
<
radians = pi / 4.0
rtod = 180 / pi
Line 1,818 ⟶ 2,223:
print "Asn: ";ASN(SIN(radians));" Rad, "; ASN(SIN(degrees*dtor))*rtod;" Deg"
print "Acs: ";ACS(COS(radians));" Rad, "; ACS(COS(degrees*dtor))*rtod;" Deg"
print "Atn: ";ATN(TAN(radians));" Rad, "; ATN(TAN(degrees*dtor))*rtod;" Deg"</
{{out}}
<pre>Sin: 0.70710678 0.70710678
Line 1,830 ⟶ 2,235:
=={{header|Logo}}==
[[UCB Logo]] has sine, cosine, and arctangent; each having variants for degrees or radians.
<
print cos 45
print arctan 1
Line 1,836 ⟶ 2,241:
print radsin :pi / 4
print radcos :pi / 4
print 4 * radarctan 1</
[[Lhogho]] has pi defined in its trigonometric functions. Otherwise the same as UCB Logo.
<
print cos 45
print arctan 1
print radsin pi / 4
print radcos pi / 4
print 4 * radarctan 1</
=={{header|Logtalk}}==
<
:- object(trignomeric_functions).
Line 1,863 ⟶ 2,268:
:- end_object.
</syntaxhighlight>
{{out}}
<
?- trignomeric_functions::show.
sin(pi/4.0) = 0.7071067811865475
Line 1,875 ⟶ 2,280:
atan2(3,4) = 0.6435011087932844
yes
</syntaxhighlight>
=={{header|Lua}}==
<
=={{header|Maple}}==
In radians:
<
cos(Pi/3);
tan(Pi/3);</
{{out}}
<pre>
Line 1,901 ⟶ 2,306:
The equivalent in degrees with identical output:
<
sin(60*Unit(degree));
cos(60*Unit(degree));
tan(60*Unit(degree));</
Note, Maple also has secant, cosecant, and cotangent:
<
sec(Pi/3);
cot(Pi/3);</
Finally, the inverse trigonometric functions:
<
arccos(1);
arctan(1);</
{{out}}
<pre>> arcsin(1);
Line 1,932 ⟶ 2,337:
Lastly, Maple also supports the two-argument arctan plus all the hyperbolic trigonometric functions.
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Cos[1]
Tan[1]
Line 1,941 ⟶ 2,346:
Sin[90 Degree]
Cos[90 Degree]
Tan[90 Degree]</syntaxhighlight>
=={{header|MATLAB}}==
A full list of built-in trig functions can be found in the [http://www.mathworks.com/access/helpdesk/help/techdoc/ref/f16-5872.html#f16-6197 MATLAB Documentation].
<
angleRadians = angleDegrees * (pi/180);
Line 1,959 ⟶ 2,363:
disp(sprintf('tan(%f)= %f\natan(%f)= %f',[angleRadians tan(angleRadians) tan(angleRadians) atan(tan(angleRadians))]));
disp(sprintf('tand(%f)= %f\narctand(%f)= %f',[angleDegrees tand(angleDegrees) tand(angleDegrees) atand(tand(angleDegrees))]));
end</
{{out}}
<
sin(1.361357)= 0.978148
asin(0.978148)= 1.361357
Line 1,976 ⟶ 2,380:
atan(4.704630)= 1.361357
tand(78.000000)= 4.704630
arctand(4.704630)= 78.000000</
=={{header|Maxima}}==
<
[sin(a), cos(a), tan(a), sec(a), csc(a), cot(a)];
Line 1,988 ⟶ 2,392:
a: 1 / 2;
[sinh(a), cosh(a), tanh(a), sech(a), csch(a), coth(a)], numer;
[asinh(a), acosh(1 / a), atanh(a), asech(a), acsch(a), acoth(1 / a)], numer;</
=={{header|MAXScript}}==
Maxscript trigonometric functions accept degrees only. The built-ins degToRad and radToDeg allow easy conversion.
<
local degrees = 45.0
Line 2,012 ⟶ 2,416:
--arctangent
print (atan (tan (radToDeg radians)))
print (atan (tan degrees))</
=={{header|Metafont}}==
Line 2,018 ⟶ 2,422:
Metafont has <code>sind</code> and <code>cosd</code>, which compute sine and cosine of an angle expressed in degree. We need to define the rest.
<
vardef torad expr x = Pi*x/180 enddef; % conversions
vardef todeg expr x = 180x/Pi enddef;
Line 2,063 ⟶ 2,467:
outcompare(tan(Pi/3), tand(60));
end</
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">pi3 = pi/3
degToRad = pi/180
print "sin PI/3 radians = " + sin(pi3)
print "sin 60 degrees = " + sin(60*degToRad)
print "arcsin 0.5 in radians = " + asin(0.5)
print "arcsin 0.5 in degrees = " + asin(0.5)/degToRad
print "cos PI/3 radians = " + cos(pi3)
print "cos 60 degrees = " + cos(60*degToRad)
print "arccos 0.5 in radians = " + acos(0.5)
print "arccos 0.5 in degrees = " + acos(0.5)/degToRad
print "tan PI/3 radians = " + tan(pi3)
print "tan 60 degrees = " + tan(60*degToRad)
print "arctan 0.5 in radians = " + atan(0.5)
print "arctan 0.5 in degrees = " + atan(0.5)/degToRad</syntaxhighlight>
{{out}}
<pre>
sin PI/3 radians = 0.866025
sin 60 degrees = 0.866025
arcsin 0.5 in radians = 0.523599
arcsin 0.5 in degrees = 30.0
cos PI/3 radians = 0.5
cos 60 degrees = 0.5
arccos 0.5 in radians = 1.047198
arccos 0.5 in degrees = 60.0
tan PI/3 radians = 1.732051
tan 60 degrees = 1.732051
arctan 0.5 in radians = 0.463648
arctan 0.5 in degrees = 26.565051
</pre>
=={{header|МК-61/52}}==
Line 2,074 ⟶ 2,509:
=={{header|Modula-2}}==
<
FROM RealMath IMPORT pi,sin,cos,tan,arctan,arccos,arcsin;
FROM RealStr IMPORT RealToStr;
Line 2,119 ⟶ 2,554:
ReadChar
END Trig.</
=={{header|NetRexx}}==
<
options replace format comments java crossref symbols nobinary utf8
Line 2,157 ⟶ 2,592:
return
</syntaxhighlight>
{{out}}
Line 2,172 ⟶ 2,607:
=={{header|Nim}}==
<
let rad = Pi/4
let deg = 45.0
echo &"Sine:
echo &"Cosine :
echo &"Tangent:
echo &"Arcsine:
echo &"
echo &"Arctangent:
</syntaxhighlight>
{{out}}
<pre>Sine: 0.7071067812 0.7071067812
Cosine : 0.7071067812 0.7071067812
Tangent: 1.0000000000 1.0000000000
Arcsine: 0.7853981634 45.0000000000
Arccosine: 0.7853981634 45.0000000000
Arctangent: 0.7853981634 45.0000000000</pre>
=={{header|OCaml}}==
OCaml's preloaded <tt>Pervasives</tt> module contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees.
<
let radians = pi /. 4.
Line 2,203 ⟶ 2,644:
Printf.printf "%f %f\n" arccos (arccos *. 180. /. pi);;
let arctan = atan (tan radians);;
Printf.printf "%f %f\n" arctan (arctan *. 180. /. pi);;</
{{out}}
<pre>
Line 2,216 ⟶ 2,657:
=={{header|Octave}}==
<
d = 180*rad/pi;
endfunction
Line 2,236 ⟶ 2,677:
ifuncs{i}, v, iv,
strcat(ifuncs{i}, "d"), vd, ivd);
endfor</
{{out}}
Line 2,259 ⟶ 2,700:
(Lacking in this code but present in GNU Octave: sinh, cosh, tanh, coth and inverses)
=={{header|Oforth}}==
<
: testTrigo
Line 2,290 ⟶ 2,730:
System.Out hyp sinh << " - " << hyp sinh asinh << cr
System.Out hyp cosh << " - " << hyp cosh acosh << cr
System.Out hyp tanh << " - " << hyp tanh atanh << cr ;</
{{out}}
Line 2,389 ⟶ 2,829:
rxCalcexp(x) limits x to 709. or so and returns '+infinity' for larger exponents
</pre>
<
* show how the functions can be used
* 03.05.2014 Walter Pachl
Line 2,406 ⟶ 2,846:
Say 'Changed type: ' .locaL~my.rxm~type()
Say 'rxmsin(1) ='rxmsin(1) -- use changed precision and type
::requires rxm.cls</
{{out}}
<pre>Default precision: 16
Line 2,420 ⟶ 2,860:
rxmsin(1) =0.84147098480789650665250232163029899962256306079837</pre>
<
* Package rxm
* implements the functions available in RxMath with high precision
Line 2,426 ⟶ 2,866:
* and rounding the result to the specified precision.
* This started 10 years ago when Vladimir Zabrodsky published his
* Album of Algorithms http://
* Gerard Schildberger suggests on rosettacode.org to use +10 digits
* Rony Flatscher suggested and helped to turn this into an ooRexx class
Line 2,767 ⟶ 3,207:
::Method LN2
v=''
v=v||0.69314718055994530941723212145817656807
Line 2,876 ⟶ 3,311:
Else Do /* Exponent is not an integer */
-- Say 'for a negative base ('||b')',
-- 'exponent ('c') must be an integer'
Return 'nan' /* Return not a number */
End
Line 3,456 ⟶ 3,891:
Say " .locaL~my.rxm~precision=50"
Say " .locaL~my.rxm~type='R'"
return 0</
=={{header|Oz}}==
<
PI = 3.14159265
Line 3,479 ⟶ 3,914:
for I#F in [Asin#Sin Acos#Cos Atan#Tan] do
{System.showInfo {I {F Radians}}#" "#{ToDegrees {I {F Radians}}}}
end</
=={{header|PARI/GP}}==
Pari accepts only radians; the conversion is simple but not included here.
<
sin(Pi/2)
tan(Pi/2)
acos(1)
asin(1)
atan(1)</
{{works with|PARI/GP|2.4.3 and above}}
<
=={{header|Pascal}}==
{{libheader|math}}
<
uses
Line 3,515 ⟶ 3,950:
writeln (arctan(tan(radians)),' Rad., or ', arctan(tan(degree/180*pi))/pi*180,' Deg.');
// ( radians ) / pi * 180 = deg.
end.</
{{out}}
<pre> 7.0710678118654750E-0001 7.0710678118654752E-0001
Line 3,528 ⟶ 3,963:
{{works with|Perl|5.8.8}}
<
my $angle_degrees = 45;
Line 3,544 ⟶ 3,979:
print $atan, ' ', rad2deg($atan), "\n";
my $acot = acot(cot($angle_radians));
print $acot, ' ', rad2deg($acot), "\n";</
{{out}}
Line 3,557 ⟶ 3,992:
0.785398163397448 45
</pre>
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">90</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">180</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">180</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">tan</span><span style="color: #0000FF;">(</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">tan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">45</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">180</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">arcsin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">2</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">arcsin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">180</span><span style="color: #0000FF;">/</span><span style="color: #004600;">PI</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">arccos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">2</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">arccos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">180</span><span style="color: #0000FF;">/</span><span style="color: #004600;">PI</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">4</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">180</span><span style="color: #0000FF;">/</span><span style="color: #004600;">PI</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 3,599 ⟶ 4,027:
=={{header|PHP}}==
<
$degrees = 45 * M_PI / 180;
echo sin($radians) . " " . sin($degrees);
Line 3,606 ⟶ 4,034:
echo asin(sin($radians)) . " " . asin(sin($radians)) * 180 / M_PI;
echo acos(cos($radians)) . " " . acos(cos($radians)) * 180 / M_PI;
echo atan(tan($radians)) . " " . atan(tan($radians)) * 180 / M_PI;</
=={{header|PicoLisp}}==
<
(de dtor (Deg)
Line 3,628 ⟶ 4,056:
(format (acos (cos (/ pi 4))) *Scl) " " (format (rtod (acos (cos (dtor 45.0)))) *Scl) )
(prinl
(format (atan (tan (/ pi 4))) *Scl) " " (format (rtod (atan (tan (dtor 45.0)))) *Scl) )</
{{out}}
<pre>0.707107 0.707107
Line 3,638 ⟶ 4,066:
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
declare (x, xd, y, v) float;
Line 3,657 ⟶ 4,085:
v = cosh(x); put skip list (v);
v = tanh(x); y = atanh(v); put skip list (y);
</syntaxhighlight>
Results:
<pre>
Line 3,676 ⟶ 4,104:
ATAN2 are accurate to 30 decimal digits.
<
pi NUMBER := 4 * atan(1);
radians NUMBER := pi / 4;
Line 3,687 ⟶ 4,115:
DBMS_OUTPUT.put_line(ACOS(COS(radians)) || ' ' || ACOS(COS(degrees * pi/180)) * 180/pi);
DBMS_OUTPUT.put_line(ATAN(TAN(radians)) || ' ' || ATAN(TAN(degrees * pi/180)) * 180/pi);
end;</
{{out}}
Line 3,698 ⟶ 4,126:
The following trigonometric functions are also available
<
SINH(n) --Hyperbolic sine
COSH(n) --Hyperbolic cosine
TANH(n) --Hyperbolic tangent</
=={{header|Pop11}}==
Line 3,707 ⟶ 4,135:
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.
<
cos(45) =>
tan(45) =>
Line 3,721 ⟶ 4,149:
arcsin(0.7) =>
arccos(0.7) =>
arctan(0.7) =></
=={{header|PostScript}}==
<
90 sin =
Line 3,736 ⟶ 4,164:
3 sqrt 1 atan =
</syntaxhighlight>
{{out}}
<pre>
Line 3,750 ⟶ 4,178:
=={{header|PowerShell}}==
{{Trans|C}}
<
$deg = 45
'{0,10} {1,10}' -f 'Radians','Degrees'
Line 3,761 ⟶ 4,189:
'{0,10:N6} {1,10:N6}' -f $temp, ($temp * 180 / [Math]::PI)
$temp = [Math]::Atan([Math]::Tan($rad))
'{0,10:N6} {1,10:N6}' -f $temp, ($temp * 180 / [Math]::PI)</
{{out}}
<pre> Radians Degrees
Line 3,774 ⟶ 4,202:
I would send the output as an array of objects containing the (<code>[double]</code>) properties: '''Radians''' and '''Degrees'''.
Notice the difference between the last decimal place in the first two objects. If you were calculating coordinates as a civil engineer or land surveyor this difference could affect your measurments. Additionally, the output is an array of objects containing <code>[double]</code> values rather than an array of strings.
<syntaxhighlight lang="powershell">
$radians = [Math]::PI / 4
$degrees = 45
Line 3,790 ⟶ 4,218:
[double]$tempVar = [Math]::Atan([Math]::Tan($radians))
[PSCustomObject]@{Radians=$tempVar; Degrees=$tempVar * 180 / [Math]::PI}
</syntaxhighlight>
{{Out}}
<pre>
Line 3,805 ⟶ 4,233:
=={{header|PureBasic}}==
<
Macro DegToRad(deg)
Line 3,828 ⟶ 4,256:
PrintN(StrF(arctan)+" "+Str(RadToDeg(arctan)))
Input()</
{{out}}
Line 3,845 ⟶ 4,273:
The <tt>math</tt> module also has <tt>degrees()</tt> and <tt>radians()</tt> functions for easy conversion.
<
Type "copyright", "credits" or "license()" for more information.
>>> from math import degrees, radians, sin, cos, tan, asin, acos, atan, pi
Line 3,864 ⟶ 4,292:
>>> print("Arctangent:", arctangent, degrees(arctangent))
Arctangent: 0.7853981633974483 45.0
>>> </
=={{header|Quackery}}==
<code>v**</code> is defined at [[Exponentiation operator#Quackery]].
'''Please note''', the code presented here is sufficient to the task, but is not a practical implementation for the reasons discussed below. The intent of this entry is to invite discussion on the subject of Padé Approximants, the method used here. To that end I have opened a section on the subject in the Discussion page of this task, and invite you to contribute to it if you have useful knowledge of Padé Approximants.
Full disclosure - I am not a mathematician, I am an amateur programmer who has recently heard of Padé Approximants and is desirous of learning more, as they look to be a useful tool, but not a panacea.
A search of Rosetta Code at the time of writing (14 July 2021) finds no references to Padé or Pade on the site. A more general search of the Internet turns up such phrases as "is the "best" approximation of a function by a rational function of given order" and "The unreasonable effectiveness of Pade approximation", which piqued my interest. Generally there are scholarly papers in the subject that whoosh right over my head, and very little at the "pop-maths" level, i.e. no videos by my go-to YouTube channels - numberphile/computerphile, 3blue1brown, mathologer.
In the absence of sources pitched at my level, this is the methodology I have developed to create this code.
''Step 1''. Use Wolfram Alpha to find Padé Approximants for a function. Here is the relevant documentation for Mathematica, which also applies to Wolfram Alpha. Link: [https://reference.wolfram.com/language/ref/PadeApproximant.html PadeApproximant].
Here are the inputs to Wolfram Alpha used in generating this Quackery code. [https://www.wolframalpha.com/input/?i=PadeApproximant%5BSin%5Bx%5D%2C+%7Bx%2C+0%2C+%7B6%2C6%7D%7D%5D sin], [https://www.wolframalpha.com/input/?i=PadeApproximant%5Bcos%5Bx%5D%2C+%7Bx%2C+0%2C+%7B7%2C7%7D%7D%5D cos], [https://www.wolframalpha.com/input/?i=PadeApproximant%5Barccos%5Bx%5D%2C+%7Bx%2C+0%2C+%7B6%2C6%7D%7D%5D tan], [https://www.wolframalpha.com/input/?i=PadeApproximant%5Barcsin%5Bx%5D%2C+%7Bx%2C+0%2C+%7B6%2C6%7D%7D%5D arcsin], [https://www.wolframalpha.com/input/?i=PadeApproximant%5Barccos%5Bx%5D%2C+%7Bx%2C+0%2C+%7B6%2C6%7D%7D%5D+%29 arccos], and [https://www.wolframalpha.com/input/?i=PadeApproximant%5Barctan%5Bx%5D%2C+%7Bx%2C+0%2C+%7B7%2C7%7D%7D%5D arctan].
Note that the exact result for <code>arccos</code> includes several instances of the irrational number π, which is not ideal given that the intent is to generate a rational approximation, so instead I used the identity arccos(x)=π/2-arcsin(x), which Wolfram Alpha lists amongst the "Alternate forms", reducing the number of uses of π to one.
''Step 2''. Use GeoGebra to see the range of arguments over which the Padé approximant is valid, and to identify the range in which it will return values correct to a given number of decimal places. In each of the following examples, function <code>f</code> is a Padé Approximant, function <code>g</code> is the function that <code>f</code> is approximating, and function <code>h</code> is the difference between <code>f</code> and <code>g</code>, multiplied by <code>10^n</code>, where <code>n</code> can be varied with a slider. Where the <code>h</code> line is very close to zero, the approximation will be good to <code>n</code> decimal places.
Your attention is drawn to the task output for <code>arccos</code>, which is only good to a couple of decimal places for the argument passed to it. This is explained by the corresponding graph in Geogebra, where we can see that the argument is outside the safe (i.e. <code>h</code> is close to zero) range for anything other than very small values of <code>n</code>.
Geogebra graphs for the functions defined in this task: [https://www.geogebra.org/m/nygvcs2s sin], [https://www.geogebra.org/m/q3myjbfd cos], [https://www.geogebra.org/m/fsdfzzfs tan], [https://www.geogebra.org/m/n6jctj7c arcsin], [https://www.geogebra.org/m/kkrhjksu arccos], [https://www.geogebra.org/m/ge4qpppf arctan].
''Step 3''. Iterate over steps 1 and 2 until you find appropriate Padé Approximants for the task at hand, or conclude that none exist. Assuming the former;
''Step 4''. Code in a suitable language (i.e. probably not Quackery - efficiency was not a design criterion for Quackery, the language is intended to be the simplest possible introduction to Concatenative/Stack based programming, and is consequently suitable for hobbyist and educational use only) with any obvious optimisations, and use symmetries and identities of the function to extend the range of arguments that can be passed to it. (Not done here - the code serves solely to demonstrate the one-to-one correspondence between a proof-of-concept coding and the formula returned by Wolfram Alpha.)
Note also that the approximation of π/2 is good to 40 decimal places. This is intentional overkill, so that I can be sure that it is not the cause of any inaccuracies. Reducing the size of the numerator and denomination to more sensible values would be part of the optimisation process.
<syntaxhighlight lang="quackery"> [ $" bigrat.qky' loadfile ] now!
[ 2646693125139304345
1684937174853026414 ] is pi/2 ( --> n/d )
[ 2dup
2dup 3 v** 2363 18183 v* v-
2over 5 v** 12671 4363920 v* v+
2swap 1 1
2over 2 v** 445 12122 v* v+
2over 4 v** 601 872784 v* v+
2swap 6 v** 121 16662240 v* v+
v/ ] is sin ( n/d --> n/d )
[ 1 1
2over 2 v** 3665 7788 v* v-
2over 4 v** 711 25960 v* v+
2over 6 v** 2923 7850304 v* v-
2swap 1 1
2over 2 v** 229 7788 v* v+
2over 4 v** 1 2360 v* v+
2swap 6 v** 127 39251520 v* v+
v/ ] is cos ( n/d --> n/d )
[ 2dup
2dup 3 v** 5 39 v* v-
2over 5 v** 2 715 v* v+
2over 7 v** 1 135135 v* v-
2swap 1 1
2over 2 v** 6 13 v* v-
2over 4 v** 10 429 v* v+
2swap 6 v** 4 19305 v* v-
v/ ] is tan ( n/d --> n/d )
[ 2dup
2dup 3 v** 2318543 2278617 v* v-
2over 5 v** 12022609 60763120 v* v+
2swap 1 1
2over 2 v** 1798875 1519078 v* v-
2over 4 v** 3891575 12152624 v* v+
2swap 6 v** 4695545 510410208 v* v-
v/ ] is arcsin ( n/d --> n/d )
[ pi/2 2swap arcsin v- ] is arccos ( n/d --> n/d )
[ 2dup
2dup 3 v** 50 39 v* v+
2over 5 v** 283 715 v* v+
2over 7 v** 256 15015 v* v+
2swap 1 1
2over 2 v** 21 13 v* v+
2over 4 v** 105 143 v* v+
2swap 6 v** 35 429 v* v+
v/ ] is arctan ( n/d --> n/d )
[ pi/2 v* 90 1 v/ ] is deg->rad ( n/d --> n/d )
[ pi/2 v/ 90 1 v* ] is rad->deg ( n/d --> n/d )
say "With an argument of 0.5 radians"
cr cr
$ "0.5" $->v drop
sin
say "Sin approximation: " 20 point$ echo$ cr
say " Actual value: 0.47942553860420300027..."
cr cr
$ "0.5" $->v drop
cos
say "Cos approximation: " 20 point$ echo$ cr
say " Actual value: 0.87758256189037271611..."
cr cr
$ "0.5" $->v drop
tan
say "Tan approximation: " 20 point$ echo$ cr
say " Actual value: 0.54630248984379051325..."
cr cr cr
say "To radians, using approximated values from previous computations"
cr cr
$ "0.47942553860423933121" $->v drop
arcsin
say "Arcsin approximation: " 20 point$ echo$ cr
say " Actual value: 0.5"
cr cr
$ "0.87758256189037190908" $->v drop
arccos
say "Arccos approximation: " 20 point$ echo$ cr
say " Actual value: 0.5"
cr cr
$ "0.54630248984379037103" $->v drop
arctan
say "Arctan approximation: " 20 point$ echo$ cr
say " Actual value: 0.5"
cr cr cr
say "0.5 radians is approx 28.64788976 degrees" cr
cr
$ "28.64788976" $->v drop
deg->rad sin
say "Sin approximation: " 20 point$ echo$ cr
say " Actual value: 0.47942553865718102604..."
cr cr
$ "28.64788976" $->v drop
deg->rad cos
say "Cos approximation: " 20 point$ echo$ cr
say " Actual value: 0.87758256186143068872..."
cr cr
$ "28.64788976" $->v drop
deg->rad tan
say "Tan approximation: " 20 point$ echo$ cr
say " Actual value: 0.54630248992217530618..."
cr cr cr
say "To degrees, using approximated values from previous computations"
cr cr
$ "0.47942553865721735699" $->v drop
arcsin rad->deg
say "Arcsin approximation: " 20 point$ echo$ cr
say " Actual value: 28.64788976..."
cr cr
$ "0.87758256186142988169" $->v drop
arccos rad->deg
say "Arccos approximation: " 20 point$ echo$ cr
say " Actual value: 28.64788976..."
cr cr
$ "0.54630248992217516396" $->v drop
arctan rad->deg
say "Arctan approximation: " 20 point$ echo$ cr
say " Actual value: 28.64788976..."</syntaxhighlight>
{{out}}
<pre>With an argument of 0.5 radians
Sin approximation: 0.47942553860423933121
Actual value: 0.47942553860420300027...
Cos approximation: 0.87758256189037190908
Actual value: 0.87758256189037271611...
Tan approximation: 0.54630248984379037103
Actual value: 0.54630248984379051325...
To radians, using approximated values from previous computations
Arcsin approximation: 0.49999997409078633068
Actual value: 0.5
Arccos approximation: 0.50090902435100642663
Actual value: 0.5
Arctan approximation: 0.50000000390223900073
Actual value: 0.5
0.5 radians is approx 28.64788976 degrees
Sin approximation: 0.47942553865721735699
Actual value: 0.47942553865718102604...
Cos approximation: 0.87758256186142988169
Actual value: 0.87758256186143068872...
Tan approximation: 0.54630248992217516396
Actual value: 0.54630248992217530618...
To degrees, using approximated values from previous computations
Arcsin approximation: 28.64788827551140385372
Actual value: 28.64788976...
Arccos approximation: 28.69997301874556855873
Actual value: 28.64788976...
Arctan approximation: 28.64788998358182581534
Actual value: 28.64788976...
Stack empty.
</pre>
=={{header|R}}==
<
rad <- function(degrees) degrees*pi/180
sind <- function(ang) sin(rad(ang))
Line 3,889 ⟶ 4,525:
print( c( asin(S), asind(S) ) )
print( c( acos(C), acosd(C) ) )
print( c( atan(T), atand(T) ) )</
=={{header|Racket}}==
<
(define radians (/ pi 4))
(define degrees 45)
Line 3,909 ⟶ 4,545:
(define arctan (atan (tan radians)))
(display (format "~a ~a" arctan (* arctan (/ 180 pi))))</
=={{header|Raku}}==
(formerly Perl 6) Borrow the degree to radian routine from [https://rosettacode.org/wiki/Length_of_an_arc_between_two_angles#Raku here].
{{works with|Rakudo|2020.12}}
<syntaxhighlight lang="raku" line># 20210212 Updated Raku programming solution
sub postfix:<°> (\ᵒ) { ᵒ × τ / 360 }
sub postfix:<㎭🡆°> (\ᶜ) { ᶜ / π × 180 }
say sin π/3 ;
say sin 60° ;
say cos π/4 ;
say cos 45° ;
say tan π/6 ;
say tan 30° ;
( asin(3.sqrt/2), acos(1/sqrt 2), atan(1/sqrt 3) )».&{ .say and .㎭🡆°.say }</syntaxhighlight>
{{out}}
<pre>
0.8660254037844386
0.8660254037844386
0.7071067811865476
0.7071067811865476
0.5773502691896257
0.5773502691896257
1.0471975511965976
60
0.7853981633974484
45.00000000000001
0.5235987755982989
30.000000000000004
</pre>
=={{header|RapidQ}}==
<
$TYPECHECK ON
Line 3,944 ⟶ 4,615:
pause("Press any key to continue.")
END 'MAIN</
=={{header|Rapira}}==
<syntaxhighlight lang="rapira">output: sin(pi/2), " ", cos(0), " ", tg(pi/4)</syntaxhighlight>
=={{header|REBOL}}==
<
Title: "Trigonometric Functions"
URL: http://rosettacode.org/wiki/Trigonometric_Functions
Line 3,973 ⟶ 4,647:
arctan: arctangent tangent degrees
print [d2r arctan arctan]</
{{out}}
Line 3,984 ⟶ 4,658:
=={{header|REXX}}==
The REXX language doesn't have any trig functions (or for that matter, a square root [SQRT] function), so if higher math
<br>functions are wanted, you'll have to roll your own. Some of the normal/regular trigonometric functions are included here.
┌──────────────────────────────────────────────────────────────────────────┐
│ One common method that ensures enough accuracy in REXX is specifying │
│ more precision (via NUMERIC DIGITS nnn) than is needed, and then │
│
│
│
└──────────────────────────────────────────────────────────────────────────┘
Most math (POW, EXP, LOG, LN, GAMMA, etc.), trigonometric, and hyperbolic functions need only five extra digits, but ten
<br>extra digits is safer in case the argument is close to an asymptotic point or a multiple or fractional part of pi or somesuch.
It should also be noted that both the '''pi''' and '''e''' constants have only around 77 decimal digits as included here, if more
<br>precision is needed, those constants should be extended. Both '''pi''' and '''e''' could've been shown with more precision,
<br>but having large precision numbers would add to this REXX program's length. If anybody wishes to see this REXX version
<br>of extended digits for '''pi''' or '''e''',
<br>a REXX (external) subroutine is used for such purposes so as to not make the program using the constant unwieldy large.
<
showdigs= 25 /*show only 25 digits of number. */
numeric digits showdigs + 10 /*DIGITS default is 9, but use */
Line 4,054 ⟶ 4,726:
return Asin(x / sqrt(1 + x*x) )
/*──────────────────────────────────────────────────────────────────────────────────────*/
cos: procedure; parse arg x;
/*──────────────────────────────────────────────────────────────────────────────────────*/
sin:
/*──────────────────────────────────────────────────────────────────────────────────────*/
.sinCos:
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); i=; m.=9; h= d+6
Line 4,085 ⟶ 4,757:
return pi /*Note: the actual PI subroutine returns PI's accuracy that */
/*matches the current NUMERIC DIGITS, up to 1 million digits.*/
/*John Machin's formula is used for calculating more digits. */</
Programming note:
╔═════════════════════════════════════════════════════════════════════════════╗
║ Functions that are not included here are (among others): ║
║ ║
║ some of the usual higher-math functions normally associated with trig ║
║ functions: POW, GAMMA, LGGAMMA, ERF, ERFC, ROOT, ATAN2, ║
║ LOG (LN), LOG2, LOG10, and all of the ║
║ hyperbolic trigonometric functions and their inverses (too many to list ║
║ here), ║
║ angle conversions/normalizations: degrees/radians/grads/mils: ║
║ a circle ≡ 2 pi radians ≡ 360 degrees ≡ 400 grads ≡ 6400 mils. ║
║ ║
║ Some of the other trigonometric functions are (hyphens added intentionally):║
║ ║
║ CHORD ║
║ COT (co-tangent) ║
║ CSC (co-secant) ║
║ CVC (co-versed cosine) ║
║ CVS (co-versed sine) ║
║ CXS (co-exsecant) ║
║ HAC (haver-cosine) ║
║ HAV (haver-sine ║
║ SEC (secant) ║
║ VCS (versed cosine or ver-cosine) ║
║ VSN (versed sine or ver-sine) ║
║ XCS (ex-secant) ║
║ COS/SIN/TAN cardinal (damped COS/SIN/TAN functions) ║
║ COS/SIN integral ║
║ ║
║ and all pertinent inverses of the above functions (AVSN, ACVS, ···). ║
╚═════════════════════════════════════════════════════════════════════════════╝
{{out|output}}
Line 4,157 ⟶ 4,829:
=={{header|Ring}}==
<
pi = 3.14
decimals(8)
Line 4,167 ⟶ 4,839:
see "atan(tan(pi/4.0)) = " + atan(tan(pi/4.0)) + nl
see "atan2(3,4) = " + atan2(3,4) + nl
</syntaxhighlight>
=={{header|RPL}}==
RPL has somewhere a system flag that defines if arguments passed to trigonometric functions are in degrees or radians. The words <code>DEG</code> and <code>RAD</code> set the flag appropriately.
We can therefore answer the task so:
π 4 / →NUM 'XRAD' STO
45 'XDEG' STO
XRAD RAD SIN XDEG DEG SIN
which will return <code>.707106781187</code> 2 times.
Another way is to stay in the same trigonometric mode and use <code>D→R</code> or <code>R→D</code> conversion words. This is the way used below:
RAD
π 4 / →NUM SIN 45 D→R SIN
π 3 / →NUM COS 60 D→R COS
π 6 / →NUM TAN 30 D→R TAN
{{out}}
<pre>
6: .707106781187
5: .707106781187
4: .499999999997
3: .499999999997
2: .577350269189
1: .577350269189
</pre>
As we have now in the stack the 6 values to be inversed, let's call the required functions in reverse order. The <code>6 ROLLD</code> instruction pushes the number from level 1 to level 6 of the stack, making thus the next number available for inversion.
ATAN R→D 6 ROLLD
ATAN 6 ROLLD
ACOS R→D 6 ROLLD
ACOS 6 ROLLD
ASIN R→D 6 ROLLD
ASIN 6 ROLLD
{{out}}
<pre>
6: .785398163397
5: 45
4: 1.0471975512
3: 60.0000000002
2: .523598775598
1: 30
</pre>
Calculations made with a HP-28S. Emulator has better precision and returns 60 for <code>60 D→R COS ACOS R→D</code>
=={{header|Ruby}}==
Ruby's <tt>Math</tt> module contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees.
<
degrees = 45.0
Line 4,198 ⟶ 4,908:
#arctangent
arctan = Math.atan(Math.tan(radians))
puts "#{arctan} #{rad2deg(arctan)}"</
{{out}}
Line 4,215 ⟶ 4,925:
{{trans|bc}}
{{works with|Ruby|1.9}}
<
require 'bigdecimal/math' # BigMath
Line 4,277 ⟶ 4,987:
"\n atan(sqrt(3)) = ",
f[ atan(sqrt(b3, prec), prec) / degrees ],
"\n")</
{{out}}
Line 4,298 ⟶ 5,008:
=={{header|Run BASIC}}==
<
deg = 45.0
Line 4,316 ⟶ 5,026:
' This code also works in Liberty BASIC.
' The above (atn(1)/45) = approx .01745329252</
{{out}}
<pre>Ratios for a 45.0 degree angle, (or 0.785398163 radian angle.)
Line 4,326 ⟶ 5,036:
Arccosine: 0.785398163 radians, (or 45.0 degrees)
Arctangent: 0.785398163 radians, (or 45.0 degrees)</pre>
=={{header|Rust}}==
{{trans|Perl}}
<syntaxhighlight lang="rust">// 20210221 Rust programming solution
use std::f64::consts::PI;
fn main() {
let angle_radians: f64 = PI/4.0;
let angle_degrees: f64 = 45.0;
println!("{} {}", angle_radians.sin(), angle_degrees.to_radians().sin());
println!("{} {}", angle_radians.cos(), angle_degrees.to_radians().cos());
println!("{} {}", angle_radians.tan(), angle_degrees.to_radians().tan());
let asin = angle_radians.sin().asin();
println!("{} {}", asin, asin.to_degrees());
let acos = angle_radians.cos().acos();
println!("{} {}", acos, acos.to_degrees());
let atan = angle_radians.tan().atan();
println!("{} {}", atan, atan.to_degrees());
}</syntaxhighlight>
{{out}}
<pre>
0.7071067811865475 0.7071067811865475
0.7071067811865476 0.7071067811865476
0.9999999999999999 0.9999999999999999
0.7853981633974482 44.99999999999999
0.7853981633974483 45
0.7853981633974483 45
</pre>
=={{header|SAS}}==
<
pi = 4*atan(1);
deg = 30;
Line 4,358 ⟶ 5,100:
b=atan(x)/k;
put a b;
run;</
=={{header|Scala}}==
{{libheader|Scala}}<
object Gonio extends App {
Line 4,383 ⟶ 5,125:
val bgtan2 = atan2(1, 1)
println(s"$bgtan ${toDegrees(bgtan)}")
}</
=={{header|Scheme}}==
<
(define radians (/ pi 4))
Line 4,422 ⟶ 5,164:
(display " ")
(display (* arctan (/ 180 pi)))
(newline)</
=={{header|Seed7}}==
Line 4,434 ⟶ 5,176:
[http://seed7.sourceforge.net/libraries/math.htm#atan%28ref_float%29 atan].
<
include "float.s7i";
include "math.s7i";
Line 4,450 ⟶ 5,192:
writeln("arccosine: " <& acos(0.70710677) digits 5 <& acos(0.70710677) * 180.0 / PI digits 5 lpad 9);
writeln("arctangent: " <& atan(1.0) digits 5 <& atan(1.0) * 180.0 / PI digits 5 lpad 9);
end func;</
{{out}}
Line 4,464 ⟶ 5,206:
=={{header|Sidef}}==
<
var angle_rad = Num.pi/4;
Line 4,483 ⟶ 5,225:
] {
say [n, rad2deg(n)].join(' ');
}</
{{out}}
<pre>
Line 4,494 ⟶ 5,236:
0.785398163397448 45
0.785398163397448 45
</pre>
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "trig" )
@( description, "If your language has a library or built-in " )
@( description, "functions for trigonometry, show examples of: ")
@( description, "sine, cosine, tangent, inverses (of the above) " )
@( description, "using the same angle in radians and degrees." )
@( description, "" )
@( description, "For the non-inverse functions, each radian/" )
@( description, "degree pair should use arguments that evaluate to " )
@( description, "the same angle (that is, it's not necessary to " )
@( description, "use the same angle for all three regular " )
@( description, "functions as long as the two sine calls use the " )
@( description, "same angle). For the inverse functions, use " )
@( description, "the same number and convert its answer to radians " )
@( description, "and degrees." )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
@( see_also, "http://rosettacode.org/wiki/Trigonometric_functions" );
pragma license( unrestricted );
pragma software_model( nonstandard );
pragma restriction( no_external_commands );
procedure trig is
degrees_cycle : constant float := 360.0;
radians_cycle : constant float := 2.0 * float( numerics.pi );
angle_degrees : constant float := 45.0;
angle_radians : constant float := float( numerics.pi ) / 4.0;
begin
put( "Sin " )
@( numerics.sin( angle_degrees, degrees_cycle ) )
@( numerics.sin( angle_radians, radians_cycle ) );
new_line;
put( "Cos " )
@( numerics.cos( angle_degrees, degrees_cycle ) )
@( numerics.cos( angle_radians, radians_cycle ) );
new_line;
put( "Tan " )
@( numerics.tan( angle_degrees, degrees_cycle ) )
@( numerics.tan( angle_radians, radians_cycle ) );
new_line;
put( "Cot " )
@( numerics.cot( angle_degrees, degrees_cycle ) )
@( numerics.cot( angle_radians, radians_cycle ) );
new_line;
put( "Arcsin" )
@( numerics.arcsin( numerics.sin( angle_degrees, degrees_cycle ), degrees_cycle ) )
@( numerics.arcsin( numerics.sin( angle_radians, radians_cycle ), radians_cycle ) );
new_line;
put( "Arccos" )
@( numerics.arccos( numerics.cos( angle_degrees, degrees_cycle ), degrees_cycle ) )
@( numerics.arccos( numerics.cos( angle_radians, radians_cycle ), radians_cycle ) );
new_line;
put( "Arctan" )
@( numerics.arctan( numerics.tan( angle_degrees, degrees_cycle ), 1, degrees_cycle ) )
@( numerics.arctan( numerics.tan( angle_radians, radians_cycle ), 1, radians_cycle ) );
new_line;
put( "Arccot" )
@( numerics.arccot( numerics.cot( angle_degrees, degrees_cycle ), 1, degrees_cycle ) )
@( numerics.arccot( numerics.cot( angle_radians, radians_cycle ), 1, radians_cycle ) );
new_line;
command_line.set_exit_status( 0 );
end trig;</syntaxhighlight>
{{out}}
<pre>
$ spar trig
Sin 7.07106781186547E-01 7.07106781186547E-01
Cos 7.07106781186547E-01 7.07106781186548E-01
Tan 1.00000000000000E+00 9.99999999999998E-01
Cot 1.00000000000000E+00 1.00000000000000E+00
Arcsin 4.50000000000000E+01 7.85398163397448E-01
Arccos 4.50000000000000E+01 7.85398163397448E-01
Arctan 45 7.85398163397448E-01
Arccot 45 7.85398163397449E-01</pre>
=={{header|SQL PL}}==
{{works with|Db2 LUW}}
With SQL only:
<syntaxhighlight lang="sql pl">
--Conversion
values degrees(3.1415926);
values radians(180);
-- This is equal to Pi.
--PI/4 45
values sin(radians(180)/4);
values sin(radians(45));
values cos(radians(180)/4);
values cos(radians(45));
values tan(radians(180)/4);
values tan(radians(45));
values cot(radians(180)/4);
values cot(radians(45));
values asin(sin(radians(180)/4));
values asin(sin(radians(45)));
values atan(tan(radians(180)/4));
values atan(tan(radians(45)));
--PI/3 60
values sin(radians(180)/3);
values sin(radians(60));
values cos(radians(180)/3);
values cos(radians(60));
values tan(radians(180)/3);
values tan(radians(60));
values cot(radians(180)/3);
values cot(radians(60));
values asin(sin(radians(180)/3));
values asin(sin(radians(60)));
values atan(tan(radians(180)/3));
values atan(tan(radians(60)));
</syntaxhighlight>
Output:
<pre>
db2 -tx
values degrees(3.1415926)
+1.79999996929531E+002
values radians(180)
+3.14159265358979E+000
values sin(radians(180)/4)
+7.07106781186547E-001
values sin(radians(45))
+7.07106781186547E-001
values cos(radians(180)/4)
+7.07106781186548E-001
values cos(radians(45))
+7.07106781186548E-001
values tan(radians(180)/4)
+1.00000000000000E+000
values tan(radians(45))
+1.00000000000000E+000
values cot(radians(180)/4)
+1.00000000000000E+000
values cot(radians(45))
+1.00000000000000E+000
values asin(sin(radians(180)/4))
+7.85398163397448E-001
values asin(sin(radians(45)))
+7.85398163397448E-001
values atan(tan(radians(180)/4))
+7.85398163397448E-001
values atan(tan(radians(45)))
+7.85398163397448E-001
values sin(radians(180)/3)
+8.66025403784439E-001
values sin(radians(60))
+8.66025403784439E-001
values cos(radians(180)/3)
+5.00000000000000E-001
values cos(radians(60))
+5.00000000000000E-001
values tan(radians(180)/3)
+1.73205080756888E+000
values tan(radians(60))
+1.73205080756888E+000
values cot(radians(180)/3)
+5.77350269189626E-001
values cot(radians(60))
+5.77350269189626E-001
values asin(sin(radians(180)/3))
+1.04719755119660E+000
values asin(sin(radians(60)))
+1.04719755119660E+000
values atan(tan(radians(180)/3))
+1.04719755119660E+000
values atan(tan(radians(60)))
+1.04719755119660E+000
</pre>
Line 4,499 ⟶ 5,445:
Stata computes only in radians, but the conversion is easy.
<
display cos(30*deg)
Line 4,511 ⟶ 5,457:
display acos(0.5)
display asin(0.5)
display atan(0.5)</
=={{header|Tcl}}==
The built-in functions only take radian arguments.
<
proc PI {} {expr {4*atan(1)}}
Line 4,532 ⟶ 5,478:
set arctan [atan [tan $radians]]; puts "$arctan [rad2deg $arctan]"
}
trig 60.0</
<pre>0.8660254037844386
0.5000000000000001
Line 4,539 ⟶ 5,485:
1.0471975511965976 59.99999999999999
1.0471975511965976 59.99999999999999</pre>
=={{header|VBA}}==
<syntaxhighlight lang="vb">Public Sub trig()
Pi = WorksheetFunction.Pi()
Debug.Print Sin(Pi / 2)
Debug.Print Sin(90 * Pi / 180)
Debug.Print Cos(0)
Debug.Print Cos(0 * Pi / 180)
Debug.Print Tan(Pi / 4)
Debug.Print Tan(45 * Pi / 180)
Debug.Print WorksheetFunction.Asin(1) * 2
Debug.Print WorksheetFunction.Asin(1) * 180 / Pi
Debug.Print WorksheetFunction.Acos(0) * 2
Debug.Print WorksheetFunction.Acos(0) * 180 / Pi
Debug.Print Atn(1) * 4
Debug.Print Atn(1) * 180 / Pi
End Sub</syntaxhighlight>{{out}}
<pre> 1
1
1
1
1
1
3,14159265358979
90
3,14159265358979
90
3,14159265358979
45
</pre>
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
Sub Main()
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)
End Sub
End Module</syntaxhighlight>
{{out}}
<pre>=== radians ===
sin (pi/3) = 0.866025403784439
cos (pi/3) = 0.5
tan (pi/3) = 1.73205080756888
arcsin (1/2) = 0.523598775598299
arccos (1/2) = 1.0471975511966
arctan (1/2) = 0.463647609000806
=== degrees ===
sin (60) = 0.866025403784439
cos (60) = 0.5
tan (60) = 1.73205080756888
arcsin (1/2) = 30
arccos (1/2) = 60
arctan (1/2) = 26.565051177078</pre>
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
var d = 30
var r = d * Num.pi / 180
var s = 0.5
var c = 3.sqrt / 2
var t = 1 / 3.sqrt
Fmt.print("sin($9.6f deg) = $f", d, (d*Num.pi/180).sin)
Fmt.print("sin($9.6f rad) = $f", r, r.sin)
Fmt.print("cos($9.6f deg) = $f", d, (d*Num.pi/180).cos)
Fmt.print("cos($9.6f rad) = $f", r, r.cos)
Fmt.print("tan($9.6f deg) = $f", d, (d*Num.pi/180).tan)
Fmt.print("tan($9.6f rad) = $f", r, r.tan)
Fmt.print("asin($f) = $9.6f deg", s, s.asin*180/Num.pi)
Fmt.print("asin($f) = $9.6f rad", s, s.asin)
Fmt.print("acos($f) = $9.6f deg", c, c.acos*180/Num.pi)
Fmt.print("acos($f) = $9.6f rad", c, c.acos)
Fmt.print("atan($f) = $9.6f deg", t, t.atan*180/Num.pi)
Fmt.print("atan($f) = $9.6f rad", t, t.atan)</syntaxhighlight>
{{out}}
<pre>
sin(30.000000 deg) = 0.500000
sin( 0.523599 rad) = 0.500000
cos(30.000000 deg) = 0.866025
cos( 0.523599 rad) = 0.866025
tan(30.000000 deg) = 0.577350
tan( 0.523599 rad) = 0.577350
asin(0.500000) = 30.000000 deg
asin(0.500000) = 0.523599 rad
acos(0.866025) = 30.000000 deg
acos(0.866025) = 0.523599 rad
atan(0.577350) = 30.000000 deg
atan(0.577350) = 0.523599 rad
</pre>
=={{header|XPL0}}==
<
def Pi = 3.14159265358979323846;
Line 4,567 ⟶ 5,623:
RlOut(0, ACos(B)); ChOut(0, 9\tab\); RlOut(0, Deg(ACos(B))); CrLf(0);
RlOut(0, ATan(C)); ChOut(0, 9\tab\); RlOut(0, Deg(ATan(C))); CrLf(0);
]</
{{out}}
Line 4,580 ⟶ 5,636:
=={{header|zkl}}==
<
(30.0).toRad().sin() //-->0.5
(60.0).toRad().cos() //-->0.5
Line 4,591 ⟶ 5,647:
(1.0).atan() //-->0.785398
(1.0).atan().toDeg() //-->45
etc</
=={{header|ZX Spectrum Basic}}==
The ZX Spectrum ROM only calculates sine and arctangent directly (via Chebyshev polynomials), and uses internal functions of these (and the square root) to generate the other functions. In particular, arcsin x is calculated as arctan ( x / ( sqrt ( 1 - x * x ) ) + 1 ) / 2, which is why some of these functions are legendarily slow.
<syntaxhighlight lang="zxbasic">10 DEF FN d(a)=a*PI/180:REM convert degrees to radians; all ZX Spectrum trig calculations are done in radians
20 DEF FN i(r)=180*r/PI:REM convert radians to degrees for inverse functions
30 LET d=45
40 LET r=PI/4
50 PRINT SIN r,SIN FN d(d)
60 PRINT COS r,COS FN d(d)
70 PRINT TAN r,TAN FN d(d)
80 PRINT
90 LET d=.5
110 PRINT ASN d,FN i(ASN d)
120 PRINT ACS d,FN i(ACS d)
130 PRINT ATN d,FN i(ATN d)</syntaxhighlight>
{{out}}
<pre>
0.70710678 0.70710678
0.70710678 0.70710678
1 1
0.52359878 30
1.0471976 60
0.46364761 26.565051
0 OK, 130:1
</pre>
{{omit from|Batch File|No access to advanced math.}}
|