IOS之數學函式

2021-06-16 14:52:23 字數 2753 閱讀 5842

#include

double floor(double x);

float floorf(floatx); 

long double floorl(long doublex);

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;

分享到:

iOS 常用數學函式

1 三角函式 double sin double 正弦 double cos double 余弦 double tan double 正切 2 反三角函式 double asin double 結果介於 pi 2,pi 2 double acos double 結果介於 0,pi double at...

iOS常用數學函式

1 三角函式 double sin double 正弦 double cos double 余弦 double tan double 正切 2 反三角函式 double asin double 結果介於 pi 2,pi 2 double acos double 結果介於 0,pi double at...

ios常用數學函式

1 三角函式 double sin double 正弦 double cos double 余弦 double tan double 正切 2 反三角函式 double asin double 結果介於 pi 2,pi 2 double acos double 結果介於 0,pi double at...