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

generic_128's long_double_to_fd128 is silently incorrect on most platforms. long double users beware #215

Open
alugowski opened this issue Jan 24, 2023 · 0 comments

Comments

@alugowski
Copy link
Contributor

Using generic_128 for long double yields incorrect outputs on most platforms. The API appears to want you to do this:

long double value; char* str_ptr;

length = generic_to_chars(long_double_to_fd128(value), str_ptr);

However, long_double_to_fd128 is implemented like this:

  uint128_t bits = 0;
  memcpy(&bits, &d, sizeof(long double));

which appears to assume that long double is an 80-bit float. This ignores the fact that the C standard does not make this guarantee, only that long double is at least as precise as a double. It is perfectly valid for long double to be 64-bit, and on nearly all non-Linux platforms this is the case.

Indeed on macOS formatting long double = 202 with generic_128 prints 1.6918505631274746047E-4932.

This is a massive silent gotcha.

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

1 participant