Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions csharp/Platform.Numbers.Tests/MathTests.cs
Original file line number Diff line number Diff line change
@@ -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<int>.Abs(-5));
Assert.Equal(5, Math<int>.Abs(5));
Assert.Equal(0, Math<int>.Abs(0));

// Test with long
Assert.Equal(123L, Math<long>.Abs(-123L));
Assert.Equal(123L, Math<long>.Abs(123L));

// Test with double
Assert.Equal(3.14, Math<double>.Abs(-3.14));
Assert.Equal(3.14, Math<double>.Abs(3.14));

// Test with unsigned types (should return same value)
Assert.Equal(42u, Math<uint>.Abs(42u));
Assert.Equal(0u, Math<uint>.Abs(0u));
}

[Fact]
public static void NegateTest()
{
// Test with signed integers
Assert.Equal(-5, Math<int>.Negate(5));
Assert.Equal(5, Math<int>.Negate(-5));
Assert.Equal(0, Math<int>.Negate(0));

// Test with long
Assert.Equal(-123L, Math<long>.Negate(123L));
Assert.Equal(123L, Math<long>.Negate(-123L));

// Test with double
Assert.Equal(-3.14, Math<double>.Negate(3.14));
Assert.Equal(3.14, Math<double>.Negate(-3.14));

// Test with float
Assert.Equal(-2.5f, Math<float>.Negate(2.5f));
Assert.Equal(2.5f, Math<float>.Negate(-2.5f));
}
}
}
53 changes: 53 additions & 0 deletions csharp/Platform.Numbers/Math[T].cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Numerics;
using System.Runtime.CompilerServices;

namespace Platform.Numbers
{
/// <summary>
/// <para>Represents a set of math operations using direct reference to operator methods of .NET classes.</para>
/// <para>Представляет набор математических операций, используя прямую ссылку на методы операторов классов .NET.</para>
/// </summary>
public static class Math<T> where T : INumberBase<T>
{
/// <summary>
/// <para>Returns the absolute value of a number.</para>
/// <para>Возвращает абсолютное значение числа.</para>
/// </summary>
/// <param name="x">The number to get the absolute value of.</param>
/// <returns>The absolute value of the input number.</returns>
[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;
}

/// <summary>
/// <para>Returns the negation of a number.</para>
/// <para>Возвращает отрицание числа.</para>
/// </summary>
/// <param name="x">The number to negate.</param>
/// <returns>The negated value of the input number.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Negate(T x)
{
return -x;
}
}
}
Loading