Блог пользователя memset0c

Автор memset0c, история, 6 лет назад, По-английски

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://codeforces.me/contest/1209/submission/92331960

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

  • Проголосовать: нравится
  • +56
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Just use GNU C++17 (64)

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

If you're down with cout then,

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

This worked -> Here

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +15 Проголосовать: не нравится

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.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +36 Проголосовать: не нравится

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.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +119 Проголосовать: не нравится
This is very off-topic but turns out you can put image as a background for your text on cf