Class MathHelper
Provides useful mathematical algorithms and functions.
Inheritance
System.Object
MathHelper
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Assembly: Mars.Numerics.dll
Syntax
public static class MathHelper
Fields
|
Improve this Doc
View Source
E
Defines the value of E as a System.Single.
Declaration
public const float E = 2.718282F
Field Value
Type |
Description |
System.Single |
|
|
Improve this Doc
View Source
Log10E
Defines the base-10 logarithm of E.
Declaration
public const float Log10E = 0.4342945F
Field Value
Type |
Description |
System.Single |
|
|
Improve this Doc
View Source
Log2E
Defines the base-2 logarithm of E.
Declaration
public const float Log2E = 1.442695F
Field Value
Type |
Description |
System.Single |
|
|
Improve this Doc
View Source
Pi
Defines the value of Pi as a System.Single.
Declaration
public const float Pi = 3.141593F
Field Value
Type |
Description |
System.Single |
|
|
Improve this Doc
View Source
PiOver2
Defines the value of Pi divided by two as a System.Single.
Declaration
public const float PiOver2 = 1.5707964F
Field Value
Type |
Description |
System.Single |
|
|
Improve this Doc
View Source
PiOver3
Defines the value of Pi divided by three as a System.Single.
Declaration
public const float PiOver3 = 1.047198F
Field Value
Type |
Description |
System.Single |
|
|
Improve this Doc
View Source
PiOver4
Defines the value of Pi divided by four as a System.Single.
Declaration
public const float PiOver4 = 0.7853982F
Field Value
Type |
Description |
System.Single |
|
|
Improve this Doc
View Source
PiOver6
Defines the value of Pi divided by six as a System.Single.
Declaration
public const float PiOver6 = 0.5235988F
Field Value
Type |
Description |
System.Single |
|
|
Improve this Doc
View Source
ThreePiOver2
Defines the value of Pi multiplied by 3 and divided by two as a System.Single.
Declaration
public const float ThreePiOver2 = 4.712389F
Field Value
Type |
Description |
System.Single |
|
|
Improve this Doc
View Source
ToDegreeFactorD
Gets the double precision factor to convert radian to degree
Declaration
public const double ToDegreeFactorD = 57.29577951308232
Field Value
Type |
Description |
System.Double |
|
|
Improve this Doc
View Source
ToDegreeFactorF
Gets the single precision factor to convert degree to radian
Declaration
public const float ToDegreeFactorF = 57.295776F
Field Value
Type |
Description |
System.Single |
|
|
Improve this Doc
View Source
ToRadianFactorD
Gets the double precision factor to convert degree to radian.
Declaration
public const double ToRadianFactorD = 0.01745329238474369
Field Value
Type |
Description |
System.Double |
|
|
Improve this Doc
View Source
ToRadianFactorF
Gets the single precision factor to convert degree to radian.
Declaration
public const float ToRadianFactorF = 0.017453292F
Field Value
Type |
Description |
System.Single |
|
|
Improve this Doc
View Source
TwoPi
Defines the value of Pi multiplied by two as a System.Single.
Declaration
public const float TwoPi = 6.283185F
Field Value
Type |
Description |
System.Single |
|
Methods
|
Improve this Doc
View Source
BinomialCoefficient(Int32, Int32)
Computes the binomial coefficient of (n, k), also read as "n choose k".
Declaration
public static long BinomialCoefficient(int n, int k)
Parameters
Type |
Name |
Description |
System.Int32 |
n |
n, must be a value equal to or greater than 0. |
System.Int32 |
k |
k, a value in the range [0, n ]. |
Returns
Type |
Description |
System.Int64 |
The binomial coefficient.
|
|
Improve this Doc
View Source
Bitmask(UInt32)
Creates the smallest bitmask that is greater than or equal to the given value.
Declaration
public static uint Bitmask(uint value)
Parameters
Type |
Name |
Description |
System.UInt32 |
value |
The value. |
Returns
Type |
Description |
System.UInt32 |
A bitmask where the left bits are 0 and the right bits are 1. The value of the bitmask
is ≥ value .
|
|
Improve this Doc
View Source
Clamp<T>(T, T, T)
Clamps the specified value.
Declaration
public static T Clamp<T>(T value, T min, T max)
where T : IComparable<T>
Parameters
Type |
Name |
Description |
T |
value |
The value which should be clamped. |
T |
min |
The min limit. |
T |
max |
The max limit. |
Returns
Type |
Description |
T |
value clamped to the interval
[min , max ].
|
Type Parameters
Name |
Description |
T |
The type of the value. |
|
Improve this Doc
View Source
Factorial(Int32)
Calculates the factorial of a given natural number.
Declaration
public static long Factorial(int n)
Parameters
Type |
Name |
Description |
System.Int32 |
n |
The number. |
Returns
Type |
Description |
System.Int64 |
n! |
|
Improve this Doc
View Source
FastFloor(Double)
Returns the largest integer less than or equal to the specified decimal number.
Declaration
public static int FastFloor(double d)
Parameters
Type |
Name |
Description |
System.Double |
d |
A decimal number. |
Returns
Type |
Description |
System.Int32 |
The largest integer less than or equal to d . |
|
Improve this Doc
View Source
Frac(Double)
Calculates the fractional part of a specified double-precision floating-point number.
Declaration
public static double Frac(double d)
Parameters
Type |
Name |
Description |
System.Double |
d |
The number. |
Returns
Type |
Description |
System.Double |
The fractional part of d . |
|
Improve this Doc
View Source
Frac(Single)
Calculates the fractional part of a specified single-precision floating-point number.
Declaration
public static float Frac(float f)
Parameters
Type |
Name |
Description |
System.Single |
f |
The number. |
Returns
Type |
Description |
System.Single |
The fractional part of f . |
|
Improve this Doc
View Source
Gaussian(Double, Double, Double, Double)
Computes the Gaussian function y = k * e^( -(x-μ)2/(2σ2)
(double-precision).
Declaration
public static double Gaussian(double x, double coefficient, double expectedValue, double standardDeviation)
Parameters
Type |
Name |
Description |
System.Double |
x |
The argument x. |
System.Double |
coefficient |
The coefficient k. |
System.Double |
expectedValue |
The expected value μ. |
System.Double |
standardDeviation |
The standard deviation σ. |
Returns
Type |
Description |
System.Double |
The height of the Gaussian bell curve at x. |
|
Improve this Doc
View Source
Gaussian(Single, Single, Single, Single)
Computes the Gaussian function y = k * e^( -(x-μ)2/(2σ2)
(single precision).
Declaration
public static float Gaussian(float x, float coefficient, float expectedValue, float standardDeviation)
Parameters
Type |
Name |
Description |
System.Single |
x |
The argument x. |
System.Single |
coefficient |
The coefficient k. |
System.Single |
expectedValue |
The expected value μ. |
System.Single |
standardDeviation |
The standard deviation σ. |
Returns
Type |
Description |
System.Single |
The height of the Gaussian bell curve at x. |
|
Improve this Doc
View Source
Hypotenuse(Double, Double)
Computes Sqrt(a*a + b*b) without underflow/overflow (double-precision).
Declaration
public static double Hypotenuse(double cathetusA, double cathetusB)
Parameters
Type |
Name |
Description |
System.Double |
cathetusA |
Cathetus a. |
System.Double |
cathetusB |
Cathetus b. |
Returns
Type |
Description |
System.Double |
The hypotenuse c, which is Sqrt(a*a + b*b). |
|
Improve this Doc
View Source
Hypotenuse(Single, Single)
Computes Sqrt(a*a + b*b) without underflow/overflow (single-precision).
Declaration
public static float Hypotenuse(float cathetusA, float cathetusB)
Parameters
Type |
Name |
Description |
System.Single |
cathetusA |
Cathetus a. |
System.Single |
cathetusB |
Cathetus b. |
Returns
Type |
Description |
System.Single |
The hypotenuse c, which is Sqrt(a*a + b*b). |
|
Improve this Doc
View Source
InverseSqrtFast(Double)
Returns an approximation of the inverse square root of left number.
Declaration
public static double InverseSqrtFast(double x)
Parameters
Type |
Name |
Description |
System.Double |
x |
A number. |
Returns
Type |
Description |
System.Double |
An approximation of the inverse square root of the specified number, with an upper error bound of 0.001 |
|
Improve this Doc
View Source
InverseSqrtFast(Single)
Returns an approximation of the inverse square root of left number.
Declaration
public static float InverseSqrtFast(float x)
Parameters
Type |
Name |
Description |
System.Single |
x |
A number. |
Returns
Type |
Description |
System.Single |
An approximation of the inverse square root of the specified number, with an upper error bound of 0.001 |
|
Improve this Doc
View Source
IsPowerOfTwo(Int32)
Determines whether the specified value is a power of two.
Declaration
public static bool IsPowerOfTwo(int value)
Parameters
Type |
Name |
Description |
System.Int32 |
value |
The value. |
Returns
Type |
Description |
System.Boolean |
true if value is a power of two; otherwise,
false.
|
|
Improve this Doc
View Source
Log2GreaterOrEqual(UInt32)
Returns the smallest non-negative integer x such that 2x ≥ value
.
Declaration
public static uint Log2GreaterOrEqual(uint value)
Parameters
Type |
Name |
Description |
System.UInt32 |
value |
The value. |
Returns
Type |
Description |
System.UInt32 |
The smallest non-negative integer x such that 2x ≥ value .
Exception: If value is 0, 0 is returned.
|
|
Improve this Doc
View Source
Log2LessOrEqual(UInt32)
Returns the largest non-negative integer x such that 2x ≤ value
.
Declaration
public static uint Log2LessOrEqual(uint value)
Parameters
Type |
Name |
Description |
System.UInt32 |
value |
The value. |
Returns
Type |
Description |
System.UInt32 |
The largest non-negative integer x such that 2x ≤ value .
Exception: If value is 0 then 0 is returned.
|
|
Improve this Doc
View Source
Magnitude(IList<Single>)
Calculates magnitude of the vector.
Declaration
public static float Magnitude(IList<float> vector)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<System.Single> |
vector |
The vector to calculate magnitude for. |
Returns
Type |
Description |
System.Single |
The magnitude. |
|
Improve this Doc
View Source
MagnitudeSimd(Single[])
Calculates magnitude of the vector using SIMD (single instruction multiple data parallelism).
Declaration
public static float MagnitudeSimd(float[] vector)
Parameters
Type |
Name |
Description |
System.Single[] |
vector |
The vector to calculate magnitude for. |
Returns
Type |
Description |
System.Single |
The magnitude. |
|
Improve this Doc
View Source
NextPowerOf2(UInt32)
Returns the smallest power of two that is greater than the given value.
Declaration
public static uint NextPowerOf2(uint value)
Parameters
Type |
Name |
Description |
System.UInt32 |
value |
The value. |
Returns
Type |
Description |
System.UInt32 |
The smallest power of two (2x) that is greater than value .
|
|
Improve this Doc
View Source
NextPowerOfTwo(Double)
Returns the next power of two that is larger than the specified number.
Declaration
public static double NextPowerOfTwo(double n)
Parameters
Type |
Name |
Description |
System.Double |
n |
The specified number. |
Returns
Type |
Description |
System.Double |
The next power of two. |
|
Improve this Doc
View Source
NextPowerOfTwo(Int32)
Returns the next power of two that is larger than the specified number.
Declaration
public static int NextPowerOfTwo(int n)
Parameters
Type |
Name |
Description |
System.Int32 |
n |
The specified number. |
Returns
Type |
Description |
System.Int32 |
The next power of two. |
|
Improve this Doc
View Source
NextPowerOfTwo(Int64)
Returns the next power of two that is larger than the specified number.
Declaration
public static long NextPowerOfTwo(long n)
Parameters
Type |
Name |
Description |
System.Int64 |
n |
The specified number. |
Returns
Type |
Description |
System.Int64 |
The next power of two. |
|
Improve this Doc
View Source
NextPowerOfTwo(Single)
Returns the next power of two that is larger than the specified number.
Declaration
public static float NextPowerOfTwo(float n)
Parameters
Type |
Name |
Description |
System.Single |
n |
The specified number. |
Returns
Type |
Description |
System.Single |
The next power of two. |
|
Improve this Doc
View Source
NormalizeSimd(Single[])
Calculates normalization of the vector using SIMD (single instruction multiple data).
Declaration
public static void NormalizeSimd(float[] vector)
Parameters
Type |
Name |
Description |
System.Single[] |
vector |
The vector to calculate magnitude for. |
|
Improve this Doc
View Source
Swap(ref Double, ref Double)
Swaps two double values.
Declaration
public static void Swap(ref double a, ref double b)
Parameters
Type |
Name |
Description |
System.Double |
a |
The first value. |
System.Double |
b |
The second value. |
|
Improve this Doc
View Source
Swap(ref Single, ref Single)
Swaps two float values.
Declaration
public static void Swap(ref float a, ref float b)
Parameters
Type |
Name |
Description |
System.Single |
a |
The first value. |
System.Single |
b |
The second value. |
|
Improve this Doc
View Source
Swap<T>(ref T, ref T)
Swaps the content of two variables.
Declaration
public static void Swap<T>(ref T obj1, ref T obj2)
Parameters
Type |
Name |
Description |
T |
obj1 |
First variable. |
T |
obj2 |
Second variable. |
Type Parameters
Name |
Description |
T |
The type of the objects. |
|
Improve this Doc
View Source
ToBearing(Double)
Gets the compass-bearing in degree for the specified radian
Declaration
public static double ToBearing(double radian)
Parameters
Type |
Name |
Description |
System.Double |
radian |
The radian, getting the bearing. |
Returns
Type |
Description |
System.Double |
The compass-bearing |
|
Improve this Doc
View Source
ToDegrees(Double)
Converts an angle value from radians to degrees (double-precision).
Declaration
public static double ToDegrees(double radians)
Parameters
Type |
Name |
Description |
System.Double |
radians |
The angle in radians. |
Returns
Type |
Description |
System.Double |
The angle in degrees. |
|
Improve this Doc
View Source
ToDegrees(Single)
Converts an angle value from radians to degrees (single-precision).
Declaration
public static float ToDegrees(float radians)
Parameters
Type |
Name |
Description |
System.Single |
radians |
The angle in radians. |
Returns
Type |
Description |
System.Single |
The angle in degrees. |
|
Improve this Doc
View Source
ToRadians(Double)
Converts an angle value from degrees to radians (double-precision).
Declaration
public static double ToRadians(double degree)
Parameters
Type |
Name |
Description |
System.Double |
degree |
The angle in degrees. |
Returns
Type |
Description |
System.Double |
The angle in radians. |
|
Improve this Doc
View Source
ToRadians(Single)
Converts an angle value from degrees to radians (single-precision).
Declaration
public static float ToRadians(float degree)
Parameters
Type |
Name |
Description |
System.Single |
degree |
The angle in degrees. |
Returns
Type |
Description |
System.Single |
The angle in radians. |