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