Skip to content

Commit

Permalink
<format>: fix std::format("{:#.precision}", floating) (#3815)
Browse files Browse the repository at this point in the history
Co-authored-by: A. Jiang <de34@live.cn>
Co-authored-by: Stephan T. Lavavej <stl@nuwen.net>
  • Loading branch information
3 people committed Jun 22, 2023
1 parent 4768199 commit 057adf2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
10 changes: 4 additions & 6 deletions stl/inc/format
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -2904,8 +2903,7 @@ _NODISCARD _OutputIt _Fmt_write(
if (_Precision == -1) {
_Precision = 6;
}
_Format = chars_format::general;
_Exponent = 'e';
_Format = chars_format::general;
break;
}

Expand Down Expand Up @@ -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<int>(_Exponent_start - _Buffer_start);

if (!_Append_decimal) {
Expand Down
4 changes: 0 additions & 4 deletions tests/libcxx/expected_results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/std/tests/GH_003003_format_decimal_point/env.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

RUNALL_INCLUDE ..\concepts_20_matrix.lst
33 changes: 33 additions & 0 deletions tests/std/tests/GH_003003_format_decimal_point/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <cassert>
#include <format>

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");
}

0 comments on commit 057adf2

Please sign in to comment.