Skip to content

Commit

Permalink
Generify FloorMod and FloorDiv
Browse files Browse the repository at this point in the history
Co-authored-by: Tyler Veness <[email protected]>
  • Loading branch information
SamCarlberg and calcmogul authored May 30, 2024
1 parent 0e83f42 commit a0ff389
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions wpimath/src/main/native/include/frc/MathUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,14 @@ constexpr units::radian_t AngleModulus(units::radian_t angle) {
* @return the largest (closest to positive infinity)
* {@code int} value that is less than or equal to the algebraic quotient.
*/
constexpr static int FloorDiv(int x, int y) {
int r = x / y;
constexpr std::integral auto FloorDiv(std::integral auto x, std::integral auto y) {
auto quot = x / y;
auto rem = x % y;
// if the signs are different and modulo not zero, round down
if ((x ^ y) < 0 && (r * y != x)) {
r--;
if ((x ^ y) < 0 && rem != 0) {
--quot;
}
return r;
return quot;
}

/**
Expand All @@ -200,7 +201,7 @@ constexpr static int FloorDiv(int x, int y) {
* @param y the divisor
* @return the floor modulus {@code x - (floorDiv(x, y) * y)}
*/
constexpr static int FloorMod(int x, int y) {
constexpr std::integral auto FloorMod(std::integral auto x, std::integral auto y) {
return x - FloorDiv(x, y) * y;
}
} // namespace frc

0 comments on commit a0ff389

Please sign in to comment.