Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
statementreply opened this issue Feb 1, 2023 · 0 comments · Fixed by #3364
Labels
bug Something isn't working fixed Something works now, yay!

Comments

@statementreply
Copy link
Contributor

statementreply commented Feb 1, 2023

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
@statementreply statementreply changed the title <xlocnum>: Incorrect rounding when parsing long floating point numbers just below exact values under some non-default rounding modes <xlocnum>: Incorrect rounding when parsing long floating point numbers just below midpoints Feb 1, 2023
@StephanTLavavej StephanTLavavej added the bug Something isn't working label Feb 1, 2023
frederick-vs-ja added a commit to frederick-vs-ja/STL that referenced this issue Feb 2, 2023
Co-authored-by: statementreply <statementreply@gmail.com>
frederick-vs-ja added a commit to frederick-vs-ja/STL that referenced this issue Feb 2, 2023
@StephanTLavavej StephanTLavavej added the fixed Something works now, yay! label May 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Something works now, yay!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants