From: Frédéric Marchal Date: Fri, 5 Feb 2010 10:14:17 +0000 (+0000) Subject: Fix rounding errors and divisions by zero in percentages X-Git-Tag: v2_2_7~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8de54acbaac001f177961db88e8a73f2f1db255d;p=thirdparty%2Fsarg.git Fix rounding errors and divisions by zero in percentages --- diff --git a/html.c b/html.c index 036c03c..d26e8b0 100644 --- a/html.c +++ b/html.c @@ -329,27 +329,22 @@ void htmlrel(void) sprintf(tmsg,"%s",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; diff --git a/topuser.c b/topuser.c index 971ada0..0a9dce6 100644 --- 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));