0

For the following snippet of code:

  long double foo = 5.32e-5;
  double bar  = foo ;

  printf("%Lf can be written %Le\n", foo, foo);
  printf("%f can be written %e\n", bar , bar);

I'm getting this output:

-1950228512509697500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000 can be written 2.725000e+002

0.000053 can be written 5.320000e-005

foo has been converted to bar without issue, this makes me think that the internal representation of the variable is fine, but when I print both these variables bar has an accurate representation and foo does not.

I believe the formats %Lf and %Le are correct, however I get the same output if I use the lowercase (%lf and %le). I am using gcc on a window computer.

Harvey
  • 11
  • 2
  • I get the same output for both. Using `gcc` – klutt Apr 13 '21 at 10:05
  • Somewhere in he back of my mind I remember some issue that C library used for MinGW under Windows systems does not support `long double` type. But I cannot really find a source for that now. – Gerhardh Apr 13 '21 at 10:29
  • Does it work with [__USE_MINGW_ANSI_STDIO](https://stackoverflow.com/a/28415046/1983398)? – ssbssa Apr 13 '21 at 10:36
  • MinGW had issues with long double - https://stackoverflow.com/questions/66996900/long-double-is-printed-incorrectly-in-codeblocks-using-mingw-long-double – nevilad Apr 13 '21 at 10:37
  • you also need `long double foo = 5.32e-5L;` to get the full `long double` precision. Note the `L` suffix – phuclv Apr 13 '21 at 10:46
  • Yes, this was the source of the problem, thanks @phuclv – Harvey Apr 13 '21 at 12:20

0 Answers0