Is there something wrong with the way I use the long double?

I recently became interested in learning about C ++ programming because I want to understand a little more deeply how computers work and process instructions. I thought I would try data types, but I really don't understand what happens to my output ...

#include <iostream> #include <iomanip> using namespace std; int main() { float fValue = 123.456789; cout << setprecision(20) << fixed << fValue << endl; cout << "Size of float: " << sizeof(float) << endl; double dValue = 123.456789; cout << setprecision(20) << fixed << dValue << endl; cout << "Size of double: " << sizeof(double) << endl; long double lValue = 123.456789; cout << setprecision(20) << fixed << lValue << endl; cout << "Size of long double: " << sizeof(long double) << endl; return 0; } 

The result that I expected would be something like this:

 123.45678710937500000000 Size of float: 4 123.45678900000000000000 Size of double: 8 123.45678900000000000000 Size of long double: 16 

This is my actual conclusion:

 123.45678710937500000000 Size of float: 4 123.45678900000000000000 Size of double: 8 -6518427077408613100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000000000000000000 Size of long double: 12 

Any ideas on what happened would be much appreciated, thanks!

Edit:

 System: Windows 10 Pro Technical Preview 64-bit Operating System, x64-based processor Eclipse CDT 8.5 

MinGW Installation

+5
source share
1 answer

From the patch fixed in earlier versions :

MinGW uses the MSvcrt.dll DLL to execute at runtime. Here's the problem: while gcc creates doubles 80 bits in length, the MS runtime accepts only 64-bit doubles.

This error occurs to me when I use 4.8.1 version 4 of MinGW-get (the latest version that it offers), but not when I use 4.8.1 version 5.

This way you are not using a long double error (although it would be better to make long double lValue = 123.456789L to make sure that it does not accept 123.456789 as a double, and then translates it into a long double).

The easiest way to fix this is 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 prefer to use printf, you can change to printf("%Lf", ...) and either:

  • add flag-position when compiling with g ++
  • add #define __USE_MINGW_ANSI_STDIO 1 to #include <cstdio> (found from the original patch )

Finally, you can even just give a double whenever you try to print a long double (there is some loss of precision, but it doesn't matter just by printing the numbers).

To find more information, you can also see my blog post on this issue .

Update. If you want to continue using Mingw 4.8, you can also just download another Mignw distribution, which did not have this problem for me.

+6
source

Source: https://habr.com/ru/post/1213406/


All Articles