qemu/qobject
Markus Armbruster f917eed306 qobject: Fix qnum_to_string() to use sufficient precision
We should serialize numbers to JSON so that they deserialize back to
the same number.  We fail to do so.

The culprit is qnum_to_string(): it uses format %f with trailing '0'
trimmed.  Results in pretty output for "nice" numbers, but is prone to
nasty rounding errors.  For instance, numbers between 0 and 0.0000005
get flushed to zero.

Where exactly the incorrect rounding can bite is tiresome to gauge.
Here's my take.

* In QMP output, type 'number':

  - query-blockstats value avg_rd_queue_depth

  - QMP query-migrate values mbps, cache-miss-rate, encoding-rate,
    busy-rate, compression-rate.

  Relatively harmless, I guess.

* In tracing QMP input.  Harmless.

* In qemu-ga output, type 'number': guest-get-users value login-time.
  Harmless.

* In output of HMP qom-get.  Harmless.

Not affected, because double values don't actually occur there (I
think):

* QMP output, type 'any':

  * qom-get value

  * qom-list, qom-list-properties value default-value

  * query-cpu-model-comparison, query-cpu-model-baseline,
    query-cpu-model-expansion value props.

* qemu-img --output json output.

* "json:" pseudo-filenames generated by bdrv_refresh_filename().

* The rbd block driver's "=keyvalue-pairs" hack.

* In -object help on property default values.  Aside: use of JSON
  feels inappropriate here.

* Output of HMP qom-get.

* Argument conversion to QemuOpts for qdev_device_add() and HMP with
  qemu_opts_from_qdict()

  QMP and HMP device_add, virtio-net failover primary creation,
  xen-usb "usb-host" creation, HMP netdev_add, object_add.

* The uses of qobject_input_visitor_new_flat_confused()

  As far as I can tell, none of the visited types contain double
  values.

* Dumping ImageInfoSpecific with dump_qobject()

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

The change to expected test output illustrates the effect: the
rounding errors are gone, but some seemingly "nice" numbers now get
converted to not so nice strings, e.g. 0.42 to "0.41999999999999998".
This is because 0.42 is not representable exactly in double.  It's
more accurate in this example than strictly necessary, though.

If ugly accuracy bothers us, we can we can try using the least number
of digits that still converts back to the same double.  In this
example, "0.42" would do.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20201210161452.2813491-7-armbru@redhat.com>
2020-12-19 10:37:16 +01:00
..
block-qdict.c qobject: Catch another straggler for use of qdict_put_str() 2018-10-26 17:17:32 +02:00
json-lexer.c json: Fix off-by-one assert check in next_state() 2019-03-26 08:10:11 +01:00
json-parser-int.h json: Eliminate lexer state IN_WHITESPACE, pseudo-token JSON_SKIP 2018-09-24 18:08:07 +02:00
json-parser.c json: Fix a memleak in parse_pair() 2020-11-17 15:39:53 +01:00
json-streamer.c json: Fix check for unbalanced right curly brace 2020-04-07 13:10:11 +02:00
meson.build libqemuutil, qapi, trace: convert to meson 2020-08-21 06:30:08 -04:00
qbool.c qobject: Drop superfluous includes of qemu-common.h 2018-08-24 20:26:37 +02:00
qdict.c qemu/: fix some comment spelling errors 2020-09-17 20:35:43 +02:00
qjson.c qobject: Eliminate qdict_iter(), use qdict_first(), qdict_next() 2020-04-30 06:51:15 +02:00
qlist.c qobject: Eliminate qlist_iter(), use QLIST_FOREACH_ENTRY() instead 2020-04-30 06:51:15 +02:00
qlit.c qapi: Replace qobject_to_X(o) by qobject_to(X, o) 2018-03-19 14:58:36 -05:00
qnull.c qobject: Drop superfluous includes of qemu-common.h 2018-08-24 20:26:37 +02:00
qnum.c qobject: Fix qnum_to_string() to use sufficient precision 2020-12-19 10:37:16 +01:00
qobject.c qobject: Drop superfluous includes of qemu-common.h 2018-08-24 20:26:37 +02:00
qstring.c qstring: add qstring_free() 2020-01-24 20:59:13 +01:00