fx: printf: adjust default floating point precision

This commit is contained in:
2026-05-29 19:58:40 +01:00
parent 5877abd33e
commit e78241c051
+35 -40
View File
@@ -101,7 +101,7 @@
// Default precision for the floating point conversion specifiers (the C
// standard sets this at 6)
#ifndef PRINTF_DEFAULT_FLOAT_PRECISION
#define PRINTF_DEFAULT_FLOAT_PRECISION 6
#define PRINTF_DEFAULT_FLOAT_PRECISION 13
#endif
// Default choice of type to use for internal floating-point computations
@@ -623,8 +623,8 @@ static void print_integer(
} else {
do {
const char digit = (char)(value % base);
buf[len++] = (char)(digit < 10
? '0' + digit
buf[len++]
= (char)(digit < 10 ? '0' + digit
: (flags & FLAGS_UPPERCASE
? 'A'
: 'a')
@@ -698,15 +698,14 @@ static struct floating_point_components get_components(
number_.is_negative = get_sign_bit(number);
floating_point_t abs_number = (number_.is_negative) ? -number : number;
number_.integral = (int_fast64_t)abs_number;
floating_point_t scaled_remainder = (abs_number
- (floating_point_t)
number_.integral)
floating_point_t scaled_remainder
= (abs_number - (floating_point_t)number_.integral)
* powers_of_10[precision];
number_.fractional = (int_fast64_t)
scaled_remainder; // for precision == 0U, this will be 0
floating_point_t remainder = scaled_remainder
- (floating_point_t)number_.fractional;
floating_point_t remainder
= scaled_remainder - (floating_point_t)number_.fractional;
const floating_point_t one_half = (floating_point_t)0.5;
if (remainder > one_half) {
@@ -779,18 +778,18 @@ static struct scaling_factor update_normalization(
result.raw_factor = sf.raw_factor * extra_multiplicative_factor;
} else {
int factor_exp2 = get_exp2(get_bit_access(sf.raw_factor));
int extra_factor_exp2 = get_exp2(
get_bit_access(extra_multiplicative_factor));
int extra_factor_exp2
= get_exp2(get_bit_access(extra_multiplicative_factor));
// Divide the larger-exponent raw raw_factor by the smaller
if (PRINTF_ABS(factor_exp2) > PRINTF_ABS(extra_factor_exp2)) {
result.multiply = false;
result.raw_factor = sf.raw_factor
/ extra_multiplicative_factor;
result.raw_factor
= sf.raw_factor / extra_multiplicative_factor;
} else {
result.multiply = true;
result.raw_factor = extra_multiplicative_factor
/ sf.raw_factor;
result.raw_factor
= extra_multiplicative_factor / sf.raw_factor;
}
}
return result;
@@ -818,25 +817,24 @@ static struct floating_point_components get_normalized_components(
return get_components(negative ? -scaled : scaled, precision);
}
components.integral = (int_fast64_t)scaled;
floating_point_t remainder = non_normalized
floating_point_t remainder
= non_normalized
- unapply_scaling(
(floating_point_t)
components.integral,
(floating_point_t)components.integral,
normalization);
floating_point_t prec_power_of_10 = powers_of_10[precision];
struct scaling_factor account_for_precision = update_normalization(
normalization,
prec_power_of_10);
floating_point_t scaled_remainder = apply_scaling(
remainder,
account_for_precision);
struct scaling_factor account_for_precision
= update_normalization(normalization, prec_power_of_10);
floating_point_t scaled_remainder
= apply_scaling(remainder, account_for_precision);
floating_point_t rounding_threshold = 0.5;
components.fractional = (int_fast64_t)
scaled_remainder; // when precision == 0, the assigned value
// should be 0
scaled_remainder -= (floating_point_t)components
.fractional; // when precision == 0, this
scaled_remainder
-= (floating_point_t)
components.fractional; // when precision == 0, this
// will not change
// scaled_remainder
@@ -960,9 +958,8 @@ static void print_decimal_number(
char *buf,
printf_size_t len)
{
struct floating_point_components value_ = get_components(
number,
precision);
struct floating_point_components value_
= get_components(number, precision);
print_broken_up_decimal(
value_,
output,
@@ -1105,9 +1102,9 @@ static void print_exponential_number(
abs_exp10_covered_by_powers_table
= PRINTF_ABS(floored_exp10)
< PRINTF_MAX_PRECOMPUTED_POWER_OF_10;
normalization.raw_factor = abs_exp10_covered_by_powers_table
? powers_of_10[PRINTF_ABS(
floored_exp10)]
normalization.raw_factor
= abs_exp10_covered_by_powers_table
? powers_of_10[PRINTF_ABS(floored_exp10)]
: p10;
}
@@ -1120,9 +1117,8 @@ static void print_exponential_number(
bool fall_back_to_decimal_only_mode = false;
if (flags & FLAGS_ADAPT_EXP) {
int required_significant_digits = (precision == 0)
? 1
: (int)precision;
int required_significant_digits
= (precision == 0) ? 1 : (int)precision;
// Should we want to fall-back to "%f" mode, and only print the
// decimal part?
fall_back_to_decimal_only_mode
@@ -1133,10 +1129,10 @@ static void print_exponential_number(
// "%g" mode, "precision" is the number of _significant digits_,
// and this is when we "translate" the precision value to an
// actual number of decimal digits.
int precision_ = fall_back_to_decimal_only_mode
int precision_
= fall_back_to_decimal_only_mode
? (int)precision - 1 - floored_exp10
: (int)precision
- 1; // the presence of the
: (int)precision - 1; // the presence of the
// exponent ensures only
// one significant digit
// comes before the
@@ -1573,9 +1569,8 @@ static inline void format_string_loop(
if (flags & FLAGS_LONG_LONG) {
#if PRINTF_SUPPORT_LONG_LONG
const long long value = va_arg(
args,
long long);
const long long value
= va_arg(args, long long);
print_integer(
output,
ABS_FOR_PRINTING(value),