Skip to content

<charconv>: hex from_chars("0.fffffffffffff8p-1022") is incorrect #931

@statementreply

Description

@statementreply

Describe the bug
Hexadecimal floating point from_chars returns incorrect results for numbers just (<= 0.5 ulp) below numeric_limits<T>::min().

The bug also exists in the cl compiler and the strtod family. (DevCom-1093829)

Command-line test case

C:\Users\He\Documents\cpp\test>type charconv.cpp
#include <cassert>
#include <charconv>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <system_error>
using namespace std;

double from_hex_double(const string& s) {
    double x;
    const auto result = from_chars(s.data(), s.data() + s.size(), x, chars_format::hex);

    assert(result.ec != errc::invalid_argument);
    assert(result.ptr == s.data() + s.size());
    return x;
}

int main() {
    constexpr double expected = 0x1p-1022;

#if CHECK_COMPILER_CONV
    static_assert(0x0.fffffffffffff8p-1022 == expected, "0x0.fffffffffffff8p-1022 isn't correctly parsed");
    static_assert(0x1.fffffffffffffp-1023 == expected, "0x1.fffffffffffffp-1023 isn't correctly parsed");
    static_assert(0x3.ffffffffffffep-1024 == expected, "0x3.ffffffffffffep-1024 isn't correctly parsed");
    static_assert(0x7.ffffffffffffcp-1025 == expected, "0x7.ffffffffffffcp-1025 isn't correctly parsed");
    static_assert(0xf.ffffffffffff8p-1026 == expected, "0xf.ffffffffffff8p-1026 isn't correctly parsed");
#endif

    const string cases[] = {
        "0x0.fffffffffffff8p-1022"s,
        "0x1.fffffffffffffp-1023"s,
        "0x3.ffffffffffffep-1024"s,
        "0x7.ffffffffffffcp-1025"s,
        "0xf.ffffffffffff8p-1026"s,
    };

    for (const auto& s : cases) {
        assert(from_hex_double(s.substr(2)) == expected);
    }

    for (const auto& s : cases) {
        assert(strtod(s.c_str(), nullptr) == expected);
    }
}

C:\Users\He\Documents\cpp\test>clang++ -std=c++17 -Wall -Wextra -DCHECK_COMPILER_CONV=1 charconv.cpp

C:\Users\He\Documents\cpp\test>cl /EHsc /std:c++17 /W4 /WX /DCHECK_COMPILER_CONV=1 charconv.cpp
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.26.28806 版
版权所有(C) Microsoft Corporation。保留所有权利。

charconv.cpp
charconv.cpp(22): error C2338: 0x0.fffffffffffff8p-1022 isn't correctly parsed
charconv.cpp(23): error C2338: 0x1.fffffffffffffp-1023 isn't correctly parsed
charconv.cpp(25): error C2338: 0x7.ffffffffffffcp-1025 isn't correctly parsed
charconv.cpp(26): error C2338: 0xf.ffffffffffff8p-1026 isn't correctly parsed

C:\Users\He\Documents\cpp\test>cl /EHsc /std:c++17 /W4 /WX /DCHECK_COMPILER_CONV=0 charconv.cpp
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.26.28806 版
版权所有(C) Microsoft Corporation。保留所有权利。

charconv.cpp
Microsoft (R) Incremental Linker Version 14.26.28806.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:charconv.exe
charconv.obj

C:\Users\He\Documents\cpp\test>.\charconv.exe
Assertion failed: from_hex_double(s.substr(2)) == expected, file charconv.cpp, line 38

STL version

Microsoft Visual Studio Community 2019
版本 16.6.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions