Skip to content

Commit cb45f4a

Browse files
Add absolute value algorithm (#462)
1 parent 6fce1f5 commit cb45f4a

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

Algorithms.Tests/Numeric/AbsTests.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Numerics;
3+
using Algorithms.Numeric;
4+
using NUnit.Framework;
5+
6+
namespace Algorithms.Tests.Numeric;
7+
8+
public static class AbsTests
9+
{
10+
[TestCase(0, 0)]
11+
[TestCase(34, 34)]
12+
[TestCase(-100000000000.0d, 100000000000.0d)]
13+
[TestCase(-3, 3)]
14+
[TestCase(-3.1443123d, 3.1443123d)]
15+
public static void GetsAbsVal<T>(T inputNum, T expected) where T : INumber<T>
16+
{
17+
// Act
18+
var result = Abs.AbsVal(inputNum);
19+
20+
// Assert
21+
Assert.That(result, Is.EqualTo(expected));
22+
}
23+
24+
[TestCase(new[] { -3, -1, 2, -11 }, -11)]
25+
[TestCase(new[] { 0, 5, 1, 11 }, 11)]
26+
[TestCase(new[] { 3.0, -10.0, -2.0 }, -10.0d)]
27+
public static void GetAbsMax<T>(T[] inputNums, T expected) where T : INumber<T>
28+
{
29+
// Act
30+
var result = Abs.AbsMax(inputNums);
31+
32+
// Assert
33+
Assert.That(result, Is.EqualTo(expected));
34+
}
35+
36+
[Test]
37+
public static void AbsMaxThrowsArgumentException()
38+
{
39+
// Arrange
40+
var inputNums = Array.Empty<int>();
41+
42+
// Assert
43+
Assert.Throws<ArgumentException>(() => Abs.AbsMax(inputNums));
44+
}
45+
46+
[TestCase(new[] { -3, -1, 2, -11 }, -1)]
47+
[TestCase(new[] { -3, -5, 1, -11 }, 1)]
48+
[TestCase(new[] { 0, 5, 1, 11 }, 0)]
49+
public static void GetAbsMin<T>(T[] inputNums, T expected) where T : INumber<T>
50+
{
51+
// Act
52+
var result = Abs.AbsMin(inputNums);
53+
54+
// Assert
55+
Assert.That(result, Is.EqualTo(expected));
56+
}
57+
58+
[Test]
59+
public static void AbsMinThrowsArgumentException()
60+
{
61+
// Arrange
62+
var inputNums = Array.Empty<int>();
63+
64+
// Assert
65+
Assert.Throws<ArgumentException>(() => Abs.AbsMin(inputNums));
66+
}
67+
}

Algorithms/Numeric/Abs.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Numerics;
3+
4+
namespace Algorithms.Numeric;
5+
6+
/// <summary>
7+
/// Find the absolute value of a number.
8+
/// </summary>
9+
public static class Abs
10+
{
11+
/// <summary>
12+
/// Returns the absolute value of a number.
13+
/// </summary>
14+
/// <typeparam name="T">Type of number.</typeparam>
15+
/// <param name="inputNum">Number to find the absolute value of.</param>
16+
/// <returns>Absolute value of the number.</returns>
17+
public static T AbsVal<T>(T inputNum) where T : INumber<T>
18+
{
19+
return T.IsNegative(inputNum) ? -inputNum : inputNum;
20+
}
21+
22+
/// <summary>
23+
/// Returns the number with the smallest absolute value on the input array.
24+
/// </summary>
25+
/// <typeparam name="T">Type of number.</typeparam>
26+
/// <param name="inputNums">Array of numbers to find the smallest absolute.</param>
27+
/// <returns>Smallest absolute number.</returns>
28+
public static T AbsMin<T>(T[] inputNums) where T : INumber<T>
29+
{
30+
if (inputNums.Length == 0)
31+
{
32+
throw new ArgumentException("Array is empty.");
33+
}
34+
35+
var min = inputNums[0];
36+
for (var index = 1; index < inputNums.Length; index++)
37+
{
38+
var current = inputNums[index];
39+
if (AbsVal(current).CompareTo(AbsVal(min)) < 0)
40+
{
41+
min = current;
42+
}
43+
}
44+
45+
return min;
46+
}
47+
48+
/// <summary>
49+
/// Returns the number with the largest absolute value on the input array.
50+
/// </summary>
51+
/// <typeparam name="T">Type of number.</typeparam>
52+
/// <param name="inputNums">Array of numbers to find the largest absolute.</param>
53+
/// <returns>Largest absolute number.</returns>
54+
public static T AbsMax<T>(T[] inputNums) where T : INumber<T>
55+
{
56+
if (inputNums.Length == 0)
57+
{
58+
throw new ArgumentException("Array is empty.");
59+
}
60+
61+
var max = inputNums[0];
62+
for (var index = 1; index < inputNums.Length; index++)
63+
{
64+
var current = inputNums[index];
65+
if (AbsVal(current).CompareTo(AbsVal(max)) > 0)
66+
{
67+
max = current;
68+
}
69+
}
70+
71+
return max;
72+
}
73+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ find more than one implementation for the same objective but using different alg
6666
* [Extended Euclidean Algorithm](./Algorithms/ModularArithmetic/ExtendedEuclideanAlgorithm.cs)
6767
* [Modular Multiplicative Inverse](./Algorithms/ModularArithmetic/ModularMultiplicativeInverse.cs)
6868
* [Numeric](./Algorithms/Numeric)
69+
* [Absolute](./Algorithms/Numeric/Abs.cs)
6970
* [Aliquot Sum Calculator](./Algorithms/Numeric/AliquotSumCalculator.cs)
7071
* [Amicable Numbers Checker](./Algorithms/Numeric/AmicableNumbersChecker.cs)
7172
* [Decomposition](./Algorithms/Numeric/Decomposition)

0 commit comments

Comments
 (0)