Your program works correctly on my system (after I remove the irrelevant cruft); the output is:
5.000000
0.546450
2.732250
If I change the %Lf
formats to %f
(which is incorrect), I get 0.00000
.
I think you're using MinGW, a C implementation for Windows that uses the gcc compiler and Microsoft's runtime library. (If you're not using MinGW, the rest of this answer is incorrect.)
MinGW has a known bug: it doesn't handle long double
consistently. gcc treats long double
a wider type with more range and precision than double
; Microsoft's runtime library, if I recall correctly, treats it as a type with the same size, range, and precision as double
.
Either representation is perfectly valid, but combining the two in a single implementation creates this kind of bug. This not a bug in gcc or in Microsoft's implementation; it's a bug in the way MinGW has combined them.
You can perform computations using long double
and get consistent results, as long as you don't call any library functions. But if you try to print a long double
using Microsoft's printf
you'll get inconsistent results.
Presumably the same problem applies to the functions in <math.h>
.
If you're using MinGW, it's probably best just to avoid using long double
, and to settle for the lesser range and precision of double
(which is the same range and precision you'd get with long double
in a pure Microsoft implementation anyway).
Incidentally, your self contained program is much bigger than it needs to be; the vast majority of it is completely irrelevant to the problem you're asking about. And the freopen
calls prevent the output from appearing. You have a comment:
//Remember to remove freopen
but you left the calls in place.
Here's a smaller version:
#include <cstdio>
int main()
{
long double a=5;
long double b=0.54645;
long double c=a*b;
printf("%Lf\n", a);
printf("%Lf\n", b);
printf("%Lf\n", c);
return 0;
}