AdaptableFunctor
computes both integral quotient and remainder of division with remainder. The quotient q and remainder r are computed such that x = q*y + r and |r| < |y| with respect to the proper integer norm of the represented ring. In particular, r is chosen to be 0 if possible. Moreover, we require q to be minimized with respect to the proper integer norm.
Note that the last condition is needed to ensure a unique computation of the pair (q,r). However, an other option is to require minimality for |r|, with the advantage that a mod(x,y) operation would return the unique representative of the residue class of x with respect to y, e.g. mod(2,3) should return -1. But this conflicts with nearly all current implementation of integer types. From there, we decided to stay conform with common implementations and require q to be computed as x/y rounded towards zero.
The following table illustrates the behavior for integers:
|
x | y | q | r |
|
3 | 3 | 1 | 0 |
2 | 3 | 0 | 2 |
1 | 3 | 0 | 1 |
0 | 3 | 0 | 0 |
-1 | 3 | 0 | -1 |
-2 | 3 | 0 | -2 |
-3 | 3 | -1 | 0 |
|
|
|
|
x | y | q | r |
|
3 | -3 | -1 | 0 |
2 | -3 | 0 | 2 |
1 | -3 | 0 | 1 |
0 | -3 | 0 | 0 |
-1 | -3 | 0 | -1 |
-2 | -3 | 0 | -2 |
-3 | -3 | 1 | 0 |
|
|
- Refines:
AdaptableFunctor
- See also
AlgebraicStructureTraits
-
AlgebraicStructureTraits_::Mod
-
AlgebraicStructureTraits_::Div