, . / %
(a / b) * b + (a % b) == a
The C89 / 90 standard says that the result of division is determined by implementation: the compiler is allowed to implement either Euclidean division (truncation towards negative infinity, non-negative remainder), or Fortran-style division (truncation to zero, possibly a negative remainder).
In Euclidean division
-7/3 = -3 -7%3 = 2
7/-3 = -2 7%-3 = 1
-7/-3 = 3 -7%-3 = 2
In the formatted style section
-7/3 = -2 -7%3 = -1
7/-3 = -2 7%-3 = 1
-7/-3 = 2 -7%-3 = -1
The C99 standard (and later) requires all C compilers to implement Fortran-style partitioning.
Side note: C ++ adheres to an implementation-specific specification, up to C ++ 03. C ++ 11 requires, in particular, Fortan's separation.
source
share