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

Workaround a double-double hexfloat format #3366

Merged
merged 1 commit into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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