# Missing Important Trigonometry Functions in .NET: Filling in the Gaps

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

Sec

Csc (cosecant)

Cot (cotangent)

Asec (inverse secant)

Acsc (inverse cosecant)

Acot (inverse cotangent)

Sech (hyperbolic secant)

Csch (hyperbolic cosecant)

Coth (hyperbolic cotangent)

Asech (inverse hyperbolic secant)

Acsch (inverse hyperbolic cosecant)

Acoth (inverse hyperbolic cotangent)

This omission can be inconvenient for developers, especially those working in fields such as computer graphics, signal processing, and scientific computing where accurate trigonometric calculations are important.

For example, in computer graphics, the inverse tangent function is often used to calculate angles when rotating and flipping objects. Without a direct implementation, developers must render it using other functions, which can lead to inefficiencies and potential bugs, as shown below.

I believe that a modern programming language designed to solve scientific problems should offer a full set of mathematical tools to facilitate accurate and efficient calculations. To fill the gaps in .NET math functions, I developed a library that includes the missing trigonometric functions, calling it MathTrigonometric. You can find the library and its documentation at GitHub.

## Why use this library?

**Arccotangent function (arccot)**

The inverse tangent function is one of the most controversial trigonometric functions due to its dual graphical representations. Both graphs are mathematically correct, but one is preferred for consistency and clarity.

In mathematical terms, the arccotangent function arccot(x) is usually defined as the angle whose cotangent is x. The preferred range for arccot(x) is (0, π) rather than (−π/2, π/2) since it maintains consistency with other inverse trigonometric functions. Here is an implementation of the arccot function using the existing 'Math.Atan' function in C#:

```
public static double Acot(double d)
{
if (Math.Abs(d) < double.Epsilon)
return Math.PI / 2.0;
return Math.Atan(1.0 / d);
}
```

This implementation does not take into account the correct range of the inverse tangent function, resulting in inaccurate results for negative input values. Here is the correct implementation, applying the symmetry property arccot(−x) = π − arccot(x) for negative input values:

```
public static double Acot(double d)
{
if (Math.Abs(d) < double.Epsilon)
return Math.PI / 2.0;
//the Trigonometric Symmetry is applied: arccot(−x) = π − arccot(x)
if (IsNegative(d))
return Math.PI - Math.Atan(1 / -d);
return Math.Atan(1.0 / d);
}
```

Mathematicians prefer the correct implementation because it adheres to the standard range (0, π), ensuring consistency and avoiding ambiguities in calculations.

**Increased accuracy**

The inverse hyperbolic cosecant function is defined as:

When evaluating this function for two opposite input values, such as -0.1E-7 and 0.1E-7, we get 19.1138 for x = 0.1E-7 and -∞ for x = -0.1E-7.

For values close to zero, the relative error (that is, the error relative to the actual value) can be significant. This is because the distance between representable floating point numbers is not uniform. As numbers approach zero, the distance between them decreases in absolute terms but increases in relative terms, leading to noticeable errors when performing arithmetic operations or mathematical functions such as logarithms.

To reduce these errors, you can use the symmetry property of the inverse hyperbolic cosecant function: arcsch(−x) = −arcsch(x). By applying this property to negative input values, you can avoid calculating values close to zero, thereby increasing the accuracy of the results.

Example:

```
public static double Acsch(double x)
{
if (Math.Abs(x) < double.Epsilon)
return double.NaN;
//the Trigonometric Symmetry is applied: arcsch(−x)=−arcsch(x)
if (IsNegative(x))
return -Math.Log(1.0 / -x + Math.Sqrt(1.0 / Math.Pow(x, 2.0) + 1.0));
return Math.Log(1.0 / x + Math.Sqrt(1.0 / Math.Pow(x, 2.0) + 1.0));
}
```

**Providing ranges of input and output values**

Trigonometric functions often require specific ranges of input values to produce valid results. For example, the inverse hyperbolic secant function arsech(x) accepts values only in the range (0, 1]. When implementing custom trigonometric functions, it is important to check the ranges of input values to prevent runtime errors and ensure accurate calculations.

Example:

```
public static double Asech(double x)
{
if (x is < double.Epsilon or > 1.0)
return double.NaN;
return Math.Log(1.0 / x + Math.Sqrt(1.0 / Math.Pow(x, 2.0) - 1.0));
}
```

## Conclusion

Including missing trigonometric functions is essential to the completeness of a modern programming language such as .NET. As designers, we need to consider precision, range, and symmetry when implementing these features. My library aims to fill these gaps and provide robust implementations for scientific computing.

To install the library, use the following command in the NuGet Package Manager console:

`Install-Package MathTrigonometric`

I'm also working on a .NET library for quickly computing complex scientific math formulas, which will greatly improve the capabilities of C#. You can support my efforts by becoming sponsored on GitHub. Your contributions will help develop open-source and improve the tools available to the community.

Thank you! Please leave ideas in the comments!