namespace PkmnLib.Static.Utils; /// /// Helper methods for numeric operations. /// public static class NumericHelpers { /// /// Checks if two floating-point values are approximately equal within a specified tolerance. /// public static bool IsApproximatelyEqualTo(this float value, float other, float tolerance = 0.0001f) => MathF.Abs(value - other) <= tolerance; /// /// Multiplies two values. If this overflows, returns . /// public static byte MultiplyOrMax(this byte value, byte multiplier) { var result = value * multiplier; return result > byte.MaxValue ? byte.MaxValue : (byte)result; } /// /// Multiplies two values. If this overflows, returns . /// public static byte MultiplyOrMax(this byte value, float multiplier) { var result = value * multiplier; return result > byte.MaxValue ? byte.MaxValue : (byte)result; } /// /// Multiplies two values. If this overflows, returns . /// public static sbyte MultiplyOrMax(this sbyte value, sbyte multiplier) { var result = value * multiplier; return result > sbyte.MaxValue ? sbyte.MaxValue : (sbyte)result; } /// /// Multiplies two values. If this overflows, returns . /// public static ushort MultiplyOrMax(this ushort value, ushort multiplier) { var result = value * multiplier; return result > ushort.MaxValue ? ushort.MaxValue : (ushort)result; } /// /// Multiplies two values. If this overflows, returns . /// public static ushort MultiplyOrMax(this ushort value, float multiplier) { var result = value * multiplier; return result > ushort.MaxValue ? ushort.MaxValue : (ushort)result; } /// /// Multiplies two values. If this overflows, returns . /// public static short MultiplyOrMax(this short value, short multiplier) { var result = value * multiplier; return result > short.MaxValue ? short.MaxValue : (short)result; } /// /// Multiplies two values. If this overflows, returns . /// public static uint MultiplyOrMax(this uint value, uint multiplier) { var result = (ulong)value * multiplier; return result > uint.MaxValue ? uint.MaxValue : (uint)result; } /// /// Multiplies two values. If this overflows, returns . /// public static uint MultiplyOrMax(this uint value, float multiplier) { var result = value * multiplier; return result > uint.MaxValue ? uint.MaxValue : (uint)result; } /// /// Multiplies two values. If this overflows, returns . /// public static int MultiplyOrMax(this int value, int multiplier) { var result = (long)value * multiplier; return result > int.MaxValue ? int.MaxValue : (int)result; } /// /// Multiplies two values. If this overflows, returns . /// public static int MultiplyOrMax(this int value, float multiplier) { var result = value * multiplier; return result > int.MaxValue ? int.MaxValue : (int)result; } }