ceil in C/C++: rounding up
👋 Hey there! In this article, we’ll dive into the ceil function, which lets us round a number up in C/C++. First, we’ll see how to use it with an example and then we’ll try implementing it ourselves. We’ll also look into the differences between ceil, ceill, and ceilf. At the end, you’ll find some exercises for further practice.
How to round a number up in C/C++
To round a number up in C/C++, we can use the built-in ceil function. This function is available after including the <math.h> file. For C++, you can include <cmath>. Here’s what the function prototype looks like:
double ceil (double x);- It takes a single argument, the number you wish to round.
- The function then returns the rounded result.
Let’s see it in action:
#include <cmath>
#include <iostream>
using namespace std;
int main() {
cout << "ceil(3) = " << ceil(3) << endl;
cout << "ceil(12.34) = " << ceil(12.34) << endl;
cout << "ceil(1.0000001) = " << ceil(1.0000001) << endl;
cout << "ceil(0) = " << ceil(0) << endl;
cout << "ceil(-0.25) = " << ceil(-0.25) << endl;
cout << "ceil(-2) = " << ceil(-2) << endl;
return 0;
}Program output:
ceil(3) = 3
ceil(12.34) = 13
ceil(1.0000001) = 2
ceil(0) = 0
ceil(-0.25) = 0
ceil(-2) = -2What will be displayed on the screen?
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a = -12.34;
cout << ceil(a) << endl;
return 0;
}Implementing ceil ourselves
Let’s give implementing the ceil function a shot:
#include <iostream>
using namespace std;
double my_ceil(double x) {
// Convert the number to an integer (this truncates the fractional part)
int int_part = (int)x;
// If the number is already whole, return it
if (x == int_part) {
return int_part;
}
// For negative numbers, just return them as integers
// This is because when rounding up a negative number, we head to the nearest
// larger negative number (e.g., ceil(-2.3) = -2),
// equivalent to a simple integer conversion
else if (x < 0) {
return int_part;
}
// For positive non-whole numbers, add 1 to the integer part
else {
return int_part + 1;
}
}
int main() {
cout << "my_ceil(3) = " << my_ceil(3) << endl;
cout << "my_ceil(12.34) = " << my_ceil(12.34) << endl;
cout << "my_ceil(1.0000001) = " << my_ceil(1.0000001) << endl;
cout << "my_ceil(0) = " << my_ceil(0) << endl;
cout << "my_ceil(-0.25) = " << my_ceil(-0.25) << endl;
cout << "my_ceil(-2) = " << my_ceil(-2) << endl;
return 0;
}Program output:
my_ceil(3) = 3
my_ceil(12.34) = 13
my_ceil(1.0000001) = 2
my_ceil(0) = 0
my_ceil(-0.25) = 0
my_ceil(-2) = -2Functions ceill and ceilf
Just like ceil, both ceill and ceilf round up the number passed to them. The difference is that they work with different data types: long double for ceill and float for ceilf. Let’s see an example:
#include <cmath>
#include <iostream>
using namespace std;
int main() {
cout << "ceill(12.34) = " << ceill(12.34) << endl;
cout << "ceil (12.34) = " << ceil(12.34) << endl;
cout << "ceilf(12.34) = " << ceilf(12.34) << endl;
return 0;
}Output:
ceill(12.34) = 13
ceil (12.34) = 13
ceilf(12.34) = 13As you can see, they all return the same value but of different types.
Exercises
-
Using
ceil:- Write a C++ program that prompts the user for a floating-point number.
- Apply the
ceilfunction to it and display the result. - The program should also display the original user input.
-
Implementing your version of
ceil:- Using the example provided in this article, write your version of the
ceilfunction. Name your functionmy_ceiland have it accept adouble. - Test it with various numbers and compare the results to the built-in
ceilfunction.
- Using the example provided in this article, write your version of the
-
Comparing
ceil,ceill, andceilf:- Write a program that demonstrates the differences between
ceil,ceill, andceilf. - Your program should call all three functions and display their results.
- Write a program that demonstrates the differences between
Discussion