-2

I want to initialize a mpz_t from GMP with an integer (e.g. 2). I've tried the following variants:

mpz_t n(2); // Compiler error
mpz_t n = 2; // Compiler error

What is the correct way of initializing the mpz_t to 2?

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
  • I think you are looking for the type `mpz_class` (from gmpxx.h), not the low-level C mpz_t type. – Marc Glisse Jan 21 '18 at 19:31
  • @MarcGlisse Yes, it would be easier using that. However `gmpxx` is a separate package to install and I wanted to do a problem-solution post on the raw `mpz_t` because some people are likely to run into that problem. – Uli Köhler Jan 21 '18 at 20:22

1 Answers1

4

See the GMP documentation on initializing integers and the GMP documentation on combined initialization and set:

mpz_t n;
mpz_init_set_ui(n); // ui means unsigned int. Use si for signed values.

Thanks to Mark Glisse for mentioning combined init & set.

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120