Skip to content

Commit 6d51c78

Browse files
committed
Cleanup FP formatting
1 parent 0f4e9d0 commit 6d51c78

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

include/fmt/format.h

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,7 +2357,7 @@ FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand,
23572357

23582358
// Numbers with exponents greater or equal to the returned value will use
23592359
// the exponential notation.
2360-
template <typename T> constexpr auto exp_upper() -> int {
2360+
template <typename T> FMT_CONSTEVAL auto exp_upper() -> int {
23612361
return std::numeric_limits<T>::digits10 != 0
23622362
? min_of(16, std::numeric_limits<T>::digits10 + 1)
23632363
: 16;
@@ -3394,20 +3394,17 @@ FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt {
33943394
if (is_constant_evaluated()) return write<Char>(out, value, format_specs());
33953395

33963396
auto s = detail::signbit(value) ? sign::minus : sign::none;
3397+
using float_type = conditional_t<sizeof(T) == sizeof(double), double, float>;
3398+
auto mask = exponent_mask<float_type>();
3399+
if ((bit_cast<decltype(mask)>(value) & mask) == mask)
3400+
return write_nonfinite<Char>(out, std::isnan(value), {}, s);
33973401

3398-
constexpr auto specs = format_specs();
3399-
using floaty = conditional_t<sizeof(T) >= sizeof(double), double, float>;
3400-
using floaty_uint = typename dragonbox::float_info<floaty>::carrier_uint;
3401-
floaty_uint mask = exponent_mask<floaty>();
3402-
if ((bit_cast<floaty_uint>(value) & mask) == mask)
3403-
return write_nonfinite<Char>(out, std::isnan(value), specs, s);
3404-
3405-
auto dec = dragonbox::to_decimal(static_cast<floaty>(value));
3402+
auto dec = dragonbox::to_decimal<float_type>(value);
34063403
int significand_size = count_digits(dec.significand);
34073404
int exp = dec.exponent + significand_size - 1;
34083405
if (use_fixed(exp, detail::exp_upper<T>())) {
34093406
return write_fixed<Char, fallback_digit_grouping<Char>>(
3410-
out, dec, significand_size, Char('.'), specs, s);
3407+
out, dec, significand_size, Char('.'), {}, s);
34113408
}
34123409

34133410
// Write value in the exponential format.
@@ -3416,11 +3413,11 @@ FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt {
34163413
to_unsigned((s != sign::none ? 1 : 0) + significand_size +
34173414
(has_decimal_point ? 1 : 0) + compute_exp_size(exp));
34183415
auto it = reserve(out, size);
3419-
if (s != sign::none) *it++ = detail::getsign<Char>(s);
3416+
if (s != sign::none) *it++ = Char('-');
34203417
// Insert a decimal point after the first digit and add an exponent.
34213418
it = write_significand(it, dec.significand, significand_size, 1,
34223419
has_decimal_point ? '.' : Char());
3423-
*it++ = static_cast<Char>('e');
3420+
*it++ = Char('e');
34243421
it = write_exponent<Char>(exp, it);
34253422
return base_iterator(out, it);
34263423
}

0 commit comments

Comments
 (0)