%g should not print 0 as infe-308 (#185)
This commit is contained in:
parent
036d90d8cf
commit
cf3182021a
@ -490,10 +490,12 @@ static size_t _etoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, d
|
||||
} conv;
|
||||
|
||||
conv.F = value;
|
||||
int expval;
|
||||
if (conv.U) {
|
||||
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);
|
||||
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;
|
||||
@ -506,6 +508,10 @@ static size_t _etoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, d
|
||||
expval--;
|
||||
conv.F /= 10;
|
||||
}
|
||||
} else {
|
||||
expval = 0;
|
||||
conv.F = 1;
|
||||
}
|
||||
|
||||
// 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;
|
||||
@ -513,7 +519,7 @@ static size_t _etoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, d
|
||||
// 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 ((value == 0.0) || ((value >= 1e-4) && (value < 1e6))) {
|
||||
if ((int) prec > expval) {
|
||||
prec = (unsigned) ((int) prec - expval - 1);
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user