Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

floating point zero handling #54

Open
ledvinap opened this issue Apr 23, 2019 · 2 comments
Open

floating point zero handling #54

ledvinap opened this issue Apr 23, 2019 · 2 comments

Comments

@ledvinap
Copy link

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"

@eyalroz
Copy link

eyalroz commented Aug 2, 2021

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 added a commit to eyalroz/printf that referenced this issue Aug 2, 2021
…te: This will probably handle non-0.0/-0.0 denormal numbers properly.
@eyalroz
Copy link

eyalroz commented Aug 2, 2021

So, fixed on the development branch of my fork.

eyalroz added a commit to eyalroz/printf that referenced this issue Aug 3, 2021
…te: This will probably handle non-0.0/-0.0 denormal numbers properly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants