bug in gmp_fprintf?

paul zimmermann Paul.Zimmermann at inria.fr
Mon Nov 23 15:04:28 UTC 2015


       Hi,

on https://gmplib.org/list-archives/gmp-discuss/2005-August/001787.html I read:

> "Support at the mpn and mpz levels for operands of up to 2^56 bits
> (on 64-bit machines). Current limits are: on 32-bit machines, 2^31 bits;
> on 64-bit machines, 2^37 bits".

However with the program below I get on a 64-bit machine:

zimmerma at tomate:/tmp$ ./a.out 8589934589
GMP version 6.1.0
P has 8589934589 bits
P has 1 bits

If I replace gmp_fprintf (fp, "%Zx\n", P) by mpz_out_str (fp, 16, P) I get:

zimmerma at tomate:/tmp$ ./a.out 8589934589
GMP version 6.1.0
P has 8589934589 bits
P has 8589934581 bits

Thus we cannot correctly print/read numbers of 2^33 bits.
What is the real limit for mpz operands on 32-bit and 64-bit machines?

Paul Zimmermann

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

int
main (int argc, char *argv[])
{
  unsigned long nbits = strtoul (argv[1], NULL, 10);
  mpz_t P;
  FILE *fp;
  int ret;

  printf ("GMP version %s\n", gmp_version);
  mpz_init (P);
  mpz_ui_pow_ui (P, 2, nbits);
  mpz_sub_ui (P, P, 17);
  printf ("P has %zu bits\n", mpz_sizeinbase (P, 2));
  fp = fopen ("/tmp/P", "w");
  gmp_fprintf (fp, "%Zx\n", P);
  fclose (fp);
  fp = fopen ("/tmp/P", "r");
  ret = gmp_fscanf (fp, "%Zx\n", P);
  printf ("P has %zu bits\n", mpz_sizeinbase (P, 2));
  assert (ret == 1);
  fclose (fp);
  mpz_clear (P);
}




More information about the gmp-bugs mailing list