From b1508637e414820d615888a11baed170392154d7 Mon Sep 17 00:00:00 2001 From: ultramage Date: Thu, 1 May 2008 13:56:24 +0000 Subject: Replaced the integers+checking approach in r12679 with usage of floating point arithmetic. Applied search&replace to use the new name of the function. git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@12680 54d463be-8e91-2dee-dedb-b68131a5f0ec --- src/common/utils.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src/common') diff --git a/src/common/utils.c b/src/common/utils.c index a9ce1389f..1e77f461a 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -11,6 +11,7 @@ #include #include #include +#include // floor() #ifdef WIN32 #include @@ -224,16 +225,23 @@ uint32 MakeDWord(uint16 word0, uint16 word1) /// calculates the value of A / B, in percent (rounded down) -unsigned int percent(const unsigned int A, const unsigned int B) +unsigned int get_percentage(const unsigned int A, const unsigned int B) { + double result; + if( B == 0 ) { - ShowError("percent(): divison by zero!\n"); + ShowError("get_percentage(): divison by zero! (A=%u,B=%u)\n", A, B); return -1; } - if( A < UINT_MAX/100 ) - return 100*A/B; - else - return A/(B/100); -} \ No newline at end of file + result = 100 * ((double)A / (double)B); + + if( result > UINT_MAX ) + { + ShowError("get_percentage(): result percentage too high! (A=%u,B=%u,result=%g)", A, B, result); + return UINT_MAX; + } + + return (unsigned int)floor(result); +} -- cgit v1.2.3-70-g09d2