Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Algorithms.Tests/Numeric/CeilTests.cs
Original file line number Diff line number Diff line change
@@ -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>(T inputNum, T expected) where T : INumber<T>
{
// Act
var result = Ceil.CeilVal(inputNum);

// Assert
Assert.That(result, Is.EqualTo(expected));
}
}
23 changes: 23 additions & 0 deletions Algorithms/Numeric/Ceil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Numerics;

namespace Algorithms.Numeric;

/// <summary>
/// Perform ceiling operation on a number.
/// </summary>
public static class Ceil
{
/// <summary>
/// Returns the smallest integer greater than or equal to the number.
/// </summary>
/// <typeparam name="T">Type of number.</typeparam>
/// <param name="inputNum">Number to find the ceiling of.</param>
/// <returns>Ceiling value of the number.</returns>
public static T CeilVal<T>(T inputNum) where T : INumber<T>
{
T intPart = T.CreateChecked(Convert.ToInt32(inputNum));

return inputNum > intPart ? intPart + T.One : intPart;
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading