See also divmod/4 (not visible in the function in the header list because it is listed under "special arithmetic functions")
A little exercise to compare the pairs
div,mod
and
//,rem
can be found here
With output:
?- test. ( -6 mod 3) = 0, ( -6 div 3) = -2, OK : 3*-2+0=-6 ( -5 mod 3) = 1, ( -5 div 3) = -2, OK : 3*-2+1=-5 ( -4 mod 3) = 2, ( -4 div 3) = -2, OK : 3*-2+2=-4 ( -3 mod 3) = 0, ( -3 div 3) = -1, OK : 3*-1+0=-3 ( -2 mod 3) = 1, ( -2 div 3) = -1, OK : 3*-1+1=-2 ( -1 mod 3) = 2, ( -1 div 3) = -1, OK : 3*-1+2=-1 ( 0 mod 3) = 0, ( 0 div 3) = 0, OK : 3*0+0=0 ( 1 mod 3) = 1, ( 1 div 3) = 0, OK : 3*0+1=1 ( 2 mod 3) = 2, ( 2 div 3) = 0, OK : 3*0+2=2 ( 3 mod 3) = 0, ( 3 div 3) = 1, OK : 3*1+0=3 ( 4 mod 3) = 1, ( 4 div 3) = 1, OK : 3*1+1=4 ( 5 mod 3) = 2, ( 5 div 3) = 1, OK : 3*1+2=5 ( 6 mod 3) = 0, ( 6 div 3) = 2, OK : 3*2+0=6 ( -6 mod -3) = 0, ( -6 div -3) = 2, OK : -3*2+0=-6 ( -5 mod -3) = -2, ( -5 div -3) = 1, OK : -3*1+-2=-5 ( -4 mod -3) = -1, ( -4 div -3) = 1, OK : -3*1+-1=-4 ( -3 mod -3) = 0, ( -3 div -3) = 1, OK : -3*1+0=-3 ( -2 mod -3) = -2, ( -2 div -3) = 0, OK : -3*0+-2=-2 ( -1 mod -3) = -1, ( -1 div -3) = 0, OK : -3*0+-1=-1 ( 0 mod -3) = 0, ( 0 div -3) = 0, OK : -3*0+0=0 ( 1 mod -3) = -2, ( 1 div -3) = -1, OK : -3*-1+-2=1 ( 2 mod -3) = -1, ( 2 div -3) = -1, OK : -3*-1+-1=2 ( 3 mod -3) = 0, ( 3 div -3) = -1, OK : -3*-1+0=3 ( 4 mod -3) = -2, ( 4 div -3) = -2, OK : -3*-2+-2=4 ( 5 mod -3) = -1, ( 5 div -3) = -2, OK : -3*-2+-1=5 ( 6 mod -3) = 0, ( 6 div -3) = -2, OK : -3*-2+0=6 Below with current_prolog_flag(integer_rounding_function,toward_zero) ( -6 rem 3) = 0, ( -6 // 3) = -2, OK : 3*-2+0=-6 ( -5 rem 3) = -2, ( -5 // 3) = -1, OK : 3*-1+-2=-5 ( -4 rem 3) = -1, ( -4 // 3) = -1, OK : 3*-1+-1=-4 ( -3 rem 3) = 0, ( -3 // 3) = -1, OK : 3*-1+0=-3 ( -2 rem 3) = -2, ( -2 // 3) = 0, OK : 3*0+-2=-2 ( -1 rem 3) = -1, ( -1 // 3) = 0, OK : 3*0+-1=-1 ( 0 rem 3) = 0, ( 0 // 3) = 0, OK : 3*0+0=0 ( 1 rem 3) = 1, ( 1 // 3) = 0, OK : 3*0+1=1 ( 2 rem 3) = 2, ( 2 // 3) = 0, OK : 3*0+2=2 ( 3 rem 3) = 0, ( 3 // 3) = 1, OK : 3*1+0=3 ( 4 rem 3) = 1, ( 4 // 3) = 1, OK : 3*1+1=4 ( 5 rem 3) = 2, ( 5 // 3) = 1, OK : 3*1+2=5 ( 6 rem 3) = 0, ( 6 // 3) = 2, OK : 3*2+0=6 ( -6 rem -3) = 0, ( -6 // -3) = 2, OK : -3*2+0=-6 ( -5 rem -3) = -2, ( -5 // -3) = 1, OK : -3*1+-2=-5 ( -4 rem -3) = -1, ( -4 // -3) = 1, OK : -3*1+-1=-4 ( -3 rem -3) = 0, ( -3 // -3) = 1, OK : -3*1+0=-3 ( -2 rem -3) = -2, ( -2 // -3) = 0, OK : -3*0+-2=-2 ( -1 rem -3) = -1, ( -1 // -3) = 0, OK : -3*0+-1=-1 ( 0 rem -3) = 0, ( 0 // -3) = 0, OK : -3*0+0=0 ( 1 rem -3) = 1, ( 1 // -3) = 0, OK : -3*0+1=1 ( 2 rem -3) = 2, ( 2 // -3) = 0, OK : -3*0+2=2 ( 3 rem -3) = 0, ( 3 // -3) = -1, OK : -3*-1+0=3 ( 4 rem -3) = 1, ( 4 // -3) = -1, OK : -3*-1+1=4 ( 5 rem -3) = 2, ( 5 // -3) = -1, OK : -3*-1+2=5 ( 6 rem -3) = 0, ( 6 // -3) = -2, OK : -3*-2+0=6 true.