diff --git a/src/main/java/com/thealgorithms/maths/Ceil.java b/src/main/java/com/thealgorithms/maths/Ceil.java index aacb9d969950..28804eb1c569 100644 --- a/src/main/java/com/thealgorithms/maths/Ceil.java +++ b/src/main/java/com/thealgorithms/maths/Ceil.java @@ -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; } } } diff --git a/src/test/java/com/thealgorithms/maths/CeilTest.java b/src/test/java/com/thealgorithms/maths/CeilTest.java index 5596760a8c40..ddd0deed41d3 100644 --- a/src/test/java/com/thealgorithms/maths/CeilTest.java +++ b/src/test/java/com/thealgorithms/maths/CeilTest.java @@ -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 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))); } }