Add IntegerDivisionRoundingAwayFromZero.

This provides the mirror of the integer division operator, which rounds towards zero.
This commit is contained in:
RoosterDragon
2016-01-09 15:29:49 +00:00
parent 8b12a4a747
commit 8ff08d4e19

View File

@@ -327,6 +327,15 @@ namespace OpenRA
return root;
}
public static int IntegerDivisionRoundingAwayFromZero(int dividend, int divisor)
{
int remainder;
var quotient = Math.DivRem(dividend, divisor, out remainder);
if (remainder == 0)
return quotient;
return quotient + (Math.Sign(dividend) == Math.Sign(divisor) ? 1 : -1);
}
public static string JoinWith<T>(this IEnumerable<T> ts, string j)
{
return string.Join(j, ts);