Math Lib: add divide_floor_i

Integer division that floors on negative output (like Python's).
This commit is contained in:
2017-09-18 13:14:58 +10:00
parent 9134529b9e
commit 990515a5a7

View File

@@ -193,6 +193,17 @@ MINLINE int divide_round_i(int a, int b)
return (2 * a + b) / (2 * b);
}
/**
* Integer division that floors negative result.
* \note This works like Python's int division.
*/
MINLINE int divide_floor_i(int a, int b)
{
int d = a / b;
int r = a % b; /* Optimizes into a single division. */
return r ? d - ((a < 0) ^ (b < 0)) : d;
}
/**
* modulo that handles negative numbers, works the same as Python's.
*/