mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-22 17:58:47 +00:00
Compare commits
No commits in common. "cb7e1308e22fac4c50601267d02e192455fc74c7" and "c7e78f5d195e0209871515c5332af2e1382f8cec" have entirely different histories.
cb7e1308e2
...
c7e78f5d19
12
CHANGELOG.md
12
CHANGELOG.md
@ -9,12 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Changed
|
||||
|
||||
- X10D: Removed `IEnumerable<T>.GreatestCommonFactor` for all integer types in favour of generic math.
|
||||
- X10D: Removed `IEnumerable<T>.LowestCommonMultiple` for all integer types in favour of generic math.
|
||||
- X10D: Removed `IEnumerable<T>.Product` for all integer types in favour of generic math.
|
||||
- X10D: Removed `IEnumerable<T>.RangeTo` for all integer types in favour of generic math.
|
||||
- X10D: Removed `T.Sign` for all numeric types in favour of generic math.
|
||||
- X10D: Removed `T.Wrap` for all numeric types in favour of generic math.
|
||||
- X1OD: `IBinaryInteger<T>.Factorial` now starts at `IBinaryInteger<T>.MultiplicativeIdentity` not
|
||||
`IBinaryInteger<T>.One`.
|
||||
- X10D: Removed `byte.Product`, `short.Product`, `ushort.Product`, `int.Product`, `uint.Product`, `long.Product`,
|
||||
and `ulong.Product`, in favour of `INumber<T>.Product`.
|
||||
- X10D: Removed `byte.RangeTo`, `short.RangeTo`, `ushort.RangeTo`, `int.RangeTo`, `uint.RangeTo`, `long.RangeTo`,
|
||||
and `ulong.RangeTo`, in favour of `INumber<T>.RangeTo`.
|
||||
|
||||
### Removed
|
||||
|
||||
|
@ -39,6 +39,24 @@ public static class BigIntegerExtensions
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the greatest common factor between this, and another, <see cref="BigInteger" />.
|
||||
/// </summary>
|
||||
/// <param name="value">The first value.</param>
|
||||
/// <param name="other">The second value.</param>
|
||||
/// <returns>The greatest common factor between <paramref name="value" /> and <paramref name="other" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static BigInteger GreatestCommonFactor(this BigInteger value, BigInteger other)
|
||||
{
|
||||
while (other != 0)
|
||||
{
|
||||
(value, other) = (other, value % other);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is not evenly divisible by 2.
|
||||
/// </summary>
|
||||
@ -91,6 +109,34 @@ public static class BigIntegerExtensions
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the lowest common multiple between the current 64-bit signed integer, and another 64-bit signed integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The first value.</param>
|
||||
/// <param name="other">The second value.</param>
|
||||
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static BigInteger LowestCommonMultiple(this BigInteger value, BigInteger other)
|
||||
{
|
||||
if (value == 0 || other == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (value == 1)
|
||||
{
|
||||
return other;
|
||||
}
|
||||
|
||||
if (other == 1)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
return value * other / value.GreatestCommonFactor(other);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the multiplicative persistence of a specified value.
|
||||
/// </summary>
|
||||
@ -136,4 +182,32 @@ public static class BigIntegerExtensions
|
||||
|
||||
return persistence;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current integer between a low and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="low">The inclusive lower bound.</param>
|
||||
/// <param name="high">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static BigInteger Wrap(this BigInteger value, BigInteger low, BigInteger high)
|
||||
{
|
||||
BigInteger difference = high - low;
|
||||
return low + (((value - low) % difference) + difference) % difference;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current integer between 0 and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="length">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static BigInteger Wrap(this BigInteger value, BigInteger length)
|
||||
{
|
||||
return ((value % length) + length) % length;
|
||||
}
|
||||
}
|
||||
|
@ -69,12 +69,31 @@ public static class BinaryIntegerExtensions
|
||||
return 1;
|
||||
}
|
||||
|
||||
long result = 1L;
|
||||
TInteger result = TInteger.MultiplicativeIdentity;
|
||||
for (TInteger i = TInteger.One; i <= value; i++)
|
||||
{
|
||||
result *= long.CreateChecked(i);
|
||||
result *= i;
|
||||
}
|
||||
|
||||
return result;
|
||||
return long.CreateChecked(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the greatest common factor between the current binary integer, and another binary integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The first value.</param>
|
||||
/// <param name="other">The second value.</param>
|
||||
/// <returns>The greatest common factor between <paramref name="value" /> and <paramref name="other" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static TInteger GreatestCommonFactor<TInteger>(this TInteger value, TInteger other)
|
||||
where TInteger : IBinaryInteger<TInteger>
|
||||
{
|
||||
while (other != TInteger.Zero)
|
||||
{
|
||||
(value, other) = (other, value % other);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,19 @@ public static class ByteExtensions
|
||||
return ((long)value).IsPrime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the lowest common multiple between the current 8-bit signed integer, and another 8-bit signed integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The first value.</param>
|
||||
/// <param name="other">The second value.</param>
|
||||
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static byte LowestCommonMultiple(this byte value, byte other)
|
||||
{
|
||||
return (byte)((long)value).LowestCommonMultiple(other);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the multiplicative persistence of a specified value.
|
||||
/// </summary>
|
||||
@ -36,4 +49,31 @@ public static class ByteExtensions
|
||||
{
|
||||
return ((long)value).MultiplicativePersistence();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 8-bit unsigned integer between a low and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="low">The inclusive lower bound.</param>
|
||||
/// <param name="high">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static byte Wrap(this byte value, byte low, byte high)
|
||||
{
|
||||
return (byte)((ulong)value).Wrap(low, high);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 8-bit unsigned integer between 0 and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="length">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static byte Wrap(this byte value, byte length)
|
||||
{
|
||||
return (byte)((ulong)value).Wrap(length);
|
||||
}
|
||||
}
|
||||
|
@ -90,6 +90,40 @@ public static class DecimalExtensions
|
||||
return System.Math.Clamp(value, 0.0m, 1.0m);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an integer that indicates the sign of this decimal number.
|
||||
/// </summary>
|
||||
/// <param name="value">A signed number.</param>
|
||||
/// <returns>
|
||||
/// A number that indicates the sign of <paramref name="value" />, as shown in the following table.
|
||||
///
|
||||
/// <list type="table">
|
||||
/// <listheader>
|
||||
/// <term>Return value</term>
|
||||
/// <description>Meaning</description>
|
||||
/// </listheader>
|
||||
///
|
||||
/// <item>
|
||||
/// <term>-1</term>
|
||||
/// <description><paramref name="value" /> is less than zero.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>0</term>
|
||||
/// <description><paramref name="value" /> is equal to zero.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>1</term>
|
||||
/// <description><paramref name="value" /> is greater than zero.</description>
|
||||
/// </item>
|
||||
/// </list>
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static int Sign(this decimal value)
|
||||
{
|
||||
return System.Math.Sign(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the square root of this decimal number.
|
||||
/// </summary>
|
||||
@ -140,4 +174,32 @@ public static class DecimalExtensions
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current decimal number between a low and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="low">The inclusive lower bound.</param>
|
||||
/// <param name="high">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static decimal Wrap(this decimal value, decimal low, decimal high)
|
||||
{
|
||||
decimal difference = high - low;
|
||||
return low + (((value - low) % difference) + difference) % difference;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current decimal number between 0 and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="length">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static decimal Wrap(this decimal value, decimal length)
|
||||
{
|
||||
return ((value % length) + length) % length;
|
||||
}
|
||||
}
|
||||
|
@ -300,6 +300,41 @@ public static class DoubleExtensions
|
||||
return System.Math.Sinh(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an integer that indicates the sign of this double-precision floating-point number.
|
||||
/// </summary>
|
||||
/// <param name="value">A signed number.</param>
|
||||
/// <returns>
|
||||
/// A number that indicates the sign of <paramref name="value" />, as shown in the following table.
|
||||
///
|
||||
/// <list type="table">
|
||||
/// <listheader>
|
||||
/// <term>Return value</term>
|
||||
/// <description>Meaning</description>
|
||||
/// </listheader>
|
||||
///
|
||||
/// <item>
|
||||
/// <term>-1</term>
|
||||
/// <description><paramref name="value" /> is less than zero.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>0</term>
|
||||
/// <description><paramref name="value" /> is equal to zero.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>1</term>
|
||||
/// <description><paramref name="value" /> is greater than zero.</description>
|
||||
/// </item>
|
||||
/// </list>
|
||||
/// </returns>
|
||||
/// <exception cref="ArithmeticException"><paramref name="value" /> is equal to <see cref="double.NaN" />.</exception>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static int Sign(this double value)
|
||||
{
|
||||
return System.Math.Sign(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the square root of this double-precision floating-point number.
|
||||
/// </summary>
|
||||
@ -386,4 +421,32 @@ public static class DoubleExtensions
|
||||
{
|
||||
return System.Math.Tanh(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current double-precision floating-point number between a low and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="low">The inclusive lower bound.</param>
|
||||
/// <param name="high">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static double Wrap(this double value, double low, double high)
|
||||
{
|
||||
double difference = high - low;
|
||||
return low + (((value - low) % difference) + difference) % difference;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current double-precision floating-point number between 0 and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="length">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static double Wrap(this double value, double length)
|
||||
{
|
||||
return ((value % length) + length) % length;
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,19 @@ public static class Int16Extensions
|
||||
return ((long)value).IsPrime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the lowest common multiple between the current 16-bit signed integer, and another 16-bit signed integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The first value.</param>
|
||||
/// <param name="other">The second value.</param>
|
||||
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static short LowestCommonMultiple(this short value, short other)
|
||||
{
|
||||
return (short)((long)value).LowestCommonMultiple(other);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the multiplicative persistence of a specified value.
|
||||
/// </summary>
|
||||
@ -37,4 +50,65 @@ public static class Int16Extensions
|
||||
{
|
||||
return ((long)value).MultiplicativePersistence();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an integer that indicates the sign of this 16-bit signed integer.
|
||||
/// </summary>
|
||||
/// <param name="value">A signed number.</param>
|
||||
/// <returns>
|
||||
/// A number that indicates the sign of <paramref name="value" />, as shown in the following table.
|
||||
///
|
||||
/// <list type="table">
|
||||
/// <listheader>
|
||||
/// <term>Return value</term>
|
||||
/// <description>Meaning</description>
|
||||
/// </listheader>
|
||||
///
|
||||
/// <item>
|
||||
/// <term>-1</term>
|
||||
/// <description><paramref name="value" /> is less than zero.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>0</term>
|
||||
/// <description><paramref name="value" /> is equal to zero.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>1</term>
|
||||
/// <description><paramref name="value" /> is greater than zero.</description>
|
||||
/// </item>
|
||||
/// </list>
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static int Sign(this short value)
|
||||
{
|
||||
return System.Math.Sign(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 16-bit signed integer between a low and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="low">The inclusive lower bound.</param>
|
||||
/// <param name="high">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static short Wrap(this short value, short low, short high)
|
||||
{
|
||||
return (short)((long)value).Wrap(low, high);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 16-bit signed integer between 0 and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="length">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static short Wrap(this short value, short length)
|
||||
{
|
||||
return (short)((long)value).Wrap(length);
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,19 @@ public static class Int32Extensions
|
||||
return ((long)value).IsPrime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the lowest common multiple between the current 32-bit signed integer, and another 32-bit signed integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The first value.</param>
|
||||
/// <param name="other">The second value.</param>
|
||||
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static int LowestCommonMultiple(this int value, int other)
|
||||
{
|
||||
return (int)((long)value).LowestCommonMultiple(other);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the multiplicative persistence of a specified value.
|
||||
/// </summary>
|
||||
@ -37,4 +50,65 @@ public static class Int32Extensions
|
||||
{
|
||||
return ((long)value).MultiplicativePersistence();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an integer that indicates the sign of this 32-bit signed integer.
|
||||
/// </summary>
|
||||
/// <param name="value">A signed number.</param>
|
||||
/// <returns>
|
||||
/// A number that indicates the sign of <paramref name="value" />, as shown in the following table.
|
||||
///
|
||||
/// <list type="table">
|
||||
/// <listheader>
|
||||
/// <term>Return value</term>
|
||||
/// <description>Meaning</description>
|
||||
/// </listheader>
|
||||
///
|
||||
/// <item>
|
||||
/// <term>-1</term>
|
||||
/// <description><paramref name="value" /> is less than zero.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>0</term>
|
||||
/// <description><paramref name="value" /> is equal to zero.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>1</term>
|
||||
/// <description><paramref name="value" /> is greater than zero.</description>
|
||||
/// </item>
|
||||
/// </list>
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static int Sign(this int value)
|
||||
{
|
||||
return System.Math.Sign(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 32-bit signed integer between a low and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="low">The inclusive lower bound.</param>
|
||||
/// <param name="high">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static int Wrap(this int value, int low, int high)
|
||||
{
|
||||
return (int)((long)value).Wrap(low, high);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 32-bit signed integer between 0 and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="length">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static int Wrap(this int value, int length)
|
||||
{
|
||||
return (int)((long)value).Wrap(length);
|
||||
}
|
||||
}
|
||||
|
@ -42,6 +42,34 @@ public static class Int64Extensions
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the lowest common multiple between the current 64-bit signed integer, and another 64-bit signed integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The first value.</param>
|
||||
/// <param name="other">The second value.</param>
|
||||
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static long LowestCommonMultiple(this long value, long other)
|
||||
{
|
||||
if (value == 0 || other == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (value == 1)
|
||||
{
|
||||
return other;
|
||||
}
|
||||
|
||||
if (other == 1)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
return value * other / value.GreatestCommonFactor(other);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the multiplicative persistence of a specified value.
|
||||
/// </summary>
|
||||
@ -87,4 +115,66 @@ public static class Int64Extensions
|
||||
|
||||
return persistence;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an integer that indicates the sign of this 64-bit signed integer.
|
||||
/// </summary>
|
||||
/// <param name="value">A signed number.</param>
|
||||
/// <returns>
|
||||
/// A number that indicates the sign of <paramref name="value" />, as shown in the following table.
|
||||
///
|
||||
/// <list type="table">
|
||||
/// <listheader>
|
||||
/// <term>Return value</term>
|
||||
/// <description>Meaning</description>
|
||||
/// </listheader>
|
||||
///
|
||||
/// <item>
|
||||
/// <term>-1</term>
|
||||
/// <description><paramref name="value" /> is less than zero.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>0</term>
|
||||
/// <description><paramref name="value" /> is equal to zero.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>1</term>
|
||||
/// <description><paramref name="value" /> is greater than zero.</description>
|
||||
/// </item>
|
||||
/// </list>
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static int Sign(this long value)
|
||||
{
|
||||
return System.Math.Sign(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 64-bit signed integer between a low and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="low">The inclusive lower bound.</param>
|
||||
/// <param name="high">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static long Wrap(this long value, long low, long high)
|
||||
{
|
||||
long difference = high - low;
|
||||
return low + (((value - low) % difference) + difference) % difference;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 64-bit signed integer between 0 and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="length">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static long Wrap(this long value, long length)
|
||||
{
|
||||
return ((value % length) + length) % length;
|
||||
}
|
||||
}
|
||||
|
@ -10,25 +10,6 @@ namespace X10D.Math;
|
||||
/// </summary>
|
||||
public static class NumberExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Calculates the greatest common factor between the current number and another number.
|
||||
/// </summary>
|
||||
/// <param name="value">The first value.</param>
|
||||
/// <param name="other">The second value.</param>
|
||||
/// <returns>The greatest common factor between <paramref name="value" /> and <paramref name="other" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static TNumber GreatestCommonFactor<TNumber>(this TNumber value, TNumber other)
|
||||
where TNumber : INumber<TNumber>
|
||||
{
|
||||
while (other != TNumber.Zero)
|
||||
{
|
||||
(value, other) = (other, value % other);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is evenly divisible by 2.
|
||||
/// </summary>
|
||||
@ -61,35 +42,6 @@ public static class NumberExtensions
|
||||
return !value.IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the lowest common multiple between the current 64-bit signed integer, and another 64-bit signed integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The first value.</param>
|
||||
/// <param name="other">The second value.</param>
|
||||
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static TNumber LowestCommonMultiple<TNumber>(this TNumber value, TNumber other)
|
||||
where TNumber : INumber<TNumber>
|
||||
{
|
||||
if (value == TNumber.Zero || other == TNumber.Zero)
|
||||
{
|
||||
return TNumber.Zero;
|
||||
}
|
||||
|
||||
if (value == TNumber.One)
|
||||
{
|
||||
return other;
|
||||
}
|
||||
|
||||
if (other == TNumber.One)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
return value * other / value.GreatestCommonFactor(other);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs a modulo operation which supports a negative dividend.
|
||||
/// </summary>
|
||||
@ -147,34 +99,4 @@ public static class NumberExtensions
|
||||
{
|
||||
return TNumber.Sign(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current integer between a low and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="low">The inclusive lower bound.</param>
|
||||
/// <param name="high">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static TNumber Wrap<TNumber>(this TNumber value, TNumber low, TNumber high)
|
||||
where TNumber : INumber<TNumber>
|
||||
{
|
||||
TNumber difference = high - low;
|
||||
return low + (((value - low) % difference) + difference) % difference;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current integer between 0 and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="length">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static TNumber Wrap<TNumber>(this TNumber value, TNumber length)
|
||||
where TNumber : INumber<TNumber>
|
||||
{
|
||||
return ((value % length) + length) % length;
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,19 @@ public static class SByteExtensions
|
||||
return ((long)value).IsPrime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the lowest common multiple between the current 8-bit signed integer, and another 8-bit signed integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The first value.</param>
|
||||
/// <param name="other">The second value.</param>
|
||||
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static sbyte LowestCommonMultiple(this sbyte value, sbyte other)
|
||||
{
|
||||
return (sbyte)((long)value).LowestCommonMultiple(other);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the multiplicative persistence of a specified value.
|
||||
/// </summary>
|
||||
@ -72,4 +85,31 @@ public static class SByteExtensions
|
||||
{
|
||||
return System.Math.Sign(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 8-bit signed integer between a low and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="low">The inclusive lower bound.</param>
|
||||
/// <param name="high">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static sbyte Wrap(this sbyte value, sbyte low, sbyte high)
|
||||
{
|
||||
return (sbyte)((long)value).Wrap(low, high);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 8-bit signed integer between 0 and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="length">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static sbyte Wrap(this sbyte value, sbyte length)
|
||||
{
|
||||
return (sbyte)((long)value).Wrap(length);
|
||||
}
|
||||
}
|
||||
|
@ -268,6 +268,40 @@ public static class SingleExtensions
|
||||
return System.Math.Clamp(value, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an integer that indicates the sign of this single-precision floating-point number.
|
||||
/// </summary>
|
||||
/// <param name="value">A signed number.</param>
|
||||
/// <returns>
|
||||
/// A number that indicates the sign of <paramref name="value" />, as shown in the following table.
|
||||
///
|
||||
/// <list type="table">
|
||||
/// <listheader>
|
||||
/// <term>Return value</term>
|
||||
/// <description>Meaning</description>
|
||||
/// </listheader>
|
||||
///
|
||||
/// <item>
|
||||
/// <term>-1</term>
|
||||
/// <description><paramref name="value" /> is less than zero.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>0</term>
|
||||
/// <description><paramref name="value" /> is equal to zero.</description>
|
||||
/// </item>
|
||||
/// <item>
|
||||
/// <term>1</term>
|
||||
/// <description><paramref name="value" /> is greater than zero.</description>
|
||||
/// </item>
|
||||
/// </list>
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static int Sign(this float value)
|
||||
{
|
||||
return MathF.Sign(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the square root of this single-precision floating-point number.
|
||||
/// </summary>
|
||||
@ -386,4 +420,31 @@ public static class SingleExtensions
|
||||
{
|
||||
return MathF.Tanh(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current single-precision floating-point number between a low and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="low">The inclusive lower bound.</param>
|
||||
/// <param name="high">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static float Wrap(this float value, float low, float high)
|
||||
{
|
||||
return (float)((double)value).Wrap(low, high);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current single-precision floating-point number between 0 and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="length">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static float Wrap(this float value, float length)
|
||||
{
|
||||
return (float)((double)value).Wrap(length);
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,20 @@ public static class UInt16Extensions
|
||||
return ((ulong)value).IsPrime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the lowest common multiple between the current 16-bit unsigned integer, and another 16-bit unsigned
|
||||
/// integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The first value.</param>
|
||||
/// <param name="other">The second value.</param>
|
||||
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static ushort LowestCommonMultiple(this ushort value, ushort other)
|
||||
{
|
||||
return (ushort)((ulong)value).LowestCommonMultiple(other);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the multiplicative persistence of a specified value.
|
||||
/// </summary>
|
||||
@ -38,4 +52,31 @@ public static class UInt16Extensions
|
||||
{
|
||||
return ((ulong)value).MultiplicativePersistence();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 16-bit unsigned integer between a low and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="low">The inclusive lower bound.</param>
|
||||
/// <param name="high">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static ushort Wrap(this ushort value, ushort low, ushort high)
|
||||
{
|
||||
return (ushort)((ulong)value).Wrap(low, high);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 16-bit unsigned integer between 0 and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="length">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static ushort Wrap(this ushort value, ushort length)
|
||||
{
|
||||
return (ushort)((ulong)value).Wrap(length);
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,20 @@ public static class UInt32Extensions
|
||||
return ((ulong)value).IsPrime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the lowest common multiple between the current 32-bit unsigned integer, and another 32-bit unsigned
|
||||
/// integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The first value.</param>
|
||||
/// <param name="other">The second value.</param>
|
||||
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static uint LowestCommonMultiple(this uint value, uint other)
|
||||
{
|
||||
return (uint)((ulong)value).LowestCommonMultiple(other);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the multiplicative persistence of a specified value.
|
||||
/// </summary>
|
||||
@ -38,4 +52,31 @@ public static class UInt32Extensions
|
||||
{
|
||||
return ((ulong)value).MultiplicativePersistence();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 32-bit unsigned integer between a low and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="low">The inclusive lower bound.</param>
|
||||
/// <param name="high">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static uint Wrap(this uint value, uint low, uint high)
|
||||
{
|
||||
return (uint)((ulong)value).Wrap(low, high);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 32-bit unsigned integer between 0 and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="length">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static uint Wrap(this uint value, uint length)
|
||||
{
|
||||
return (uint)((ulong)value).Wrap(length);
|
||||
}
|
||||
}
|
||||
|
@ -43,6 +43,35 @@ public static class UInt64Extensions
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the lowest common multiple between the current 64-bit unsigned integer, and another 64-bit unsigned
|
||||
/// integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The first value.</param>
|
||||
/// <param name="other">The second value.</param>
|
||||
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static ulong LowestCommonMultiple(this ulong value, ulong other)
|
||||
{
|
||||
if (value == 0 || other == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (value == 1)
|
||||
{
|
||||
return other;
|
||||
}
|
||||
|
||||
if (other == 1)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
return value * other / value.GreatestCommonFactor(other);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the multiplicative persistence of a specified value.
|
||||
/// </summary>
|
||||
@ -88,4 +117,32 @@ public static class UInt64Extensions
|
||||
|
||||
return persistence;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 64-bit unsigned integer between a low and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="low">The inclusive lower bound.</param>
|
||||
/// <param name="high">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static ulong Wrap(this ulong value, ulong low, ulong high)
|
||||
{
|
||||
ulong difference = high - low;
|
||||
return low + (((value - low) % difference) + difference) % difference;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps the current 64-bit unsigned integer between 0 and a high value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap.</param>
|
||||
/// <param name="length">The exclusive upper bound.</param>
|
||||
/// <returns>The wrapped value.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||
public static ulong Wrap(this ulong value, ulong length)
|
||||
{
|
||||
return ((value % length) + length) % length;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user