c++ - 0.2的增量不遵守循环条件?
问题描述
我们有关于循环的作业,它显示两个计算并通过输入的 i 增加一个变量。这些代码运行并且一切都很好,直到我输入a=1, b=3 i=0.2
,发生的是3
即使 while 条件是它也不会达到a<=b
。唯一有效的时候a=1, b=2, and i=0.2
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double inputI(double i){
for(i=0;i<=0;cin>>i){
cout<<"i must be greater than 0"<<endl;
cout<<"Input i: ";
}
return i;
}
double compX(double s, double b){
double x;
x = s/cbrt(b)+2*pow(s,2);
return x;
}
double compY(double s, double x){
double y;
y = (x+s/x)+3*s;
return y;
}
void display(double x, double y,double a){
cout<<fixed<<setprecision(2)<<a<<"\t";
cout<<fixed<<setprecision(4);
cout<<x<<" "<<y<<endl;
}
int main(){
double x,y,a,b,i;
cout<<"Input a: ";
cin>>a;
cout<<"Input b: ";
cin>>b;
i = inputI(i);
//is there something wrong???
do{
x = compX(a,b);
y = compY(a,x);
display(x,y,a);
a+=i;
}while(a<=b);
}
解决方案
推荐阅读
- webpack - 无法使用 nextjs 项目解析兄弟文件夹中的别名
- javascript - 导入电子时 fs.existsSync 不是函数
- javascript - 无法解析方括号
- ibeacon - Altbeacon 停止在 Android10 上工作,并且根本没有调用 didEnterRegion
- python-3.x - Python3 插入 MariaDB 失败
- javascript - 在表格中以角度显示单个对象的数据时出现问题
- azure-sql-database - adf v2 链接服务连接池
- alloy - 是否可以在签名事实中使用无点表达式?
- docker - 在 giblab-ci.yaml 中,作业上的“服务”是否会在给定相同“别名”的情况下生成单独的实例?
- python - 将代码 kivy.py 转录为 kivy.py - .kv (TextCheckBox)