]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Fix rounding errors and divisions by zero in percentages
authorFrédéric Marchal <fmarchal@users.sourceforge.net>
Fri, 5 Feb 2010 10:14:17 +0000 (10:14 +0000)
committerFrédéric Marchal <fmarchal@users.sourceforge.net>
Fri, 5 Feb 2010 10:14:17 +0000 (10:14 +0000)
html.c
topuser.c

diff --git a/html.c b/html.c
index 036c03cfd5bb2a012066b0f1e63f05450aeafcb2..d26e8b02239c30a29fe2d91a7506fb5a262ee76b 100644 (file)
--- a/html.c
+++ b/html.c
@@ -329,27 +329,22 @@ void htmlrel(void)
             sprintf(tmsg,"<td class=\"data\">%s</td>",text[46]);
          else bzero(tmsg, 50);
 
-         if(nnbytes) {
-            perc=nnbytes * 100;
-            perc=perc / tnbytes;
+         if(tnbytes) {
+            perc=nnbytes * 100. / tnbytes;
          } else {
             perc=0;
-            ouperc=0;
          }
 
-         if(nnelap) {
-            perc2=nnelap * 100;
-            perc2=perc2 / tnelap;
+         if(tnelap) {
+            perc2=nnelap * 100. / tnelap;
          } else perc2=0;
 
-         if(incache) {
-            inperc=incache * 100;
-            inperc=inperc / nnbytes;
+         if(nnbytes) {
+            inperc=incache * 100. / nnbytes;
          } else inperc=0;
 
-         if(oucache) {
-            ouperc=oucache * 100;
-            ouperc=ouperc / nnbytes;
+         if(nnbytes) {
+            ouperc=oucache * 100. / nnbytes;
          } else ouperc=0;
 
          strcpy(wwork1,fixnum(twork,1));
@@ -547,24 +542,20 @@ void htmlrel(void)
          unlink(arqip);
       unlink(arqin);
 
-      if(tnbytes) {
-         perc=totbytes / 100;
-         perc=tnbytes / perc;
+      if(totbytes) {
+         perc=tnbytes *100. / totbytes;
       } else perc=0;
 
-      if(tnelap) {
-         perc2=totelap / 100;
-         perc2=tnelap / perc2;
+      if(totelap) {
+         perc2=tnelap *100. / totelap;
       } else perc2=0;
 
       if(tnoucache) {
-         ouperc=tnoucache * 100;
-         ouperc=ouperc / tnbytes;
+         ouperc=tnoucache * 100. / tnbytes;
       } else ouperc=0;
 
       if(tnincache) {
-         inperc=tnincache * 100;
-         inperc=inperc / tnbytes;
+         inperc=tnincache * 100. / tnbytes;
       } else inperc=0;
 
       sprintf(wwork1,"%s",fixnum(tnacc,1));
@@ -685,14 +676,12 @@ void htmlrel(void)
          totbytes2=totbytes/ntotuser;
          totelap2=totelap/ntotuser;
 
-         if(totbytes2) {
-            perc = totbytes / 100;
-            perc = totbytes2 / perc;
+         if(totbytes) {
+            perc = totbytes2 * 100. / totbytes;
          } else perc=0;
 
-         if(totelap2) {
-            perc2 = totelap / 100;
-            perc2 = totelap2 / perc2;
+         if(totelap) {
+            perc2 = totelap2 * 100. / totelap;
          } else perc2=0;
 
          twork2=ntotuser;
index 971ada06a83048f20d442968bfa34c10f89d0dcf..0a9dce6a467cc2aa40a2089f6cc2c27a1226c71c 100644 (file)
--- a/topuser.c
+++ b/topuser.c
@@ -406,24 +406,20 @@ void topuser(void)
       strcpy(user2,user);
       tnbytes=nbytes;
 
-      if(tnbytes) {
-         perc=tnbytes * 100;
-         perc=perc / ttnbytes;
+      if(ttnbytes) {
+         perc=tnbytes * 100. / ttnbytes;
       } else perc = 0;
 
-      if(elap) {
-         perc2=elap;
-         perc2=((perc2 * 100) / ttnelap);
+      if(ttnelap) {
+         perc2=elap * 100. / ttnelap;
       } else perc2 = 0;
 
-      if(incac) {
-         inperc=incac;
-         inperc=((inperc * 100) / tnbytes);
+      if(tnbytes) {
+         inperc=incac * 100. / tnbytes;
       } else inperc = 0;
 
-      if(oucac) {
-         ouperc=oucac;
-         ouperc=((ouperc * 100) / tnbytes);
+      if(tnbytes) {
+         ouperc=oucac * 100. / tnbytes;
       } else ouperc = 0;
 
       if(strcmp(user,"TOTAL") != 0){
@@ -567,14 +563,12 @@ void topuser(void)
 
       if(strstr(user,"TOTAL") != 0) {
 
-         if(incac) {
-            inperc=ttnbytes / 100;
-            inperc=incac / inperc;
+         if(ttnbytes) {
+            inperc=incac * 100. / ttnbytes;
          } else inperc = 0;
 
-         if(oucac) {
-            ouperc=ttnbytes / 100;
-            ouperc=oucac / ouperc;
+         if(ttnbytes) {
+            ouperc=oucac * 100. / ttnbytes;
          } else ouperc = 0;
 
          sprintf(wwork1,"%s",fixnum(ttnacc,1));
@@ -635,8 +629,13 @@ void topuser(void)
    if(ttnbytes) tnbytes=ttnbytes / totuser;
    else tnbytes=0;
 
-   twork=ttnacc/totuser;
-   twork2=ttnelap/totuser;
+   if (totuser>0) {
+      twork=ttnacc/totuser;
+      twork2=ttnelap/totuser;
+   } else {
+      twork=0;
+      twork2=0;
+   }
    strcpy(wwork1,fixnum(twork,1));
    strcpy(wwork2,fixnum(tnbytes,1));
    strcpy(wwork3,fixnum2(twork2,1));