[PATCH] Explicitly convert type to double to avoid losing precision

Egor Makrushin posted 1 patch 1 month ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20240123092834.14371-1-emakrushin@astralinux.ru
examples/c/domain/domtop.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] Explicitly convert type to double to avoid losing precision
Posted by Egor Makrushin 1 month ago
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>
---
 examples/c/domain/domtop.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/examples/c/domain/domtop.c b/examples/c/domain/domtop.c
index dd35ec8661..65663a1c39 100644
--- a/examples/c/domain/domtop.c
+++ b/examples/c/domain/domtop.c
@@ -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)
-- 
2.30.2
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org
Re: [PATCH] Explicitly convert type to double to avoid losing precision
Posted by Martin Kletzander 1 month ago
On Tue, Jan 23, 2024 at 12:28:34PM +0300, Egor Makrushin wrote:
>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>

Thanks for the patch.

>---
> examples/c/domain/domtop.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
>diff --git a/examples/c/domain/domtop.c b/examples/c/domain/domtop.c
>index dd35ec8661..65663a1c39 100644
>--- a/examples/c/domain/domtop.c
>+++ b/examples/c/domain/domtop.c
>@@ -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)
>-- 
>2.30.2
>_______________________________________________
>Devel mailing list -- devel@lists.libvirt.org
>To unsubscribe send an email to devel-leave@lists.libvirt.org
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org