From 3252636a6f6c4758ebbffc80311cc5af2325d639 Mon Sep 17 00:00:00 2001 From: Paulo Date: Mon, 28 Oct 2024 22:32:15 +0000 Subject: [PATCH] Add Ceil numeric algorithm --- Algorithms.Tests/Numeric/CeilTests.cs | 27 +++++++++++++++++++++++++++ Algorithms/Numeric/Ceil.cs | 23 +++++++++++++++++++++++ README.md | 1 + 3 files changed, 51 insertions(+) create mode 100644 Algorithms.Tests/Numeric/CeilTests.cs create mode 100644 Algorithms/Numeric/Ceil.cs diff --git a/Algorithms.Tests/Numeric/CeilTests.cs b/Algorithms.Tests/Numeric/CeilTests.cs new file mode 100644 index 00000000..a43904fa --- /dev/null +++ b/Algorithms.Tests/Numeric/CeilTests.cs @@ -0,0 +1,27 @@ +using System; +using System.Numerics; +using Algorithms.Numeric; +using NUnit.Framework; + +namespace Algorithms.Tests.Numeric; + +public static class CeilTests +{ + [TestCase(0.0, 0)] + [TestCase(1.1, 2)] + [TestCase(1.9, 2)] + [TestCase(1.0, 1)] + [TestCase(-1.1, -1)] + [TestCase(-1.9, -1)] + [TestCase(-1.0, -1)] + [TestCase(1000000000.1, 1000000001)] + [TestCase(1, 1)] + public static void GetsCeilVal(T inputNum, T expected) where T : INumber + { + // Act + var result = Ceil.CeilVal(inputNum); + + // Assert + Assert.That(result, Is.EqualTo(expected)); + } +} \ No newline at end of file diff --git a/Algorithms/Numeric/Ceil.cs b/Algorithms/Numeric/Ceil.cs new file mode 100644 index 00000000..26b718b7 --- /dev/null +++ b/Algorithms/Numeric/Ceil.cs @@ -0,0 +1,23 @@ +using System; +using System.Numerics; + +namespace Algorithms.Numeric; + +/// +/// Perform ceiling operation on a number. +/// +public static class Ceil +{ + /// + /// Returns the smallest integer greater than or equal to the number. + /// + /// Type of number. + /// Number to find the ceiling of. + /// Ceiling value of the number. + public static T CeilVal(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 8a1308e2..d64f7803 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ find more than one implementation for the same objective but using different alg * [Addition Without Arithmetic](./Algorithms/Numeric/AdditionWithoutArithmetic.cs) * [Aliquot Sum Calculator](./Algorithms/Numeric/AliquotSumCalculator.cs) * [Amicable Numbers Checker](./Algorithms/Numeric/AmicableNumbersChecker.cs) + * [Ceil](./Algorithms/Numeric/Ceil.cs) * [Decomposition](./Algorithms/Numeric/Decomposition) * [LU Decomposition](./Algorithms/Numeric/Decomposition/LU.cs) * [Thin Singular Vector Decomposition](./Algorithms/Numeric/Decomposition/ThinSVD.cs)