player/command: change how floating point number are printed

Use "%.7g" to show 7 significant digits. Removes the trailing zeros, and
in general makes it more readable, than fixed 3 decimal digits.

For avsync use "%+.2g" to add plus sign, similar to display-sync
values.
This commit is contained in:
Kacper Michajłow 2023-09-17 17:06:50 +02:00 committed by Niklas Haas
parent 52fc378494
commit 9dddfc4fcc
2 changed files with 12 additions and 7 deletions

View File

@ -1023,12 +1023,12 @@ static char *print_double(const m_option_t *opt, const void *val)
return talloc_asprintf(NULL, "%f", f); return talloc_asprintf(NULL, "%f", f);
} }
static char *print_double_f3(const m_option_t *opt, const void *val) static char *print_double_7g(const m_option_t *opt, const void *val)
{ {
double f = VAL(val); double f = VAL(val);
if (isnan(f)) if (isnan(f))
return print_double(opt, val); return print_double(opt, val);
return talloc_asprintf(NULL, "%.3f", f); return talloc_asprintf(NULL, "%.7g", f);
} }
static void add_double(const m_option_t *opt, void *val, double add, bool wrap) static void add_double(const m_option_t *opt, void *val, double add, bool wrap)
@ -1100,7 +1100,7 @@ const m_option_type_t m_option_type_double = {
.size = sizeof(double), .size = sizeof(double),
.parse = parse_double, .parse = parse_double,
.print = print_double, .print = print_double,
.pretty_print = print_double_f3, .pretty_print = print_double_7g,
.copy = copy_opt, .copy = copy_opt,
.add = add_double, .add = add_double,
.multiply = multiply_double, .multiply = multiply_double,
@ -1126,7 +1126,7 @@ const m_option_type_t m_option_type_aspect = {
.flags = M_OPT_TYPE_CHOICE | M_OPT_TYPE_USES_RANGE, .flags = M_OPT_TYPE_CHOICE | M_OPT_TYPE_USES_RANGE,
.parse = parse_double_aspect, .parse = parse_double_aspect,
.print = print_double, .print = print_double,
.pretty_print = print_double_f3, .pretty_print = print_double_7g,
.copy = copy_opt, .copy = copy_opt,
.add = add_double, .add = add_double,
.multiply = multiply_double, .multiply = multiply_double,
@ -1157,7 +1157,7 @@ static char *print_float(const m_option_t *opt, const void *val)
static char *print_float_f3(const m_option_t *opt, const void *val) static char *print_float_f3(const m_option_t *opt, const void *val)
{ {
double tmp = VAL(val); double tmp = VAL(val);
return print_double_f3(opt, &tmp); return print_double_7g(opt, &tmp);
} }
static void add_float(const m_option_t *opt, void *val, double add, bool wrap) static void add_float(const m_option_t *opt, void *val, double add, bool wrap)

View File

@ -419,7 +419,7 @@ static int mp_property_av_speed_correction(void *ctx, struct m_property *prop,
} }
if (action == M_PROPERTY_PRINT) { if (action == M_PROPERTY_PRINT) {
*(char **)arg = talloc_asprintf(NULL, "%+.05f%%", (val - 1) * 100); *(char **)arg = talloc_asprintf(NULL, "%+.3g%%", (val - 1) * 100);
return M_PROPERTY_OK; return M_PROPERTY_OK;
} }
@ -652,7 +652,12 @@ static int mp_property_avsync(void *ctx, struct m_property *prop,
if (!mpctx->ao_chain || !mpctx->vo_chain) if (!mpctx->ao_chain || !mpctx->vo_chain)
return M_PROPERTY_UNAVAILABLE; return M_PROPERTY_UNAVAILABLE;
if (action == M_PROPERTY_PRINT) { if (action == M_PROPERTY_PRINT) {
*(char **)arg = talloc_asprintf(NULL, "%7.3f", mpctx->last_av_difference); // Don't print small values resulting from calculation inaccuracies
if (fabs(mpctx->last_av_difference) < 1e-5) {
*(char **)arg = talloc_strdup(NULL, "0");
} else {
*(char **)arg = talloc_asprintf(NULL, "%+.2g", mpctx->last_av_difference);
}
return M_PROPERTY_OK; return M_PROPERTY_OK;
} }
return m_property_double_ro(action, arg, mpctx->last_av_difference); return m_property_double_ro(action, arg, mpctx->last_av_difference);