]> granicus.if.org Git - procps-ng/commitdiff
vmstat: precision issues in unitConvert()
authorQin Fandong <shell_way@foxmail.com>
Wed, 18 Jan 2023 06:02:53 +0000 (17:02 +1100)
committerCraig Small <csmall@dropbear.xyz>
Wed, 18 Jan 2023 06:02:53 +0000 (17:02 +1100)
Fix conversion errors due to precision issues in function unitConvert

For example: unitConvert(98720620) will return 98720624, not 98720620.

Because we do (unsigned long)(float)98720620 in function unitConvert
and this is wrong! We should do (unsigned long)(double)98720620 here.

Signed-off-by: Craig Small <csmall@dropbear.xyz>
References:
 procps-ng/procps!75

NEWS
src/vmstat.c

diff --git a/NEWS b/NEWS
index cec971efaeab94cdbf3feb6e8f29e9d9eda3de4e..40a39e9abc7e0261760dd942f4203d767c48e8f9 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,7 @@ procps-ng-NEXT
   * vmstat: Referesh memory statistics                     Debian #1027963
   * vmstat: Fix initial si,so,bi,bo,in & cs values         issue #15
                                                            Debian #668580
+  * vmstat: Fix conversion errors due to precision         merge #75
   * w: Add --pids option                                   merge #159
   * watch: Pass through beep                               issue #104
   * watch: -r option to not re-exec on SIGWINCH            merge #125
index b4bcb8ff167db8f2b36d3e4052f1cbfb969bd5ff..eed130c47cc4096369bbfaf69e472cb68580d75f 100644 (file)
@@ -336,8 +336,8 @@ static void new_header(void)
 
 static unsigned long unitConvert(unsigned long size)
 {
-    float cvSize;
-    cvSize = (float)size / dataUnit * ((statMode == SLABSTAT) ? 1 : 1024);
+    double cvSize;
+    cvSize = (double)size / dataUnit * ((statMode == SLABSTAT) ? 1 : 1024);
     return ((unsigned long)cvSize);
 }