Add Sign() for signed numeric types

Behaves as Math.Sign()
This commit is contained in:
Oliver Booth 2022-04-21 11:04:05 +01:00
parent 3a58ed88c9
commit 86b6a94adc
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
6 changed files with 223 additions and 4 deletions

View File

@ -1,3 +1,6 @@
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace X10D; namespace X10D;
/// <summary> /// <summary>
@ -43,4 +46,38 @@ public static class SByteExtensions
{ {
return ((short)value).IsPrime(); return ((short)value).IsPrime();
} }
/// <summary>
/// Returns an integer that indicates the sign of this 8-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 sbyte value)
{
return Math.Sign(value);
}
} }

View File

@ -1,4 +1,7 @@
namespace X10D; using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace X10D;
/// <summary> /// <summary>
/// Extension methods for <see cref="double" />. /// Extension methods for <see cref="double" />.
@ -123,4 +126,38 @@ public static class DoubleExtensions
{ {
return Math.Round(value / nearest) * nearest; return Math.Round(value / nearest) * nearest;
} }
/// <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>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int Sign(this double value)
{
return Math.Sign(value);
}
} }

View File

@ -1,4 +1,6 @@
using System.Diagnostics.Contracts;
using System.Net; using System.Net;
using System.Runtime.CompilerServices;
namespace X10D; namespace X10D;
@ -177,6 +179,40 @@ public static class Int16Extensions
return MathUtils.Lerp(value, target, alpha); return MathUtils.Lerp(value, target, alpha);
} }
/// <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(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int Sign(this short value)
{
return Math.Sign(value);
}
/// <summary> /// <summary>
/// Converts an integer value from network byte order to host byte order. /// Converts an integer value from network byte order to host byte order.
/// </summary> /// </summary>

View File

@ -1,4 +1,6 @@
using System.Net; using System.Diagnostics.Contracts;
using System.Net;
using System.Runtime.CompilerServices;
namespace X10D; namespace X10D;
@ -241,6 +243,40 @@ public static class Int32Extensions
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 Math.Sign(value);
}
/// <summary> /// <summary>
/// Converts an integer value from network byte order to host byte order. /// Converts an integer value from network byte order to host byte order.
/// </summary> /// </summary>

View File

@ -1,4 +1,6 @@
using System.Net; using System.Diagnostics.Contracts;
using System.Net;
using System.Runtime.CompilerServices;
namespace X10D; namespace X10D;
@ -202,6 +204,40 @@ public static class Int64Extensions
return MathUtils.Lerp(value, target, alpha); return MathUtils.Lerp(value, target, alpha);
} }
/// <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(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int Sign(this long value)
{
return Math.Sign(value);
}
/// <summary> /// <summary>
/// Converts an integer value from network byte order to host byte order. /// Converts an integer value from network byte order to host byte order.
/// </summary> /// </summary>

View File

@ -1,4 +1,7 @@
namespace X10D; using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace X10D;
/// <summary> /// <summary>
/// Extension methods for <see cref="float" />. /// Extension methods for <see cref="float" />.
@ -123,4 +126,38 @@ public static class SingleExtensions
{ {
return MathF.Round(value / nearest) * nearest; return MathF.Round(value / nearest) * nearest;
} }
/// <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(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int Sign(this float value)
{
return MathF.Sign(value);
}
} }