DEGwer's blog

By DEGwer, 7 years ago, In English,

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.

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

»
7 years ago, # |
  Vote: I like it +9 Vote: I do not like it

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.

»
7 years ago, # |
Rev. 2   Vote: I like it +34 Vote: I do not like it

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.

»
7 years ago, # |
  Vote: I like it +6 Vote: I do not like it

Thank you!