Skip to content

<xlocnum>: Incorrect rounding when parsing long floating point numbers just below midpoints #3378

Description

@statementreply

Describe the bug
The decimal code path of num_get::do_get records the initial 768 significant digits. If there are any remaining non-zero digits, it adds 1 to the last recorded digit (with carry). This is incorrect when the result is just below the rounding boundary.

Command-line test case 1

D:\Temp>type test.cpp
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
    // just below 2^-1022 + 2^-1074 + 2^-1075
    istringstream stream(
        "2.225073858507202124188701479202220329072405282794390378143031338374351073192441946867544064325638818513821882"
        "18502438069999947733013005649884107791928741341929297200970481951993067993290969042784064731682041565926728632"
        "93363047467012331685298342215274451726083585965456631928283524478778779989431077978383369915928859455521371418"
        "11284582511455843192230798975043950868594124572308917389461693683723211913736589779777232866988403563902510444"
        "43035457396733706583981055420456693824658413747607155981176573877626747665912387199931904006317334709003012790"
        "18817520344719025002806127777791679839109057858400646471594381051148915428277504117468219413395246668250343130"
        "61815878293790042053923750720833666932415800027583911188541886415131684784363130802375962957739830017089843749"
        "e-308");
    cout.precision(17);
    double x;
    if (stream >> x) {
        cout << "actual:   " << x << "\n";
    } else {
        cout << "actual:   (failure)\n";
    }
    cout << "expected: 2.2250738585072019e-308\n";
    return 0;
}
D:\Temp>cl /EHsc /W4 test.cpp
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.35.32213 版
版权所有(C) Microsoft Corporation。保留所有权利。

test.cpp
Microsoft (R) Incremental Linker Version 14.35.32213.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj

D:\Temp>.\test
actual:   2.2250738585072024e-308
expected: 2.2250738585072019e-308

Command-line test case 2

D:\Temp>type test.cpp
#include <cfenv>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

#pragma fenv_access(on)

int main() {
    istringstream stream("1.4" + string(1000, '9'));

    fesetround(FE_DOWNWARD);
    double x;
    stream >> x;
    fesetround(FE_TONEAREST);

    cout.precision(17);
    if (stream) {
        cout << "actual:   " << x << "\n";
    } else {
        cout << "actual:   (failure)\n";
    }
    cout << "expected: 1.4999999999999998\n";
    return 0;
}
D:\Temp>cl /EHsc /W4 test.cpp
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.35.32213 版
版权所有(C) Microsoft Corporation。保留所有权利。

test.cpp
Microsoft (R) Incremental Linker Version 14.35.32213.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj

D:\Temp>.\test
actual:   1.5
expected: 1.4999999999999998

STL version

Microsoft Visual Studio Community 2022 (64 位) - Preview
版本 17.5.0 Preview 5.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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