summaryrefslogtreecommitdiff
path: root/src/core/math_func.hpp
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2010-04-18 17:13:01 +0000
committerfrosch <frosch@openttd.org>2010-04-18 17:13:01 +0000
commitaf7051178de9f4b3eccbf9a4c0bd347e12a1f9dd (patch)
tree33dd7c5f98fa5caeffe3f8c477ef3a8da1d7c667 /src/core/math_func.hpp
parent2e90f7f8b975d380c3d544995ef6db9d6c8a86d8 (diff)
downloadopenttd-af7051178de9f4b3eccbf9a4c0bd347e12a1f9dd.tar.xz
(svn r19671) -Fix (r19670): RoundDiv() needs to deal with signed numerators.
Diffstat (limited to 'src/core/math_func.hpp')
-rw-r--r--src/core/math_func.hpp12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/core/math_func.hpp b/src/core/math_func.hpp
index 8f31dd273..19f2f53d6 100644
--- a/src/core/math_func.hpp
+++ b/src/core/math_func.hpp
@@ -330,14 +330,20 @@ static FORCEINLINE uint CeilDiv(uint a, uint b)
}
/**
- * Computes round(a / b) for non-negative a and b.
+ * Computes round(a / b) for signed a and unsigned b.
* @param a Numerator
* @param b Denominator
* @return Quotient, rounded to nearest
*/
-static FORCEINLINE uint RoundDiv(uint a, uint b)
+static FORCEINLINE int RoundDivSU(int a, uint b)
{
- return (a + b / 2) / b;
+ if (a > 0) {
+ /* 0.5 is rounded to 1 */
+ return (a + (int)b / 2) / (int)b;
+ } else {
+ /* -0.5 is rounded to 0 */
+ return (a - ((int)b - 1) / 2) / (int)b;
+ }
}
#endif /* MATH_FUNC_HPP */