Enhanced C#
Language of your choice: library documentation

Documentation moved to ecsharp.net

GitHub doesn't support HTTP redirects, so you'll be redirected in 3 seconds.

 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
Static Public Member Functions | List of all members
Loyc.Math.MathEx Class Reference

Provides additional math functions that are not available in System.Math. More...


Source file:

Remarks

Provides additional math functions that are not available in System.Math.

Static Public Member Functions

static int Sign< T > (T num)
 Returns the sign of a number (-1 for negative, 1 for positive, 0 for zero). More...
 
static int Sign (int num)
 Returns the sign of a number (-1 for negative, 1 for positive, 0 for zero). More...
 
static int Sign (long num)
 Returns the sign of a number (-1 for negative, 1 for positive, 0 for zero). More...
 
static int Sign (float num)
 Returns the sign of a number (-1 for negative, 1 for positive, 0 for zero). More...
 
static int Sign (double num)
 Returns the sign of a number (-1 for negative, 1 for positive, 0 for zero). More...
 
static int MulShift (int a, int mulBy, int shiftBy)
 Multiplies two integers, internally producing a double-size result so that overflow is not possible, then divides the result by the specified power of two using a right shift. More...
 
static uint MulShift (uint a, uint mulBy, int shiftBy)
 
static long MulShift (long a, long mulBy, int shiftBy)
 
static ulong MulShift (ulong a, ulong mulBy, int shiftBy)
 
static int MulDiv (int a, int mulBy, int divBy, out int remainder)
 Multiplies two integers, internally producing a double-size result so that overflow is not possible, then divides the result by the specified number. More...
 
static uint MulDiv (uint a, uint mulBy, uint divBy, out uint remainder)
 
static long MulDiv (long a, long mulBy, long divBy, out long remainder)
 
static ulong MulDiv (ulong a, ulong mulBy, ulong divBy, out ulong remainder)
 
static int MulDiv (int a, int mulBy, int divBy)
 
static uint MulDiv (uint a, uint mulBy, uint divBy)
 
static long MulDiv (long a, long mulBy, long divBy)
 
static ulong MulDiv (ulong a, ulong mulBy, ulong divBy)
 
static double Mod (double x, double y)
 
static float Mod (float x, float y)
 
static int Mod (int x, int y)
 
static long Mod (long x, long y)
 
static uint RoL (uint value, int amt)
 Rotates a bit pattern left by the specified number of bits. More...
 
static int RoL (int value, int amt)
 Rotates a bit pattern left by the specified number of bits. More...
 
static ulong RoL (ulong value, int amt)
 Rotates a bit pattern left by the specified number of bits. More...
 
static long RoL (long value, int amt)
 Rotates a bit pattern left by the specified number of bits. More...
 
static ushort RoL (ushort value, int amt)
 Rotates a bit pattern left by the specified number of bits. More...
 
static byte RoL (byte value, int amt)
 Rotates a bit pattern left by the specified number of bits. More...
 
static uint RoR (uint value, int amt)
 Rotates a bit pattern right by the specified number of bits. More...
 
static int RoR (int value, int amt)
 Rotates a bit pattern right by the specified number of bits. More...
 
static ulong RoR (ulong value, int amt)
 Rotates a bit pattern right by the specified number of bits. More...
 
static long RoR (long value, int amt)
 Rotates a bit pattern right by the specified number of bits. More...
 
static ushort RoR (ushort value, int amt)
 Rotates a bit pattern right by the specified number of bits. More...
 
static byte RoR (byte value, int amt)
 Rotates a bit pattern right by the specified number of bits. More...
 
static uint Sqrt (long value)
 
static uint Sqrt (ulong value)
 
static int Sqrt (int value)
 
static uint Sqrt (uint value)
 
static float Sqrt (float value)
 
static double Sqrt (double value)
 
static int CountOnes (uint x)
 Returns the number of '1' bits in x More...
 
static byte CountOnes (byte x)
 
static int CountOnes (ushort x)
 
static int CountOnes (int x)
 
static int CountOnes (long x)
 
static int CountOnes (ulong x)
 
static int Log2Floor (uint x)
 
static int Log2Floor (int x)
 Returns the floor of the base-2 logarithm of x. e.g. 1024 -> 10, 1000 -> 9 More...
 
static int Log2Floor (ulong x)
 
static int Log2Floor (long x)
 
static uint NextPowerOf2 (uint x)
 Gets the next higher power of 2, e.g. 4=>8, 13=>16. More...
 
static ulong NextPowerOf2 (ulong x)
 Gets the next higher power of 2, e.g. 4=>8, 13=>16. More...
 
static int NextPowerOf2 (int x)
 Calls NextPowerOf2(uint). More...
 
static int NextPowerOf2 (long x)
 Calls NextPowerOf2(ulong). More...
 
static int FindFirstOne (uint i)
 Returns the bit position of the first '1' bit in a uint, or -1 the input is zero. More...
 
static int FindFirstZero (uint i)
 Returns the bit position of the first '0' bit in a uint, or -1 if there are no zeros. More...
 
static int FindLastOne (uint i)
 Returns the bit position of the first '1' bit in a uint, or -1 the input is zero. More...
 
static int FindLastZero (uint i)
 
static double Int64BitsToDouble (long bits)
 
static long DoubleToInt64Bits (double value)
 
static double Int32BitsToSingle (int bits)
 
static long SingleToInt32Bits (float value)
 
static float NextHigher (float a)
 
static float NextLower (float a)
 
static double NextHigher (double num)
 
static double NextLower (double num)
 
static double ShiftLeft (double num, int amount)
 
static double ShiftRight (double num, int amount)
 
static float ShiftLeft (float num, int amount)
 
static float ShiftRight (float num, int amount)
 
static int ShiftLeft (int num, int amount)
 
static long ShiftLeft (long num, int amount)
 
static int ShiftRight (int num, int amount)
 
static long ShiftRight (long num, int amount)
 
static T ShiftLeft< T > (T num, int amount)
 
static T Min< T > (T a, T b)
 
static T Max< T > (T a, T b)
 
static void Swap< T > (ref T a, ref T b)
 
static bool SortPair< T > (ref T lo, ref T hi, Comparison< T > comp)
 
static bool SortPair< T > (ref T lo, ref T hi)
 
static int Average (int x, int y)
 Computes the average of two integers. Will not overflow. More...
 
static long Average (long x, long y)
 Computes the average of two integers. Will not overflow. More...
 
static float Average (float x, float y)
 Computes the average of two numbers. Will not overflow. More...
 
static double Average (double x, double y)
 Computes the average of two numbers. Will not overflow. More...
 
static T Average< T > (T x, T y)
 Computes the average of two numbers. Slow. No overflow protection. More...
 
static int Square (int x)
 
static long Square (long x)
 
static uint Square (uint x)
 
static ulong Square (ulong x)
 
static float Square (float x)
 
static double Square (double x)
 
static int Cube (int x)
 
static long Cube (long x)
 
static uint Cube (uint x)
 
static ulong Cube (ulong x)
 
static float Cube (float x)
 
static double Cube (double x)
 
static bool IsPrime (int num)
 Returns true if num is a prime number, meaning that it is greater than 1 and is divisible only by 1 and itself. More...
 

Member Function Documentation

static int Loyc.Math.MathEx.Average ( int  x,
int  y 
)
inlinestatic

Computes the average of two integers. Will not overflow.

static long Loyc.Math.MathEx.Average ( long  x,
long  y 
)
inlinestatic

Computes the average of two integers. Will not overflow.

static float Loyc.Math.MathEx.Average ( float  x,
float  y 
)
inlinestatic

Computes the average of two numbers. Will not overflow.

static double Loyc.Math.MathEx.Average ( double  x,
double  y 
)
inlinestatic

Computes the average of two numbers. Will not overflow.

static T Loyc.Math.MathEx.Average< T > ( x,
y 
)
inlinestatic

Computes the average of two numbers. Slow. No overflow protection.

static int Loyc.Math.MathEx.CountOnes ( uint  x)
inlinestatic

Returns the number of '1' bits in x

For example, CountOnes(0xF0) == 4.

Some processors have a dedicated instruction for this operation, but the .NET framework provides no access to it.

static int Loyc.Math.MathEx.FindFirstOne ( uint  i)
inlinestatic

Returns the bit position of the first '1' bit in a uint, or -1 the input is zero.

static int Loyc.Math.MathEx.FindFirstZero ( uint  i)
inlinestatic

Returns the bit position of the first '0' bit in a uint, or -1 if there are no zeros.

static int Loyc.Math.MathEx.FindLastOne ( uint  i)
inlinestatic

Returns the bit position of the first '1' bit in a uint, or -1 the input is zero.

static bool Loyc.Math.MathEx.IsPrime ( int  num)
inlinestatic

Returns true if num is a prime number, meaning that it is greater than 1 and is divisible only by 1 and itself.

Parameters
numA number to test.

This test takes O(sqrt(num)) time and it is moderately optimized, although faster algorithms exist (e.g. the Sieve of Eratosthenes can be faster per number, but is designed to find a list of all the prime numbers rather than to test a single number.

static int Loyc.Math.MathEx.Log2Floor ( int  x)
inlinestatic

Returns the floor of the base-2 logarithm of x. e.g. 1024 -> 10, 1000 -> 9

The return value is -1 for an input that is zero or negative.

Some processors have a dedicated instruction for this operation, but the .NET framework provides no access to it.

static int Loyc.Math.MathEx.MulDiv ( int  a,
int  mulBy,
int  divBy,
out int  remainder 
)
inlinestatic

Multiplies two integers, internally producing a double-size result so that overflow is not possible, then divides the result by the specified number.

Parameters
remainderThe remainder of the division is placed here. The remainder is computed properly even if the main result overflows.
Returns
a * mulBy / divBy, without overflow during multiplication.

If the final result does not fit in the original data type, this method returns largest possible value of the result type (int.MaxValue, or int.MinValue if the overflowing result is negative).

static uint Loyc.Math.MathEx.MulDiv ( uint  a,
uint  mulBy,
uint  divBy,
out uint  remainder 
)
inlinestatic

If the final result does not fit in the original data type, this method returns largest possible value of the result type (uint.MaxValue).

static long Loyc.Math.MathEx.MulDiv ( long  a,
long  mulBy,
long  divBy,
out long  remainder 
)
inlinestatic

If the final result does not fit in the original data type, this method returns largest possible value of the result type (long.MaxValue, or long.MinValue if the overflowing result is negative).

static ulong Loyc.Math.MathEx.MulDiv ( ulong  a,
ulong  mulBy,
ulong  divBy,
out ulong  remainder 
)
inlinestatic

If the final result does not fit in the original data type, this method returns largest possible value of the result type (ulong.MaxValue).

static int Loyc.Math.MathEx.MulShift ( int  a,
int  mulBy,
int  shiftBy 
)
inlinestatic

Multiplies two integers, internally producing a double-size result so that overflow is not possible, then divides the result by the specified power of two using a right shift.

Returns
a * mulBy >> shiftBy, without overflow during multiplication.

This method does not handle the case that the result is too large to fit in the original data type.

static uint Loyc.Math.MathEx.NextPowerOf2 ( uint  x)
inlinestatic

Gets the next higher power of 2, e.g. 4=>8, 13=>16.

For negative values of x, NextPowerOf2((uint)x) is 0.

static ulong Loyc.Math.MathEx.NextPowerOf2 ( ulong  x)
inlinestatic

Gets the next higher power of 2, e.g. 4=>8, 13=>16.

For negative values of (long)x, NextPowerOf2((ulong)x) is 0.

static int Loyc.Math.MathEx.NextPowerOf2 ( int  x)
inlinestatic
static int Loyc.Math.MathEx.NextPowerOf2 ( long  x)
inlinestatic
static uint Loyc.Math.MathEx.RoL ( uint  value,
int  amt 
)
inlinestatic

Rotates a bit pattern left by the specified number of bits.

static int Loyc.Math.MathEx.RoL ( int  value,
int  amt 
)
inlinestatic

Rotates a bit pattern left by the specified number of bits.

static ulong Loyc.Math.MathEx.RoL ( ulong  value,
int  amt 
)
inlinestatic

Rotates a bit pattern left by the specified number of bits.

static long Loyc.Math.MathEx.RoL ( long  value,
int  amt 
)
inlinestatic

Rotates a bit pattern left by the specified number of bits.

static ushort Loyc.Math.MathEx.RoL ( ushort  value,
int  amt 
)
inlinestatic

Rotates a bit pattern left by the specified number of bits.

static byte Loyc.Math.MathEx.RoL ( byte  value,
int  amt 
)
inlinestatic

Rotates a bit pattern left by the specified number of bits.

static uint Loyc.Math.MathEx.RoR ( uint  value,
int  amt 
)
inlinestatic

Rotates a bit pattern right by the specified number of bits.

static int Loyc.Math.MathEx.RoR ( int  value,
int  amt 
)
inlinestatic

Rotates a bit pattern right by the specified number of bits.

static ulong Loyc.Math.MathEx.RoR ( ulong  value,
int  amt 
)
inlinestatic

Rotates a bit pattern right by the specified number of bits.

static long Loyc.Math.MathEx.RoR ( long  value,
int  amt 
)
inlinestatic

Rotates a bit pattern right by the specified number of bits.

static ushort Loyc.Math.MathEx.RoR ( ushort  value,
int  amt 
)
inlinestatic

Rotates a bit pattern right by the specified number of bits.

static byte Loyc.Math.MathEx.RoR ( byte  value,
int  amt 
)
inlinestatic

Rotates a bit pattern right by the specified number of bits.

static int Loyc.Math.MathEx.Sign ( int  num)
inlinestatic

Returns the sign of a number (-1 for negative, 1 for positive, 0 for zero).

static int Loyc.Math.MathEx.Sign ( long  num)
inlinestatic

Returns the sign of a number (-1 for negative, 1 for positive, 0 for zero).

static int Loyc.Math.MathEx.Sign ( float  num)
inlinestatic

Returns the sign of a number (-1 for negative, 1 for positive, 0 for zero).

static int Loyc.Math.MathEx.Sign ( double  num)
inlinestatic

Returns the sign of a number (-1 for negative, 1 for positive, 0 for zero).

static int Loyc.Math.MathEx.Sign< T > ( num)
inlinestatic

Returns the sign of a number (-1 for negative, 1 for positive, 0 for zero).

Type Constraints
T :IComparable<T> 
static uint Loyc.Math.MathEx.Sqrt ( long  value)
inlinestatic