首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于输入输出流的很费解的有关问题

2012-06-06 
关于输入输出流的很费解的问题程序如下:#includeiostream.h#includeiomanip.hvoid main(){floatx[ 4 ]

关于输入输出流的很费解的问题
程序如下:
#include<iostream.h>
#include<iomanip.h>
void main()
{ float x[ 4 ] = { 1111.12345, 222.1234, 33.123, 4.12 };
cout << setiosflags( ios::left ) << setfill( '#' );
for ( int i=0; i<4; i++ ) { cout.width(10); cout<<x[ i ]<<" "; }
cout << endl << resetiosflags( ios::left );
for ( i=0; i<4; i++ ) { cout.width(10); cout<<x[ i ]<<" "; }
cout << endl << setiosflags( ios::scientific );
for ( i=0; i<4; i++ ) { cout.width(10); cout<<x[ i ]<<" "; }
cout << endl << resetiosflags( ios::scientific );
cout << setiosflags( ios::fixed );
for ( i=0; i<4; i++ ) { cout.width(10); cout<<x[ i ]<<" "; }
cout <<endl<<setprecision( 4 );
for ( i=0; i<4; i++ ) { cout.width(10); cout<<x[ i ]<<" "; }
cout << endl ; }
输出结果如图:

请高人解释下第四行为什么会输出1111.123413后面的13哪里来的

[解决办法]
浮点数本来就不是绝对精确的保存方法,float的精度最多也就7位,过了当然不精确啦
你把float改成double就没这个问题了
[解决办法]
用10进制小数不能精确表示某些三进制小数0.1(3)=0.33333333333……(10)
同理,用二进制小数也不能精确表示某些10进制小数。

热点排行