diff options
author | ultramage <ultramage@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2008-05-01 13:56:24 +0000 |
---|---|---|
committer | ultramage <ultramage@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2008-05-01 13:56:24 +0000 |
commit | b1508637e414820d615888a11baed170392154d7 (patch) | |
tree | b7900e1469a6b03f543049af58202d5000cf2165 /src/common/utils.c | |
parent | 5524938f8650e659b3c838df44212de56bf72584 (diff) | |
download | hercules-b1508637e414820d615888a11baed170392154d7.tar.gz hercules-b1508637e414820d615888a11baed170392154d7.tar.bz2 hercules-b1508637e414820d615888a11baed170392154d7.tar.xz hercules-b1508637e414820d615888a11baed170392154d7.zip |
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
Diffstat (limited to 'src/common/utils.c')
-rw-r--r-- | src/common/utils.c | 22 |
1 files changed, 15 insertions, 7 deletions
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 <stdarg.h> #include <stdlib.h> #include <string.h> +#include <math.h> // floor() #ifdef WIN32 #include <windows.h> @@ -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); +} |