MathTrigonometric 2.1.0

dotnet add package MathTrigonometric --version 2.1.0
                    
NuGet\Install-Package MathTrigonometric -Version 2.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MathTrigonometric" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MathTrigonometric" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="MathTrigonometric" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MathTrigonometric --version 2.1.0
                    
#r "nuget: MathTrigonometric, 2.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MathTrigonometric@2.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MathTrigonometric&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=MathTrigonometric&version=2.1.0
                    
Install as a Cake Tool

Math Trigonometric Functions in .NET

NuGet package: MathTrigonometric

NuGet Downloads NuGet Version

Overview

This C# library provides implementations of all standard trigonometric functions, including basic functions like sine, cosine, and tangent, as well as their hyperbolic counterparts, inverse functions, and more. This library is designed to offer a comprehensive set of tools for mathematical, engineering, and scientific applications requiring trigonometric calculations.

Missing Trigonometric Functions in .NET

The .NET includes basic trigonometric functions (sin, cos, tan, asin, acos, atan) and their hyperbolic counterparts (sinh, cosh, tanh, asinh, acosh, atanh) in the Math class. However, the following trigonometric functions are missing in .NET and are implemented in this library:

  1. Cot (Cotangent)
  2. Sec (Secant)
  3. Csc (Cosecant)
  4. Acot (Inverse Cotangent)
  5. Asec (Inverse Secant)
  6. Acsc (Inverse Cosecant)
  7. Coth (Hyperbolic Cotangent)
  8. Sech (Hyperbolic Secant)
  9. Csch (Hyperbolic Cosecant)
  10. Acoth (Inverse Hyperbolic Cotangent)
  11. Asech (Inverse Hyperbolic Secant)
  12. Acsch (Inverse Hyperbolic Cosecant)

You can find a detailed explanation of the implementation and approach in my article on Medium.

Functions Included

In version 1.1.0, support for Complex numbers has been introduced through overloaded methods for the trigonometric functions listed below. This allows the library to handle both real and complex inputs seamlessly.

In version 1.3.0, support for float and Half numbers has been introduced through overloaded.

In version 2.0.0, on .NET 11 and higher, support for the generic Complex<T> has been introduced through generic overloads of all the functions listed below. They work with any T that Complex<T> accepts, including the IEEE 754 decimal types Decimal32, Decimal64 and Decimal128, as well as float, Half, BFloat16 and double.

Version 2.0.0 is a breaking release: it also corrects the complex functions and makes their results identical on every .NET version. Some results change, in particular on branch cuts:

  • Inverse functions return principal values. For example, Acosh and Asech previously returned the negated value in the left half-plane, such as Acosh(-2) = -1.317 + πi instead of 1.317 + πi.
  • On a branch cut, the sign of a zero component selects the side, following IEEE 754 and ISO C99 Annex G. For example, Acos(new Complex(2, 0)) = 0 − 1.317i and Acos(new Complex(2, -0.0)) = 0 + 1.317i.
  • Infinite arguments return the limit value where one exists, for example Atan(2 − ∞i) = π/2 and Tan(x + ∞i) = i.
  • Results no longer depend on the runtime's System.Numerics.Complex, whose edge-case behavior changed in .NET 11.
  • Accuracy is within a few ulps, including for tiny and huge arguments and near poles. The real Asinh, Acosh, Atanh, Acsch, Asech, Acoth, Acsc, Asec, Csch and Sech no longer overflow or lose precision for extreme arguments.

In version 2.1.0, on .NET 8 and higher, generic overloads of all the real functions listed below have been introduced, such as T Sec<T>(T x) where T : IFloatingPointIeee754<T>, IMinMaxValue<T>. They work with float and Half and, on .NET 11 and higher, with BFloat16, Decimal32, Decimal64 and Decimal128. Existing code is not affected: double arguments still use the double overloads, and the float and Half overloads are kept and return the same results as before. For types narrower than double (float, Half, BFloat16, Decimal32), functions built from several operations are evaluated in double precision, so the result is the same whether a float overload or the generic overload is called.

Basic Trigonometric Functions

  1. Sin

    Sine of the angle is ratio of the opposite leg to hypotenuse.

    double Sin(double a);
    
    • Input: Angle in radians (any real number).
    • Output: The sine of the input angle in range: [-1, 1].
  2. Cos

    Cosine of the angle is ratio of the adjacent leg to hypotenuse.

     double Cos(double a);
    
    • Input: Angle in radians (any real number).
    • Output: The cosine of the input angle in range: [-1, 1].
  3. Tan

    Tangent of the angle is ratio of the opposite leg to adjacent one.

     double Tan(double a);
    
    • Input: Angle in radians (any real number).
    • Output: The tangent of the input angle (any real number).
  4. Cot

    Cotangent of the angle is ratio of the adjacent leg to opposite one.

     double Cot(double a);
    
    • Input: Angle in radians (any real number).
    • Output: The cotangent of the input angle (any real number).
  5. Sec

    Secant of the angle is ratio of the hypotenuse to adjacent leg.

     double Sec(double a);
    
    • Input: Angle in radians (any real number).
    • Output: The secant of the input angle in range: (-∞, -1] ∪ [1, ∞).
  6. Csc

    Cosecant of the angle is ratio of the hypotenuse to opposite leg.

     double Csc(double a);
    
    • Input: Angle in radians (any real number).
    • Output: The cosecant of the input angle in range: (-∞, -1] ∪ [1, ∞).

Inverse Trigonometric Functions

  1. Asin

    Arc sine is inverse of the Sine function.

     double Asin(double d);
    
    • Input: Value in range: [-1, 1].
    • Output: Angle in radians is limited to the range [−π/2, π/2].
  2. Acos

    Arc cosine is inverse of the Cosine function.

     double Acos(double d);
    
    • Input: Value in range: [-1, 1].
    • Output: Angle in radians is limited to the range [0, π].
  3. Atan

    Arc tangent is inverse of the Tangent function.

     double Atan(double d);
    
    • Input: Any real number.
    • Output: Angle in radians is limited to the range (−π/2, π/2).
  4. Acot

    Arc cotangent is inverse of the Cotangent function.

    double Acot(double d);
    
    • Input: Any real number.
    • Output: Angle in radians is limited to the range (0, π).
  5. Asec

    Arc secant is inverse of the Secant function.

    double Asec(double d);
    
    • Input: Value in range: (-∞, -1] ∪ [1, ∞).
    • Output: Angle in radians is limited to the range [0, π/2)∪(π/2, π].
  6. Acsc

    Arc cosecant is inverse of the Cosecant function.

    double Acsc(double d);
    
    • Input: Value in range: (-∞, -1] ∪ [1, ∞).
    • Output: Angle in radians is limited to the range [−π/2, 0)∪(0, π/2].

Hyperbolic Trigonometric Functions

  1. Sinh

    Hyperbolic sine is defined as Sinh(x) = (e^x − e^−x)/2.

    double Sinh(double x);
    
    • Input: Any real number.
    • Output: Value (any real number).
  2. Cosh

    Hyperbolic cosine is defined as Cosh(x) = (e^x + e^−x)/2.

    double Cosh(double x);
    
    • Input: Any real number.
    • Output: Value in range: [1, +∞).
  3. Tanh

    Hyperbolic tangent is defined as Tanh(x) = (e^x − e^−x)/(e^x + e^−x).

    double Tanh(double x);
    
    • Input: Any real number.
    • Output: Value in range: (-1, 1).
  4. Coth

    Hyperbolic cotangent is defined as Coth(x) = (e^x + e^−x)/(e^x − e^−x).

    double Coth(double x);
    
    • Input: Value in range: (−∞, 0)∪(0, +∞).
    • Output: Value in range: (−∞, -1)∪(1, +∞).
  5. Sech

    Hyperbolic secant is defined as Sech(x) = 2/(e^x + e^−x).

    double Sech(double x);
    
    • Input: Any real number.
    • Output: Value in range: (0, 1].
  6. Csch

    Hyperbolic cosecant is defined as Csch(x) = 2/(e^x − e^−x).

    double Csch(double x);
    
    • Input: Value in range: (−∞, 0)∪(0, +∞).
    • Output: Value in range: (−∞, 0)∪(0, +∞).

Inverse Hyperbolic Trigonometric Functions

  1. Asinh

    Arc-hyperbolic sine is inverse of the Hyperbolic sine function is defined as Arsinh(x) = ln[x + √(x^2 + 1)].

    double Asinh(double x);
    
    • Input: Any real number.
    • Output: Value (any real number).
  2. Acosh

    Arc-hyperbolic cosine is inverse of the Hyperbolic cosine function is defined as Arcosh(x) = ln[x + √(x^2 - 1)].

    double Acosh(double x);
    
    • Input: Value in range: [1, +∞).
    • Output: Value in range: [0, +∞).
  3. Atanh

    Arc-hyperbolic tangent is inverse of the Hyperbolic tangent function is defined as Artanh(x) = ln[(1 + x)/(1 − x)]/2.

    double Atanh(double x);
    
    • Input: Value in range: (-1, 1).
    • Output: Value (any real number).
  4. Acoth

    Arc-hyperbolic cotangent is inverse of the Hyperbolic cotangent function is defined as Arcoth(x) = ln[(1 + x)/(x − 1)]/2.

    double Acoth(double x);
    
    • Input: Value in range: (−∞, -1)∪(1, +∞).
    • Output: Value in range: (−∞, 0)∪(0, +∞).
  5. Asech

    Arc-hyperbolic secant is inverse of the Hyperbolic secant function is defined as Arsech(x) = ln([1 + √(1 − x^2)]/x).

    double Asech(double x);
    
    • Input: Value in range: (0, 1].
    • Output: Value in range: [0, +∞).
  6. Acsch

    Arc-hyperbolic cosecant is inverse of the Hyperbolic cosecant function is defined as Arcsch(x) = ln[1/x + √(1/(x^2) + 1)].

    double Acsch(double x);
    
    • Input: Value in range: (−∞, 0)∪(0, +∞).
    • Output: Value in range: (−∞, -0)∪(0, +∞).

Extra functions

  1. DegreesToRadians

    Converts degrees to radians.

    double DegreesToRadians(double degrees);
    
    • Input: Angle in degrees (any real number).
    • Output: Angle in radians (any real number).
  2. RadiansToDegrees

    Converts radians to degrees.

    double RadiansToDegrees(double radians);
    
    • Input: Angle in radians (any real number).
    • Output: Angle in degrees (any real number).

How to use trigonometry in C#

Here are some examples of how to use the trigonometric functions in this library:

Basic Trigonometric Functions

using MathTrigonometric;

class Program
{
    static void Main()
    {
        double angle = Math.PI / 4; // 45 degrees in radians

        double sine = MathTrig.Sin(angle);
        double cosine = MathTrig.Cos(angle);
        double tangent = MathTrig.Tan(angle);
        double cotangent = MathTrig.Cot(angle);
        double secant = MathTrig.Sec(angle);
        double cosecant = MathTrig.Csc(angle);

        Console.WriteLine($"Sin({angle}) = {sine}");
        Console.WriteLine($"Cos({angle}) = {cosine}");
        Console.WriteLine($"Tan({angle}) = {tangent}");
        Console.WriteLine($"Cot({angle}) = {cotangent}");
        Console.WriteLine($"Sec({angle}) = {secant}");
        Console.WriteLine($"Csc({angle}) = {cosecant}");
    }
}

Inverse Trigonometric Functions

using MathTrigonometric;

class Program
{
    static void Main()
    {
        double value = 0.5;

        double angleAsin = MathTrig.Asin(value);
        double angleAcos = MathTrig.Acos(value);
        double angleAtan = MathTrig.Atan(value);
        double angleAcot = MathTrig.Acot(value);
        double angleAsec = MathTrig.Asec(2); // sec(π/3) = 2
        double angleAcsc = MathTrig.Acsc(2); // csc(π/6) = 2

        Console.WriteLine($"Asin({value}) = {angleAsin}");
        Console.WriteLine($"Acos({value}) = {angleAcos}");
        Console.WriteLine($"Atan({value}) = {angleAtan}");
        Console.WriteLine($"Acot({value}) = {angleAcot}");
        Console.WriteLine($"Asec(2) = {angleAsec}");
        Console.WriteLine($"Acsc(2) = {angleAcsc}");
    }
}

Hyperbolic Trigonometric Functions

using MathTrigonometric;

class Program
{
    static void Main()
    {
        double value = 1.0;

        double sinh = MathTrig.Sinh(value);
        double cosh = MathTrig.Cosh(value);
        double tanh = MathTrig.Tanh(value);
        double coth = MathTrig.Coth(value);
        double sech = MathTrig.Sech(value);
        double csch = MathTrig.Csch(value);

        Console.WriteLine($"Sinh({value}) = {sinh}");
        Console.WriteLine($"Cosh({value}) = {cosh}");
        Console.WriteLine($"Tanh({value}) = {tanh}");
        Console.WriteLine($"Coth({value}) = {coth}");
        Console.WriteLine($"Sech({value}) = {sech}");
        Console.WriteLine($"Csch({value}) = {csch}");
    }
}

Inverse Hyperbolic Trigonometric Functions

using MathTrigonometric;

class Program
{
    static void Main()
    {
        double value = 0.5;

        double asinh = MathTrig.Asinh(value);
        double acosh = MathTrig.Acosh(1.5);
        double atanh = MathTrig.Atanh(value);
        double acoth = MathTrig.Acoth(2);
        double asech = MathTrig.Asech(value);
        double acsch = MathTrig.Acsch(2);

        Console.WriteLine($"Asinh({value}) = {asinh}");
        Console.WriteLine($"Acosh(1.5) = {acosh}");
        Console.WriteLine($"Atanh({value}) = {atanh}");
        Console.WriteLine($"Acoth(2) = {acoth}");
        Console.WriteLine($"Asech({value}) = {asech}");
        Console.WriteLine($"Acsch(2) = {acsch}");
    }
}

Generic Real Numbers (.NET 8+)

using System.Numerics;
using MathTrigonometric;

class Program
{
    static void Main()
    {
        float sec = MathTrig.Sec(1f);
        Half acoth = MathTrig.Acoth((Half)2);
        Decimal64 acsch = MathTrig.Acsch(Decimal64.Parse("0.5")); // .NET 11+

        Console.WriteLine($"Sec(1) = {sec}");
        Console.WriteLine($"Acoth(2) = {acoth}");
        Console.WriteLine($"Acsch(0.5) = {acsch}");
    }

    // works for any IEEE 754 floating-point type
    static T Hypotenuse<T>(T leg, T angle)
        where T : IFloatingPointIeee754<T>, IMinMaxValue<T>
        => leg * MathTrig.Csc(angle);
}

Generic Complex Numbers (.NET 11+)

using System.Numerics;
using MathTrigonometric;

class Program
{
    static void Main()
    {
        var z = new Complex<Decimal64>(Decimal64.Parse("0.1"), Decimal64.Parse("0.2"));

        Complex<Decimal64> cot = MathTrig.Cot(z);
        Complex<Decimal64> asech = MathTrig.Asech(z);
        Complex<float> sec = MathTrig.Sec(new Complex<float>(1f, 2f));

        Console.WriteLine($"Cot({z}) = {cot}");
        Console.WriteLine($"Asech({z}) = {asech}");
        Console.WriteLine($"Sec(1 + 2i) = {sec}");
    }
}

Contributing

Contributions are welcome! Please fork the repository and submit pull requests for any enhancements or bug fixes. If you enjoy my work and find it valuable, please consider becoming my sponsor on GitHub. Your support will enable me to share more open-source code. Together, we can make a positive impact in the developer community!

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contact

If you have any questions or suggestions, feel free to open an issue or contact me directly.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net10.0

    • No dependencies.
  • net11.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on MathTrigonometric:

Package Downloads
MathEvaluator

The MathEvaluator .NET library allows you to evaluate and compile any mathematical expressions from a string dynamically. It supports a wide range of operations and allows for the use of custom variables, operators, and functions. The evaluator can be configured for different contexts, such as scientific or programming math expressions, making it highly versatile for various use cases. This flexibility, combined with its high performance, makes it an excellent choice for developers needing a robust mathematical evaluation tool.

MathEvaluator.Fork

The MathEvaluator .NET library allows you to evaluate and compile any mathematical expressions from a string dynamically. It supports a wide range of operations and allows for the use of custom variables, operators, and functions. The evaluator can be configured for different contexts, such as scientific or programming math expressions, making it highly versatile for various use cases. This flexibility, combined with its high performance, makes it an excellent choice for developers needing a robust mathematical evaluation tool.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.0 51 11/21/2026
2.0.0 98 11/15/2026
1.3.0 7,045 9/30/2026
1.2.0 151,928 12/21/2024
1.1.0 8,073 9/27/2024
1.0.7 2,017 6/19/2024
1.0.6 284 6/19/2024
1.0.5 325 6/15/2024
1.0.4 269 6/14/2024
1.0.3 254 6/14/2024
1.0.2 269 6/14/2024
1.0.1 284 6/14/2024
1.0.0 238 6/14/2024

It targets .NET Standard 2.0 and higher version. Supports double, float, Half and complex numbers. New in 2.1.0: on .NET 8 and higher, generic overloads of all real functions for any IEEE 754 floating-point type (float, Half and, on .NET 11 and higher, BFloat16, Decimal32, Decimal64, Decimal128); the float and Half overloads are kept and return the same results; fixed accuracy of generic complex Atanh and Acoth at Re z = ±1. On .NET 11 and higher, also supports generic Complex<T>. Since 2.0.0, complex functions return principal values with IEEE 754 / C99 branch-cut conventions, identical on all .NET versions.