问题
I use Microsoft Visual Studio 2010 and Intel C++ Compiler XE 14.0. Project configuration is ‘x64’. I need to work with the long double
data type. I added a compile option /Qlong_double
and wrote a test example:
long double ld = 2;
long double res = std::sqrt(ld);
printf("long double size: %i, value: %Lf", sizeof(res), res);
printf("double value of res: %f", (double)res);
Output:
long double size: 16, value: 0,000000
double value of res: 1,414214
I found that the problem is that standard C libraries for Windows does not support long double. Tried to use MPFR:
long double ld = 2;
long double res = std::sqrt(ld);
mpfr_t test;
mpfr_init2(test, 100);
mpfr_set_ld(test, res, GMP_RNDN);
mpfr_printf("\nmpfr long double: %Rf\n", test);
Output:
mpfr long double: 0
Is there no of the method (including low-level ways) to correctly print long double values on Microsoft Windows? Maybe i can retrieve and print separate significand and exponent?
UPD:
I wrote example to see whether calculations are performed with long double:
int count = 0;
long double e = static_cast<long double>(1.0);
while (static_cast<long double>(1.0) + e / static_cast<long double>(2.0) > static_cast<long double>(1.0)){
e /= static_cast<long double>(2.0);
count++;
}
cout<< endl<< "COUNT LONG DOUBLE: " << count<<endl;
double de = 1.0;
count = 0;
while (1.0 + de / 2.0 > 1.0){
de /= 2.0;
count++;
}
cout<< endl<< "COUNT DOUBLE: " << count<<endl;
Output:
COUNT LONG DOUBLE: 52
COUNT DOUBLE: 52
This means that work is done with double instead of long double (although /Qlong_double
flag is set). What are some ideas?
来源:https://stackoverflow.com/questions/28927835/print-long-double-on-windows