lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, Jul 02, 2014 at 04:06:08PM -0500, Paige DePol wrote:
> However, there is one issue that appears to be Lua based, the default
> floating point format specifiers.
> 
> By default we have these defines for float, double and long double:
> 
> float:	%.7g
> double:	%.14g
> lngdbl:	%.19Lg
> 
> The problem, according to the IEEE floating point entries on Wikipedia, is
> that there are not enough digits specified. As per the Wikipedia articles
> the number of significant digits are as follows:
> 
> float:	6-9 
> double:	15-17
> 80bit:	18-21 (probably `long double` for most people)
> 128bit:	33-36 (`long double` for SPARC, PowerPC, Itanium)
<snip>
> >From what I have been able to find online these should be the proper format specifiers to display any numbers of the relevant data type:
> 
> float:	%.8f
> double:	%.16f
> 80bit:	%.20Lf
> 128bit:	%.35Lf
> 

I believe the portable way to do this is to use DBL_DECIMAL_DIG, like

	printf("%.*g", DBL_DECIMAL_DIG, n);

although it may be more complicated than that.

However, DBL_DECIMAL_DIG was only added in C11. C99 has DECIMAL_DIG, which
is the maximum number of decimal digits needed to round-trip the largest
supported floating point type.

The * precision placeholder was added in C99.