---
title: ceil()
url: https://docs.vultr.com/cpp/standard-library/cmath/ceil
description: Round Up to Integer
publish_date: 2024-11-12T19:00:00.060245Z
last_updated: 2024-11-12T19:00:00.060245Z
header_image: https://docs.vultr.com/public/doc-assets/collection-items/779/97598262-9ecf-4b36-87fb-fb28ef05a9c2.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# C++ cmath ceil() - Round Up to Integer

## Introduction

The `ceil()` function in C++ is a standard library function that performs a mathematical ceil operation, which rounds up the result to the nearest integer greater than or equal to the given number. This function is part of the C++ `<cmath>` library, which includes various math operations essential for handling floating-point numbers effectively.

In this article, learn how to leverage the `ceil()` function to round up decimal numbers to the nearest integer. Understand through practical examples how this method behaves with different types of floating-point values and explore its significance in real-world applications.

## Understanding ceil()

### Basic Usage of ceil()
1. Include the `<cmath>` library in your C++ program.
1. Use the `ceil()` function to round up a floating-point number.

    ```cpp
    #include <iostream>
    #include <cmath>

    int main() {
        double num = 3.14;
        double result = ceil(num);
        std::cout << result << std::endl;  // Output will be 4
    }
    ```

    In this code, `ceil(num)` rounds up the value `3.14` to `4`, as `ceil()` always rounds up to the nearest whole number.

### Handling Negative Values
1. Be aware that `ceil()` rounds up, which means it moves away from zero for negative numbers.
1. Test `ceil()` with a negative floating-point number.

    ```cpp
    #include <iostream>
    #include <cmath>

    int main() {
        double num = -3.14;
        double result = ceil(num);
        std::cout << result << std::endl;  // Output will be -3
    }
    ```

    Here, `ceil()` rounds `-3.14` to `-3` because rounding up negative numbers still increases their value (making them less negative).

### Dealing with Zero and Positive Integers
1. Recognize that using `ceil()` on zero or any positive integer results in the number itself.
1. Apply the `ceil()` function to zero and a positive integer.

    ```cpp
    #include <iostream>
    #include <cmath>

    int main() {
        std::cout << ceil(0.0) << std::endl;  // Output is 0
        std::cout << ceil(5.0) << std::endl;  // Output is 5
    }
    ```

    The output for `ceil(0.0)` is `0` and for `ceil(5.0)` is `5`, demonstrating that numbers without fractional parts remain unchanged.

## Practical Applications of ceil()
Using `ceil()` in practical scenarios can significantly aid in various fields like financial calculations, graphics rendering, and any domain where adjustments to the next whole number are necessary.

* **Budget Projections**: Round up cost estimates to avoid under-budgeting.
* **Pixel Calculations**: Compute discrete pixel values in computer graphics.
* **Statistical Roundings**: Adjust sample sizes and other statistics.

## Conclusion

Master the `ceil()` function from the C++ `<cmath>` library to round up floating-point numbers to their nearest integers. Because of its straightforward yet powerful ability to adjust values upwards, `ceil()` finds extensive use in mathematical computing, graphics, finance, and beyond. Implementing the `ceil()` function in your C++ projects ensures precise control over numeric calculations, fostering accuracy and efficiency in your programming tasks.