gmp_printf bug?

Zimmermann Paul Paul.Zimmermann at loria.fr
Tue Jul 19 09:03:39 CEST 2011


       Hi,

following a problem reported on the MPFR list, it seems gmp_printf has strange
limitations for large floating-point values. Consider the following code:

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

main(int argc, char *argv[])
{
  mpf_t f;

  mpf_init2 (f, 2);
  mpf_set_ui (f, 1);
  mpf_mul_2exp (f, f, 1345466883500933130);
  mpf_dump (f);
  gmp_printf("%Fe\n", f);
  mpf_clear (f);
}

It yields on a 64-bit computer:

tarte% ./a.out
0.111349511949672485115e405025890106316193
*** glibc detected *** ./a.out: realloc(): invalid pointer: 0x00000000018c1008 ***
======= Backtrace: =========
/lib/libc.so.6(+0x774b6)[0x2b30c2dd44b6]
/lib/libc.so.6(realloc+0x352)[0x2b30c2ddb112]
/users/caramel/logiciels/gmp-5.0.2/core2/lib/libgmp.so.10(__gmp_default_reallocate+0x1c)[0x2b30c2ae24cc]
/users/caramel/logiciels/gmp-5.0.2/core2/lib/libgmp.so.10(__gmpf_get_str+0x11a)[0x2b30c2ae4bda]
/users/caramel/logiciels/gmp-5.0.2/core2/lib/libgmp.so.10(__gmp_doprnt_mpf2+0x78)[0x2b30c2b2f5a8]
/users/caramel/logiciels/gmp-5.0.2/core2/lib/libgmp.so.10(__gmp_doprnt+0x48e)[0x2b30c2b2edbe]
...

Note that the mpf_dump call succeeds, but gmp_printf fails.

With mpf_mul_2exp (f, f, 1345466883500933129) both functions succeed.

I have no idea why this limit of 1345466883500933130 which is not near a power
of two (it is about 2^60.22).

Paul Zimmermann



More information about the gmp-bugs mailing list