关于输入输出流的很费解的问题
程序如下:
#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进制小数。