STL icon indicating copy to clipboard operation
STL copied to clipboard

<format>: incorrect output for some floating-point values

Open cpplearner opened this issue 1 year ago • 1 comments
trafficstars

Describe the bug

Revealed by libc++ test std/utilities/format/format.functions/locale-specific_form.pass.cpp.

When neither precision nor type is specified, a floating-point value should be formatted as if by to_chars(first, last, value) ([format.string.std]). But MSVC STL uses to_chars(first, last, value, chars_format::general).

Command-line test case

D:\test>type test-format-2.cpp
#include <format>
#include <iostream>

int main() {
    std::cout << std::format("{}\n", 12345678.0);
    std::cout << std::format("{:}\n", 12345678.0);
}

D:\test>cl /std:c++20 /EHs /utf-8 test-format-2.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.39.33321 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

test-format-2.cpp
Microsoft (R) Incremental Linker Version 14.39.33321.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test-format-2.exe
test-format-2.obj

D:\test>test-format-2
12345678
1.2345678e+07

Expected behavior

The second line should also be 12345678.

STL version

https://github.com/microsoft/STL/commit/3eac329d1f614ecf138d96c22a3b02f87076bc4a

cpplearner avatar Jan 16 '24 05:01 cpplearner

Yep, this is definitely a bug - plain shortest is different from chars_format::general. Thanks for analyzing this!

StephanTLavavej avatar Jan 17 '24 22:01 StephanTLavavej