Show / Hide Table of Contents

Class MathHelper

Contains common mathematical functions and constants.

Inheritance
Object
MathHelper
Inherited Members
Object.ToString()
Object.Equals(Object)
Object.Equals(Object, Object)
Object.ReferenceEquals(Object, Object)
Object.GetHashCode()
Object.GetType()
Object.MemberwiseClone()
Namespace: OpenTK
Assembly: OpenTK.dll
Syntax
public static class MathHelper

Fields

E

Defines the value of E as a Single.

Declaration
public const float E = 2.71828175F
Field Value
Type Description
Single

Log10E

Defines the base-10 logarithm of E.

Declaration
public const float Log10E = 0.4342945F
Field Value
Type Description
Single

Log2E

Defines the base-2 logarithm of E.

Declaration
public const float Log2E = 1.442695F
Field Value
Type Description
Single

Pi

Defines the value of Pi as a Single.

Declaration
public const float Pi = 3.14159274F
Field Value
Type Description
Single

PiOver2

Defines the value of Pi divided by two as a Single.

Declaration
public const float PiOver2 = 1.57079637F
Field Value
Type Description
Single

PiOver3

Defines the value of Pi divided by three as a Single.

Declaration
public const float PiOver3 = 1.04719758F
Field Value
Type Description
Single

PiOver4

Definesthe value of Pi divided by four as a Single.

Declaration
public const float PiOver4 = 0.7853982F
Field Value
Type Description
Single

PiOver6

Defines the value of Pi divided by six as a Single.

Declaration
public const float PiOver6 = 0.5235988F
Field Value
Type Description
Single

ThreePiOver2

Defines the value of Pi multiplied by 3 and divided by two as a Single.

Declaration
public const float ThreePiOver2 = 4.712389F
Field Value
Type Description
Single

TwoPi

Defines the value of Pi multiplied by two as a Single.

Declaration
public const float TwoPi = 6.28318548F
Field Value
Type Description
Single

Methods

ApproximatelyEqual(Single, Single, Int32)

Approximates floating point equality with a maximum number of different bits. This is typically used in place of an epsilon comparison. see: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ see: https://stackoverflow.com/questions/3874627/floating-point-comparison-functions-for-c-sharp

Declaration
public static bool ApproximatelyEqual(float a, float b, int maxDeltaBits)
Parameters
Type Name Description
Single a

the first value to compare

Single b

the second value to compare

Int32 maxDeltaBits

the number of floating point bits to check

Returns
Type Description
Boolean

ApproximatelyEqualEpsilon(Double, Double, Double)

Approximates double-precision floating point equality by an epsilon (maximum error) value. This method is designed as a "fits-all" solution and attempts to handle as many cases as possible.

Declaration
public static bool ApproximatelyEqualEpsilon(double a, double b, double epsilon)
Parameters
Type Name Description
Double a

The first float.

Double b

The second float.

Double epsilon

The maximum error between the two.

Returns
Type Description
Boolean

true if the values are approximately equal within the error margin; otherwise, false.

ApproximatelyEqualEpsilon(Single, Single, Single)

Approximates single-precision floating point equality by an epsilon (maximum error) value. This method is designed as a "fits-all" solution and attempts to handle as many cases as possible.

Declaration
public static bool ApproximatelyEqualEpsilon(float a, float b, float epsilon)
Parameters
Type Name Description
Single a

The first float.

Single b

The second float.

Single epsilon

The maximum error between the two.

Returns
Type Description
Boolean

true if the values are approximately equal within the error margin; otherwise, false.

ApproximatelyEquivalent(Double, Double, Double)

Approximates equivalence between two double-precision floating-point numbers on a direct human scale. It is important to note that this does not approximate equality - instead, it merely checks whether or not two numbers could be considered equivalent to each other within a certain tolerance. The tolerance is inclusive.

Declaration
public static bool ApproximatelyEquivalent(double a, double b, double tolerance)
Parameters
Type Name Description
Double a

The first value to compare.

Double b

The second value to compare·

Double tolerance

The tolerance within which the two values would be considered equivalent.

Returns
Type Description
Boolean

Whether or not the values can be considered equivalent within the tolerance.

ApproximatelyEquivalent(Single, Single, Single)

Approximates equivalence between two single-precision floating-point numbers on a direct human scale. It is important to note that this does not approximate equality - instead, it merely checks whether or not two numbers could be considered equivalent to each other within a certain tolerance. The tolerance is inclusive.

Declaration
public static bool ApproximatelyEquivalent(float a, float b, float tolerance)
Parameters
Type Name Description
Single a

The first value to compare.

Single b

The second value to compare·

Single tolerance

The tolerance within which the two values would be considered equivalent.

Returns
Type Description
Boolean

Whether or not the values can be considered equivalent within the tolerance.

BinomialCoefficient(Int32, Int32)

Calculates the binomial coefficient n above k.

Declaration
public static long BinomialCoefficient(int n, int k)
Parameters
Type Name Description
Int32 n

The n.

Int32 k

The k.

Returns
Type Description
Int64

n! / (k! * (n - k)!)

Clamp(Double, Double, Double)

Clamps a number between a minimum and a maximum.

Declaration
public static double Clamp(double n, double min, double max)
Parameters
Type Name Description
Double n

The number to clamp.

Double min

The minimum allowed value.

Double max

The maximum allowed value.

Returns
Type Description
Double

min, if n is lower than min; max, if n is higher than max; n otherwise.

Clamp(Int32, Int32, Int32)

Clamps a number between a minimum and a maximum.

Declaration
public static int Clamp(int n, int min, int max)
Parameters
Type Name Description
Int32 n

The number to clamp.

Int32 min

The minimum allowed value.

Int32 max

The maximum allowed value.

Returns
Type Description
Int32

min, if n is lower than min; max, if n is higher than max; n otherwise.

Clamp(Single, Single, Single)

Clamps a number between a minimum and a maximum.

Declaration
public static float Clamp(float n, float min, float max)
Parameters
Type Name Description
Single n

The number to clamp.

Single min

The minimum allowed value.

Single max

The maximum allowed value.

Returns
Type Description
Single

min, if n is lower than min; max, if n is higher than max; n otherwise.

DegreesToRadians(Double)

Convert degrees to radians

Declaration
public static double DegreesToRadians(double degrees)
Parameters
Type Name Description
Double degrees

An angle in degrees

Returns
Type Description
Double

The angle expressed in radians

DegreesToRadians(Single)

Convert degrees to radians

Declaration
public static float DegreesToRadians(float degrees)
Parameters
Type Name Description
Single degrees

An angle in degrees

Returns
Type Description
Single

The angle expressed in radians

Factorial(Int32)

Calculates the factorial of a given natural number.

Declaration
public static long Factorial(int n)
Parameters
Type Name Description
Int32 n

The number.

Returns
Type Description
Int64

n!

InverseSqrtFast(Double)

Returns an approximation of the inverse square root of left number.

Declaration
public static double InverseSqrtFast(double x)
Parameters
Type Name Description
Double x

A number.

Returns
Type Description
Double

An approximation of the inverse square root of the specified number, with an upper error bound of 0.001

Remarks

This is an improved implementation of the the method known as Carmack's inverse square root which is found in the Quake III source code. This implementation comes from http://www.codemaestro.com/reviews/review00000105.html. For the history of this method, see http://www.beyond3d.com/content/articles/8/

InverseSqrtFast(Single)

Returns an approximation of the inverse square root of left number.

Declaration
public static float InverseSqrtFast(float x)
Parameters
Type Name Description
Single x

A number.

Returns
Type Description
Single

An approximation of the inverse square root of the specified number, with an upper error bound of 0.001

Remarks

This is an improved implementation of the the method known as Carmack's inverse square root which is found in the Quake III source code. This implementation comes from http://www.codemaestro.com/reviews/review00000105.html. For the history of this method, see http://www.beyond3d.com/content/articles/8/

NextPowerOfTwo(Double)

Returns the next power of two that is greater than or equal to the specified number.

Declaration
public static double NextPowerOfTwo(double n)
Parameters
Type Name Description
Double n

The specified number.

Returns
Type Description
Double

The next power of two.

NextPowerOfTwo(Int32)

Returns the next power of two that is greater than or equal to the specified number.

Declaration
public static int NextPowerOfTwo(int n)
Parameters
Type Name Description
Int32 n

The specified number.

Returns
Type Description
Int32

The next power of two.

NextPowerOfTwo(Int64)

Returns the next power of two that is greater than or equal to the specified number.

Declaration
public static long NextPowerOfTwo(long n)
Parameters
Type Name Description
Int64 n

The specified number.

Returns
Type Description
Int64

The next power of two.

NextPowerOfTwo(Single)

Returns the next power of two that is greater than or equal to the specified number.

Declaration
public static float NextPowerOfTwo(float n)
Parameters
Type Name Description
Single n

The specified number.

Returns
Type Description
Single

The next power of two.

RadiansToDegrees(Double)

Convert radians to degrees

Declaration
public static double RadiansToDegrees(double radians)
Parameters
Type Name Description
Double radians

An angle in radians

Returns
Type Description
Double

The angle expressed in degrees

RadiansToDegrees(Single)

Convert radians to degrees

Declaration
public static float RadiansToDegrees(float radians)
Parameters
Type Name Description
Single radians

An angle in radians

Returns
Type Description
Single

The angle expressed in degrees

Swap(ref Double, ref Double)

Swaps two double values.

Declaration
public static void Swap(ref double a, ref double b)
Parameters
Type Name Description
Double a

The first value.

Double b

The second value.

Swap(ref Single, ref Single)

Swaps two float values.

Declaration
public static void Swap(ref float a, ref float b)
Parameters
Type Name Description
Single a

The first value.

Single b

The second value.

In This Article
Back to top Generated by DocFX