C / C + + rounding function ceil(), floor ()

#include <math.h>
 
double floor(double 
x
);
 

float floorf(float x); 
long double floorl(long double x);

double floor(double x);
double ceil(double x);

USES the floor function. Floor (x) returns the largest integer less than or equal to x.
such as: floor ten floor (10.5) = = = = (10.5) – 11

USES the ceil function. Ceil (x) returns the smallest integer greater than x.
for example: ceil(10.5) == 11 ceil(-10.5) ==-10

floor() is rounded to negative infinity, floor(-10.5) == -11;
ceil() is rounded to positive infinity, and ceil(-10.5) == -10

(ceil()) (floor) (floor)

1. /
//Test “/”
cout & lt; < “Test \”/\”!” < < endl;
cout & lt; < “7/2 =” < < 7/2 < < endl;// 3
cout & lt; < “7/2.0 =” < < 7/2.0 & lt; < endl;// 3.5
cout & lt; < “7.0/2 =” < < 7.0/2 & lt; < endl;// 3.5
cout & lt; < “7.0/2.0 =” < < 7.0/2.0 & lt; < endl;// 3.5
cout & lt; < “7/3 =” < < 7/3 < < endl;// 2
cout & lt; < endl;
2. %
//Test “%”
cout < < “Test \”%\”!” < < endl;
cout & lt; < 9%3 = “< < 9%3 < < endl;// 0
cout & lt; < “9%4 = “< < 9%4 < < endl; 1
//// cout & lt; < “9.0% 3 =” < < 9.0% 3 & lt; < endl;
// cout & lt; < 9%3.0 = “< < 9% 3.0 & lt; < endl;
cout & lt; < endl;
3. Round
//Test round()
cout < < “Test \” Round \”!” < < endl;
double dRoundA = 1.4;
double dRoundB = 1.6;
double dRoundLowA = -1.4;
double dRoundLowB = -1.6;
double dRoundLowC = 0.0;
cout & lt; < dRoundA < < ” = ” < < RoundEx(dRoundA) < < endl;// 1
cout & lt; < dRoundB < < ” = ” < < RoundEx(dRoundB) < < endl;// 2
cout & lt; < dRoundLowA < < ” = ” < < RoundEx(dRoundLowA) < < endl;// – 1
cout & lt; < dRoundLowB < < ” = ” < < RoundEx(dRoundLowB) < < endl;// – 2
cout & lt; < dRoundLowC < < ” = ” < < RoundEx(dRoundLowC) < < endl;// 0
cout & lt; < endl;
double RoundEx(const double&
{
double dIn = dInput;
if (dInput & gt; = 0.0)//???
{
return int(dIn + 0.5);

} else
{
return int (dIn 0.5);

}}
4. The ceil () take up the whole
// Test ceil () take up the whole
cout & lt; < “Test ceil() round up!” < < endl;
cout & lt; < “Ceil 1.2 =” < < The ceil (1.2) & lt; < endl;// 2
cout & lt; < “Ceil 1.8 =” < < The ceil (1.8) & lt; < endl;// 2
cout & lt; < “Ceil-1.2 =” < < The ceil (1.2) & lt; < endl;// – 1
cout & lt; < “Eil-1.8 =” < < The ceil (1.8) & lt; < endl;// – 1
cout & lt; < “Ceil 0.0 =” < < The ceil (0.0) & lt; < endl;// 0
cout & lt; < endl;
5. Floor () the whole
/down/Test floor () the whole down
cout & lt; < “Test floor() down full!” < < endl;
cout & lt; < “Floor 1.2 =” & LT; < Floor (1.2) & lt; < endl;// 1
cout & lt; < “Floor 1.8 =” < < Floor (1.8) & lt; < endl;// 1
cout & lt; < “Floor -1.2 =” < < Floor (1.2) & lt; < endl;// – 2
cout & lt; < “Floor -1.8 =” < < Floor (1.8) & lt; < endl;// – 2
cout & lt; < “Floor 0.0 =” < < Floor (0.0) & lt; < endl;// 0
cout & lt; < endl;

Read More: