get_string limitation to 32 bit number.

Sergio Abreu dosergio at ig.com.br
Sun Dec 12 15:12:14 CET 2010


GMP in use: 5.0.1
I tried to compile it as static and dynamic but the bug persists.
What is wrong:
If the value of mpf_t is less than 2^32 the function mpf_get_str works 
fine. Otherwhise, you loose the upper 32 bits and the function only 
returns the number corresponding to the lowest 32 bits.
Version of gcc and g++, both 4.5.0
./config.guess = athlon64-pc-mingw32
./configfsf.guess = i686-pc-mingw32
uname -a = MINGW32_NT-5.1 MYCOMPUTERNAME 1.0.16/(0.48/3/2) DATE HOUR 
i686 Msys

Source code for testing:

// ------ BEGIN SOURCE -------------

/*
 * File:   main.cpp
 * Author: Sergio Abreu
 * http://sites.sitesbr.net
 * Created on 2010, Dezember 10th, 12:24
 */

#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
 
int main(int argc, char** argv) {

    mpz_t a;
    mpf_t x, y, z;
    mp_exp_t exp;
    char *c;
    double d;
    unsigned long long ul;
  /* You can run the program with one argument in the command line: Ex: 
gmptest 33,
     or change the 31 below to 32 or bigger and recompile,
     to see the problem with no arguments in the command line. In this case,
     to see the correct behavior you must supply a less than 32 argument 
Ex gmptest 31 or less.
  */

    int power = argc > 1 ? atoi(argv[1]) : 31; 

    mpz_init(a);
    mpf_init(x);
    mpf_init(y);
    mpf_init(z);

    mpz_set_ui( a, 2 );
    mpf_set_ui( x, 2 );
    mpf_set_ui( y, 2 );
    mpz_pow_ui( a, a, power);

    gmp_printf("Initial value: %Zd, power in test is %d\n",  a, power );

    ul = 137;
    mpz_add_ui( a, a, ul);
    gmp_printf("After sum: %Zd \n", a );

    ul = mpz_get_ui( a);
    mpf_set_ui( x, ul);

    c = mpf_get_str( NULL, &exp, 2, 0, x);
    printf("Bits representing mpf_t x value: %s exp= %lu\n", c, exp);

    mpf_div(z, x, y);
 
    gmp_printf ("Result: %1.1Ff divided by  %1.1Ff is %1.1Ff\n", x , y , 
z );

    mpz_clear(a);
    mpf_clear(x);
    mpf_clear(y);
    mpf_clear(z);

    return 0;
}



// ------- END OF SOURCE






More information about the gmp-bugs mailing list