0

I printed such a code to see max value of 128 bits long double variable:

long double num = numeric_limits<long double>::max();
printf("%Lf", num);

and got output of more than 4 thousands digits which is can not be stored in 128 bits. Then what is stored in long double variable? In my case it is equal to 1.18973e+4932. I guess long double stores 118973, 4932, and then multiplying occurs, but in what rules and where the code fore this routine is kept? How essentially number of 4 thousands digits appears?

Max Popov
  • 357
  • 2
  • 12
  • 3
    Have a read of https://en.wikipedia.org/wiki/IEEE_754 – Richard Critten Oct 21 '22 at 12:06
  • You actually get >4,000 digits displayed? Or do you mean that the number is more than 10^4000? But the format of an IEEE long double is well explained in various places ... – Adrian Mole Oct 21 '22 at 12:06
  • 1
    there are no >4000 digits in `1.18973e+4932` there are 6 (at least in the number as displayed). You are counting the digits before the `.` which is rather arbitrary and not how floating point numbers are represented – 463035818_is_not_an_ai Oct 21 '22 at 12:11
  • [Information](https://en.wikipedia.org/wiki/Entropy_(information_theory)) ;) – Pepijn Kramer Oct 21 '22 at 12:18
  • Four thousand digits is far beyond the precision of a long double. Output routines for floating-point values can keep producing digits as long as you want them to, but after 5, 10, 20 or so, the digits don't mean anything; they're just leftovers from the inherently inexact conversion routine. – Pete Becker Oct 21 '22 at 12:36
  • @AdrianMole I literally got 4981 digits including a dot and zeros after it in outline – Max Popov Oct 21 '22 at 12:39
  • @AdrianMole: A number of recent implementations are indeed capable of producing the exact representation down to the last digit. This is non-trivial. But as Pete Becker notes, an exact representation of an inexact value is not worth that much. – MSalters Oct 21 '22 at 12:43
  • @MaxPopv: If you have read the explanation about floating-point values, and still want to know how all those digits are calculated, then you can edit your question. Just remove the parts of the question that are already answered elsewhere. – MSalters Oct 21 '22 at 12:47
  • `long double` will only have so many digits of precision (the significand bits). Here's an example of using [`std::numeric_limits::max_digits10`](https://stackoverflow.com/a/50970282/4641116). Also [here](https://stackoverflow.com/a/71979978/4641116) is some code for dumping float bits which could be adapted for `long double`. – Eljay Oct 21 '22 at 12:57
  • Consider using a "big number" library. Search the internet for "C++ big number library". – Thomas Matthews Oct 21 '22 at 17:02

0 Answers0