From 7a4f1f66089436edb8696a1cb6f8adc6a8dd8f47 Mon Sep 17 00:00:00 2001 From: Paulo Date: Wed, 30 Oct 2024 17:31:50 +0000 Subject: [PATCH] Add Floor numeric algorithm --- Algorithms.Tests/Numeric/FloorTests.cs | 27 ++++++++++++++++++++++++++ Algorithms/Numeric/Floor.cs | 23 ++++++++++++++++++++++ README.md | 1 + 3 files changed, 51 insertions(+) create mode 100644 Algorithms.Tests/Numeric/FloorTests.cs create mode 100644 Algorithms/Numeric/Floor.cs diff --git a/Algorithms.Tests/Numeric/FloorTests.cs b/Algorithms.Tests/Numeric/FloorTests.cs new file mode 100644 index 00000000..9221c0d2 --- /dev/null +++ b/Algorithms.Tests/Numeric/FloorTests.cs @@ -0,0 +1,27 @@ +using System; +using System.Numerics; +using Algorithms.Numeric; +using NUnit.Framework; + +namespace Algorithms.Tests.Numeric; + +public static class FloorTests +{ + [TestCase(0.0, 0)] + [TestCase(1.1, 1)] + [TestCase(1.9, 1)] + [TestCase(1.0, 1)] + [TestCase(-1.1, -2)] + [TestCase(-1.9, -2)] + [TestCase(-1.0, -1)] + [TestCase(1000000000.1, 1000000000)] + [TestCase(1, 1)] + public static void GetsFloorVal(T inputNum, T expected) where T : INumber + { + // Act + var result = Floor.FloorVal(inputNum); + + // Assert + Assert.That(result, Is.EqualTo(expected)); + } +} \ No newline at end of file diff --git a/Algorithms/Numeric/Floor.cs b/Algorithms/Numeric/Floor.cs new file mode 100644 index 00000000..b59131df --- /dev/null +++ b/Algorithms/Numeric/Floor.cs @@ -0,0 +1,23 @@ +using System; +using System.Numerics; + +namespace Algorithms.Numeric; + +/// +/// Perform floor operation on a number. +/// +public static class Floor +{ + /// + /// Returns the largest integer less than or equal to the number. + /// + /// Type of number. + /// Number to find the floor of. + /// Floor value of the number. + public static T FloorVal(T inputNum) where T : INumber + { + T intPart = T.CreateChecked(Convert.ToInt32(inputNum)); + + return inputNum < intPart ? intPart - T.One : intPart; + } +} diff --git a/README.md b/README.md index d64f7803..8cce7949 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ find more than one implementation for the same objective but using different alg * [Decomposition](./Algorithms/Numeric/Decomposition) * [LU Decomposition](./Algorithms/Numeric/Decomposition/LU.cs) * [Thin Singular Vector Decomposition](./Algorithms/Numeric/Decomposition/ThinSVD.cs) + * [Floor](./Algorithms/Floor.cs) * [Greatest Common Divisor](./Algorithms/Numeric/GreatestCommonDivisor) * [Euclidean GCD](./Algorithms/Numeric/GreatestCommonDivisor/EuclideanGreatestCommonDivisorFinder.cs) * [Binary GCD](./Algorithms/Numeric/GreatestCommonDivisor/BinaryGreatestCommonDivisorFinder.cs)