com.alicizax.unity.cysharp..../Number/MathEx.cs

264 lines
5.9 KiB
C#
Raw Normal View History

2025-01-09 13:57:51 +08:00
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
namespace System
{
internal static class MathEx
{
public static uint DivRem(uint a, uint b, out uint result)
{
uint div = a / b;
result = a - (div * b);
return div;
}
public static ulong DivRem(ulong a, ulong b, out ulong result)
{
ulong div = a / b;
result = a - (div * b);
return div;
}
public static int DivRem(int a, int b, out int result)
{
int div = a / b;
result = a - (div * b);
return div;
}
public static long DivRem(long a, long b, out long result)
{
long div = a / b;
result = a - (div * b);
return div;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte Clamp(byte value, byte min, byte max)
{
if (min > max)
{
ThrowMinMaxException(min, max);
}
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static decimal Clamp(decimal value, decimal min, decimal max)
{
if (min > max)
{
ThrowMinMaxException(min, max);
}
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Clamp(double value, double min, double max)
{
if (min > max)
{
ThrowMinMaxException(min, max);
}
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short Clamp(short value, short min, short max)
{
if (min > max)
{
ThrowMinMaxException(min, max);
}
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Clamp(int value, int min, int max)
{
if (min > max)
{
ThrowMinMaxException(min, max);
}
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Clamp(long value, long min, long max)
{
if (min > max)
{
ThrowMinMaxException(min, max);
}
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static sbyte Clamp(sbyte value, sbyte min, sbyte max)
{
if (min > max)
{
ThrowMinMaxException(min, max);
}
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Clamp(float value, float min, float max)
{
if (min > max)
{
ThrowMinMaxException(min, max);
}
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort Clamp(ushort value, ushort min, ushort max)
{
if (min > max)
{
ThrowMinMaxException(min, max);
}
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Clamp(uint value, uint min, uint max)
{
if (min > max)
{
ThrowMinMaxException(min, max);
}
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Clamp(ulong value, ulong min, ulong max)
{
if (min > max)
{
ThrowMinMaxException(min, max);
}
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
return value;
}
private static void ThrowMinMaxException<T>(T min, T max)
{
throw new ArgumentException($"Argument_MinMaxValue, min:{min} max:{max}");
}
}
}