2024-07-28 10:18:12 +00:00
|
|
|
namespace PkmnLib.Static.Utils;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Helper methods for numeric operations.
|
|
|
|
/// </summary>
|
|
|
|
public static class NumericHelpers
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Multiplies two values. If this overflows, returns <see cref="byte.MaxValue"/>.
|
|
|
|
/// </summary>
|
|
|
|
public static byte MultiplyOrMax(this byte value, byte multiplier)
|
|
|
|
{
|
|
|
|
var result = value * multiplier;
|
|
|
|
return result > byte.MaxValue ? byte.MaxValue : (byte)result;
|
|
|
|
}
|
2025-03-02 16:19:57 +00:00
|
|
|
|
2024-07-28 10:18:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Multiplies two values. If this overflows, returns <see cref="byte.MaxValue"/>.
|
|
|
|
/// </summary>
|
|
|
|
public static byte MultiplyOrMax(this byte value, float multiplier)
|
|
|
|
{
|
|
|
|
var result = value * multiplier;
|
|
|
|
return result > byte.MaxValue ? byte.MaxValue : (byte)result;
|
|
|
|
}
|
2025-03-02 16:19:57 +00:00
|
|
|
|
2024-07-28 10:18:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Multiplies two values. If this overflows, returns <see cref="sbyte.MaxValue"/>.
|
|
|
|
/// </summary>
|
|
|
|
public static sbyte MultiplyOrMax(this sbyte value, sbyte multiplier)
|
|
|
|
{
|
|
|
|
var result = value * multiplier;
|
|
|
|
return result > sbyte.MaxValue ? sbyte.MaxValue : (sbyte)result;
|
|
|
|
}
|
2025-03-02 16:19:57 +00:00
|
|
|
|
2024-07-28 10:18:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Multiplies two values. If this overflows, returns <see cref="ushort.MaxValue"/>.
|
|
|
|
/// </summary>
|
|
|
|
public static ushort MultiplyOrMax(this ushort value, ushort multiplier)
|
|
|
|
{
|
|
|
|
var result = value * multiplier;
|
|
|
|
return result > ushort.MaxValue ? ushort.MaxValue : (ushort)result;
|
|
|
|
}
|
2025-03-02 16:19:57 +00:00
|
|
|
|
2024-07-28 10:18:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Multiplies two values. If this overflows, returns <see cref="short.MaxValue"/>.
|
|
|
|
/// </summary>
|
|
|
|
public static short MultiplyOrMax(this short value, short multiplier)
|
|
|
|
{
|
|
|
|
var result = value * multiplier;
|
|
|
|
return result > short.MaxValue ? short.MaxValue : (short)result;
|
|
|
|
}
|
2025-03-02 16:19:57 +00:00
|
|
|
|
2025-02-01 14:00:22 +00:00
|
|
|
public static uint MultiplyOrMax(this uint value, uint multiplier)
|
|
|
|
{
|
|
|
|
var result = (ulong)value * multiplier;
|
|
|
|
return result > uint.MaxValue ? uint.MaxValue : (uint)result;
|
|
|
|
}
|
2024-07-28 10:18:12 +00:00
|
|
|
}
|