From c3eb6a173e5076a19ad1cf03653b8cc6b610e2c5 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 23 Mar 2018 13:47:58 +0000 Subject: [PATCH] [Fix] Add rounding when printing a lot of FP variables --- src/libserver/symbols_cache.c | 46 +++++++++++++++++++++++++---------- src/libutil/str_util.c | 8 +++--- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/libserver/symbols_cache.c b/src/libserver/symbols_cache.c index 060f3875f..ecc569573 100644 --- a/src/libserver/symbols_cache.c +++ b/src/libserver/symbols_cache.c @@ -653,6 +653,8 @@ rspamd_symbols_cache_load_items (struct symbols_cache *cache, const gchar *name) return TRUE; } +#define ROUND_DOUBLE(x) (floor((x) * 100.0) / 100.0) + static gboolean rspamd_symbols_cache_save_items (struct symbols_cache *cache, const gchar *name) { @@ -695,17 +697,21 @@ rspamd_symbols_cache_save_items (struct symbols_cache *cache, const gchar *name) while (g_hash_table_iter_next (&it, &k, &v)) { item = v; elt = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (elt, ucl_object_fromdouble (item->st->weight), + ucl_object_insert_key (elt, + ucl_object_fromdouble (ROUND_DOUBLE (item->st->weight)), "weight", 0, false); - ucl_object_insert_key (elt, ucl_object_fromdouble (item->st->time_counter.mean), + ucl_object_insert_key (elt, + ucl_object_fromdouble (ROUND_DOUBLE (item->st->time_counter.mean)), "time", 0, false); - ucl_object_insert_key (elt, ucl_object_fromdouble (item->st->total_hits), + ucl_object_insert_key (elt, ucl_object_fromint (item->st->total_hits), "count", 0, false); freq = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (freq, ucl_object_fromdouble (item->st->frequency_counter.mean), + ucl_object_insert_key (freq, + ucl_object_fromdouble (ROUND_DOUBLE (item->st->frequency_counter.mean)), "avg", 0, false); - ucl_object_insert_key (freq, ucl_object_fromdouble (item->st->frequency_counter.stddev), + ucl_object_insert_key (freq, + ucl_object_fromdouble (ROUND_DOUBLE (item->st->frequency_counter.stddev)), "stddev", 0, false); ucl_object_insert_key (elt, freq, "frequency", 0, false); @@ -722,6 +728,8 @@ rspamd_symbols_cache_save_items (struct symbols_cache *cache, const gchar *name) return ret; } +#undef ROUND_DOUBLE + gint rspamd_symbols_cache_add_symbol (struct symbols_cache *cache, const gchar *name, @@ -1930,6 +1938,8 @@ struct counters_cbdata { struct symbols_cache *cache; }; +#define ROUND_DOUBLE(x) (floor((x) * 100.0) / 100.0) + static void rspamd_symbols_cache_counters_cb (gpointer v, gpointer ud) { @@ -1949,23 +1959,31 @@ rspamd_symbols_cache_counters_cb (gpointer v, gpointer ud) g_assert (item->parent < (gint)cbd->cache->items_by_id->len); parent = g_ptr_array_index (cbd->cache->items_by_id, item->parent); - ucl_object_insert_key (obj, ucl_object_fromdouble (item->st->weight), + ucl_object_insert_key (obj, + ucl_object_fromdouble (ROUND_DOUBLE (item->st->weight)), "weight", 0, false); - ucl_object_insert_key (obj, ucl_object_fromdouble (parent->st->avg_frequency), + ucl_object_insert_key (obj, + ucl_object_fromdouble (ROUND_DOUBLE (parent->st->avg_frequency)), "frequency", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint (parent->st->total_hits), + ucl_object_insert_key (obj, + ucl_object_fromint (parent->st->total_hits), "hits", 0, false); - ucl_object_insert_key (obj, ucl_object_fromdouble (parent->st->avg_time), + ucl_object_insert_key (obj, + ucl_object_fromdouble (ROUND_DOUBLE (parent->st->avg_time)), "time", 0, false); } else { - ucl_object_insert_key (obj, ucl_object_fromdouble (item->st->weight), + ucl_object_insert_key (obj, + ucl_object_fromdouble (ROUND_DOUBLE (item->st->weight)), "weight", 0, false); - ucl_object_insert_key (obj, ucl_object_fromdouble (item->st->avg_frequency), + ucl_object_insert_key (obj, + ucl_object_fromdouble (ROUND_DOUBLE (item->st->avg_frequency)), "frequency", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint (item->st->total_hits), + ucl_object_insert_key (obj, + ucl_object_fromint (item->st->total_hits), "hits", 0, false); - ucl_object_insert_key (obj, ucl_object_fromdouble (item->st->avg_time), + ucl_object_insert_key (obj, + ucl_object_fromdouble (ROUND_DOUBLE (item->st->avg_time)), "time", 0, false); } @@ -1973,6 +1991,8 @@ rspamd_symbols_cache_counters_cb (gpointer v, gpointer ud) } } +#undef ROUND_DOUBLE + ucl_object_t * rspamd_symbols_cache_counters (struct symbols_cache * cache) { diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c index 570f9a948..8026ea7e5 100644 --- a/src/libutil/str_util.c +++ b/src/libutil/str_util.c @@ -1910,16 +1910,14 @@ static int rspamd_fstring_emit_append_double (double val, void *ud) { rspamd_fstring_t **buf = ud; - const double delta = 0.0000001; +#define MAX_PRECISION 6 if (isfinite (val)) { if (val == (double) ((gint) val)) { rspamd_printf_fstring (buf, "%.1f", val); - } else if (fabs (val - (double) (int) val) < delta) { - /* Write at maximum precision */ - rspamd_printf_fstring (buf, "%.*g", DBL_DIG, val); } else { - rspamd_printf_fstring (buf, "%f", val); + rspamd_printf_fstring (buf, "%." G_STRINGIFY (MAX_PRECISION) "f", + val); } } else { -- 2.39.5