Ceil function in C++

Ceil function in C++

3 mins read1.5K Views Comment
Updated on Aug 27, 2024 14:29 IST

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

2023_01_MicrosoftTeams-image-19.jpg

Table of contents

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; // 3
Copy code

Please include the <cmath> header in your program before using this function.

Master Member Functions in C++
New and Delete Operators in C++
Recommended online courses

Best-suited C++ courses for you

Learn C++ with these high-rated online courses

– / –
4 months
4.24 K
6 weeks
– / –
15 days
– / –
– / –
– / –
1 hours
Free
2 hours
Free
1 hours
Free
1 hours

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.0
Copy 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.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio