0

The mod operator gives different results if a dividend or divisor is negative, that depend on the division function I think, as it can be truncate division or a floored division.

Instead, if we use the Euclidean division the quotient and the remainder are unique.

The results are correct but why there is this ambiguity? Is it related to performance and efficiency?

  • 2
    "that depend on the division function I think," --> No ambiguity since C99 - the quotient and remainder are defined - no implementation defined behavior. Division is always "truncate". – chux - Reinstate Monica Aug 28 '17 at 18:29
  • 1
    @harold Perhaps, yet OP starts from a false premise of ambiguity and promotes Euclidean division given non-ambiguous results. It is not OT as to why, yet an answer would need the rational of the C99 committee to detail their justification. Likely the solution enshrined the common practice. – chux - Reinstate Monica Aug 28 '17 at 18:36
  • Typically C and C++ round quotients towards zero, and the modulo returns a remainder of zero or a remainder with the same sign as the dividend (this is how many processors implement division). In math and some other programming languages, the quotient is rounded towards negative infinity, and modulo returns a remainder of zero or a remainder with the same sign as the divisor (post division correction is used if the native divide doesn't follow the rules of math). – rcgldr Aug 28 '17 at 21:02

0 Answers0