Is there something wrong with the way I am using a long double?

后端 未结 1 1383
醉话见心
醉话见心 2021-01-18 22:41

I have recently become interested in learning about programming in c++, because I want to get a bit deeper understanding of the way computers work and handle instructions. I

相关标签:
1条回答
  • 2021-01-18 23:00

    From the patch that fixed this in earlier versions:

    MinGW uses the Microsoft runtime DLL msvcrt.dll. Here lies a problem: while gcc creates 80 bits long doubles, the MS runtime accepts 64 bit long doubles only.

    This bug happens to me when I use 4.8.1 revision 4 from MinGW-get (the most recent version it offers), but not when I use 4.8.1 revision 5.

    So you are not using long double wrong (although there would be better accuracy to do long double lValue = 123.456789L to make sure it doesn't take 123.456789 as a double, then cast it to a long double).

    The easiest way to fix this would be to simply change the version of MinGW you are using to 4.9 or 4.7, depending on what you need (you can get 4.9 here).

    If you are willing to instead use printf, you could change to printf("%Lf", ...), and either:

    • add the flag -posix when you compile with g++
    • add #define __USE_MINGW_ANSI_STDIO 1 before #include <cstdio> (found this from the origional patch)

    Finally, you can even just cast to a double whenever you try to print out the long double (there is some loss of accuracy, but it shouldn't matter when just printing out numbers).

    To find more details, you can also look at my blog post on this issue.

    Update: If you want to continue to use Mingw 4.8, you can also just download a different distribution of Mignw, which didn't have that problem for me.

    0 讨论(0)
提交回复
热议问题