Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

C++基础编程问题

Posted by Acaini at 2009-10-31 19:23:54
double x[41];
 x[0]=-1;
 for(int i=1;i<41;i++)
 {
  x[i]=x[i-1]+0.05;
 }
 cout<<"加法情况:"<<endl;
 cout<<"x[20]="<<x[20];
 cout<<endl;
 cout<<"x[19]="<<x[19]<<endl;
 cout<<"x[20]="<<(x[19]+0.05)<<endl;
//加法,x[20]为无穷小量,如3.19189e-016等,但理论上x[20]应该为0

 for(int j=0;j<41;j++)
 {
  x[j]=x[0]+0.05*j;
 }
 cout<<"乘法情况:"<<endl;
 cout<<"x[20]="<<x[20];
 cout<<endl;
 cout<<"x[19]="<<x[19]<<endl;
 cout<<"x[20]="<<(-1+0.05*20)<<endl;//乘法,x[20]为0

我的运行结果为:
加法情况:
x[20]=3.19189e-016
x[19]=-0.05
x[20]=3.19189e-016
乘法情况:
x[20]=0
x[19]=-0.05
x[20]=0
Press any key to continue
为什么前一种算法得到的x[20]是个极小量,而不是0呢?
请各位指点迷津,谢谢

加法情况:
x[20]=3.19189e-16
x[19]=-0.05
x[20]=3.19189e-16
乘法情况:
x[20]=5.55112e-17
x[19]=-0.05
x[20]=0  

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator