llvm / llvm/llvm-project

[libc++] `std::from_chars` incorrectly rejects `1e10000000000000000`

Open
#171,617 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

libc++
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/4T93nx3zG
```cpp
#include
#include
#include

bool is_parsable(std::string_view s) {
float x;
const auto [p, ec] = std::from_chars(s.data(), s.data() + s.size(), x);
return ec == std::errc{};
}

int main() {
static_assert(std::numeric_limits::has_infinity);
return is_parsable("1e10000000000000000");
}
```
This program returns `0` (i.e. `1e10000000000000000` cannot be parsed by `from_chars`), but it is required to return `1`.

https://eel.is/c++draft/charconv.from.chars#1 states that `ec` is set to `errc::result_out_of_range` if the parsed value is not in the range representable by `float`.

https://eel.is/c++draft/basic.fundamental#13 says that the range of representable values includes all positive real numbers if positive infinity is representable by `float`. Therefore, no exponent should be too large, and the result is simply rounded to positive infinity.

See also:
- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123078
- https://cplusplus.github.io/LWG/issue3081
- https://cplusplus.github.io/CWG/issues/2723.html

The issue is that while the wording is perfectly clear that `std::from_chars` should accept `1e10000000000000000`, that design has been created entirely through an unrelated change to core wording, and it's not intentional. If LWG 3081 was accepted, the behavior of `from_chars` in libc++ would be correct, but LWG 3081 predates CWG 2723, and so it's no longer fixing any lack of clarity or defect in the wording.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Godbolt example and checking the cited charconv.from.chars wording. Read LWG 3081 and CWG 2723 alongside the linked GCC issue to determine whether libc++ behavior or the standard needs resolution; done requires an agreed interpretation and corresponding libc++ behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.