0

I had the following code :

#include <bits/stdc++.h>
using namespace std;

int main()
{
        double a;
        cin >> a;
        cout << floor(a*100.00) << endl;
        return 0;
}

So when I give input 1.15 the expected answer should have been 115, input of 1.16 gives 115. Why is this happening ? Link to ideone https://ideone.com/

Apoorv Jain
  • 113
  • 9
  • 3
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Algirdas Preidžius May 25 '20 at 11:44
  • 1
    The closest IEEE 754 64 bit binary float to 1.16 is 1.1599999999999999200639422269887290894985198974609375. Multiplying by 100 rounds to 115.9999999999999857891452847979962825775146484375. Applying floor to that gives 115. Consider adding a small rounding error adjustment so that number just under an integer are treated as being over it. – Patricia Shanahan May 25 '20 at 11:48
  • So what should be the typical range of the rounding error adjustment ? @PatriciaShanahan – Apoorv Jain May 25 '20 at 12:11
  • How big a difference is likely to be significant in your program. You could try something like 1e-12. – Patricia Shanahan May 25 '20 at 21:42

0 Answers0