]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Explicitly convert type to double to avoid losing precision
authorEgor Makrushin <emakrushin@astralinux.ru>
Tue, 23 Jan 2024 09:28:34 +0000 (12:28 +0300)
committerMartin Kletzander <mkletzan@redhat.com>
Wed, 24 Jan 2024 09:21:23 +0000 (10:21 +0100)
Division between integers will also be integer.
Thus, to preserve fractional part explicitly
convert first operand to double.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 28d54aab05 ("examples: Introduce domtop")
Signed-off-by: Egor Makrushin <emakrushin@astralinux.ru>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
examples/c/domain/domtop.c

index dd35ec8661df264d481a05b9ea67708c4e1bf5e2..65663a1c392b95a8031da6196972828578cb8545 100644 (file)
@@ -224,7 +224,7 @@ print_cpu_usage(size_t cpu,
          * performed has a bad effect on the precision, so instead of dividing
          * @now_params and @then_params by 1000 and then multiplying again by
          * 100, we divide only once by 10 and get the same result. */
-        usage = (now_params[pos].value.ul - then_params[pos].value.ul) /
+        usage = (double)(now_params[pos].value.ul - then_params[pos].value.ul) /
                 (now - then) / 10;
 
         if (delim)