1

I am trying to do the problem 200B on codeforces. I tested my code, and the output was all right. But when I uploaded it to the online judge system, I failed on the very first test case. It said my output was -0.000000000000, instead of 66.666666666667.
But I have compiled and run on Visual Studio C++ 2010, MacOS clang 13.0.0, and Linux GCC 6.3.0, the outputs were all the same as mine, 66.666666666667. I am very curious and want to figure out in what situation the output could be -0.000000000000.
On my computer,
Input:
3
50 50 100
Output:
66.666666666667
On the online judge system,
Input:
3
50 50 100
Participant's output
-0.000000000000
Jury's answer
66.666666666667
Checker comment
wrong answer 1st numbers differ - expected: '66.66667', found: '-0.00000', error = '1.00000'

#include <stdio.h>

int main(void)
{
    int n;
    double sumOrange = 0;
    double sumDrink = 0;
    scanf ("%d", &n);
    
    while (n-- > 0) {
        int m;
        scanf("%d", &m);

        sumOrange += m / 100.0;
        sumDrink++;
    }

    printf("%.12lf\n", (sumOrange / sumDrink) * 100.0);
    return 0;
}

I just don't understand why my output could be -0.000000000000. Please help, thanks.

Update: Tested on different versions of GCC (4.9, 5.1, 6.3), the wrong output does not appear. Guess the cause might lie in the specific implementation of printf.

Wu Xiliang
  • 316
  • 3
  • 10
  • 1
    By the way use this format string %.12f instead of this %.12lf – Vlad from Moscow Dec 23 '21 at 06:59
  • 1
    Leon, Aside: could remove the `/ 100.0` and `* 100.0`. They only inject minute errors in the calculations. – chux - Reinstate Monica Dec 23 '21 at 08:33
  • @chux-ReinstateMonica, yeah, you are right. I was just confused about the output. So I tried every possible way to find the cause, but failed. – Wu Xiliang Dec 23 '21 at 09:12
  • @VladfromMoscow, Thanks for you comment. I have just updated my code and passed the tests, but still I want to know when, in what situation, or by what compiler, the compiled executable could produce this error. I just have tried Windows compiler VS 2010, Linux GCC and MacOS XCode (clang), the output is all the same, no problem. – Wu Xiliang Dec 23 '21 at 09:16
  • For `sumDrink++'` See: [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) and [Why Are Floating Point Numbers Inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) and [Floating point comparison `a != 0.7`](https://stackoverflow.com/questions/6883306/floating-point-comparison-a-0-7). As an example, with `float f = 1.1;` is actually `1.100000023842`. After `f++;`, you have `2.099999904633` -- do be aware of the floating-point math aspect of post-increment. – David C. Rankin Dec 24 '21 at 03:59

1 Answers1

1

The problem is because printf function in GNU gcc C11 does not support %.12lf format. It should be changed to %.12f For more information, you can read the article below:

Correct format specifier for double in printf

konata39
  • 196
  • 6
  • Thanks. I feel so happy to know the cause of the problem, but what I really want is to get the wrong output if possible. – Wu Xiliang Dec 23 '21 at 09:18
  • Maybe it is depends on GCC version. The GCC version in codeforces is 5.1, and your VS C++ version is 13.0(clang), so it can only represent wrong answer in lower GCC. Maybe the newer version of clang is fix `%lf` problem, and it will auto convert %`lf` to `%f`. Therefore, you cannot represent the error output in VS C++. – konata39 Dec 23 '21 at 09:29
  • 1
    [`%.12lf` works fine in GCC 5.1](https://godbolt.org/z/9WjT1EG5x). It is actually the GNU C Library which implements `printf`, not GCC. But the problem is in the online judge’s C implementation, the version of which is not stated. – Eric Postpischil Dec 23 '21 at 11:39
  • @EricPostpischil I tried GCC 4.9 on Debian jessie, the output is also okay. I agree with you. The cause should lie in the specific implementation of `printf`. Really happy that I have learned something I didn't understand. – Wu Xiliang Dec 24 '21 at 00:02