namespace SPTarkov.Server.Core.Extensions
{
public static class MathExtensions
{
///
/// Helper to create the cumulative sum of all enumerable elements
/// [1, 2, 3, 4].CumulativeSum() = [1, 3, 6, 10]
///
/// The enumerable with numbers of which to calculate the cumulative sum
/// cumulative sum of values
public static IEnumerable CumulativeSum(this IEnumerable values)
{
double sum = 0;
foreach (var value in values)
{
sum += value;
yield return sum;
}
}
///
/// Helper to create the cumulative sum of all enumerable elements
/// [1, 2, 3, 4].CumulativeSum() = [1, 3, 6, 10]
///
/// The enumerable with numbers of which to calculate the cumulative sum
/// cumulative sum of values
public static IEnumerable CumulativeSum(this IEnumerable values)
{
float sum = 0;
foreach (var value in values)
{
sum += value;
yield return sum;
}
}
///
/// Helper to create the product of each element times factor
///
/// The enumerable of numbers which shall be multiplied by the factor
/// Number to multiply each element by
/// An enumerable of elements all multiplied by the factor
public static IEnumerable Product(this IEnumerable values, double factor)
{
return values.Select(v => v * factor);
}
///
/// Helper to create the product of each element times factor
///
/// The enumerable of numbers which shall be multiplied by the factor
/// Number to multiply each element by
/// An enumerable of elements all multiplied by the factor
public static IEnumerable Product(this IEnumerable values, float factor)
{
return values.Select(v => v * factor);
}
///
/// Helper to determine if one double is approx equal to another double
///
/// Value to check
/// Target value
/// Error value
/// True if value is approx target within the error range
public static bool Approx(this double value, double target, double error = 0.001d)
{
return Math.Abs(value - target) <= error;
}
///
/// Helper to determine if one float is approx equal to another float
///
/// Value to check
/// Target value
/// Error value
/// True if value is approx target within the error range
public static bool Approx(this float value, float target, float error = 0.001f)
{
return Math.Abs(value - target) <= error;
}
}
}