立此存照(11)[C++]cfloat標頭檔案即浮點數的最大值與最小值

urgemule發表於2014-10-19

C++標準標頭檔案cfloat存放著C++基本型別float,double,long double等基本型別的最大值與最小值,以及其他資訊

1.編譯環境:VS2008

#include <iostream>
#include <iomanip>//needs setw
#include <cfloat>
using std::cout;
using std::endl;
using std::setw;
using std::ios;

int main()
{
	const int COUT_WIDTH = 25;
	cout<<setiosflags(ios::left);
	//float
	cout<<setw(COUT_WIDTH)<<"sizeof(float):"<<sizeof(float)<<endl;
	cout<<setw(COUT_WIDTH)<<"float Min:"<<FLT_MIN<<endl;
	cout<<setw(COUT_WIDTH)<<"float Max:"<<FLT_MAX<<endl<<endl;
	
	//double
	cout<<setw(COUT_WIDTH)<<"sizeof(double):"<<sizeof(double)<<endl;
	cout<<setw(COUT_WIDTH)<<"double Min:"<<DBL_MIN<<endl;
	cout<<setw(COUT_WIDTH)<<"double Max:"<<DBL_MAX<<endl<<endl;

	//long double
	cout<<setw(COUT_WIDTH)<<"sizeof(long double):"<<sizeof(long double)<<endl;
	cout<<setw(COUT_WIDTH)<<"long double Min:"<<LDBL_MIN<<endl;
	cout<<setw(COUT_WIDTH)<<"long double Max:"<<LDBL_MAX<<endl;
	

return 0;
}

執行結果:

編譯環境:g++


2.編譯環境:VC++6.0

#include <iostream>
#include <iomanip>//needs setw
#include <cfloat>
using std::cout;
using std::endl;
using std::setw;
using std::ios;

int main()
{
	const int COUT_WIDTH = 25;
//	cout<<setiosflags(ios::left);
	cout.setf(ios::left);
	//float
	cout<<setw(COUT_WIDTH)<<"sizeof(float):"<<sizeof(float)<<endl;
	cout<<setw(COUT_WIDTH)<<"float Min:"<<FLT_MIN<<endl;
	cout<<setw(COUT_WIDTH)<<"float Max:"<<FLT_MAX<<endl<<endl;
	
	//double
	cout<<setw(COUT_WIDTH)<<"sizeof(double):"<<sizeof(double)<<endl;
	cout<<setw(COUT_WIDTH)<<"double Min:"<<DBL_MIN<<endl;
	cout<<setw(COUT_WIDTH)<<"double Max:"<<DBL_MAX<<endl<<endl;

	//long double
	cout<<setw(COUT_WIDTH)<<"sizeof(long double):"<<sizeof(long double)<<endl;
	cout<<setw(COUT_WIDTH)<<"long double Min:"<<LDBL_MIN<<endl;
	cout<<setw(COUT_WIDTH)<<"long double Max:"<<LDBL_MAX<<endl;
	

return 0;
}
執行結果:





相關文章