5.4 Similarities and differences between rem and mod

rem and mod are quite similar – they only differ if dividend and divisor have different signs:

  • With rem, the result has the same sign as the dividend (first operand):

    1. > 5 rem 4
    2. 1
    3. > -5 rem 4
    4. -1
    5. > 5 rem -4
    6. 1
    7. > -5 rem -4
    8. -1
  • With mod, the result has the same sign as the divisor (second operand):

    1. > 5 mod 4
    2. 1
    3. > -5 mod 4
    4. 3
    5. > 5 mod -4
    6. -3
    7. > -5 mod -4
    8. -1