]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Fix] Add rounding when printing a lot of FP variables
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 23 Mar 2018 13:47:58 +0000 (13:47 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 23 Mar 2018 13:47:58 +0000 (13:47 +0000)
src/libserver/symbols_cache.c
src/libutil/str_util.c

index 060f3875f494d82ad7c12103a8aa09a6701548d8..ecc569573fd8b6de91dbef0261a7373374fd7c1d 100644 (file)
@@ -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)
 {
index 570f9a94815864c972414422d8da4739481df5e7..8026ea7e5629e60c33cae5221c3b12f987f1d389 100644 (file)
@@ -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 {