Trigonometric functions: Difference between revisions
Content added Content deleted
Kinitawowi (talk | contribs) (add ZX Spectrum) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 747: | Line 747: | ||
0.785398 45.000000 |
0.785398 45.000000 |
||
</pre> |
</pre> |
||
=={{header|C sharp|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> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 781: | Line 808: | ||
}</lang> |
}</lang> |
||
=={{header|C sharp|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> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 1,066: | Line 1,067: | ||
{{out}} |
{{out}} |
||
true |
true |
||
=={{header|F_Sharp|F#}}== |
|||
<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</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 1,312: | Line 1,437: | ||
Arc Tangent : 0.7853981633974483 45 |
Arc Tangent : 0.7853981633974483 45 |
||
</pre> |
</pre> |
||
=={{header|F_Sharp|F#}}== |
|||
<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</lang> |
|||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
Line 2,402: | Line 2,403: | ||
(Lacking in this code but present in GNU Octave: sinh, cosh, tanh, coth and inverses) |
(Lacking in this code but present in GNU Octave: sinh, cosh, tanh, coth and inverses) |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
Line 3,700: | Line 3,700: | ||
0.785398163397448 45 |
0.785398163397448 45 |
||
</pre> |
</pre> |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2016.01}} |
|||
<lang perl6>say sin(pi/3); |
|||
say cos(pi/4); |
|||
say tan(pi/6); |
|||
say asin(sqrt(3)/2); |
|||
say acos(1/sqrt 2); |
|||
say atan(1/sqrt 3);</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 4,053: | Line 4,042: | ||
(define arctan (atan (tan radians))) |
(define arctan (atan (tan radians))) |
||
(display (format "~a ~a" arctan (* arctan (/ 180 pi))))</lang> |
(display (format "~a ~a" arctan (* arctan (/ 180 pi))))</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|Rakudo|2016.01}} |
|||
<lang perl6>say sin(pi/3); |
|||
say cos(pi/4); |
|||
say tan(pi/6); |
|||
say asin(sqrt(3)/2); |
|||
say acos(1/sqrt 2); |
|||
say atan(1/sqrt 3);</lang> |
|||
=={{header|RapidQ}}== |
=={{header|RapidQ}}== |
||
Line 4,831: | Line 4,832: | ||
45 |
45 |
||
</pre> |
</pre> |
||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |