Re: GMP Problem with % under C++

2020-11-06 Thread Torbjörn Granlund
Miki Hermann  writes:

  I am very well familiar with the % operator in %.

Swell.

  In the document, whose link you sent me, and which I have read before,
  it is mentioned that

  Function: mpz_class operator% (mpz_class a, mpz_class d)

  This means, that the % operator in GMP overloads the usual % operator
  in C++.

Correctamente.

  Just to recall you, for integer a and b, b > 0, the result of the
  modulus operator is an integer in the range 0..b-1. If a is negative,
  the result is equal to (k*b + a) % b for any positive integer k making
  the expression (k*b + a) positive. By definition for a >= 0 and b > 0,
  we have a = q*b + (a % b), where q is an integer called quotient.

Right, but that is irrelevant since % is not a mathematical modulus
operator.  Did you not just say you were "very well familiar" with it?

Did you *really* read the page Marc pointed you to.  This is all
mentioned there.  Please do read it and get on with your life.

-- 
Torbjörn
Please encrypt, key id 0xC8601622
___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Re: GMP Problem with % under C++

2020-11-06 Thread Miki Hermann
I am very well familiar with the % operator in %. In the document,
whose link you sent me, and which I have read before, it is mentioned
that

Function: mpz_class operator% (mpz_class a, mpz_class d)

This means, that the % operator in GMP overloads the usual % operator
in C++.

Just to recall you, for integer a and b, b > 0, the result of the
modulus operator is an integer in the range 0..b-1. If a is negative,
the result is equal to (k*b + a) % b for any positive integer k making
the expression (k*b + a) positive. By definition for a >= 0 and b > 0,
we have a = q*b + (a % b), where q is an integer called quotient.

Best regards,
Miki

On Thu, 2020-11-05 at 22:15 +0100, Marc Glisse wrote:
> On Thu, 5 Nov 2020, Miki Hermann wrote:
> 
> > I am not sure if I used the GMP package properly or if I did
> > everything
> > right, but it seems to me that very probably there is a problem
> > somewhere in the GMP package and/or its interface with C++.
> 
> It is very probable that you are not familiar with the % operator in
> C or 
> C++, and that you did not read our documentation at
> 
> https://gmplib.org/manual/C_002b_002b-Interface-Integers
> 

___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Re: GMP Problem with % under C++

2020-11-06 Thread sisyphus
On Fri, Nov 6, 2020 at 8:27 AM Niels Möller  wrote:

> [snip]
>
> So your problem really is with % in C++, GMP just follows the
> conventions for the builtin integers.
>

Rather, the OP is expecting that the ''%" operator in his C++ program will
behave as mpz_mod() does - which is the same way as the "%" operator
behaves in perl.
To me, it's not surprising that he would have that expectation.
I'm not saying that it's a *reasonable* expectation, as I don't know C++.

To the op:
If you want to get gmp performance into your perl script, then install
Math::GMP and, instead of:

use bigint;
do:
use bigint lib => 'GMP';
___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Re: GMP Problem with % under C++

2020-11-05 Thread Marco Bodrato

Ciao,

Il 2020-11-05 22:27 Niels Möller ha scritto:

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,


Every language has its own definition :-)

https://en.wikipedia.org/wiki/Modulo_operation#In_programming_languages

Ĝis,
m
___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Re: GMP Problem with % under C++

2020-11-05 Thread Niels Möller
Miki Hermann  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.

Regards,
/Niels

-- 
Niels Möller. PGP-encrypted email is preferred. Keyid 368C6677.
Internet email is subject to wholesale government surveillance.
___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Re: GMP Problem with % under C++

2020-11-05 Thread Marc Glisse

On Thu, 5 Nov 2020, Miki Hermann wrote:


I am not sure if I used the GMP package properly or if I did everything
right, but it seems to me that very probably there is a problem
somewhere in the GMP package and/or its interface with C++.


It is very probable that you are not familiar with the % operator in C or 
C++, and that you did not read our documentation at


https://gmplib.org/manual/C_002b_002b-Interface-Integers

--
Marc Glisse
___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs