Search code examples
c++gmp

How to initialize a GMP mpz_t with an integer


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?


Solution

  • 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.