56 lines
1.7 KiB
C#
Executable File
56 lines
1.7 KiB
C#
Executable File
using System;
|
|
namespace MBS.Framework
|
|
{
|
|
public static class MathExtensions
|
|
{
|
|
/// <summary>
|
|
/// Multiplies a floating point value x by the number 2 raised to the exp power.
|
|
/// </summary>
|
|
/// <returns>The ldexp.</returns>
|
|
/// <param name="x">The x coordinate.</param>
|
|
/// <param name="exp">Exp.</param>
|
|
public static float ldexp(float x, int exp)
|
|
{
|
|
return (float)(x * Math.Pow(2, exp));
|
|
}
|
|
/// <summary>
|
|
/// Multiplies a floating point value x by the number 2 raised to the exp power.
|
|
/// </summary>
|
|
/// <returns>The ldexp.</returns>
|
|
/// <param name="x">The x coordinate.</param>
|
|
/// <param name="exp">Exp.</param>
|
|
public static double ldexp(double x, int exp)
|
|
{
|
|
return (x * Math.Pow(2, exp));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a random <see cref="long" /> between
|
|
/// <paramref name="minValue"/> and <paramref name="maxValue" />.
|
|
/// </summary>
|
|
/// <returns>The random number.</returns>
|
|
/// <param name="random">The instance of <see cref="Random" /> being extended.</param>
|
|
/// <param name="minValue">The inclusive minimum bound of the resulting random value.</param>
|
|
/// <param name="maxValue">The exclusive maximum bound of the resulting random value.</param>
|
|
public static long NextLong (this Random random, long minValue = 0, long maxValue = long.MaxValue)
|
|
{
|
|
return (long)((random.NextDouble() * (maxValue - minValue)) + minValue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calculates the sum of all elements in the array.
|
|
/// </summary>
|
|
/// <returns>The sum of all elements in the array.</returns>
|
|
/// <param name="array">The array to sum.</param>
|
|
public static ushort Sum(this ushort[] array)
|
|
{
|
|
ushort value = 0;
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
value += array[i];
|
|
}
|
|
return value;
|
|
}
|
|
}
|
|
}
|