C++ == 运算符不工作

标签 c++ floating-point comparison-operators

我在 C++ 中应用 Regula Falsi 方法,但问题是当 F(x3) 变为 0 时 == 运算符,然后 if (fabs( f(x3))==0 应该停止并出现脱离循环但它并没有停止为什么为什么为什么...... 像下面第 12 次迭代后的输出 f(x3)=0 但 if(fabs(f(x3)==0)) 不运行。循环不停止它不应该去第 13 次迭代

float f(float x)
{

float  f_x;
f_x= pow(x,3)+(3*x)-5;
return f_x;
 }
int main(int argc, char** argv) 
{

float  a,b,tol,x3;

int itr,n;

cout << "enter the iterations";
cin >> itr;
cout << "enter the interval a";
cin >> a;
cout <<"enter the interval b";
cin >> b;
cout << "enter the toleration";
cin >> tol;

cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
cout.precision(5);

//cout<<"fa="<<f(a)<<"fb"<<f(b);
cout<<"n\t\ta\t\tb\t\tx3\t\tf(a)\t\tf(b)\t\tf(x3)" <<endl;
if (f(a)*f(b)<0 && a<b)
{
    for (n=0;n<itr;n++)
    {
        x3=a-((b-a)*f(a))/(f(b)-f(a));
       cout << "xx"<<fabs(f(x3));

       if (fabs( f(x3))==0)
       {
             cout << "Solution"<<fabs(f(x3));
                break;
       }
        else
         {
            cout<<n+1 <<"\t\t"<<a <<"\t\t"<<b <<"\t\t"<<x3<<"\t\t"<<f(a) 
            <<"\t"<<f(b)<<"\t\t"<<f(x3) <<endl; 
             if(f(x3)*f(a)<0)
                     b=x3;
              else
                    if(f(x3)*f(b)<0)
                     a=x3;
         }
    }
}
else
    cout<< "No Solution Exist";

return 0;

输出

输入迭代13

输入区间a1

输入区间b2

输入公差1

**n   a          b             x3          f(a)           f(b)          f(x3)**


 1  1.00000    2.00000        1.10000     -1.00000        9.00000      -0.36900

 2  1.10000    2.00000        1.13545     -0.36900        9.00000      -0.12980

 3  1.13545    2.00000        1.14774     -0.12980        9.00000      -0.04487

 4  1.14774    2.00000        1.15197     -0.04487        9.00000      -0.01542

 5  1.15197    2.00000        1.15342     -0.01542        9.00000      -0.00529

 6  1.15342    2.00000        1.15391     -0.00529        9.00000      -0.00181

 7  1.15391    2.00000        1.15408     -0.00181        9.00000      -0.00062

 8  1.15408    2.00000        1.15414     -0.00062        9.00000      -0.00021

 9  1.15414    2.00000        1.15416     -0.00021        9.00000      -0.00007

 10 1.15416    2.00000         1.15417    -0.00007        9.00000      -0.00003

 11 1.15417    2.00000         1.15417    -0.00003        9.00000      -0.00001

 12 1.15417    2.00000         1.15417    -0.00001        9.00000       0.00000

 13 1.15417    2.00000         1.15417    -0.00000        9.00000       0.00000

最佳答案

浮点运算存在精度误差,因此在大多数情况下最好不要直接比较浮点值,使用 epsilon:

bool float_equal(float a , float b)
{
    return std::abs(a-b) < 0.001;
}

请注意,在您的情况下(与零相比)精度更为重要:浮点实现旨在提供围绕零的更高精度。因此,例如,您可以拥有像 0,000000000001 或 0,0000000000000000001 这样的数字,它们不被视为等于零。

查看此线程以了解更多注意事项:What is the most effective way for float and double comparison?

此外,请注意 std::setprecision是一个操纵器,它改变输出(打印)操作的精度,而不是浮点“系统”的精度。

关于C++ == 运算符不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18932336/

相关文章:

c++ - 链接 CEF3 的问题

c++ - 为什么 boost ublas 中的 compress_matrix 会为非零元素分配更多的内存?

c - 为什么我需要 17 位有效数字(而不是 16 位)来表示 double ?

java - 如何左填充 float 的字符串表示形式?

c++ - 比较C++中的结构

objective-c - 在 Objective C 中,if (object == nil) 和 if (nil == object) 之间有区别吗?

c++ - 父类(super class)运算符重载何时以及为何隐藏在子类中?

c++ - 为什么需要在列表拼接功能cpp中使用列表参数

objective-c - 获取 float 小数点后的值

linux - 大于/小于中断 bash 脚本的多个 -a