瀏覽代碼

string-output-visitor: Fix to use sufficient precision

The string output visitor should serialize numbers so that the string
input visitor deserializes them back to the same number.  It fails to
do so.

print_type_number() uses format %f.  This is prone to nasty rounding
errors.  For instance, numbers between 0 and 0.0000005 get flushed to
zero.

We currently use this visitor only for HMP info migrate, info network,
info qtree, and info memdev.  No double values occur there as far as I
can tell.

Fix anyway by formatting with %.17g.  17 decimal digits always suffice
for IEEE double.

See also recent commit "qobject: Fix qnum_to_string() to use
sufficient precision".

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20201210161452.2813491-9-armbru@redhat.com>
Markus Armbruster 4 年之前
父節點
當前提交
54addb01d8
共有 2 個文件被更改,包括 2 次插入2 次删除
  1. 1 1
      qapi/string-output-visitor.c
  2. 1 1
      tests/test-string-output-visitor.c

+ 1 - 1
qapi/string-output-visitor.c

@@ -258,7 +258,7 @@ static bool print_type_number(Visitor *v, const char *name, double *obj,
                               Error **errp)
                               Error **errp)
 {
 {
     StringOutputVisitor *sov = to_sov(v);
     StringOutputVisitor *sov = to_sov(v);
-    string_output_set(sov, g_strdup_printf("%f", *obj));
+    string_output_set(sov, g_strdup_printf("%.17g", *obj));
     return true;
     return true;
 }
 }
 
 

+ 1 - 1
tests/test-string-output-visitor.c

@@ -136,7 +136,7 @@ static void test_visitor_out_number(TestOutputVisitorData *data,
     visit_type_number(data->ov, NULL, &value, &error_abort);
     visit_type_number(data->ov, NULL, &value, &error_abort);
 
 
     str = visitor_get(data);
     str = visitor_get(data);
-    g_assert_cmpstr(str, ==, "3.141593");
+    g_assert_cmpstr(str, ==, "3.1415926535897931");
 }
 }
 
 
 static void test_visitor_out_string(TestOutputVisitorData *data,
 static void test_visitor_out_string(TestOutputVisitorData *data,