simp1eton's blog

By simp1eton, 8 years ago, In English,
Hi all,

I have some problems with outputting long double.

Here is my code for Yandex Round 1 Problem C Petya and Tree:

http://pastebin.com/sHKuXg15

I submitted this code but got WA for testcase 1, even though it worked on my com. In the end, I realised that I cannot printf long doubles (something similar to printf long long int?), so I changed to cout. However, in that case it seems that I started to output integers :(. All my decimals places got cut off. Can someone help me?
 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

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

I think there is no real need in accuracy, so you may cast your long double to a double.

        printf("%.9lf\n",double(AVG[A[P]]));

Probably there is no way to output long doubles.

UPD:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
   long double d = 0.12345678910;
   cout << fixed;
   cout << setprecision(12) << d;
   return 0;
}

8 years ago, # |
  Vote: I like it -8 Vote: I do not like it
As far as I know, in C++ long double have the same precision as double.
  • 8 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it
    In MS compiler - yes, but not in GCC.
  • 8 years ago, # ^ |
      Vote: I like it +9 Vote: I do not like it
    I don't understand why I'm having -2 for my last message, so I'm going to prove that long double exists for some angry trolls and haters.

    Run this code here with GNU and with MS compiler and see the difference.

    GNU:
    Comparing d & ld: 0
    Comparing d & d:  1
    0.00000000000000001851275209518998800000000000000000

    MS:
    Comparing d & ld: 1
    Comparing d & d:  1
    0.00000000000000000000000000000000000000000000000000
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it
long double x;
......

printf("%Lf", x);
  • 8 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    Did you try to run that on gcc compiler?

    #include <cstdio>
    int main()
    {
       long double d = 1;
       printf("%Lf",d);
       return 0;
    }

    It outputs
    0.000000

    MS compiler outputs 1.0... just because there is no difference between double and long double.
    • 8 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      What GCC do you use???

      Please, read "man 3 printf":

      "L - A following a, A, e, E, f, F, g, or G conversion corresponds to a long double argument. (C99 allows %LF, but SUSv2 does not.)"

      So...
      % cat >test.cpp <<EOF
      #include <cstdio>
      int main()
      {
      long double d = 1;
      printf("%Lf",d);
      return 0;
      }
      EOF
      % gcc test.cpp && ./a.out
      1.000000
    • 8 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it
      However, 

      long double x ;
      ....

      cout << x;
      works pretty fine
      • 8 years ago, # ^ |
        Rev. 2   Vote: I like it +1 Vote: I do not like it

        #include <iostream>
        #include <iomanip>
        using namespace std;
        int main()
        {
           long double d = 0.12345678910;
           cout << fixed;
           cout << setprecision(12) << d;
           return 0;
        }
        =====
        0.123456789100

        YARLY. :-)
        • 8 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it
          Probably, we are facing some issues with MinGW compiler.
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it
You may use specifier Lg for long double:

long double x;
...
printf("%Lg",x);
with GNU C++
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it
Thanks everyone!

May I know what is the difference between a MS compiler and GNU compiler?