Elementary Functions

The Math class in the .NET framework Base Class Libraries implements a large number of elementary functions. Unfortunately, this list is far from complete. The Elementary class in the Numerics.NET namespace defines many elementary functions that are missing from Math class.

Utility Functions

Some functions exist in the .NET Framework, but there implementation is far from optimal. We've implemented them in a more efficient way that in some cases is up to 50x faster.

Utility functions

Method

Description

IsNaN

Checks if a value is Not-a-Number.

IsFinite

Checks if a value represents a finite real number.

IsFinite

Checks if a value represents positive or negative infinity.

Clip

Restricts a value to the specified interval.

Step

Returns the Heaviside step function.

Trigonometric Functions

Calculating a trigonometric function for an argument close to a multiple of π/2 is often problematic. Fortunately, in the actual expressions, the argument is often expressed as a multiple of π. In these cases, it is possible to compute the exact values of the sine, cosine, and tangent. The following methods implement this functionality:

Method

Description

CosPi

Accurately computes the cosine of a value times π.

SinPi

Accurately computes the sine of a value times π.

TanPi

Accurately computes the tangent of a value times π.

Sinc

Returns the unnormalized Sinc function.

A related problem is that the standard methods quickly become less accurate for larger arguments. To make the calculation of a trigonometric function fast and accurate, the argument must be reduced to a relatively small interval around zero. This is done by subtracting an integer multiple of π from the argument. The standard implementation uses a value of π with only 66 bits of precision. Some precision is lost for arguments as small as 30,000.

Worse still, for arguments greater than or equal to 2^63, the argument is returned unchanged. Aside from the fact that this result is completely meaningless, it can cause problems in code that relies on the fact that the sine and cosine are bounded by -1 and +1.

The trigonometric methods Sin, Cos, and Tan perform complete argument reduction, and return accurate results even for very large arguments.

More trigonometric functions

Method

Description

Cos

Accurately computes the cosine of a value.

Sin

Accurately computes the sine of a value.

Tan

Accurately computes the tangent of a value.

Hyperbolic Functions

Method

Description

Cosh

Hyperbolic cosine.

Coth

Hyperbolic cotangent.

Csch

Hyperbolic co-secant.

Sinh

Hyperbolic sine.

Sech

Hyperbolic secant.

Tanh

Hyperbolic tangent.

Inverse Hyperbolic Functions

Method

Description

Acosh

Inverse hyperbolic cosine.

Acoth

Inverse hyperbolic cotangent.

Acsch

Inverse hyperbolic co-secant.

Asech

Inverse hyperbolic secant.

Asinh

Inverse hyperbolic sine.

Atanh

Inverse hyperbolic tangent.

Exponential, Logarithmic and Miscellaneous Functions

The functions in this section calculate expressions with a higher accuracy than using the standard expression would produce.

Exponential, logarithmic, and miscellaneous functions

Method

Description

ExpMinus1

The exponential function minus one, ex-1, calculated to high precision when x is near zero.

Hypot

The hypotenuse of a right-angled triangle with specified sides.

LambertW

Lambert's W function, the (real) solution W of x = WeW.

Log1PlusX

The natural logarithm of 1+x calculated to high precision for x close to zero.

Pow

A number raised to an integer power.

Sqrt1pxm1

The difference between the square root of a number close to 1 and 1.

The Pow method is useful only for larger values of the exponent. For small values, the JIT compiler automatically converts the expression to an optimized multiplication.

Elementary Functions of Decimal Values

The Decimal type is a decimal floating-point type with up to 28 digits of precision. Transcendental functions like sine, cosine, exponential and logarithm are not available in the Math class. The DecimalMath class implements all the methods that are defined for Double for the decimal type to full precision. Because the implementation is mostly in software, the evaluation of these functions takes significantly longer than the built-in functions for double-precision numbers.

Elementary functions of decimal values

Method

Description

Acos

Inverse cosine.

Asin

Inverse sine.

Atan

Inverse tangent.

Atan2

Inverse tangent with two arguments.

Cos

Cosine.

Cosh

Hyperbolic cosine.

Exp

Exponential function.

Log

Natural logarithm.

Log

Logarithm with specified base.

Log10

Base 10 logarithm.

Sin

Sine.

Sinh

Hyperbolic sine.

Sqrt

Square root.

Tan

Tangent.

Tanh

Hyperbolic tangent.