GMP Problem with % under C++

Niels Möller nisse at
Thu Nov 5 21:27:44 UTC 2020

Miki Hermann <hermann at> writes:

> The result of the command '22b-gmp < 22input.txt' is:
>    *** b = 62010736820046
>    f(1) - b = -15681174147849
>    a = -15681174147849
>    On position 2020 is the card 102220661749926

I think this is the same result you get with a plain (64-bit) long. The
/ and % operators in C++ produce a quotient rounded towards 0, so (f(1)
- b) / DECKSIZE == 0, and the corresponding remainder is negative.

I'm not that familiar with perl, but I guess it uses the mathematically
more sane definition of always rounding the quotient towards -infinity,
so that (f(1) - b) / DECKSIZE == -1.

So your problem really is with % in C++, GMP just follows the
conventions for the builtin integers. I'm not even sure if rounding is
specified by the C++ standards, but rounding towards zero is what all C
and C++ implementations I'm aware of use.


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

More information about the gmp-bugs mailing list