dimanche 18 octobre 2020

why the (if) condition is not satisfied, but the function is working? [duplicate]

The code is below. When the variate i*dx is equal to 140, this would not meet the condition, but why cout works? if I changed the 1.4 to 1.3 or other numbers, this weird thing does not happen.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{   
    double dx = 0.01;
    for(size_t i = 0; i < 200; ++i)
    {
        if(  (i*dx > 1.4)   &&   (abs(i*dx - 1.4) > 0)    &&   (i*dx < 1.6)  )
        {
            cout << "i = " << i << endl;
            cout << "i*dx = " << i*dx << endl;
            printf("printf. i*dx = %f \n", i*dx);      
            break;
        }
    }
    return 0;
}

why i*dx = 1.4, the condition is meet??

Aucun commentaire:

Enregistrer un commentaire