printf icon indicating copy to clipboard operation
printf copied to clipboard

floating point zero handling

Open ledvinap opened this issue 5 years ago • 2 comments

When printing "%e" or "%g" with zero as argument, 0e-308 is assumed instead of zero

printf("%e", 0):
expected: "0.000000e+00"
got: "0.000000e-308"

printf("%g", 0):
expected: "0"
got: "0.000000e-308"

ledvinap avatar Apr 23 '19 20:04 ledvinap

Well, the problem is in _etoa, which has a line which estimates the base-10 exponent of the value to print using its base-2 exponent:

int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168);

This uses a linear approximation of the ln(x) function at x = 1.5 . This is itself a bit fishy, but I suppose we could live with it... for normal floats. With 0.0, it is sub-normal - the most extreme case of sub-normality.

We have two options on how to handle this:

  1. Fix denormal handling generally by counting the leading 0 bits of the mantissa when the exponent is 0
  2. Just special-case 0 and ignore handling of other denormals.

I intend to go with option (2.) for now, and open a separate bug for (1.) - unless there are any objections.

eyalroz avatar Aug 02 '21 21:08 eyalroz

So, fixed on the development branch of my fork.

eyalroz avatar Aug 02 '21 22:08 eyalroz