无法显示 for 循环后的最后一个数字
Can't display the last number after the for loop
如何显示for
循环后的最后一个数字?
该程序用于计算欧拉法
- 对于
xO
,我输入0 - 为
yO
输入1 -
send
I输入1
我试图在x=1.0
时获得y
的最后一个值,但现在我无法完成。
float num,xO,yO,xend,x,y,delx,func,exact,diff;
char next ='y';
while( next =='y') {
cout << "Please enter the method of choice;press 1 for Euler's Method,"<< endl;
cout << "press 2 for Heun's Method:";
cout <<setiosflags(ios::showpoint);
cin >> num;
if ( num==1 ) {
cout <<"Solution for the equation dy/dx = f(x) by Euler's Method"<< endl;
cout <<"Enter the initial condition xO :";
cin >> xO;
cout <<"Enter the initial condition yO :";
cin >> yO;
cout <<"Enter the final value of x :";
cin >> xend;
cout <<"Enter the step size delta_x :";
cin >> delx;
y=yO;
for ( x=xO; x <=1.00000016; x+=delx ) {
cout<<setw(10)<<setiosflags(ios::left)<<setprecision(2)<<x<<setw(10)<<setiosflags(ios::left)<<setprecision(6)<<y<<endl;
func=4*x*y;
func=sqrt(func);
y=y+((delx)*(func));*//the last y is 2.54052
}
cout<<"nThe solution y10 at x=1 is "<<y<<endl;
}
我想显示的数字是2.54052,但现在它是2.85930。我该如何纠正这个问题?
在循环的条件部分使用整数而不是浮点数。
例如,用"int x <= 100000016"代替"float x <=1.00000016",这样不会丢失精度。
然后,将循环的花括号内的值除以1e8,并显式地将它们转换为float类型。
float func = static_cast<float>(4 * x * y) / 100000000;
相关文章:
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 我必须更改我的数字最后一个数字和第一个数字,但不要使用仅带有整数或循环的函数.例如从 12345 到 52341
- 带有文件结束函数的 while 循环重复输出文件中的最后一个数字两次
- 递归推动POP POSTFIX计算器无法正确执行操作,只需将最后一个数字作为结果
- 代码问题.使用矢量删除相同的数字,除了它的最后一个入口
- C++:对数组进行排序,最后一个数字重复两次
- C++ - 查找数组中第一个和最后一个负数之间的数字
- 我不想要最后一个数字/值后面的逗号,如何删除它(c ++,循环)
- 如何删除C++中浮点数的最后一个有效数字/尾数位
- 逐行计算文件:EOF 重复最后一个数字(不能使用中断)
- 如何确定浮点数中的最后一个非零十进制数字
- 为什么我可以索引到数字超出范围的三维数组中,但仍然可以访问数组中的最后一个数字
- 无法显示 for 循环后的最后一个数字
- 不能算出这个按数字顺序打印数字的c++小程序的最后一个
- 查找升序列表中降序的第一个和最后一个数字
- 冒泡排序不排序最后一个数字与此算法
- 排序程序仅显示第一个和最后一个数字
- 如何在读取文本文件时跳到每行最后一个数字之后的下一行
- 为什么 C++ 中的编译器不增加循环中最后一个数字的值?
- 把最后一个数字切掉,做成双份