mpz_invert (x, 1, 0)

Niels Möller nisse at
Fri Mar 2 10:25:17 CET 2012

Joerg Arndt <arndt at> writes:

> In fact, I'd be surprised if mod zero would do anything else.
> Performance reasons suggest that no extra branch in any mod-code
> should be done, leaving it to the user to handle the corner case
> "equivalence mod zero" being "simple equality".

Let me see if I understand you. You're saying that the ring "Z_0",
integers modulo 0, is the same as Z, because x mod 0 == x for all
integers x, and x = y (mod 0) iff x = y. Hence the invertible elements
are the units in Z, +1 and -1, which each is its own inverse. All other
integers are non-invertible.

A question: How established is it that x mod 0 = x for all x? I don't
have Knuth's book at hand.

I don't think other GMP functions honor the convention that x mod 0 = x.
E.g., mpz_mod is documented as always having a non-negative return
value, so it can't return -1 for -1 mod 0. I think gmp rather uses a
definition based on division, something like

  a mod b = a - b floor (a/b)

or possibly

  a mod b = a - |b| floor (a/|b|)

valid only for b != 0.

I don't think it makes sense to use the x mod 0 == x convention for
mpz_invert only, and not for other gmp functions. For corner cases like
this, which are unlikely to be very useful for applications, I think
consistency is a pretty important design consideration.


Niels Möller. PGP-encrypted email is preferred. Keyid C0B98E26.
Internet email is subject to wholesale government surveillance.

More information about the gmp-devel mailing list