Skip to content

Commit

Permalink
Workaround a double-double hexfloat format (#3366)
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Shchapov <[email protected]>
  • Loading branch information
phprus authored Apr 1, 2023
1 parent bce8d4e commit 97aedea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 7 additions & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3344,7 +3344,7 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> value,
}

// Formats a floating-point number using the hexfloat format.
template <typename Float>
template <typename Float, FMT_ENABLE_IF(!is_double_double<Float>::value)>
FMT_CONSTEXPR20 void format_hexfloat(Float value, int precision,
float_specs specs, buffer<char>& buf) {
// float is passed as double to reduce the number of instantiations and to
Expand Down Expand Up @@ -3425,6 +3425,12 @@ FMT_CONSTEXPR20 void format_hexfloat(Float value, int precision,
format_decimal<char>(appender(buf), abs_e, detail::count_digits(abs_e));
}

template <typename Float, FMT_ENABLE_IF(is_double_double<Float>::value)>
FMT_CONSTEXPR20 void format_hexfloat(Float value, int precision,
float_specs specs, buffer<char>& buf) {
format_hexfloat(static_cast<double>(value), precision, specs, buf);
}

template <typename Float>
FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs,
buffer<char>& buf) -> int {
Expand Down
11 changes: 9 additions & 2 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1489,8 +1489,15 @@ TEST(format_test, format_long_double) {
safe_sprintf(buffer, "%Le", 392.65l);
EXPECT_EQ(buffer, fmt::format("{0:e}", 392.65l));
EXPECT_EQ("+0000392.6", fmt::format("{0:+010.4g}", 392.64l));
safe_sprintf(buffer, "%La", 3.31l);
EXPECT_EQ(buffer, fmt::format("{:a}", 3.31l));

auto ld = 3.31l;
if (fmt::detail::is_double_double<decltype(ld)>::value) {
safe_sprintf(buffer, "%a", static_cast<double>(ld));
EXPECT_EQ(buffer, fmt::format("{:a}", ld));
} else {
safe_sprintf(buffer, "%La", ld);
EXPECT_EQ(buffer, fmt::format("{:a}", ld));
}
}

TEST(format_test, format_char) {
Expand Down

0 comments on commit 97aedea

Please sign in to comment.