0

so I was trying a question from codechef contest and the question was pretty simple. The question isn't necessary for my doubt, actually the doubt is, that when I change the data type of the variable "value" from float to double the answer remains the same but the comparison operators work differently

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        float k1, k2, k3, v; 
        float value; // <= This line
        cin >> k1 >> k2 >> k3 >> v;

        //Task
        float final = 100.0 / (k1 * k2 * k3 * v);

        //Round off to two decimal places
        value = (int)(final * 100 + .5);
        value = (float)value / 100;

        //Check value
        cout << value << endl;

        //Further task
        if (value == 9.58)
        {
            cout << "yes" << endl;
        }
        else
        {
            cout << "no" << endl;
        }
    }
    return 0;
}

Now the answer for the test case

1
1.0 1.0 1.0 10.44

comes out to be

9.58
no

Now if I change the data type of value from float to double

#include<bits/stdc++.h>

using namespace std;

    int main()
    {
        int t;
        cin >> t;
        while (t--)
        {
            float k1, k2, k3, v; 
            double value; // <= This line
            cin >> k1 >> k2 >> k3 >> v;
    
            //Task
            float final = 100.0 / (k1 * k2 * k3 * v);
    
            //Round off to two decimal places
            value = (int)(final * 100 + .5);
            value = (float)value / 100;
    
            //Check value
            cout << value << endl;
    
            //Further task
            if (value == 9.58)
            {
                cout << "yes" << endl;
            }
            else
            {
                cout << "no" << endl;
            }
        }
        return 0;
    }

The output is

9.58
yes

for the same test case.

So, why is it so, why does the comparison operator work differently even with same value with float and double. Does it have something to do with the round off to two decimal part?

  • 2
    Sidenote: you may be interested in reading [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Brian61354270 Apr 17 '21 at 21:00
  • 2
    Note that `9.58` is of type `double` and Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). Don't do fixed precision maths with floating point numbers. – Richard Critten Apr 17 '21 at 21:04
  • 1
    For an example like this, don't make people who are trying to help you type in those values. Initialize `k1`, `k2`, `k3`, and `v` in your code and remove that `while (t--)` loop. – Pete Becker Apr 17 '21 at 21:37
  • duplicate: [strange output in comparison of float with float literal](https://stackoverflow.com/q/1839422), [What is the need of suffix 'f' when define the variable to be float type in c++?](https://stackoverflow.com/q/67138567) – phuclv Apr 18 '21 at 04:03
  • The comparison operator does not work differently. The differences in the results arise in the earlier arithmetic operations. The differences are sufficiently small that you do not see them in the output printed with only two decimal places, but they are present, and the comparison operator correctly reports its results. – Eric Postpischil Apr 19 '21 at 19:54

0 Answers0