summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorultramage <ultramage@54d463be-8e91-2dee-dedb-b68131a5f0ec>2008-05-01 12:14:06 +0000
committerultramage <ultramage@54d463be-8e91-2dee-dedb-b68131a5f0ec>2008-05-01 12:14:06 +0000
commit5524938f8650e659b3c838df44212de56bf72584 (patch)
tree9696c2ef858b4ebd81eda804389f8ece788a994b /src/common
parent3bba210cc5151bfc2971be296ab5c4391ea28639 (diff)
downloadhercules-5524938f8650e659b3c838df44212de56bf72584.tar.gz
hercules-5524938f8650e659b3c838df44212de56bf72584.tar.bz2
hercules-5524938f8650e659b3c838df44212de56bf72584.tar.xz
hercules-5524938f8650e659b3c838df44212de56bf72584.zip
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
Diffstat (limited to 'src/common')
-rw-r--r--src/common/utils.c16
-rw-r--r--src/common/utils.h3
2 files changed, 19 insertions, 0 deletions
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
diff --git a/src/common/utils.h b/src/common/utils.h
index 21e8e090c..93563dc34 100644
--- a/src/common/utils.h
+++ b/src/common/utils.h
@@ -18,6 +18,9 @@ void findfile(const char *p, const char *pat, void (func)(const char*));
//Caps values to min/max
#define cap_value(a, min, max) ((a >= max) ? max : (a <= min) ? min : a)
+/// calculates the value of A / B, in percent (rounded down)
+unsigned int get_percentage(const unsigned int A, const unsigned int B);
+
//////////////////////////////////////////////////////////////////////////
// byte word dword access [Shinomori]
//////////////////////////////////////////////////////////////////////////