Skip to content

Commit

Permalink
player/command: change how floating point number are printed
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kasper93 committed Oct 1, 2023
1 parent 1cda3d2 commit e88c4d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 5 additions & 5 deletions options/m_option.c
Original file line number Diff line number Diff line change
Expand Up @@ -1023,12 +1023,12 @@ static char *print_double(const m_option_t *opt, const void *val)
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);
if (isnan(f))
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)
Expand Down Expand Up @@ -1100,7 +1100,7 @@ const m_option_type_t m_option_type_double = {
.size = sizeof(double),
.parse = parse_double,
.print = print_double,
.pretty_print = print_double_f3,
.pretty_print = print_double_7g,
.copy = copy_opt,
.add = add_double,
.multiply = multiply_double,
Expand All @@ -1126,7 +1126,7 @@ const m_option_type_t m_option_type_aspect = {
.flags = M_OPT_TYPE_CHOICE | M_OPT_TYPE_USES_RANGE,
.parse = parse_double_aspect,
.print = print_double,
.pretty_print = print_double_f3,
.pretty_print = print_double_7g,
.copy = copy_opt,
.add = add_double,
.multiply = multiply_double,
Expand Down Expand Up @@ -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)
{
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)
Expand Down
8 changes: 6 additions & 2 deletions player/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ static int mp_property_av_speed_correction(void *ctx, struct m_property *prop,
}

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;
}

Expand Down Expand Up @@ -652,7 +652,11 @@ static int mp_property_avsync(void *ctx, struct m_property *prop,
if (!mpctx->ao_chain || !mpctx->vo_chain)
return M_PROPERTY_UNAVAILABLE;
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_double_ro(action, arg, mpctx->last_av_difference);
Expand Down

0 comments on commit e88c4d3

Please sign in to comment.