From 6d51c78c1e35650602a33d73cbd4af3900b1cfae Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 30 May 2025 16:41:03 -0700 Subject: [PATCH] Cleanup FP formatting --- include/fmt/format.h | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 4cdea2ce..90fe56d7 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2357,7 +2357,7 @@ FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand, // Numbers with exponents greater or equal to the returned value will use // the exponential notation. -template constexpr auto exp_upper() -> int { +template FMT_CONSTEVAL auto exp_upper() -> int { return std::numeric_limits::digits10 != 0 ? min_of(16, std::numeric_limits::digits10 + 1) : 16; @@ -3394,20 +3394,17 @@ FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt { if (is_constant_evaluated()) return write(out, value, format_specs()); auto s = detail::signbit(value) ? sign::minus : sign::none; + using float_type = conditional_t; + auto mask = exponent_mask(); + if ((bit_cast(value) & mask) == mask) + return write_nonfinite(out, std::isnan(value), {}, s); - constexpr auto specs = format_specs(); - using floaty = conditional_t= sizeof(double), double, float>; - using floaty_uint = typename dragonbox::float_info::carrier_uint; - floaty_uint mask = exponent_mask(); - if ((bit_cast(value) & mask) == mask) - return write_nonfinite(out, std::isnan(value), specs, s); - - auto dec = dragonbox::to_decimal(static_cast(value)); + auto dec = dragonbox::to_decimal(value); int significand_size = count_digits(dec.significand); int exp = dec.exponent + significand_size - 1; if (use_fixed(exp, detail::exp_upper())) { return write_fixed>( - out, dec, significand_size, Char('.'), specs, s); + out, dec, significand_size, Char('.'), {}, s); } // Write value in the exponential format. @@ -3416,11 +3413,11 @@ FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt { to_unsigned((s != sign::none ? 1 : 0) + significand_size + (has_decimal_point ? 1 : 0) + compute_exp_size(exp)); auto it = reserve(out, size); - if (s != sign::none) *it++ = detail::getsign(s); + if (s != sign::none) *it++ = Char('-'); // Insert a decimal point after the first digit and add an exponent. it = write_significand(it, dec.significand, significand_size, 1, has_decimal_point ? '.' : Char()); - *it++ = static_cast('e'); + *it++ = Char('e'); it = write_exponent(exp, it); return base_iterator(out, it); }