1

The following program

#include <charconv>
#include <string>
#include <iostream>

int main() {
    std::string s = "0X1.BC70A3D70A3D7P+6";
    std::cout << "strtod: " << std::strtod(s.c_str(), nullptr) << std::endl;
    double d = 42;
    std::from_chars(s.data(), s.data() + s.size(), d, std::chars_format::general);
    std::cout << "from_chars: " << d << std::endl;
}

prints

strtod: 111.11
from_chars: 0

when compiled with GCC on Linux. Can std::from_chars not handle hexadecimal floating-point numbers?

19
  • 1
    As per std::from_chars it doesn't handle the leading 0X. Commented Nov 15, 2024 at 19:00
  • 1
    @Eljay only if fmt is std::chars_format::hex if I understood that documentation correctly Commented Nov 15, 2024 at 19:01
  • if fmt is std::chars_format::hex, the prefix "0x" or "0X" is not permitted (the string "0x123" parses as the value "0" with unparsed remainder "x123") Commented Nov 15, 2024 at 19:04
  • 1
    @AhmedAEK found it. Looks like cppref and the standard agree. Commented Nov 15, 2024 at 19:30
  • 2
    The important part is eel.is/c++draft/charconv#to.chars-3 -- the std::chars_format::general expects the string to be in %g format. That's not a hex format. Commented Nov 15, 2024 at 19:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.