Hello I'm having trouble with this code...

Josh Brown lovell_josh at hotmail.co.uk
Fri Apr 8 08:44:42 CEST 2011


Hello I'm having trouble with this code, it says there is a Floating point error but I can't seem to work out what's the matter. Have I found a bug or could it just be my poor programming skills?

#include <stdlib.h>
#include <stdio.h>
#include <gmp.h>
#include <limits.h>

int main() {

mpz_t **primes = malloc(sizeof(mpz_t));
primes[0]= malloc(sizeof(mpz_t));

mpz_init_set_str(primes[0][0], "2", 10);

unsigned int sizeofa=1;
unsigned int sizeofb=1;
unsigned int sizeofc=1;

mpz_t upto;
mpz_init_set_str(upto, "20", 10);

mpz_t inttocheck;
mpz_init_set_str(inttocheck,"3",10);

mpz_t remainder;
mpz_init_set_str(remainder,"0",10);

int continuesearching = -1;

    while(continuesearching <= 0){
int foundprime = 1;
int indexa, indexb;
        for (indexa=0;indexa < sizeofa; ++indexa)
        {
            if (indexa == sizeofa-1)
            {
                for (indexb=0;indexb < sizeofb; ++indexb)
                {
                    mpz_cdiv_r(remainder,inttocheck,primes[indexa][indexb]);
                    if ( remainder == 0)
                    {
                        foundprime=-1;
                        break;
                    }
                }
            }
            else
            {
                for (indexb=0;indexb < INT_MAX; ++indexb)
                {
                    mpz_cdiv_r(remainder,inttocheck,primes[indexa][indexb]);
                    if ( remainder == 0)
                    {
                        foundprime=-1;
                        break;
                    }
                }
            }    

        }

        if (foundprime == 1)
        {
            if (sizeofb == INT_MAX)
            {
                primes = realloc(primes, sizeof(primes)+sizeof(mpz_t));
                primes[sizeofa] = realloc(primes[sizeofa], sizeof(mpz_t));
                sizeofc=1;
                mpz_set(primes[sizeofa][sizeofb-1],inttocheck);
                ++sizeofa;
            }
            else
            {

                primes = realloc(primes, sizeof(primes)+sizeof(mpz_t));
                primes[sizeofa-1] = realloc(primes[sizeofa-1], sizeof(primes[sizeofa-1]) + sizeof(mpz_t));
                mpz_set(primes[sizeofa-1][sizeofb-1],inttocheck);

                ++sizeofb; /* Something near here seems to be the problem */

                mpz_out_str(stdout, 10, inttocheck);
                printf("\n");
            }
        }
    mpz_add_ui(inttocheck,inttocheck,1);
    continuesearching=mpz_cmp(inttocheck,upto);
    }
printf("%d",INT_MAX);
}
 		 	   		  


More information about the gmp-discuss mailing list