Ceil function in C++
In this article, Ceil function will be explored with different programming examples. Limitations of ceiling functions are also covered.
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);
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
Please include the <cmath> header in your program before using this function.
Also explore: Default Arguments in C++
Must read: Understanding Operators in C++
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
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);
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
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++:
- 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.
- 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.
- 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.
- 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.
- 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;
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;}
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;}
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.
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