C C 取整函式ceil ,floor

2021-07-13 05:31:43 字數 2953 閱讀 4822

2011-08-08 21:31:46

|  分類:

c++|舉報|

字型大小訂閱

我的**書  |

c/c++ 取整函式ceil(),floor()

#include double floor(doublex);

float floorf(floatx); 

long double floorl(long double

x);

double floor(double x);

double ceil(double x);

使用floor函式。floor(x)返回的是小於或等於x的最大整數。

如:     floor(10.5) == 10    floor(-10.5) == -11

使用ceil函式。ceil(x)返回的是大於x的最小整數。

如:     ceil(10.5) == 11    ceil(-10.5) ==-10

floor()是向負無窮大捨入,floor(-10.5) == -11;

ceil()是向正無窮大捨入,ceil(-10.5) == -10

不得不說點:/ % 四捨五入 向上取整(ceil()) 向下取整(floor)

1. /

//test "/"

cout << "test \"/\"!" << endl;

cout << "7   / 2   = " << 7/2 << endl;      //3

cout << "7   / 2.0 = " << 7/2.0 << endl;   

cout << "7.0 / 2   = " << 7.0/2 << endl;   

cout << "7.0 / 2.0 = " << 7.0/2.0 << endl;

cout << "7   / 3   = " << 7/3 << endl;      //2

cout << endl;

2. %

//test "%"

cout << "test \"%\"!" << endl;

cout << "9   % 3   = " << 9%3 << endl;      //0

cout << "9   % 4   = " << 9%4 << endl;      //1

//cout << "9.0 % 3   = " << 9.0%3 << endl;

//cout << "9   % 3.0 = " << 9%3.0 << endl;

cout << endl;

3. 四捨五入

//test round()

cout << "test \"四捨五入\"!" << endl;

double drounda = 1.4;

double droundb = 1.6;

double droundlowa = -1.4;

double droundlowb = -1.6;

double droundlowc = 0.0;

cout << drounda << " = " << roundex(drounda) << endl;         //1

cout << droundb << " = " << roundex(droundb) << endl;         //2

cout << droundlowa << " = " << roundex(droundlowa) << endl;   //-1

cout << droundlowb << " = " << roundex(droundlowb) << endl;   //-2

cout << droundlowc << " = " << roundex(droundlowc) << endl;   //0

cout << endl;

double roundex(const double& dinput)

else}

4. ceil() 向上取整

//test ceil() 向上取整

cout << "test ceil() 向上取整!" << endl; 

cout << "ceil 1.2 = " << ceil(1.2) << endl;      //2

cout << "ceil 1.8 = " << ceil(1.8) << endl;      //2

cout << "ceil -1.2 = " << ceil(-1.2) << endl;    //-1

cout << "ceil -1.8 = " << ceil(-1.8) << endl;    //-1

cout << "ceil 0.0 = " << ceil(0.0) << endl;      //0

cout << endl;

5. floor() 向下取整

//test floor() 向下取整

cout << "test floor() 向下取整!" << endl;

cout << "floor 1.2 = " << floor(1.2) << endl;    //1

cout << "floor 1.8 = " << floor(1.8) << endl;    //1

cout << "floor -1.2 = " << floor(-1.2) << endl; //-2

cout << "floor -1.8 = " << floor(-1.8) << endl; //-2

cout << "floor 0.0 = " << floor(0.0) << endl;    //0

cout << endl;

C C 取整函式ceil ,floor

include double floor double x float floorf floatx long double floorl long doublex double floor double x double ceil double x 使用floor函式。floor x 返回的是小於或...

C C 取整函式ceil ,floor

使用floor函式。floor x 返回的是小於或等於x的最大整數。如 floor 10.5 10 floor 10.5 11 使用ceil函式。ceil x 返回的是大於x的最小整數。如 ceil 10.5 11 ceil 10.5 10 floor 是向負無窮大捨入,floor 10.5 11 ...

有關取整的函式ceil,floor

標頭檔案 include floor floor x 返回的是小於或等於x的最大整數。如 floor 10.5 10 floor 10.5 11 ceil ceil x 返回的是大於x的最小整數。如 ceil 10.5 11 ceil 10.5 10 floor 是向負無窮大捨入,floor 10....