From 5524938f8650e659b3c838df44212de56bf72584 Mon Sep 17 00:00:00 2001 From: ultramage Date: Thu, 1 May 2008 12:14:06 +0000 Subject: Implemented get_percentage() for compact and safe calculation of percentual values. Fixed integer arithmetic overflows that were occuring in several supernovice checks (bugreport:1135). git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@12679 54d463be-8e91-2dee-dedb-b68131a5f0ec --- src/common/utils.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/common/utils.c') diff --git a/src/common/utils.c b/src/common/utils.c index 354bd3814..a9ce1389f 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -221,3 +221,19 @@ uint32 MakeDWord(uint16 word0, uint16 word1) ( (uint32)(word0 ) )| ( (uint32)(word1 << 0x10) ); } + + +/// calculates the value of A / B, in percent (rounded down) +unsigned int percent(const unsigned int A, const unsigned int B) +{ + if( B == 0 ) + { + ShowError("percent(): divison by zero!\n"); + return -1; + } + + if( A < UINT_MAX/100 ) + return 100*A/B; + else + return A/(B/100); +} \ No newline at end of file -- cgit v1.2.3-60-g2f50