Class MathHelper
Contains common mathematical functions and constants.
Inherited Members
Namespace: OpenTK.Mathematics
Assembly: OpenTK.Mathematics.dll
Syntax
public static class MathHelper
Fields
E
Defines the value of E as a float.
Declaration
public const float E = 2.7182817
Field Value
Type | Description |
---|---|
float |
Log10E
Defines the base-10 logarithm of E.
Declaration
public const float Log10E = 0.4342945
Field Value
Type | Description |
---|---|
float |
Log2E
Defines the base-2 logarithm of E.
Declaration
public const float Log2E = 1.442695
Field Value
Type | Description |
---|---|
float |
Pi
Defines the value of Pi as a float.
Declaration
public const float Pi = 3.1415927
Field Value
Type | Description |
---|---|
float |
PiOver2
Defines the value of Pi divided by two as a float.
Declaration
public const float PiOver2 = 1.5707964
Field Value
Type | Description |
---|---|
float |
PiOver3
Defines the value of Pi divided by three as a float.
Declaration
public const float PiOver3 = 1.0471976
Field Value
Type | Description |
---|---|
float |
PiOver4
Defines the value of Pi divided by four as a float.
Declaration
public const float PiOver4 = 0.7853982
Field Value
Type | Description |
---|---|
float |
PiOver6
Defines the value of Pi divided by six as a float.
Declaration
public const float PiOver6 = 0.5235988
Field Value
Type | Description |
---|---|
float |
ThreePiOver2
Defines the value of Pi multiplied by 3 and divided by two as a float.
Declaration
public const float ThreePiOver2 = 4.712389
Field Value
Type | Description |
---|---|
float |
TwoPi
Defines the value of Pi multiplied by two as a float.
Declaration
public const float TwoPi = 6.2831855
Field Value
Type | Description |
---|---|
float |
Methods
ApproximatelyEqual(float, float, int)
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
[Pure]
public static bool ApproximatelyEqual(float a, float b, int maxDeltaBits)
Parameters
Type | Name | Description |
---|---|---|
float | a | the first value to compare. |
float | b |
|
int | maxDeltaBits | the number of floating point bits to check. |
Returns
Type | Description |
---|---|
bool | true if the values are approximately equal; otherwise, false. |
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
[Pure]
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 |
---|---|
bool |
ApproximatelyEqualEpsilon(float, float, float)
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
[Pure]
public static bool ApproximatelyEqualEpsilon(float a, float b, float epsilon)
Parameters
Type | Name | Description |
---|---|---|
float | a | The first float. |
float | b | The second float. |
float | epsilon | The maximum error between the two. |
Returns
Type | Description |
---|---|
bool |
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
[Pure]
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 |
---|---|
bool | Whether or not the values can be considered equivalent within the tolerance. |
ApproximatelyEquivalent(float, float, float)
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
[Pure]
public static bool ApproximatelyEquivalent(float a, float b, float tolerance)
Parameters
Type | Name | Description |
---|---|---|
float | a | The first value to compare. |
float | b | The second value to compare. |
float | tolerance | The tolerance within which the two values would be considered equivalent. |
Returns
Type | Description |
---|---|
bool | Whether or not the values can be considered equivalent within the tolerance. |
BinomialCoefficient(int, int)
Calculates the binomial coefficient n
above k
.
Declaration
[Pure]
public static long BinomialCoefficient(int n, int k)
Parameters
Type | Name | Description |
---|---|---|
int | n | The n. |
int | k | The k. |
Returns
Type | Description |
---|---|
long | n! / (k! * (n - k)!). |
ClampAngle(double)
Clamps an angle to the range [0, 360).
Declaration
public static double ClampAngle(double angle)
Parameters
Type | Name | Description |
---|---|---|
double | angle | The angle to clamp in degrees. |
Returns
Type | Description |
---|---|
double | The clamped angle in the range [0, 360). |
ClampAngle(float)
Clamps an angle to the range [0, 360).
Declaration
public static float ClampAngle(float angle)
Parameters
Type | Name | Description |
---|---|---|
float | angle | The angle to clamp in degrees. |
Returns
Type | Description |
---|---|
float | The clamped angle in the range [0, 360). |
ClampRadians(double)
Clamps an angle to the range [0, 2π).
Declaration
public static double ClampRadians(double angle)
Parameters
Type | Name | Description |
---|---|---|
double | angle | The angle to clamp in radians. |
Returns
Type | Description |
---|---|
double | The clamped angle in the range [0, 2π). |
ClampRadians(float)
Clamps an angle to the range [0, 2π).
Declaration
public static float ClampRadians(float angle)
Parameters
Type | Name | Description |
---|---|---|
float | angle | The angle to clamp in radians. |
Returns
Type | Description |
---|---|
float | The clamped angle in the range [0, 2π). |
DegreesToRadians(double)
Convert degrees to radians.
Declaration
[Pure]
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(float)
Convert degrees to radians.
Declaration
[Pure]
public static float DegreesToRadians(float degrees)
Parameters
Type | Name | Description |
---|---|---|
float | degrees | An angle in degrees. |
Returns
Type | Description |
---|---|
float | The angle expressed in radians. |
Elerp(double, double, double)
Exponentially interpolates between start
and end
by t
.
Equivalent to start * pow(end/start, t)
.
Useful for scaling and zooms where constant change in t
should result in a multiplicative change in output.
Declaration
public static double Elerp(double start, double end, double t)
Parameters
Type | Name | Description |
---|---|---|
double | start | Start value. Must be non-negative. |
double | end | End value. Must be non-negative. |
double | t | Value of the interpolation between |
Returns
Type | Description |
---|---|
double | The interpolated result between the |
Elerp(float, float, float)
Exponentially interpolates between start
and end
by t
.
Equivalent to start * pow(end/start, t)
.
Useful for scaling and zooms where constant change in t
should result in a multiplicative change in output.
Declaration
public static float Elerp(float start, float end, float t)
Parameters
Type | Name | Description |
---|---|---|
float | start | Start value. Must be non-negative. |
float | end | End value. Must be non-negative. |
float | t | Value of the interpolation between |
Returns
Type | Description |
---|---|
float | The interpolated result between the |
Factorial(int)
Calculates the factorial of a given natural number.
Declaration
[Pure]
public static long Factorial(int n)
Parameters
Type | Name | Description |
---|---|---|
int | n | The number. |
Returns
Type | Description |
---|---|
long | The factorial of |
Lerp(double, double, double)
Linearly interpolates between start
and end
by t
.
Declaration
[Pure]
public static double Lerp(double start, double end, double t)
Parameters
Type | Name | Description |
---|---|---|
double | start | Start value. |
double | end | End value. |
double | t | Value of the interpolation between |
Returns
Type | Description |
---|---|
double | The interpolated result between the |
Lerp(float, float, float)
Linearly interpolates between start
and end
by t
.
Declaration
[Pure]
public static float Lerp(float start, float end, float t)
Parameters
Type | Name | Description |
---|---|---|
float | start | Start value. |
float | end | End value. |
float | t | Value of the interpolation between |
Returns
Type | Description |
---|---|
float | The interpolated result between the |
MapRange(double, double, double, double, double)
Scales the specified number linearly between a minimum and a maximum.
Declaration
[Pure]
public static double MapRange(double value, double valueMin, double valueMax, double resultMin, double resultMax)
Parameters
Type | Name | Description |
---|---|---|
double | value | The number to scale. |
double | valueMin | The minimum expected number (inclusive). |
double | valueMax | The maximum expected number (inclusive). |
double | resultMin | The minimum output number (inclusive). |
double | resultMax | The maximum output number (inclusive). |
Returns
Type | Description |
---|---|
double | The number, scaled linearly between min and max. |
Remarks
If the value range is zero, this function will throw a divide by zero exception.
MapRange(int, int, int, int, int)
Scales the specified number linearly between a minimum and a maximum.
Declaration
[Pure]
public static int MapRange(int value, int valueMin, int valueMax, int resultMin, int resultMax)
Parameters
Type | Name | Description |
---|---|---|
int | value | The number to scale. |
int | valueMin | The minimum expected number (inclusive). |
int | valueMax | The maximum expected number (inclusive). |
int | resultMin | The minimum output number (inclusive). |
int | resultMax | The maximum output number (inclusive). |
Returns
Type | Description |
---|---|
int | The number, scaled linearly between min and max. |
Remarks
If the value range is zero, this function will throw a divide by zero exception.
MapRange(float, float, float, float, float)
Scales the specified number linearly between a minimum and a maximum.
Declaration
[Pure]
public static float MapRange(float value, float valueMin, float valueMax, float resultMin, float resultMax)
Parameters
Type | Name | Description |
---|---|---|
float | value | The number to scale. |
float | valueMin | The minimum expected number (inclusive). |
float | valueMax | The maximum expected number (inclusive). |
float | resultMin | The minimum output number (inclusive). |
float | resultMax | The maximum output number (inclusive). |
Returns
Type | Description |
---|---|
float | The number, scaled linearly between min and max. |
Remarks
If the value range is zero, this function will throw a divide by zero exception.
NextPowerOfTwo(double)
Returns the next power of two that is greater than or equal to the specified number.
Declaration
[Pure]
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(int)
Returns the next power of two that is greater than or equal to the specified number.
Declaration
[Pure]
public static int NextPowerOfTwo(int n)
Parameters
Type | Name | Description |
---|---|---|
int | n | The specified number. |
Returns
Type | Description |
---|---|
int | The next power of two. |
NextPowerOfTwo(long)
Returns the next power of two that is greater than or equal to the specified number.
Declaration
[Pure]
public static long NextPowerOfTwo(long n)
Parameters
Type | Name | Description |
---|---|---|
long | n | The specified number. |
Returns
Type | Description |
---|---|
long | The next power of two. |
NextPowerOfTwo(float)
Returns the next power of two that is greater than or equal to the specified number.
Declaration
[Pure]
public static float NextPowerOfTwo(float n)
Parameters
Type | Name | Description |
---|---|---|
float | n | The specified number. |
Returns
Type | Description |
---|---|
float | The next power of two. |
NormalizeAngle(double)
Normalizes an angle to the range (-180, 180].
Declaration
public static double NormalizeAngle(double angle)
Parameters
Type | Name | Description |
---|---|---|
double | angle | The angle in degrees to normalize. |
Returns
Type | Description |
---|---|
double | The normalized angle in the range (-180, 180]. |
NormalizeAngle(float)
Normalizes an angle to the range (-180, 180].
Declaration
public static float NormalizeAngle(float angle)
Parameters
Type | Name | Description |
---|---|---|
float | angle | The angle in degrees to normalize. |
Returns
Type | Description |
---|---|
float | The normalized angle in the range (-180, 180]. |
NormalizeRadians(double)
Normalizes an angle to the range (-π, π].
Declaration
public static double NormalizeRadians(double angle)
Parameters
Type | Name | Description |
---|---|---|
double | angle | The angle in radians to normalize. |
Returns
Type | Description |
---|---|
double | The normalized angle in the range (-π, π]. |
NormalizeRadians(float)
Normalizes an angle to the range (-π, π].
Declaration
public static float NormalizeRadians(float angle)
Parameters
Type | Name | Description |
---|---|---|
float | angle | The angle in radians to normalize. |
Returns
Type | Description |
---|---|
float | The normalized angle in the range (-π, π]. |
RadiansToDegrees(double)
Convert radians to degrees.
Declaration
[Pure]
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(float)
Convert radians to degrees.
Declaration
[Pure]
public static float RadiansToDegrees(float radians)
Parameters
Type | Name | Description |
---|---|---|
float | radians | An angle in radians. |
Returns
Type | Description |
---|---|
float | The angle expressed in degrees. |
Swap<T>(ref T, ref T)
Swaps two values.
Declaration
public static void Swap<T>(ref T a, ref T b)
Parameters
Type | Name | Description |
---|---|---|
T | a | The first value. |
T | b | The second value. |
Type Parameters
Name | Description |
---|---|
T | The type of the values to swap. |