possible problems with mpfr

Paul Zimmermann Paul.Zimmermann at loria.fr
Thu Feb 26 11:08:25 CET 2004


Dear Scott,

   Hello all,

   I am having two problems with Lib-GMP's mpfr implementation.  I am sure 
   that I'm doing something wrong but can't figure it out.  I am using 
   Lib-GMP v4.1.2 compiled with --enable-cxx and --enable-mpfr.  I have GCC 
   2.95.3 and ld 2.11.2 but have also tried it with GCC 3.2 and ld 2.13.

   The following code exhibits both of my problems:

   /*
    * Compile:  gcc isint_test.c -Wall -lgmp -lmpfr -o isint_test
    * For some reason, the order of the compile options is important here.
    * If I try this compile line:  gcc -Wall -lgmp -lmpfr -o isint_test isint_test.c
    * then it does not compile (i.e., cannot resolve mpfr functions).
    */
   #include <stdio.h>
   #include <stdlib.h>
   #include <gmp.h>
   #include <mpfr.h>

   int main(void) {
     mpfr_t x;

     mpfr_init_set_str(x,"0.428734892794",10,GMP_RNDN);
     printf("x is set to ");
     mpfr_out_str(stdout,10,0,x,GMP_RNDN);

     mpfr_floor(x,x);
     printf("\nAfter floor, x is ");
     mpfr_out_str(stdout,10,0,x,GMP_RNDN);

     printf("\nmpfr_isinteger(x) returned %d\n",mpfr_isinteger(x));
     if( mpfr_cmp_ui(x,0L)==0 ) printf("mpfr_cmp_ui claims x is 0\n");
     exit(0);
   }

   First (as the comments say), when I change the order of the compilation 
   command line, I get linker errors about undefined references to the mpfr 
   functions.  This is weird.  However, the much more important issue, is my 
   problem with the mpfr_isinteger() function.  If I actually want it to work 
   correctly when the mpfr_t equals zero, I need to set it to 1 using 
   mpfr_set_ui() then subtract 1 like this:

	   if( mpfr_cmp_ui(x,0L)==0 ) {
		   mpfr_set_ui(x,1L,GMP_RNDN);
		   mpfr_sub_ui(x,x,1L,GMP_RNDN);
	   }


   This only seems to be a problem when the mpfr_t equals zero exactly (such 
   as in this above floor() function).

   Any help or ideas?  Thank you very much.

The mpfr_isinteger was an internal and undocumented function in the version
of mpfr distributed within gmp-4.1.2. You should not use it!

We advise you to switch to mpfr-2.0.3, which you can download on www.mpfr.org,
and to use the mpfr_integer_p function.

Regards,
Paul Zimmermann


More information about the gmp-bugs mailing list