Skip to content

Commit

Permalink
Add absolute value algorithm (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoRibeiroRodrigues authored Aug 25, 2024
1 parent 6fce1f5 commit cb45f4a
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
67 changes: 67 additions & 0 deletions Algorithms.Tests/Numeric/AbsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Numerics;
using Algorithms.Numeric;
using NUnit.Framework;

namespace Algorithms.Tests.Numeric;

public static class AbsTests
{
[TestCase(0, 0)]
[TestCase(34, 34)]
[TestCase(-100000000000.0d, 100000000000.0d)]
[TestCase(-3, 3)]
[TestCase(-3.1443123d, 3.1443123d)]
public static void GetsAbsVal<T>(T inputNum, T expected) where T : INumber<T>
{
// Act
var result = Abs.AbsVal(inputNum);

// Assert
Assert.That(result, Is.EqualTo(expected));
}

[TestCase(new[] { -3, -1, 2, -11 }, -11)]
[TestCase(new[] { 0, 5, 1, 11 }, 11)]
[TestCase(new[] { 3.0, -10.0, -2.0 }, -10.0d)]
public static void GetAbsMax<T>(T[] inputNums, T expected) where T : INumber<T>
{
// Act
var result = Abs.AbsMax(inputNums);

// Assert
Assert.That(result, Is.EqualTo(expected));
}

[Test]
public static void AbsMaxThrowsArgumentException()
{
// Arrange
var inputNums = Array.Empty<int>();

// Assert
Assert.Throws<ArgumentException>(() => Abs.AbsMax(inputNums));
}

[TestCase(new[] { -3, -1, 2, -11 }, -1)]
[TestCase(new[] { -3, -5, 1, -11 }, 1)]
[TestCase(new[] { 0, 5, 1, 11 }, 0)]
public static void GetAbsMin<T>(T[] inputNums, T expected) where T : INumber<T>
{
// Act
var result = Abs.AbsMin(inputNums);

// Assert
Assert.That(result, Is.EqualTo(expected));
}

[Test]
public static void AbsMinThrowsArgumentException()
{
// Arrange
var inputNums = Array.Empty<int>();

// Assert
Assert.Throws<ArgumentException>(() => Abs.AbsMin(inputNums));
}
}
73 changes: 73 additions & 0 deletions Algorithms/Numeric/Abs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Numerics;

namespace Algorithms.Numeric;

/// <summary>
/// Find the absolute value of a number.
/// </summary>
public static class Abs
{
/// <summary>
/// Returns the absolute value of a number.
/// </summary>
/// <typeparam name="T">Type of number.</typeparam>
/// <param name="inputNum">Number to find the absolute value of.</param>
/// <returns>Absolute value of the number.</returns>
public static T AbsVal<T>(T inputNum) where T : INumber<T>
{
return T.IsNegative(inputNum) ? -inputNum : inputNum;
}

/// <summary>
/// Returns the number with the smallest absolute value on the input array.
/// </summary>
/// <typeparam name="T">Type of number.</typeparam>
/// <param name="inputNums">Array of numbers to find the smallest absolute.</param>
/// <returns>Smallest absolute number.</returns>
public static T AbsMin<T>(T[] inputNums) where T : INumber<T>
{
if (inputNums.Length == 0)
{
throw new ArgumentException("Array is empty.");
}

var min = inputNums[0];
for (var index = 1; index < inputNums.Length; index++)
{
var current = inputNums[index];
if (AbsVal(current).CompareTo(AbsVal(min)) < 0)
{
min = current;
}
}

return min;
}

/// <summary>
/// Returns the number with the largest absolute value on the input array.
/// </summary>
/// <typeparam name="T">Type of number.</typeparam>
/// <param name="inputNums">Array of numbers to find the largest absolute.</param>
/// <returns>Largest absolute number.</returns>
public static T AbsMax<T>(T[] inputNums) where T : INumber<T>
{
if (inputNums.Length == 0)
{
throw new ArgumentException("Array is empty.");
}

var max = inputNums[0];
for (var index = 1; index < inputNums.Length; index++)
{
var current = inputNums[index];
if (AbsVal(current).CompareTo(AbsVal(max)) > 0)
{
max = current;
}
}

return max;
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ find more than one implementation for the same objective but using different alg
* [Extended Euclidean Algorithm](./Algorithms/ModularArithmetic/ExtendedEuclideanAlgorithm.cs)
* [Modular Multiplicative Inverse](./Algorithms/ModularArithmetic/ModularMultiplicativeInverse.cs)
* [Numeric](./Algorithms/Numeric)
* [Absolute](./Algorithms/Numeric/Abs.cs)
* [Aliquot Sum Calculator](./Algorithms/Numeric/AliquotSumCalculator.cs)
* [Amicable Numbers Checker](./Algorithms/Numeric/AmicableNumbersChecker.cs)
* [Decomposition](./Algorithms/Numeric/Decomposition)
Expand Down

0 comments on commit cb45f4a

Please sign in to comment.