Skip to content

Commit 0aea7e7

Browse files
Adding the absolute value algorithm
1 parent 351b95b commit 0aea7e7

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Numerics;
2+
using Algorithms.Numeric;
3+
using NUnit.Framework;
4+
5+
namespace Algorithms.Tests.Numeric;
6+
7+
public static class AbsTests
8+
{
9+
[TestCase(0, 0)]
10+
[TestCase(34, 34)]
11+
[TestCase(-100000000000.0d, 100000000000.0d)]
12+
[TestCase(-3, 3)]
13+
[TestCase(-3.1443123d, 3.1443123d)]
14+
public static void GetsAbsVal<T>(T inputNum, T expected) where T : INumber<T>
15+
{
16+
// Act
17+
var result = Abs.AbsVal(inputNum);
18+
19+
// Assert
20+
Assert.That(result, Is.EqualTo(expected));
21+
}
22+
23+
[TestCase(new int[] { -3, -1, 2, -11 }, -11)]
24+
[TestCase(new int[] { 0, 5, 1, 11 }, 11)]
25+
[TestCase(new double[] { 3.0, -10.0, -2.0 }, -10.0d)]
26+
public static void GetAbsMax<T>(T[] inputNums, T expected) where T : INumber<T>
27+
{
28+
// Act
29+
var result = Abs.AbsMax(inputNums);
30+
31+
// Assert
32+
Assert.That(result, Is.EqualTo(expected));
33+
}
34+
35+
[TestCase(new int[] { -3, -1, 2, -11 }, -1)]
36+
[TestCase(new int[] { -3, -5, 1, -11 }, 1)]
37+
[TestCase(new int[] { 0, 5, 1, 11 }, 0)]
38+
public static void GetAbsMin<T>(T[] inputNums, T expected) where T : INumber<T>
39+
{
40+
// Act
41+
var result = Abs.AbsMin(inputNums);
42+
43+
// Assert
44+
Assert.That(result, Is.EqualTo(expected));
45+
}
46+
}

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)