-1 mod 7 seems different from 0 - 1 mod 7

ls this a bug ? Or am I missing some arithmetic basics ?
gprolog also behaves as SWI-Prolog for the query.

| ?- I is -1 mod 7.
I = 6
yes
D
| ?- J=0, I is J-1 mod 7.
I = -1
J = 0

EDIT.

I noticed that a paren is needed:

?- current_op(X,Y, mod)
X = 400,
Y = yfx.

?- current_op(X, Y, -).
X = 200,
Y = fy ;
X = 500,
Y = yfx.

I usually use write_canonical/1 to check operators’ precedence and associativity.

?- write_canonical((J-1) mod 7).
mod(-(_,1),7)
true.

?- write_canonical(J-1 mod 7).
-(_,mod(1,7))

P.S. I also found that SWI-Prolog defines mod/2 in terms of the floored division:

?- I is 8 mod 3.
I = 2.

?- I is (-8) mod 3.
I = 1.

?- I is 8 mod (-3).
I = -1.

?- I is (-8) mod (-3).
I = -2.

Reference On div and mod

1 Like

Thanks for the link. Interesting reading

1 Like