mersenneforum.org > Math calculation of modulo Mp
 Register FAQ Search Today's Posts Mark Forums Read

2021-11-03, 17:34   #12
paulunderwood

Sep 2002
Database er0rr

397510 Posts

Quote:
 Originally Posted by bhelmes calling function is mpz_mod_mp (m_A, m_A, Mp); The result is not the same (I replaced it with a mpz_mod) Where is the logical error ?
You call with mpz_mod_mp (m_A, m_A, Mp); Are the variables in the call initialized?

2021-11-03, 17:39   #13
bhelmes

Mar 2016

3·53 Posts

Quote:
 Originally Posted by paulunderwood You call with mpz_mod_mp (m_A, m_A, Mp); Are the variables in the call initialized?
yes, otherwise the program would not run.

2021-11-03, 17:44   #14
paulunderwood

Sep 2002
Database er0rr

1111100001112 Posts

Quote:
 Originally Posted by bhelmes I had some problems to use the function, described before, mpz_mod_Mp void mpz_mod_mp (mpz_t r, mpz_t a, mp_bitcnt_t p) { mpz_t res; mpz_init (res); // assumed that r is initialized and a is expendable do { mpz_fdiv_r_2exp (res, a, p); mpz_fdiv_q_2exp (a, a, p); mpz_add (res, res, a); } while (mpz_sizeinbase (res,2) > p); mpz_set (r, res); mpz_clear (res); } calling function is mpz_mod_mp (m_A, m_A, Mp); The result is not the same (I replaced it with a mpz_mod) Where is the logical error ?
You call with Mp but the argument is p. This could be the reason for the error.

Last fiddled with by paulunderwood on 2021-11-03 at 17:52

2021-11-03, 17:52   #15
bhelmes

Mar 2016

3·53 Posts

Quote:
 Originally Posted by paulunderwood You call with Mp but the parameter is p. This could be the reason for the error.

nice stroke, Mp was uint64, p was mp_bitcnt_t.
I replaced the mp_bitcnt_t by uint64 and the results are correct.
Thanks a lot.

 2021-11-13, 19:11 #16 bhelmes     Mar 2016 3×53 Posts A peaceful and pleasant day for you, there is a bug in the function, and I have no idea, where the reason is: void mpz_mod_mp (mpz_t r, mpz_t a, uint64 p) { mpz_t res_1, res_2; mpz_init (res_1); mpz_init (res_2); // 1. Calculation mpz_mod (res_1, a, f); // 2. Calculation mp_bitcnt_t mp=mp_bitcnt_t(p); // assumed that r is initialized and a is expendable if (mpz_cmp_ui (a, 0)==0) { mpz_set_ui (res_2, 0); } else { do { mpz_fdiv_r_2exp (res_2, a, mp); mpz_fdiv_q_2exp (a, a, mp); mpz_add (res_2, res_2, a); } while (mpz_sizeinbase (res_2,2) > mp); } // Compairison if (mpz_cmp (res_1, res_2)!=0) { mpz_out_str (stdout, 10, a); cout << endl; mpz_out_str (stdout, 10, res_1); cout << endl; mpz_out_str (stdout, 10, res_2); cout << endl; exit (1); } mpz_set (r, res_2); mpz_clear (res_1); mpz_clear (res_2); } The error appears if a=0 and the output is 0 10584739298409185657387898722659423897260 89430325577680987480245132687307139815991 If you have an idea, where the error is, I will be glad.
2021-11-13, 19:24   #17
paulunderwood

Sep 2002
Database er0rr

F8716 Posts

Quote:
 Originally Posted by bhelmes there is a bug in the function, and I have no idea, where the reason is: void mpz_mod_mp (mpz_t r, mpz_t a, uint64 p) // 2. Calculation mp_bitcnt_t mp=mp_bitcnt_t(p);
It usual for p to be a prime and Mp to be the corresponding Mersenne number. If you have passed in p then why do you need Mp?

What is f?

Last fiddled with by paulunderwood on 2021-11-13 at 19:32

2021-11-13, 19:44   #18
bhelmes

Mar 2016

3·53 Posts

Quote:
 Originally Posted by paulunderwood It usual for p to be a prime and Mp to be the corresponding Mersenne number. If you have passed in p then why do you need Mp?
I need for the function mpz_fdiv_r_2exp the variable as mp_bitcnt_t, therefore I made a change from uint_64 to mp_bitcnt_t

Quote:
 Originally Posted by paulunderwood What is f?
f is Mp as mpz_t variable

But why is res_1 and res_2 not equal 0 ?
In other cases res_1 and res_2 are the same.

2021-11-13, 19:51   #19
paulunderwood

Sep 2002
Database er0rr

3·52·53 Posts

Quote:
 Originally Posted by bhelmes I need for the function mpz_fdiv_r_2exp the variable as mp_bitcnt_t, therefore I made a change from uint_64 to mp_bitcnt_t
Without checking the manual that may well be the case, but mpz_sizeinbase returns an int?

2021-11-13, 20:01   #20
bhelmes

Mar 2016

3×53 Posts

Quote:
 Originally Posted by paulunderwood Without checking the manual that may well be the case, but mpz_sizeinbase returns an int?

for a=0 the loop with mpz_sizeinbase is not entered.

2021-11-13, 20:08   #21
paulunderwood

Sep 2002
Database er0rr

3·52·53 Posts

Quote:
 Originally Posted by bhelmes f is Mp as mpz_t variable
can you output p and f in that function for us to see?

2021-11-13, 20:18   #22
paulunderwood

Sep 2002
Database er0rr

3·52·53 Posts

Quote:
 Originally Posted by bhelmes f is Mp as mpz_t variable
Has it been initialized?

 Similar Threads Thread Thread Starter Forum Replies Last Post axn Computer Science & Computational Number Theory 66 2011-09-01 21:55 smslca Math 3 2011-04-18 17:18 T.Rex Math 7 2009-03-13 10:46 Cyclamen Persicum Math 2 2003-12-10 10:52 eepiccolo Math 7 2003-01-08 03:07

All times are UTC. The time now is 16:58.

Mon Jan 17 16:58:03 UTC 2022 up 178 days, 11:27, 0 users, load averages: 1.32, 1.28, 1.25