-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6fce1f5
commit cb45f4a
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters