src/melon/modules/string/format.c
static size_t _etoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
{
	// check for NaN and special values
	if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX))
	{
		return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags);
	}

	// determine the sign
	const bool negative = value < 0;
	if (negative)
	{
		value = -value;
	}

	// default precision
	if (!(flags & FLAGS_PRECISION))
	{
		prec = PRINTF_DEFAULT_FLOAT_PRECISION;
	}

	// determine the decimal exponent
	// based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c)
	union {
		uint64_t U;
		double F;
	} conv;

	conv.F = value;
	int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023;			 // effectively log2
	conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U); // drop the exponent so conv.F is now in [1,2)
	// now approximate log10 from the log2 integer part and an expansion of ln around 1.5
	int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168);
	// now we want to compute 10^expval but we want to be sure it won't overflow
	exp2 = (int)(expval * 3.321928094887362 + 0.5);
	const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453;
	const double z2 = z * z;
	conv.U = (uint64_t)(exp2 + 1023) << 52U;
	// compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex
	conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));
	// correct for rounding errors
	if (value < conv.F)
	{
		expval--;
		conv.F /= 10;
	}

	// the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters
	unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;

	// in "%g" mode, "prec" is the number of *significant figures* not decimals
	if (flags & FLAGS_ADAPT_EXP)
	{
		// do we want to fall-back to "%f" mode?
		if ((value >= 1e-4) && (value < 1e6))
		{
			if ((int)prec > expval)
			{
				prec = (unsigned)((int)prec - expval - 1);
			}
			else
			{
				prec = 0;
			}
			flags |= FLAGS_PRECISION; // make sure _ftoa respects precision
			// no characters in exponent
			minwidth = 0U;
			expval = 0;
		}
		else
		{
			// we use one sigfig for the whole part
			if ((prec > 0) && (flags & FLAGS_PRECISION))
			{
				--prec;
			}
		}
	}

	// will everything fit?
	unsigned int fwidth = width;
	if (width > minwidth)
	{
		// we didn't fall-back so subtract the characters required for the exponent
		fwidth -= minwidth;
	}
	else
	{
		// not enough characters, so go back to default sizing
		fwidth = 0U;
	}
	if ((flags & FLAGS_LEFT) && minwidth)
	{
		// if we're padding on the right, DON'T pad the floating part
		fwidth = 0U;
	}

	// rescale the float value
	if (expval)
	{
		value /= conv.F;
	}

	// output the floating part
	const size_t start_idx = idx;
	idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP);

	// output the exponent part
	if (minwidth)
	{
		// output the exponential symbol
		out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen);
		// output the exponent value
		idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth - 1, FLAGS_ZEROPAD | FLAGS_PLUS);
		// might need to right-pad spaces
		if (flags & FLAGS_LEFT)
		{
			while (idx - start_idx < width)
				out(' ', buffer, idx++, maxlen);
		}
	}
	return idx;
}
// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <m.jasperse@gmail.com>

internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse 
internal ftoa variant for exponential floating - point type contributed by Martijn Jasperse
static size_t _etoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
{
	if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX))
	{
		return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags);
	}
	const bool negative = value < 0;
	if (negative)
	{
		value = -value;
	}
	if (!(flags & FLAGS_PRECISION))
	{
		prec = PRINTF_DEFAULT_FLOAT_PRECISION;
	}
	union {
		uint64_t U;
		double F;
	} conv;
	conv.F = value;
	int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023;			 
	conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U); 
	int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168);
	exp2 = (int)(expval * 3.321928094887362 + 0.5);
	const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453;
	const double z2 = z * z;
	conv.U = (uint64_t)(exp2 + 1023) << 52U;
	conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));
	if (value < conv.F)
	{
		expval--;
		conv.F /= 10;
	}
	unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;
	if (flags & FLAGS_ADAPT_EXP)
	{
		if ((value >= 1e-4) && (value < 1e6))
		{
			if ((int)prec > expval)
			{
				prec = (unsigned)((int)prec - expval - 1);
			}
			else
			{
				prec = 0;
			}
			flags |= FLAGS_PRECISION; 
			minwidth = 0U;
			expval = 0;
		}
		else
		{
			if ((prec > 0) && (flags & FLAGS_PRECISION))
			{
				--prec;
			}
		}
	}
	unsigned int fwidth = width;
	if (width > minwidth)
	{
		fwidth -= minwidth;
	}
	else
	{
		fwidth = 0U;
	}
	if ((flags & FLAGS_LEFT) && minwidth)
	{
		fwidth = 0U;
	}
	if (expval)
	{
		value /= conv.F;
	}
	const size_t start_idx = idx;
	idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP);
	if (minwidth)
	{
		out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen);
		idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth - 1, FLAGS_ZEROPAD | FLAGS_PLUS);
		if (flags & FLAGS_LEFT)
		{
			while (idx - start_idx < width)
				out(' ', buffer, idx++, maxlen);
		}
	}
	return idx;
}
static size_t _etoa ( out_fct_type out , char * buffer , size_t idx , size_t maxlen , double value , unsigned int prec , unsigned int width , unsigned int flags ) { if ( ( value != value ) || ( value > DBL_MAX ) || ( value < - DBL_MAX ) ) { return _ftoa ( out , buffer , idx , maxlen , value , prec , width , flags ) ; } const bool negative = value < 0 ; if ( negative ) { value = - value ; } if ( ! ( flags & FLAGS_PRECISION ) ) { prec = PRINTF_DEFAULT_FLOAT_PRECISION ; } union { uint64_t U ; double F ; } conv ; conv . F = value ; int exp2 = ( int ) ( ( conv . U >> 52U ) & 0x07FFU ) - 1023 ; conv . U = ( conv . U & ( ( 1ULL << 52U ) - 1U ) ) | ( 1023ULL << 52U ) ; int expval = ( int ) ( 0.1760912590558 + exp2 * 0.301029995663981 + ( conv . F - 1.5 ) * 0.289529654602168 ) ; exp2 = ( int ) ( expval * 3.321928094887362 + 0.5 ) ; const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453 ; const double z2 = z * z ; conv . U = ( uint64_t ) ( exp2 + 1023 ) << 52U ; conv . F *= 1 + 2 * z / ( 2 - z + ( z2 / ( 6 + ( z2 / ( 10 + z2 / 14 ) ) ) ) ) ; if ( value < conv . F ) { expval -- ; conv . F /= 10 ; } unsigned int minwidth = ( ( expval < 100 ) && ( expval > -100 ) ) ? 4U : 5U ; if ( flags & FLAGS_ADAPT_EXP ) { if ( ( value >= 1e-4 ) && ( value < 1e6 ) ) { if ( ( int ) prec > expval ) { prec = ( unsigned ) ( ( int ) prec - expval - 1 ) ; } else { prec = 0 ; } flags |= FLAGS_PRECISION ; minwidth = 0U ; expval = 0 ; } else { if ( ( prec > 0 ) && ( flags & FLAGS_PRECISION ) ) { -- prec ; } } } unsigned int fwidth = width ; if ( width > minwidth ) { fwidth -= minwidth ; } else { fwidth = 0U ; } if ( ( flags & FLAGS_LEFT ) && minwidth ) { fwidth = 0U ; } if ( expval ) { value /= conv . F ; } const size_t start_idx = idx ; idx = _ftoa ( out , buffer , idx , maxlen , negative ? - value : value , prec , fwidth , flags & ~ FLAGS_ADAPT_EXP ) ; if ( minwidth ) { out ( ( flags & FLAGS_UPPERCASE ) ? ' ' : ' ' , buffer , idx ++ , maxlen ) ; idx = _ntoa_long ( out , buffer , idx , maxlen , ( expval < 0 ) ? - expval : expval , expval < 0 , 10 , 0 , minwidth - 1 , FLAGS_ZEROPAD | FLAGS_PLUS ) ; if ( flags & FLAGS_LEFT ) { while ( idx - start_idx < width ) out ( ' ' , buffer , idx ++ , maxlen ) ; } } return idx ; }
internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <m.jasperse@gmail.com>
internal ftoa variant for exponential floating - point type contributed by Martijn Jasperse <m . jasperse@gmail . com >
// check for NaN and special values
 // determine the sign
 // default precision
 // determine the decimal exponent
 // based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c)
 // effectively log2
 // drop the exponent so conv.F is now in [1,2)
 // now approximate log10 from the log2 integer part and an expansion of ln around 1.5
 // now we want to compute 10^expval but we want to be sure it won't overflow
 // compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex
 // correct for rounding errors
 // the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters
 // in "%g" mode, "prec" is the number of *significant figures* not decimals
 // do we want to fall-back to "%f" mode?
 // make sure _ftoa respects precision
 // no characters in exponent
 // we use one sigfig for the whole part
 // will everything fit?
 // we didn't fall-back so subtract the characters required for the exponent
 // not enough characters, so go back to default sizing
 // if we're padding on the right, DON'T pad the floating part
 // rescale the float value
 // output the floating part
 // output the exponent part
 // output the exponential symbol
 // output the exponent value
 // might need to right-pad spaces
