diff --git a/csharp/Platform.Numbers.Tests/MathTests.cs b/csharp/Platform.Numbers.Tests/MathTests.cs new file mode 100644 index 0000000..79934b9 --- /dev/null +++ b/csharp/Platform.Numbers.Tests/MathTests.cs @@ -0,0 +1,49 @@ +using Xunit; + +namespace Platform.Numbers.Tests +{ + public static class MathTests + { + [Fact] + public static void AbsTest() + { + // Test with signed integers + Assert.Equal(5, Math.Abs(-5)); + Assert.Equal(5, Math.Abs(5)); + Assert.Equal(0, Math.Abs(0)); + + // Test with long + Assert.Equal(123L, Math.Abs(-123L)); + Assert.Equal(123L, Math.Abs(123L)); + + // Test with double + Assert.Equal(3.14, Math.Abs(-3.14)); + Assert.Equal(3.14, Math.Abs(3.14)); + + // Test with unsigned types (should return same value) + Assert.Equal(42u, Math.Abs(42u)); + Assert.Equal(0u, Math.Abs(0u)); + } + + [Fact] + public static void NegateTest() + { + // Test with signed integers + Assert.Equal(-5, Math.Negate(5)); + Assert.Equal(5, Math.Negate(-5)); + Assert.Equal(0, Math.Negate(0)); + + // Test with long + Assert.Equal(-123L, Math.Negate(123L)); + Assert.Equal(123L, Math.Negate(-123L)); + + // Test with double + Assert.Equal(-3.14, Math.Negate(3.14)); + Assert.Equal(3.14, Math.Negate(-3.14)); + + // Test with float + Assert.Equal(-2.5f, Math.Negate(2.5f)); + Assert.Equal(2.5f, Math.Negate(-2.5f)); + } + } +} \ No newline at end of file diff --git a/csharp/Platform.Numbers/Math[T].cs b/csharp/Platform.Numbers/Math[T].cs new file mode 100644 index 0000000..799fdf2 --- /dev/null +++ b/csharp/Platform.Numbers/Math[T].cs @@ -0,0 +1,53 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; + +namespace Platform.Numbers +{ + /// + /// Represents a set of math operations using direct reference to operator methods of .NET classes. + /// Представляет набор математических операций, используя прямую ссылку на методы операторов классов .NET. + /// + public static class Math where T : INumberBase + { + /// + /// Returns the absolute value of a number. + /// Возвращает абсолютное значение числа. + /// + /// The number to get the absolute value of. + /// The absolute value of the input number. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T Abs(T x) + { + if (typeof(T) == typeof(sbyte)) + return T.CreateTruncating(System.Math.Abs((sbyte)(object)x)); + if (typeof(T) == typeof(short)) + return T.CreateTruncating(System.Math.Abs((short)(object)x)); + if (typeof(T) == typeof(int)) + return T.CreateTruncating(System.Math.Abs((int)(object)x)); + if (typeof(T) == typeof(long)) + return T.CreateTruncating(System.Math.Abs((long)(object)x)); + if (typeof(T) == typeof(float)) + return T.CreateTruncating(System.Math.Abs((float)(object)x)); + if (typeof(T) == typeof(double)) + return T.CreateTruncating(System.Math.Abs((double)(object)x)); + if (typeof(T) == typeof(decimal)) + return T.CreateTruncating(System.Math.Abs((decimal)(object)x)); + + // For unsigned types, absolute value is the number itself + return x; + } + + /// + /// Returns the negation of a number. + /// Возвращает отрицание числа. + /// + /// The number to negate. + /// The negated value of the input number. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T Negate(T x) + { + return -x; + } + } +} \ No newline at end of file