Skip to content

refactor: refactor Ceil and improved tests #6366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 14, 2025
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: 19 additions & 8 deletions src/main/java/com/thealgorithms/maths/Ceil.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
package com.thealgorithms.maths;

/**
* Utility class to compute the ceiling of a given number.
*/
public final class Ceil {

private Ceil() {
}

/**
* Returns the smallest (closest to negative infinity)
* Returns the smallest double value that is greater than or equal to the input.
* Equivalent to mathematical ⌈x⌉ (ceiling function).
*
* @param number the number
* @return the smallest (closest to negative infinity) of given
* {@code number}
* @param number the number to ceil
* @return the smallest double greater than or equal to {@code number}
*/
public static double ceil(double number) {
if (number - (int) number == 0) {
if (Double.isNaN(number) || Double.isInfinite(number) || number == 0.0 || number < Integer.MIN_VALUE || number > Integer.MAX_VALUE) {
return number;
} else if (number - (int) number > 0) {
return (int) (number + 1);
}

if (number < 0.0 && number > -1.0) {
return -0.0;
}

long intPart = (long) number;
if (number > 0 && number != intPart) {
return intPart + 1.0;
} else {
return (int) number;
return intPart;
}
}
}
30 changes: 22 additions & 8 deletions src/test/java/com/thealgorithms/maths/CeilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,30 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;

public class CeilTest {

@Test
void testCeil() {
assertEquals(8, Ceil.ceil(7.057));
assertEquals(8, Ceil.ceil(7.004));
assertEquals(-13, Ceil.ceil(-13.004));
assertEquals(1, Ceil.ceil(.98));
assertEquals(-11, Ceil.ceil(-11.357));
@ParameterizedTest
@CsvSource({"7.057, 8", "7.004, 8", "-13.004, -13", "0.98, 1", "-11.357, -11"})
void testCeil(double input, int expected) {
assertEquals(expected, Ceil.ceil(input));
}

@ParameterizedTest
@MethodSource("edgeCaseProvider")
void testEdgeCases(TestData data) {
assertEquals(Ceil.ceil(data.input), data.expected);
}

record TestData(double input, double expected) {
}

static Stream<TestData> edgeCaseProvider() {
return Stream.of(new TestData(Double.MAX_VALUE, Double.MAX_VALUE), new TestData(Double.MIN_VALUE, Math.ceil(Double.MIN_VALUE)), new TestData(0.0, Math.ceil(0.0)), new TestData(-0.0, Math.ceil(-0.0)), new TestData(Double.NaN, Math.ceil(Double.NaN)),
new TestData(Double.NEGATIVE_INFINITY, Math.ceil(Double.NEGATIVE_INFINITY)), new TestData(Double.POSITIVE_INFINITY, Math.ceil(Double.POSITIVE_INFINITY)));
}
}
Loading