Home

Arithmetic: The Ceiling of a Number

 

Introduction

Consider a floating number such as 12.155. As you can see, this number is between integer 12 and integer 13

In the same way, consider a number such as 24.06. As this number is negative, it is between 24 and 25, with 24 being greater.

In arithmetic, the ceiling of a number is the closest integer that is greater or higher than the number considered. In the first case, the ceiling of 12.155 is 13 because 13 is the closest integer greater than or equal to 12.155. The ceiling of 24.06 is 24.

 

The ceil() Function

In C++, the function used to obtain the ceiling of a number uses the following syntax:

double ceil(double Value);

The function takes one argument, which is the floating number to be evaluated, and the function returns a double-precision number that is the integer that is greater than or equal to Value. Here is an example:

//---------------------------------------------------------------------------
#include <iostream>
using namespace std;

#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsusedint main(int argc, char* argv[])
{
	double Value1 = 155.55; double Value2 = -24.06;
	
	cout << "The ceiling of " << Value1 << " is " << ceil(Value1) << endl;
	cout << "The ceiling of " << Value2 << " is " << ceil(Value2) << endl;
	
	cout << "\nPress any key to continue...";
	getchar();
	return 0;
}
//---------------------------------------------------------------------------

This would produce:

The ceiling of -24.06 is -24
Press any key to continue...

The Ceil() Function

In C++ Builder, the function used to get the ceiling of a number is:

int __fastcall Ceil(Extended Value);

The Ceil() function takes an argument that represents a long double value. The function returns the greater or equal integer of Value. To use the Ceil() function, include the math.hpp header to your program. Here is an example:

//---------------------------------------------------------------------------
#include <iostream.h>
#include <math.hpp>

#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused

int main(int argc, char* argv[])
{
	Extended Value1 = 312.44;
	Extended Value2 = -4002.35;
	
	cout << "The ceiling of " << Value1 << " is" << Ceil(Value1) << endl;
	cout << "The ceiling of " << Value2 << " is" << Ceil(Value2);
	
	cout << "\n\nPress any key to continue...";
	getchar();
	return 0;
}
//---------------------------------------------------------------------------

This would produce:

The ceiling of 312.44 is 313
The ceiling of -4002.35 is -4002

Press any key to continue...

 

 
Home Copyright 2004-2007 FunctionX, Inc.