mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2019-04-23, 17:49   #1
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

22·19·43 Posts
Default GMP mpz_set_ui Troubles

I'm programming this in C++ using Code::Blocks, which I've used for many other GMP programs.

Can anyone tell me why tf2 cannot be set, when it is initialized identically to tf1?

Overall program:
Code:
#include <iostream>
#include <gmp.h>

using namespace std;

int main(int argc, char **argv)
{
    mpz_t comp,zero,one,ten,factor1,factor2,tf1[500],tf2[500],tc1,tc2,testcomp,mplier[500];
    mpz_inits(comp,zero,one,ten,factor1,factor2,tf1[500],tf2[500],tc1,tc2,testcomp,mplier[500],NULL);
    unsigned long int a, b, c, i[1000], j, k;
    int check;
    char composite1[] = "161"; //test case (23*7)
    char composite2[] = "996685583"; // test case (11579*86077)
    char composite3[] = "4033064567209"; // test case (1535441*2626649)
    char composite4[] = "80246333911201573"; // test case (377060293*212820961)
    char composite5[] = "35051259095366121511"; //test case (377060293*92959295227)
    char composite6[] = "600215441637324288364976551"; // test case (10092006740707*59474340144493)

    mpz_set_str(comp,composite2,10); // initialize gmp composite
    mpz_set_str(zero,"0", 10);       // initialize a gmp variable equal to 0
    mpz_set_str(one,"1", 10);        // initialize a gmp variable equal to 1
    mpz_set_str(ten,"10", 10);       // initialize a gmp variable equal to 10

    cout<<"\nThe composite  is: ";
    mpz_out_str(stdout, 10, comp);   // print value of composite
    cout<<endl<<endl;

    mpz_set(mplier[0],one);
    for(a=1;a<20;a++){
        mpz_mul(mplier[a],mplier[a-1],ten);
    }

    for(i[0]=0;i[0]<10;i[0]++){
        mpz_set_ui(tf1[0],i[0]);
        mpz_set_ui(tf2[0],i[0]);
        cout<<"tf1[0] is ";
        mpz_out_str(stdout, 10, tf1[0]);
        cout<<endl;
    }

    return 0;
}
With the bold line above commented (//) out:
Code:
The composite  is: 996685583

tf1[0] is 0
tf1[0] is 1
tf1[0] is 2
tf1[0] is 3
tf1[0] is 4
tf1[0] is 5
tf1[0] is 6
tf1[0] is 7
tf1[0] is 8
tf1[0] is 9

Process returned 0 (0x0)   execution time : 0.004 s
Press ENTER to continue.
With the bold line above NOT commented (//) out:
Code:
The composite  is: 996685583


Process returned -1 (0xFFFFFFFF)   execution time : 0.078 s
Press ENTER to continue.
I have changed tf2[0] to tf1[0] in the bold line and all runs fine, so tf2 must be the problem. But, I can't see why.

I have also swapped the bold line with the one above and the bold line still causes the trouble.

Also, I get no compile errors, only warnings about the unused variables.
EdH is offline   Reply With Quote
Old 2019-04-23, 17:54   #2
henryzz
Just call me Henry
 
henryzz's Avatar
 
"David"
Sep 2007
Cambridge (GMT)

26×89 Posts
Default

I can't see any issue. Have you tried compiling with -Wall?

edit:
Is mpz_inits designed to work with arrays of mpz_t? I suspect not and memory corruption is causing your issues;

Last fiddled with by henryzz on 2019-04-23 at 18:03
henryzz is offline   Reply With Quote
Old 2019-04-23, 18:02   #3
bsquared
 
bsquared's Avatar
 
"Ben"
Feb 2007

22×32×7×13 Posts
Default

I have never used mpz_inits, but after reading about it, I would not expect it to know that tf1 or tf2 are length 500 arrays.

If I were me, I'd use a loop to initialize the mpz_t arrays one element at a time.
bsquared is offline   Reply With Quote
Old 2019-04-23, 18:21   #4
paulunderwood
 
paulunderwood's Avatar
 
Sep 2002
Database er0rr

333610 Posts
Default

Have you tried mpz_inits without the array definition, which after all are declared in the mpz_t line?, i.e. just tf1 and tf2 in mpz_inits.

Last fiddled with by paulunderwood on 2019-04-23 at 18:21
paulunderwood is offline   Reply With Quote
Old 2019-04-23, 18:51   #5
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

22×19×43 Posts
Default

Thanks guys! It was the array portion of the mpz_inits. All seems well now with just tf1 and tf2 in that line. (I also removed the array portion from mplier.)

I will be setting each tf1[] and tf2[] prior to use, so if they aren't truly zeroed, I should still be OK at this point. If I need, I'll go ahead and use a loop later.

I appreciate the help.
EdH is offline   Reply With Quote
Old 2019-04-23, 19:57   #6
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

22×19×43 Posts
Default

Just for completeness, I did have to initialize all the elements individually:
Code:
    for(a=0;a<500;a++){
        mpz_init(tf1[a]);
        mpz_init(tf2[a]);
        mpz_init(mplier[a]);
    }
EdH is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Apparent Ubuntu Version Troubles with ecmpi EdH GMP-ECM 1 2020-05-04 13:16
mpz_aprcl.h and jacobi_sum.h Troubles with r343 EdH YAFU 2 2015-11-03 13:04
[URGENT] Pain: troubles on implementing SIQS sieve Hermes Factoring 27 2008-10-14 13:54
Troubles with Debian Netinst ET_ Linux 4 2007-03-13 20:41

All times are UTC. The time now is 04:15.

Wed Aug 12 04:15:13 UTC 2020 up 26 days, 2 mins, 1 user, load averages: 1.96, 2.27, 2.31

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.