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

Автор DEGwer, 11 лет назад, По-английски

I tried to solve a problem in C++, I tried to output the value which class is long double. When I used "%Lf" the output of my solution is -0.000000 on test #1 (it is a wrong answer) but according to my environment, the output is 1.333333 (it is a correct output). On Codeforces, can we use "%Lf" in C++? Or like "%lld", aren't we allowed to use it?

UPD: When I cast it to double, my solution worked.

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

»
11 лет назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

You can use the Long double. But there are features associated with the compiler. Look at this:

#include <cstdio>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    long double s = 1.23456789012345;
    cout << "Size of long: " << sizeof(double) << endl;
    cout << "Size of long double: " << sizeof(long double) << endl;
    printf("%.5Lf\n", s);
    cout << setprecision (5) << fixed << s << endl;
    return 0;
}

Output of GNU C++:

Size of long: 8
Size of long double: 12
-0.00000
1.23457

Output of MV C++:

Size of long: 8
Size of long double: 8
1.23457
1.23457

So you should use the GNU C++ and iostream to display the result.

»
11 лет назад, # |
Rev. 2   Проголосовать: нравится +34 Проголосовать: не нравится

Yep, the printing of long doubles (in printf/scanf) is broken in MinGW (port of GCC for Microsoft Windows) for who knows how long. The best solution (as you've already worked out) is to convert from long double to double when doing I/O — you don't need that precision when you're just reading or writing numbers anyway.

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

Thank you!