Math.trigonometric
This C# library includes functions that are missing in .NET: cot, sec, csc, acot, asec, acsc, coth, sech, csch, acoth, asech, and acsch. Also, it includes standard functions: sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, and atanh.
Install / Use
/learn @AntonovAnton/Math.trigonometricREADME
Math Trigonometric Functions in .NET
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:
- Cot (Cotangent)
- Sec (Secant)
- Csc (Cosecant)
- Acot (Inverse Cotangent)
- Asec (Inverse Secant)
- Acsc (Inverse Cosecant)
- Coth (Hyperbolic Cotangent)
- Sech (Hyperbolic Secant)
- Csch (Hyperbolic Cosecant)
- Acoth (Inverse Hyperbolic Cotangent)
- Asech (Inverse Hyperbolic Secant)
- Acsch (Inverse Hyperbolic Cosecant)
You can find a detailed explanation of the implementation and approach in my article on Medium.
Installation
You can install this library via NuGet Package Manager. To do this, follow these steps:
-
Open your project in Visual Studio.
-
Go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
-
Search for MathTrigonometric.
-
Select the package and click Install. Alternatively, you can install the package using the NuGet Package Manager Console:
Install-Package MathTrigonometric
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.
Basic Trigonometric Functions
-
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].
-
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].
-
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).
-
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).
-
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, ∞).
-
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
-
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].
-
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, π].
-
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).
-
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, π).
-
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, π].
-
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
-
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).
-
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, +∞).
-
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).
-
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, +∞).
-
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].
-
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
-
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).
-
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, +∞).
-
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).
-
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, +∞).
-
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, +∞).
-
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
-
DegreesToRadians
Converts degrees to radians.
double DegreesToRadians(double degrees);- Input: Angle in degrees (any real number).
- Output: Angle in radians (any real number).
-
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 angleAc
Related Skills
node-connect
349.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
