Function signedRemainder [src]
C % operator for signed integers
C standard states: "If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a"
The quotient is not representable if denominator is zero, or if numerator is the minimum integer for
the type and denominator is -1. C has undefined behavior for those two cases; this function has safety
checked undefined behavior
Prototype
pub fn signedRemainder(numerator: anytype, denominator: anytype) @TypeOf(numerator, denominator)
Source
pub fn signedRemainder(numerator: anytype, denominator: anytype) @TypeOf(numerator, denominator) {
std.debug.assert(@typeInfo(@TypeOf(numerator, denominator)).int.signedness == .signed);
if (denominator > 0) return @rem(numerator, denominator);
return numerator - @divTrunc(numerator, denominator) * denominator;
}