Add missing IsEven/IsOdd/Sign methods

This commit is contained in:
Oliver Booth 2022-04-25 00:06:14 +01:00
parent 7fb9459a91
commit 7bf8a89f82
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
6 changed files with 126 additions and 41 deletions

View File

@ -1,7 +1,4 @@
using System.Diagnostics.Contracts; using System.Net;
using System.Net;
using System.Runtime.CompilerServices;
using X10D.Math;
namespace X10D; namespace X10D;

View File

@ -1,6 +1,4 @@
using System.Diagnostics.Contracts; using System.Net;
using System.Net;
using System.Runtime.CompilerServices;
using X10D.Math; using X10D.Math;
namespace X10D; namespace X10D;
@ -82,40 +80,6 @@ public static class Int32Extensions
return BitConverter.GetBytes(value); return BitConverter.GetBytes(value);
} }
/// <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(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int Sign(this int value)
{
return System.Math.Sign(value);
}
/// <summary> /// <summary>
/// Returns an enumerable collection of 32-bit signed integers that range from the current value to a specified value. /// Returns an enumerable collection of 32-bit signed integers that range from the current value to a specified value.
/// </summary> /// </summary>

View File

@ -215,4 +215,38 @@ public static class Int32Extensions
int r = dividend % divisor; int r = dividend % divisor;
return r < 0 ? r + divisor : r; return r < 0 ? r + divisor : r;
} }
/// <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(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int Sign(this int value)
{
return System.Math.Sign(value);
}
} }

View File

@ -46,4 +46,34 @@ public static class UInt16Extensions
return result; return result;
} }
/// <summary>
/// Returns a value indicating whether the current value is evenly divisible by 2.
/// </summary>
/// <param name="value">The value whose parity to check.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="value" /> is evenly divisible by 2, or <see langword="false" />
/// otherwise.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool IsEven(this ushort value)
{
return (value & 1) == 0;
}
/// <summary>
/// Returns a value indicating whether the current value is not evenly divisible by 2.
/// </summary>
/// <param name="value">The value whose parity to check.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="value" /> is not evenly divisible by 2, or <see langword="false" />
/// otherwise.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool IsOdd(this ushort value)
{
return !value.IsEven();
}
} }

View File

@ -46,4 +46,34 @@ public static class UInt32Extensions
return result; return result;
} }
/// <summary>
/// Returns a value indicating whether the current value is evenly divisible by 2.
/// </summary>
/// <param name="value">The value whose parity to check.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="value" /> is evenly divisible by 2, or <see langword="false" />
/// otherwise.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool IsEven(this uint value)
{
return (value & 1) == 0;
}
/// <summary>
/// Returns a value indicating whether the current value is not evenly divisible by 2.
/// </summary>
/// <param name="value">The value whose parity to check.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="value" /> is not evenly divisible by 2, or <see langword="false" />
/// otherwise.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool IsOdd(this uint value)
{
return !value.IsEven();
}
} }

View File

@ -46,4 +46,34 @@ public static class UInt64Extensions
return result; return result;
} }
/// <summary>
/// Returns a value indicating whether the current value is evenly divisible by 2.
/// </summary>
/// <param name="value">The value whose parity to check.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="value" /> is evenly divisible by 2, or <see langword="false" />
/// otherwise.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool IsEven(this ulong value)
{
return (value & 1) == 0;
}
/// <summary>
/// Returns a value indicating whether the current value is not evenly divisible by 2.
/// </summary>
/// <param name="value">The value whose parity to check.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="value" /> is not evenly divisible by 2, or <see langword="false" />
/// otherwise.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool IsOdd(this ulong value)
{
return !value.IsEven();
}
} }