diff --git a/src/printf/printf.c b/src/printf/printf.c index 0a4eef91..d9b01427 100644 --- a/src/printf/printf.c +++ b/src/printf/printf.c @@ -650,11 +650,20 @@ static struct scaling_factor update_normalization(struct scaling_factor sf, doub return result; } -static struct double_components get_normalized_components(bool negative, printf_size_t precision, double non_normalized, struct scaling_factor normalization) +static struct double_components get_normalized_components(bool negative, printf_size_t precision, double non_normalized, struct scaling_factor normalization, int floored_exp10) { struct double_components components; components.is_negative = negative; - components.integral = (int_fast64_t) apply_scaling(non_normalized, normalization); + double scaled = apply_scaling(non_normalized, normalization); + + bool close_to_representation_extremum = ( (-floored_exp10 + (int) precision) >= DBL_MAX_10_EXP - 1 ); + if (close_to_representation_extremum) { + // We can't have a normalization factor which also accounts for the precision, i.e. moves + // some decimal digits into the mantissa, since it's unrepresentable, or nearly unrepresentable. + // So, we'll give up early on getting extra precision... + return get_components(negative ? -scaled : scaled, precision); + } + components.integral = (int_fast64_t) scaled; double remainder = non_normalized - unapply_scaling((double) components.integral, normalization); double prec_power_of_10 = powers_of_10[precision]; struct scaling_factor account_for_precision = update_normalization(normalization, prec_power_of_10); @@ -819,6 +828,8 @@ static double log10_of_positive(double positive_number) static double pow10_of_int(int floored_exp10) { + if (floored_exp10 == 308) { return 1e308; } + if (floored_exp10 == -308) { return 1e-308; } // Compute 10^(floored_exp10) but (try to) make sure that doesn't overflow double_with_bit_access dwba; int exp2 = bastardized_floor(floored_exp10 * 3.321928094887362 + 0.5); @@ -887,7 +898,7 @@ static void print_exponential_number(output_gadget_t* output, double number, pri struct double_components decimal_part_components = should_skip_normalization ? get_components(negative ? -abs_number : abs_number, precision) : - get_normalized_components(negative, precision, abs_number, normalization); + get_normalized_components(negative, precision, abs_number, normalization, floored_exp10); // Account for roll-over, e.g. rounding from 9.99 to 100.0 - which effects // the exponent and may require additional tweaking of the parts diff --git a/test/test_suite.cpp b/test/test_suite.cpp index 6c98350d..39bc91e3 100644 --- a/test/test_suite.cpp +++ b/test/test_suite.cpp @@ -1012,6 +1012,9 @@ TEST_CASE("floating-point specifiers, precision and flags", "[]" ) { PRINTING_CHECK("-4e+04", ==, sprintf_, buffer, "%.1g", -40661.5); PRINTING_CHECK("-4.e+04", ==, sprintf_, buffer, "%#.1g", -40661.5); PRINTING_CHECK("100.", ==, sprintf_, buffer, "%#.3g", 99.998580932617187500); + // Ensure high precision does make us exceed the representation threshold + // internally (e.g. with normalization factors and such + PRINTING_CHECK("1.2345678901e-308", ==, sprintf_, buffer, "%.10e", 1.2345678901e-308); // Rounding-focused checks PRINTING_CHECK("4.895512e+04", ==, sprintf_, buffer, "%e", 48955.125); PRINTING_CHECK("9.2524e+04", ==, sprintf_, buffer, "%.4e", 92523.5);