From: Keith Marshall <keithmarshall@us...>  20080703 23:42:24

On Wednesday 02 July 2008 04:53:50 Greg Chicares wrote: > >>  Printing 1e+161 in fixed instead of scientific notation gives > >> 10000000000000000377[...], whereas the msvc rtl prints no > >> nonzero digit after the initial '1'. Probably a QoI issue as > >> discussed here: > >> > >> http://lists.nongnu.org/archive/html/lmi/200806/msg00017.html > > > > This is a (broken) feature of David Gay's gdtoa formatting > > algorithm, > > Indeed. Zeros would be less astonishing than random digits, Less astonishing maybe, but no more accurate. In fact the digits are not random; they are the result of continued division by ten, with filler bits, (likely zeroes, but I can't say for sure), appended to the mantissa after all of its 53 available bits have been exhausted, until the total number of bits is sufficient to represent the number of decimal digits required to precede the radix point, in the value to be emitted. > but I believe C99 permits either. I believe so too. However, neither is more accurate than the other; there is a duty of interpretation placed on the user, to recognize the limitations on the precision of the data. This is related to the number of bits in the mantissa of the floating point representation, according to the relationship: significantdecimaldigits = floor( mantissabits * log10(2) ) which, for the three floating point data types yields: type mantissasize maximumprecision    float 24 bits 7 decimal digits double 53 bits 15 decimal digits [*] long double 64 bits 19 decimal digits ([*] not 17, as I mistakenly suggested previously). A consequence of this is that the maximum value which can be reliably represented with "%f" format is 1e+15, (and only 1e+7, in cases where the original data type is float, which is promoted to double in the printf call), while in the case of "%Lf" format, it is 1e+19. A further consequence is that to specify a precision of more than 14, in say "%.14e" (or > 18 in "%.18Le), or correspondingly > 15 (19) in the equivalent "%.15g" ("%.19Lg") cases, may result in output which is potentially misleading. Regards, Keith. 