
Date: Sun, 9 Oct 2022 10:45:08 +0200 From: Markus Wichmann <nullplan@....net> To: musl@...ts.openwall.com Subject: Why thousands of digits in floatscan.c? Hi all, so I recently read this wonderful paper: https://dl.acm.org/doi/10.1145/103162.103163 Great read, I encourage it for everyone, even though some things are not directly applicable to the C application programmer. Anyway, towards the end of the paper, Goldberg makes the argument that a 9 digit decimal number is always enough to uniquely identify a singleprecision number. Generalizing out from his argument, I calculated that 36 decimal digits are needed for a quadprecision number (the digit count for a w bit mantissa is roundup(w * lg 2) + 1, which is 9 for singleprec, 17 for doubleprec, 21 for doubleextended, and 33 for doubledouble). Now, I looked again at floatscan.c. The algorithm in decfloat() mostly consists of reading all the decimal digits into a base 1 billion number, which could have any length at all, then aligning the radix point with a word boundary, and then shifting that number such that exactly one mantissa worth of bits are left of the radix point. For the most part, the algorithm does not care about the remaining bits except for two bits of information: If the tail is equal to 0, or else how it compares to 0.5. So now I am left wondering why then, if only the first LDBL_MANT_DIG bits of the number, plus two more for the tail, ever count for anything, the algorithm makes room for thousands of decimal digits. Would it not be enough to save a few dozen? What purpose do the farback digits have? To be clear, I am totally aware of why there needs to be room for thousands of digits in fmt_fp(): All of those digits are in the number and could be requested. The smallest representable positive number on the PC is 2^16447 (that is, it has an exponent of 16384 and a mantissa of 2^63), and that is a number with 16447 decimal places (though the first 4950 of those are 0 and don't need to be saved explicitly, but then you'd need wraparound semantics there as well). Ciao, Markus
Powered by blists  more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.