2

When I execute this code the value of ans1, ans2 is 50002896 and 50005000.
I know there is some issues with ceil function but was not able to figure out the exact cause.

#include <bits/stdc++.h>
using namespace std;
int main()
{
      long long ans1 = 0, ans2 = 0;

      for (long long i = 1; i <= 10000; i++)
      {
            ans1 = ans1 + ceil((float)i / 1);
            ans2 = ans2 + i;
      }
      cout << ans1 << " " << ans2 << endl;
}
11
  • 2
    #include <bits/stdc++.h> -- Include the proper header files, not this one. Commented Nov 20, 2022 at 6:29
  • 3
    By not using the proper standard header files, plus the usage of using namespace std; without knowing what that non-standard header file is pulling, in, we don't know if it is std::ceil or ceil from the C runtime library. That's why using bits...whatever is a bad idea, let alone it isn't standard. Commented Nov 20, 2022 at 6:37
  • 3
    Different results when using std::ceil and ceil Commented Nov 20, 2022 at 6:40
  • 1
    @RohanBari There is no guarantee of compilation if you don't include the proper header files. Commented Nov 20, 2022 at 6:47
  • 2
    Why should I not #include <bits/stdc++.h>?, Why is using namespace std; considered bad practice? Commented Nov 20, 2022 at 10:32

2 Answers 2

2

The source of the problem is not the ceil function, but rather that not all integers can be represented accuratly as floating point values.

Some more info about floating point representation: Wikipedia IEEE 754. And a related post: Which is the first integer that an IEEE 754 float is incapable of representing exactly?.

The following code is a minimal demonstration of the same issue that causes your issue:

float f1 = 100000000;
f1++;
std::cout << std::to_string(f1) << std::endl;

[Wrong] Output (expected: +1):

100000000.000000

One approach would be to use double instead of float.
This will not solve the principle issue, but will make the range of representable integers a lot bigger:

double f1 = 100000000;
f1++;
std::cout << std::to_string(f1) << std::endl;

Output:

100000001.000000

Some side notes:

  1. better to avoid #include <bits/stdc++.h> - see here: Why should I not #include <bits/stdc++.h>?.
  2. better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.
Sign up to request clarification or add additional context in comments.

Comments

1

First, try to use specific headers like #include , in this case, .because #include <bits/stdc++.h> will bring lots of junk.

So the issue is with float not ceil explained below

floating-point values do not represent exact values.

Code:-

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

// Driver Code
int main()
{
    float num1 = 10000.29;
    float num2 = 10000.2;

    // Output should be 0.0900000000
    cout << std::setprecision(15)
        << (num1 - num2);
    return 0;
}

Output :-

0.08984375

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.