* spark_core is the appropriate project for such 'vocabulary' types. * unit tests in the spark_tests project can now reference these types (this was the immediate impetus for the move).
80 lines
3.5 KiB
C#
80 lines
3.5 KiB
C#
using System;
|
|
using System.Linq;
|
|
|
|
namespace mbts {
|
|
public static class DoubleHelper {
|
|
public static bool any_nan_or_infinity(params double[] values) {
|
|
for (int i = 0; i < values.Length; i++) {
|
|
if (double.IsNaN(values[i]) || double.IsInfinity(values[i]))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool are_close(double v1, double v2, double threshold) =>
|
|
!any_nan_or_infinity(v1, v2, threshold) && Math.Abs(threshold) > Math.Abs(v1 - v2);
|
|
|
|
// bc double.IsNaN gets annoying write, and a lazy programmer is a happy programmer ;)
|
|
public static bool is_nan(this double value) =>
|
|
double.IsNaN(value);
|
|
|
|
/// <summary>
|
|
/// If the specified value is a <see cref="double"/>, <see cref="int"/>, or <see cref="uint"/>, returns the equivalent <see cref="double"/>; otherwise, returns <see cref="double.NaN"/>.
|
|
/// </summary>
|
|
/// <param name="value">The value to convert to a <see cref="double"/>.</param>
|
|
/// <returns>The <see cref="double"/> equivalent value if the cast succeeds, otherwise <see cref="double.NaN"/>.</returns>
|
|
public static double nan_cast(object value) {
|
|
// devs: add numeric type checks as needed
|
|
if (value is double d) return d;
|
|
if (value is int i) return i;
|
|
if (value is uint u) return u;
|
|
return double.NaN;
|
|
}
|
|
|
|
public static bool IsNaNOrInfinity(double d) => double.IsNaN(d) || double.IsInfinity(d);
|
|
public static bool IsNaNOrInfinityOr(double d, params double[] values) => IsNaNOrInfinity(d) || values.Contains(d);
|
|
public static bool IsOneOf(double d, params double[] values) => values.Contains(d);
|
|
|
|
public static bool IsNonZero(this double value) =>
|
|
value != 0 && !double.IsNaN(value) && !double.IsInfinity(value);
|
|
|
|
public static double CoerceNaN(double value) => CoerceNaN(value, 0.0);
|
|
public static double CoerceNaN(double value, double if_nan) => double.IsNaN(value) ? if_nan : value;
|
|
public static double CoerceNaNOrInfinity(double value) => CoerceNaNOrInfinity(value, 0.0);
|
|
public static double CoerceNaNOrInfinity(double value, double if_nan_or_infinity) => IsNaNOrInfinity(value) ? if_nan_or_infinity : value;
|
|
public static double MultiplyIfNotNaN(double value, double mult) => double.IsNaN(mult) ? value : value * mult;
|
|
public static uint safe_convert_to_uint(double value) {
|
|
switch (value) {
|
|
case double.PositiveInfinity: return uint.MaxValue;
|
|
case double.NegativeInfinity:
|
|
case double.NaN: return 0;
|
|
default:
|
|
if (value < 0) {
|
|
return 0;
|
|
} else if (value > uint.MaxValue) {
|
|
return uint.MaxValue;
|
|
}
|
|
return (uint)value;
|
|
}
|
|
}
|
|
|
|
public static double? nan_nullable(double value) =>
|
|
double.IsNaN(value) ? (double?)null : value;
|
|
|
|
public static int? to_optional_int32(double value) {
|
|
if (double.IsNaN(value) || double.IsInfinity(value))
|
|
return null;
|
|
if (value > 0) {
|
|
value = Math.Floor(value);
|
|
if (value <= int.MaxValue)
|
|
return (int)value;
|
|
} else if (value < 0) {
|
|
value = Math.Ceiling(value);
|
|
if (value >= int.MinValue)
|
|
return (int)value;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|