memset0c's blog

By memset0c, history, 4 years ago, In English

Excuse me, can anyone tell me how to use printf to output long double values?

I tried to use %Lf, but it just output zero when I chose GNU C++11.

However, I got AC when choosing GNU C++17 (64).

Can anyone tell me what should I do? Thank you~

GNU C++11 submission (WA on 1): https://mirror.codeforces.com/contest/1209/submission/92331960

GNU C++17 (64) submission (AC): https://mirror.codeforces.com/contest/1209/submission/92331832

  • Vote: I like it
  • +56
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Just use GNU C++17 (64)

»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

If you're down with cout then,

std::cout<<std::setprecision(10)<<ans<<std::endl;

This worked -> Here

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It still doesn't work with GNU C++11.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I have used simple cout in your code with both C++14 and C++11 with outputs 361.569 and 1000 respectively.

      Now I don't think this is possible unless you've used something else that is broken in C++11. I might be wrong.

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Only using std::cin & std::cout instead of scanf & printf can it work with GNU C++11.

»
4 years ago, # |
  Vote: I like it +15 Vote: I do not like it

A tangential answer: note that, most of the time, casting to a double would be fine in output, as a relative error of $$$10^{-9}$$$ is allowed.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    well, it's quite complicated.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Huh? How much more complicated is printf ("%.6lf", (double) (x));?

      Instead of just printf ("%.6Lf", x);.

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +8 Vote: I do not like it

        I also need to use

        double temp;
        scanf("%lf", &temp);
        target = temp;
        

        instead of

        scanf("%Lf", &temp);
        
        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Yeah, that, too. But reading and writing values is an easy part of the problem. Usually.

»
4 years ago, # |
  Vote: I like it +36 Vote: I do not like it

Short answer is, you don't. The implementation used by codeforces's version of C++11 does not implement printf for long doubles.

You can switch to the mingw implementation by either submitting in C++11 with #define __USE_MINGW_ANSI_STDIO 1 before your code, or by submitting in C++17 (64). Be aware though that mingw printf is slow so you are exposing yourself to potential TLEs by doing this.

To be safe, I would either switch to using cout and a more recent version of C++, or use C++11 printf after casting to double.

»
4 years ago, # |
  Vote: I like it +119 Vote: I do not like it
This is very off-topic but turns out you can put image as a background for your text on cf