Skip to content

ostream writes a double that istream can't read #38360

@llvmbot

Description

@llvmbot
Bugzilla Link 39012
Version unspecified
OS All
Reporter LLVM Bugzilla Contributor
CC @ecnelises,@mclow,@N-Dekker,@pazner

Extended Description

It appears libc++ incorrectly sets input stream failbit when reading subnormal floating point numbers. Here's an example:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    constexpr double x {1e-320};
    std::ostringstream oss {};
    oss << x;
    const auto str_x = oss.str();
    std::istringstream iss {str_x};
    double y;
    if (iss >> y) {
        std::cout << y << std::endl;
    } else {
        std::cout << "Nope" << std::endl;
    }
}

Compiling with Clang/libc++, results in "Nope" being printed, while with GCC/libstdc++, the value of the double is printed - as I would expect.

I also reported this behaviour in StackOverflow: https://stackoverflow.com/questions/52410931/why-does-clang-stdostream-write-a-double-that-stdistream-cant-read.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillaconfirmedVerified by a second partylibc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions