Ceil function in C++

# Ceil function in C++

Updated on Feb 9, 2023 16:49 IST

In this article, Ceil function will be explored with different programming examples. Limitations of ceiling functions are also covered.

The ceil function in C++ is a mathematical function that rounds a decimal number up to the nearest integer. It is a part of the C++ Standard Template Library (STL) and is defined in the <cmath> header. The function takes a single argument, a decimal number, and returns the smallest integer greater than or equal to the input value.

For example, ceil(2.3) would return 3, and ceil(5.0) would return 5.

### Syntax:

The syntax for the ceil function in C++ is as follows:

`double ceil(double x);Copy code`

Here, x is the decimal number you want to round up to the nearest integer. The function returns the smallest integer greater than or equal to x.

It’s worth noting that the return type of ceil function is double; if you want to use it with other data types like float or long double, you need to cast the input argument to be double before calling the function.

`float num = 2.3;cout<<ceil(double(num))<<endl; // 3Copy code`

Master Member Functions in C++
If you are new to C++, it is crucial to learn about the Member function. With different types of member functions in C++ as well as the methods of declaration...read more
New and Delete Operators in C++
The new operator is used to dynamically allocate memory on the heap for an object or an array of objects. And delete operator is used to deallocate the memory. This...read more

Also explore: Default Arguments in C++

Must read: Understanding Operators in C++

## Ceil() Prototypes

In C++, the ceil() function is a part of the C++ Standard Template Library (STL) and is defined in the <cmath> header. The <cmath> header provides several mathematical functions, including ceil(). The function is defined with multiple prototypes, depending on the data type of the input argument and the return value.

The prototypes are as follows:

`double ceil(double x);float ceil(float x);long double ceil(long double x);Copy code`

In addition to ceil(), C++ also provides similar functions ceilf() and ceill(), for operating on float and long double data types, respectively. These functions work in the same way as ceil(), but they return the result as a float and long double, respectively.

For example:

```float f = 3.14f;float result = ceilf(f); //result will be 4.0f long double l = 3.14;long double result = ceill(l); //result will be 4.0Copy code```

It is important to note that the ceil(), ceilf(), and ceill() functions are defined in the <cmath> header file. So it should be included to use these functions.

## Limitations of ceil() function

There are a few drawbacks to using the ceil() function in C++:

1. The function can cause overflow or underflow: If the input value is too large or too small, the function can produce unexpected results or cause errors in the program.
2. It can also cause performance issues: The ceil() function is computationally expensive, especially when used in tight loops or with large arrays. This can slow down the performance of your program.
3. Loss of precision: The ceil() function rounds the input value to the next highest integer, which can cause a loss of precision for certain decimal values.
4. Limited range of values: The ceil() function can only be used for floating-point numbers, so it is not suitable for other types of data.
5. It doesn’t handle NaN, +infinity, and -infinity: The function can’t handle the special cases of NaN, +infinity, and -infinity. It will output an error in such cases.

Remembering these drawbacks when using the ceil() function in your C++ program is important. In some cases, an alternative approach may be more appropriate, such as using a round() function or manual rounding.

## Examples of ceil() Function

Here are some examples of how the ceil() function can be used in C++:

### Example 1: Rounding a decimal value to the next highest integer

```#include <iostream>#include <cmath> int main(){ double num = 3.14; std::cout << "Original value: " << num << std::endl; std::cout << "Rounded value: " << std::ceil(num) << std::endl; return 0;Copy code```

Output:

Original value: 3.14

Rounded value: 4

### Example 2: Rounding a negative decimal value

```#include <iostream>#include <cmath> int main(){ double num = -2.99; std::cout << "Original value: " << num << std::endl; std::cout << "Rounded value: " << std::ceil(num) << std::endl; return 0;}Copy code```

Output:

Original value: -2.99

Rounded value: -2

### Example 3: Rounding a decimal value with a precision of 2 decimal places

```#include <iostream>#include <cmath>#include <iomanip> int main(){ double num = 3.14; std::cout << "Original value: " << std::setprecision(2) << num << std::endl; std::cout << "Rounded value: " << std::ceil(num) << std::endl; return 0;}Copy code```

Output:

Original value: 3.14

Rounded value: 4

You can also use ceilf() and ceill() in a similar way for float and long double, respectively.

It would be best to be careful when using this function on large numbers, as it can cause overflow or underflow.

## Conclusion

The ceil() function in C++ is found in the <cmath> header file. When called, it accepts a double value as an input and returns the smallest integer value greater than or equal to the input number. This returned value is referred to as the ceiling value of the number passed as a parameter.