From 057adf2b6fd2979094913d87399030938041b78c Mon Sep 17 00:00:00 2001 From: achabense <60953653+achabense@users.noreply.github.com> Date: Fri, 23 Jun 2023 03:00:45 +0800 Subject: [PATCH] ``: fix `std::format("{:#.precision}", floating)` (#3815) Co-authored-by: A. Jiang Co-authored-by: Stephan T. Lavavej --- stl/inc/format | 10 +++--- tests/libcxx/expected_results.txt | 4 --- tests/std/test.lst | 1 + .../GH_003003_format_decimal_point/env.lst | 4 +++ .../GH_003003_format_decimal_point/test.cpp | 33 +++++++++++++++++++ 5 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 tests/std/tests/GH_003003_format_decimal_point/env.lst create mode 100644 tests/std/tests/GH_003003_format_decimal_point/test.cpp diff --git a/stl/inc/format b/stl/inc/format index 3f673b2f8a..1ed63d4ec7 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2867,7 +2867,7 @@ _NODISCARD _OutputIt _Fmt_write( auto _To_upper = false; auto _Format = chars_format::general; - auto _Exponent = '\0'; + auto _Exponent = 'e'; auto _Precision = _Specs._Precision; switch (_Specs._Type) { @@ -2885,8 +2885,7 @@ _NODISCARD _OutputIt _Fmt_write( if (_Precision == -1) { _Precision = 6; } - _Format = chars_format::scientific; - _Exponent = 'e'; + _Format = chars_format::scientific; break; case 'F': _To_upper = true; @@ -2904,8 +2903,7 @@ _NODISCARD _OutputIt _Fmt_write( if (_Precision == -1) { _Precision = 6; } - _Format = chars_format::general; - _Exponent = 'e'; + _Format = chars_format::general; break; } @@ -3018,7 +3016,7 @@ _NODISCARD _OutputIt _Fmt_write( _Zeroes_to_append = _Extra_precision; break; case chars_format::general: - if (_Specs._Alt) { + if (_Specs._Alt && (_Specs._Type == 'g' || _Specs._Type == 'G')) { auto _Digits = static_cast(_Exponent_start - _Buffer_start); if (!_Append_decimal) { diff --git a/tests/libcxx/expected_results.txt b/tests/libcxx/expected_results.txt index e0f46e6c3e..a815353ffd 100644 --- a/tests/libcxx/expected_results.txt +++ b/tests/libcxx/expected_results.txt @@ -1062,11 +1062,7 @@ std/ranges/range.adaptors/range.take/adaptor.pass.cpp FAIL std/ranges/range.factories/range.single.view/cpo.pass.cpp FAIL std/thread/futures/futures.task/futures.task.members/ctor2.compile.pass.cpp FAIL std/utilities/format/format.functions/escaped_output.ascii.pass.cpp FAIL -std/utilities/format/format.functions/format.locale.pass.cpp FAIL -std/utilities/format/format.functions/format.pass.cpp FAIL std/utilities/format/format.functions/locale-specific_form.pass.cpp FAIL -std/utilities/format/format.functions/vformat.locale.pass.cpp FAIL -std/utilities/format/format.functions/vformat.pass.cpp FAIL std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/ctad.static.compile.pass.cpp FAIL std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke.pass.cpp:0 FAIL std/utilities/function.objects/refwrap/refwrap.const/type_conv_ctor.pass.cpp:0 FAIL diff --git a/tests/std/test.lst b/tests/std/test.lst index 094f4c7d4f..b6511acf8b 100644 --- a/tests/std/test.lst +++ b/tests/std/test.lst @@ -218,6 +218,7 @@ tests\GH_002769_handle_deque_block_pointers tests\GH_002789_Hash_vec_Tidy tests\GH_002989_nothrow_unwrappable tests\GH_002992_unwrappable_iter_sent_pairs +tests\GH_003003_format_decimal_point tests\GH_003022_substr_allocator tests\GH_003105_piecewise_densities tests\GH_003119_error_category_ctor diff --git a/tests/std/tests/GH_003003_format_decimal_point/env.lst b/tests/std/tests/GH_003003_format_decimal_point/env.lst new file mode 100644 index 0000000000..d6d824b587 --- /dev/null +++ b/tests/std/tests/GH_003003_format_decimal_point/env.lst @@ -0,0 +1,4 @@ +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +RUNALL_INCLUDE ..\concepts_20_matrix.lst diff --git a/tests/std/tests/GH_003003_format_decimal_point/test.cpp b/tests/std/tests/GH_003003_format_decimal_point/test.cpp new file mode 100644 index 0000000000..1b5d1e99f9 --- /dev/null +++ b/tests/std/tests/GH_003003_format_decimal_point/test.cpp @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include +#include + +int main() { + assert(std::format("{:#.0}", 0.0) == "0."); + assert(std::format("{:#.1}", 0.0) == "0."); + assert(std::format("{:#.2}", 0.0) == "0."); + + assert(std::format("{:#.0}", 1200.0) == "1.e+03"); + assert(std::format("{:#.1}", 1200.0) == "1.e+03"); + assert(std::format("{:#.2}", 1200.0) == "1.2e+03"); + assert(std::format("{:#.3}", 1200.0) == "1.2e+03"); + assert(std::format("{:#.4}", 1200.0) == "1200."); + assert(std::format("{:#.5}", 1200.0) == "1200."); + assert(std::format("{:#.6}", 1200.0) == "1200."); + + assert(std::format("{:#.0}", 0.123) == "0.1"); + assert(std::format("{:#.1}", 0.123) == "0.1"); + assert(std::format("{:#.2}", 0.123) == "0.12"); + assert(std::format("{:#.3}", 0.123) == "0.123"); + assert(std::format("{:#.4}", 0.123) == "0.123"); + assert(std::format("{:#.5}", 0.123) == "0.123"); + + assert(std::format("{:#.0}", 10.1) == "1.e+01"); + assert(std::format("{:#.1}", 10.1) == "1.e+01"); + assert(std::format("{:#.2}", 10.1) == "10."); + assert(std::format("{:#.3}", 10.1) == "10.1"); + assert(std::format("{:#.4}", 10.1) == "10.1"); + assert(std::format("{:#.5}", 10.1) == "10.1"); +}