From 86b6a94adc37ec3e73d53f4003a0244e22b5d3bf Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 21 Apr 2022 11:04:05 +0100 Subject: [PATCH] Add Sign() for signed numeric types Behaves as Math.Sign() --- X10D/src/ByteExtensions/SByteExtensions.cs | 37 ++++++++++++++++++ X10D/src/DoubleExtensions/DoubleExtensions.cs | 39 ++++++++++++++++++- X10D/src/Int16Extensions/Int16Extensions.cs | 36 +++++++++++++++++ X10D/src/Int32Extensions/Int32Extensions.cs | 38 +++++++++++++++++- X10D/src/Int64Extensions/Int64Extensions.cs | 38 +++++++++++++++++- X10D/src/SingleExtensions/SingleExtensions.cs | 39 ++++++++++++++++++- 6 files changed, 223 insertions(+), 4 deletions(-) diff --git a/X10D/src/ByteExtensions/SByteExtensions.cs b/X10D/src/ByteExtensions/SByteExtensions.cs index 03830b6..b60697c 100644 --- a/X10D/src/ByteExtensions/SByteExtensions.cs +++ b/X10D/src/ByteExtensions/SByteExtensions.cs @@ -1,3 +1,6 @@ +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; + namespace X10D; /// @@ -43,4 +46,38 @@ public static class SByteExtensions { return ((short)value).IsPrime(); } + + /// + /// Returns an integer that indicates the sign of this 8-bit signed integer. + /// + /// A signed number. + /// + /// A number that indicates the sign of , as shown in the following table. + /// + /// + /// + /// Return value + /// Meaning + /// + /// + /// + /// -1 + /// is less than zero. + /// + /// + /// 0 + /// is equal to zero. + /// + /// + /// 1 + /// is greater than zero. + /// + /// + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static int Sign(this sbyte value) + { + return Math.Sign(value); + } } diff --git a/X10D/src/DoubleExtensions/DoubleExtensions.cs b/X10D/src/DoubleExtensions/DoubleExtensions.cs index e0285be..d130389 100644 --- a/X10D/src/DoubleExtensions/DoubleExtensions.cs +++ b/X10D/src/DoubleExtensions/DoubleExtensions.cs @@ -1,4 +1,7 @@ -namespace X10D; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; + +namespace X10D; /// /// Extension methods for . @@ -123,4 +126,38 @@ public static class DoubleExtensions { return Math.Round(value / nearest) * nearest; } + + /// + /// Returns an integer that indicates the sign of this double-precision floating-point number. + /// + /// A signed number. + /// + /// A number that indicates the sign of , as shown in the following table. + /// + /// + /// + /// Return value + /// Meaning + /// + /// + /// + /// -1 + /// is less than zero. + /// + /// + /// 0 + /// is equal to zero. + /// + /// + /// 1 + /// is greater than zero. + /// + /// + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static int Sign(this double value) + { + return Math.Sign(value); + } } diff --git a/X10D/src/Int16Extensions/Int16Extensions.cs b/X10D/src/Int16Extensions/Int16Extensions.cs index 26cdd7d..1c93055 100644 --- a/X10D/src/Int16Extensions/Int16Extensions.cs +++ b/X10D/src/Int16Extensions/Int16Extensions.cs @@ -1,4 +1,6 @@ +using System.Diagnostics.Contracts; using System.Net; +using System.Runtime.CompilerServices; namespace X10D; @@ -177,6 +179,40 @@ public static class Int16Extensions return MathUtils.Lerp(value, target, alpha); } + /// + /// Returns an integer that indicates the sign of this 16-bit signed integer. + /// + /// A signed number. + /// + /// A number that indicates the sign of , as shown in the following table. + /// + /// + /// + /// Return value + /// Meaning + /// + /// + /// + /// -1 + /// is less than zero. + /// + /// + /// 0 + /// is equal to zero. + /// + /// + /// 1 + /// is greater than zero. + /// + /// + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static int Sign(this short value) + { + return Math.Sign(value); + } + /// /// Converts an integer value from network byte order to host byte order. /// diff --git a/X10D/src/Int32Extensions/Int32Extensions.cs b/X10D/src/Int32Extensions/Int32Extensions.cs index 3340077..f7ae159 100644 --- a/X10D/src/Int32Extensions/Int32Extensions.cs +++ b/X10D/src/Int32Extensions/Int32Extensions.cs @@ -1,4 +1,6 @@ -using System.Net; +using System.Diagnostics.Contracts; +using System.Net; +using System.Runtime.CompilerServices; namespace X10D; @@ -241,6 +243,40 @@ public static class Int32Extensions return r < 0 ? r + divisor : r; } + /// + /// Returns an integer that indicates the sign of this 32-bit signed integer. + /// + /// A signed number. + /// + /// A number that indicates the sign of , as shown in the following table. + /// + /// + /// + /// Return value + /// Meaning + /// + /// + /// + /// -1 + /// is less than zero. + /// + /// + /// 0 + /// is equal to zero. + /// + /// + /// 1 + /// is greater than zero. + /// + /// + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static int Sign(this int value) + { + return Math.Sign(value); + } + /// /// Converts an integer value from network byte order to host byte order. /// diff --git a/X10D/src/Int64Extensions/Int64Extensions.cs b/X10D/src/Int64Extensions/Int64Extensions.cs index 55aab75..f523be8 100644 --- a/X10D/src/Int64Extensions/Int64Extensions.cs +++ b/X10D/src/Int64Extensions/Int64Extensions.cs @@ -1,4 +1,6 @@ -using System.Net; +using System.Diagnostics.Contracts; +using System.Net; +using System.Runtime.CompilerServices; namespace X10D; @@ -202,6 +204,40 @@ public static class Int64Extensions return MathUtils.Lerp(value, target, alpha); } + /// + /// Returns an integer that indicates the sign of this 64-bit signed integer. + /// + /// A signed number. + /// + /// A number that indicates the sign of , as shown in the following table. + /// + /// + /// + /// Return value + /// Meaning + /// + /// + /// + /// -1 + /// is less than zero. + /// + /// + /// 0 + /// is equal to zero. + /// + /// + /// 1 + /// is greater than zero. + /// + /// + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static int Sign(this long value) + { + return Math.Sign(value); + } + /// /// Converts an integer value from network byte order to host byte order. /// diff --git a/X10D/src/SingleExtensions/SingleExtensions.cs b/X10D/src/SingleExtensions/SingleExtensions.cs index ec7594e..93adee1 100644 --- a/X10D/src/SingleExtensions/SingleExtensions.cs +++ b/X10D/src/SingleExtensions/SingleExtensions.cs @@ -1,4 +1,7 @@ -namespace X10D; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; + +namespace X10D; /// /// Extension methods for . @@ -123,4 +126,38 @@ public static class SingleExtensions { return MathF.Round(value / nearest) * nearest; } + + /// + /// Returns an integer that indicates the sign of this single-precision floating-point number. + /// + /// A signed number. + /// + /// A number that indicates the sign of , as shown in the following table. + /// + /// + /// + /// Return value + /// Meaning + /// + /// + /// + /// -1 + /// is less than zero. + /// + /// + /// 0 + /// is equal to zero. + /// + /// + /// 1 + /// is greater than zero. + /// + /// + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static int Sign(this float value) + { + return MathF.Sign(value); + } }