C++ 多种取整函数的使用和区别: ceil() floor() round() trunc() rint() nearbyint()

取整函数:

ceil()     右向取整:数轴上右边最靠近的整数,向大的方向取值;ceil “天花板”
floor()   左向取整:数轴上左边最靠近的整数,向小的方向取值;floor “地板”
round() 接近取整:不管正负,即四舍五入后留下整数部分  round(x) = floor(x+0.5)
trunc()  零向取整:不管正负,直接舍掉小数部分只留下整数部分,向原点0的方向取值
rint() nearbyint():四种方法任选其一,由 fesetround() 配合设置,见例程二说明

例程一:ceil() floor() round() trunc()用法

#include <iostream>
#include <cmath>
 
using namespace std;
 
int main()
{
	double num[4] = {13.6, 3.23, -13.6, -3.23};
	
	for (auto n:num){
		cout<< "    ceil(x) floor(x) round(x) trunc(x):x = " << n << endl;
		cout<< "\t" << ceil(n) << "\t" << floor(n) << "\t"
			<< round(n) << "\t" << trunc(n) << endl << endl;
	}
    
    return 0;
}
 
/*
    ceil(x) floor(x) round(x) trunc(x):x = 13.6
        14      13      14      13

    ceil(x) floor(x) round(x) trunc(x):x = 3.23
        4       3       3       3

    ceil(x) floor(x) round(x) trunc(x):x = -13.6
        -13     -14     -14     -13

    ceil(x) floor(x) round(x) trunc(x):x = -3.23
        -3      -4      -3      -3

--------------------------------
Process exited after 0.6186 seconds with return value 0
请按任意键继续. . .
*/

例程二:fegetround() fesetround()用法

函数原型:
int fesetround(int mode); 设置取整模式
int fegetround(void); 获取当前取整模式

mode 值:
Round to nearest value   FE_TONEAREST     == 0
Round downward            FE_DOWNWARD     == 1024
Round upward                 FE_UPWARD           == 2048
Round towards zero        FE_TOWARDZERO  == 3072
default value:                FE_TONEAREST      == 0

#include <iostream>
#include <cmath>
#include <cfenv>
using namespace std;
 
int getround(void)
{
	switch (fegetround()) { 
	case FE_TONEAREST:
		cout << "\nRound to nearest value \t FE_TONEAREST\t== "; 
		break; //round()
	case FE_DOWNWARD:
		cout << "\nRound downward \t\t FE_DOWNWARD\t== "; 
		break; //ceil()
	case FE_UPWARD:
		cout << "\nRound upward \t\t FE_UPWARD\t== "; 
		break; //floor()
	case FE_TOWARDZERO:
		cout << "\nRound towards zero \t FE_TOWARDZERO\t== "; 
		break; //trunc()
	default: 
		cout << "unknown";
	}
	
	return fegetround();
}
 
int main(void)
{
 	for(int i=0;i<4;i++){
		fesetround(i*1024);
		cout << getround();
	}
	
	cout << "\n\nDaufult: TONEAREST == 0" << endl; 
	
	return 0; 
}
 
/*

Round to nearest value   FE_TONEAREST   == 0
Round downward           FE_DOWNWARD    == 1024
Round upward             FE_UPWARD      == 2048
Round towards zero       FE_TOWARDZERO  == 3072

Daufult: TONEAREST == 0

--------------------------------
Process exited after 0.8902 seconds with return value 0
请按任意键继续. . .
*/

例程三:nearbyint() rint()用法

#include <iostream>
#include <array>
#include <cmath>
#include <cfenv>
using namespace std;

int main(void)
{
	array<double,4> num={13.6, 3.23, -13.6, -3.23};
	array<string,6> str={"nearbyint(x), x = \n", "\nmode = ",
						"FE_TONEAREST(default) = ",
						"FE_DOWNWARD = ",
						"FE_UPWARD = ",
						"FE_TOWARDZERO = "	};
	
	cout<<str.at(0);
	for (auto n:num) cout<<"\t"<<n; cout<<endl;
	for (int i=0;i<4;i++){
		fesetround(i*1024);
		cout<<str.at(1)<<str.at(i+2)<<fegetround()<<endl;
		for (auto n:num) cout<<"\t"<<nearbyint(n); cout<<endl;
	}

	return 0;
}

/*
nearbyint(x), x =
        13.6    3.23    -13.6   -3.23

mode = FE_TONEAREST(default) = 0
        14      3       -14     -3

mode = FE_DOWNWARD = 1024
        13      3       -14     -4

mode = FE_UPWARD = 2048
        14      4       -13     -3

mode = FE_TOWARDZERO = 3072
        13      3       -13     -3

--------------------------------
Process exited after 0.6 seconds with return value 0
请按任意键继续. . .
*/

猜你喜欢

转载自blog.csdn.net/boysoft2002/article/details/114218122