Math.Cos, Sin and TanUse the Math.Acos, Cos, Cosh, Asin, Sin, Sinh, Atan, Tan and Tanh methods.
C#
Cos, Sin, Tan. Trigonometric functions are available in .NET. We test them for correctness. They are found in the Math type in the System namespace.
We examine the Acos, Cos, Cosh, Asin, Sin, Sinh, Atan, Tan and Tanh methods. For an optimization, we can sometimes use a switch-statement with precomputed values.
Memoization
Math
To begin, we present a program that simply calls Acos, Cos, Cosh, Asin, Sin, Sinh, Atan, Tan and Tanh. All of these methods receive arguments in units of radians represented by the double type.
double
Next In this program, the C# compiler implicitly converts the integer arguments into double arguments.
using System; class Program { static void Main() { Console.WriteLine(Math.Acos(0)); // Inverse cosine Console.WriteLine(Math.Cos(2)); // Cosine Console.WriteLine(Math.Cosh(5)); // Hyperbolic cosine Console.WriteLine(Math.Asin(0.2)); // Inverse sine Console.WriteLine(Math.Sin(2)); // Sine Console.WriteLine(Math.Sinh(-5)); // Hyperbolic sine Console.WriteLine(Math.Atan(-5)); // Inverse tangent Console.WriteLine(Math.Tan(1)); // Tangent Console.WriteLine(Math.Tanh(0.1)); // Hyperbolic tangent } }
1.5707963267949 -0.416146836547142 74.2099485247878 0.201357920790331 0.909297426825682 -74.2032105777888 -1.37340076694502 1.5574077246549 0.0996679946249558
Benchmark. I decided to benchmark Math.Sin to see if it was worth optimization. This next program shows an optimized method and benchmarks it.
Version 1 This version of the code uses the Math.Sin method directly. It does not require a custom method.
Version 2 I developed a method called SinFast. It replaces Math.Sin calls on small ints with a switch-statement.
Switch
Case
Result The optimized method, on the expected inputs, performs almost ten times faster.
Benchmark
using System; using System.Diagnostics; class Program { /// <summary> /// Optimized version of Sin. /// </summary> static double SinFast(int value) { switch (value) { case 0: return 0; case 1: return 0.841470984807897; case 2: return 0.909297426825682; case 3: return 0.141120008059867; case 4: return -0.756802495307928; case 5: return -0.958924274663138; case 6: return -0.279415498198926; case 7: return 0.656986598718789; case 8: return 0.989358246623382; case 9: return 0.412118485241757; default: return Math.Sin(value); } } const int _max = 1000000; static void Main() { var s1 = Stopwatch.StartNew(); // Version 1: use Math.sin. for (int i = 0; i < _max; i++) { for (int a = 0; a < 10; a++) { double v = Math.Sin(a); if (v == -1) { throw new Exception(); } } } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: use SinFast. for (int i = 0; i < _max; i++) { for (int a = 0; a < 10; a++) { double v = Program.SinFast(a); if (v == -1) { throw new Exception(); } } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); } }
301.76 ns 35.82 ns
A summary. We can apply the standard trigonometric methods, including Acos, Cos, Cosh, Asin, Sin, Sinh, Atan, Tan, and Tanh from .NET.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.