0

I try to initialize a long double with the following:

using namespace std;

int main(){

    long double initial = 0.9999952L;

    cout << initial << endl;

    return 0;
}

But I get the following output:

C:\MinGW\bin>a.exe
-1.#QNAN

What is the appropriate way to intialize a long double?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
JoeBass
  • 519
  • 7
  • 18
  • 5
    Seems ok to me. Works fine too. https://ideone.com/4I3kyf. – R Sahu Mar 14 '19 at 18:17
  • Could this be the version of the compiler i'm using, such as 32-bit vs 64-bit or something – JoeBass Mar 14 '19 at 18:24
  • @JoeBass what compiler, compiler version, and OS are you using? – Chi Mar 14 '19 at 18:24
  • @DaichiJameson Windows 10, ../src/gcc-5.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 – JoeBass Mar 14 '19 at 18:27
  • maybe this is a windows platform issue? MSVC doesn't support "real" `long double`. Maybe mingw uses a 10-byte `long double`, while the C library uses a 8-byte one. – geza Mar 14 '19 at 18:29
  • @geza sizeof(long double) returns 12 – JoeBass Mar 14 '19 at 18:31
  • @JoeBass: that means that mingw uses 10-byte `long double`s (it is just aligned to 4-byte, so it becomes 12-byte sized). The question is, how `cout` works. If it boils down to some MSVCRT.dll or LIBCMT from MSVC, then it is very likely that this issue is what I've said previously. Maybe mingw has a switch that it should use the GNU standard library? – geza Mar 14 '19 at 18:38
  • MinGW has a well known problem with type `long double`. The compiler, gcc, uses one representation for `long double`; the runtime library, from Microsoft, uses a different representation. (Neither is incorrect; the bug is in the way they're integrated into the MinGW implementation.) I'll mark this as a duplicate when I find an appropriate question that deals with it specifically. – Keith Thompson Mar 14 '19 at 18:56
  • The initialization is correct. The problem is in printing it. My answer to the duplicate question provides a workaround, `-D__USE_MINGW_ANSI_STDIO`, that will likely work for C++ as well as for C. – Keith Thompson Mar 14 '19 at 18:59
  • @KeithThompson yes that was it – JoeBass Mar 14 '19 at 22:46

0 Answers0