]> git.proxmox.com Git - mirror_qemu.git/commitdiff
qapi: String visitor, use %f representation for floats
authorMichael Roth <mdroth@linux.vnet.ibm.com>
Mon, 30 Apr 2012 14:33:30 +0000 (09:33 -0500)
committerAndreas Färber <afaerber@suse.de>
Fri, 8 Jun 2012 14:11:14 +0000 (16:11 +0200)
Currently string-output-visitor formats floats as %g, which is nice in
that trailing 0's are automatically truncated, but otherwise this causes
some issues:

 - it uses 6 significant figures instead of 6 decimal places, which
   means something like 155777.5 (which even has an exact floating point
   representation) will be rounded to 155778 when converted to a string.

 - output will be presented in scientific notation when the normalized
   form requires a 10^x multiplier. Not a huge deal, but arguably less
   readable for command-line arguments.

 - due to using scientific notation for numbers requiring more than 6
   significant figures, instead of hard-defined decimal places, it
   fails a lot of the test-visitor-serialization unit tests for floats.

Instead, let's just use %f, which is what the QJSON and the QMP visitors
use.

Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
Signed-off-by: Andreas Färber <afaerber@suse.de>
qapi/string-output-visitor.c
tests/test-string-output-visitor.c

index 92b03052127b668828af758f05b8d6fcaeea3001..34e525eadd00bb3e2c148804ee47966734bb1e30 100644 (file)
@@ -52,7 +52,7 @@ static void print_type_number(Visitor *v, double *obj, const char *name,
                               Error **errp)
 {
     StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v);
-    string_output_set(sov, g_strdup_printf("%g", *obj));
+    string_output_set(sov, g_strdup_printf("%f", *obj));
 }
 
 char *string_output_get_string(StringOutputVisitor *sov)
index 22909b84efe483c7fcca4a5b812d92573b00f637..608f14a5deb313edde6977af9cc876b9e407ffe2 100644 (file)
@@ -84,7 +84,7 @@ static void test_visitor_out_number(TestOutputVisitorData *data,
 
     str = string_output_get_string(data->sov);
     g_assert(str != NULL);
-    g_assert_cmpstr(str, ==, "3.14");
+    g_assert_cmpstr(str, ==, "3.140000");
     g_free(str);
 }