GMP with Large Integers

I have a program that factors very large integers. I need to use GMP since these integers easily overflow even the long long unsigned int type on a 64-bit machine. I wrote my program, and it works fine, until I get the part when I throw big numbers at it. Here's the gist of the problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <gmp.h>
#include <gmpxx.h>

using namespace std;

int main()
{
    mpz_class n = 12623773;
    //mpz_class n = 48112959837082048697; //this blows up
    return 0;
}


This program runs just fine. But if I switch which lines are commented, I get this error when I compile:

$ g++ -o main main.cpp  -lgmpxx -lgmp
main.cpp: In function ‘int main()’:
main.cpp:21:19: error: conversion from ‘long long int’ to ‘mpz_class’ is ambiguous
/usr/include/gmpxx.h:1563:3: note: candidates are: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(double)
/usr/include/gmpxx.h:1562:3: note:                 __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(float)
/usr/include/gmpxx.h:1560:3: note:                 __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(long unsigned int)
/usr/include/gmpxx.h:1559:3: note:                 __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(long int)
/usr/include/gmpxx.h:1557:3: note:                 __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(short unsigned int)
/usr/include/gmpxx.h:1556:3: note:                 __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(short int)
/usr/include/gmpxx.h:1554:3: note:                 __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(unsigned int)
/usr/include/gmpxx.h:1553:3: note:                 __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(int)
/usr/include/gmpxx.h:1551:3: note:                 __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(unsigned char)
/usr/include/gmpxx.h:1550:3: note:                 __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(signed char)


The whole idea of using GMP is that it should allow me to work with these large numbers. What am I doing wrong? Thanks!
Last edited on
Despite that a library like this is provided to be able to do arithmetic on very large or very high-precision numbers, you still have to work THROUGH the library as it's intended to be accessed and used, you can't just include it and then expect the abilities it provides to suddenly be known to C++'s basic syntax. Here is the documentation on using the GMP library correctly:

http://gmplib.org/manual/

In particular, for this instance, you might find the section on "Integer Functions" useful. Hope that helps! :)
Thank you. Yes, I know that I have to use the library's functions in order to operate on the numbers. But as you can see in what I have posted above, I am getting hung up on the most basic step of declaring a variable and initializing it.

I've been all over the gmp manual, and I think that I am handling this correctly. Obviously, I'm not, which is why I am posting here. I'm hoping that somebody with experience with GMP can show me the steps to get a large number set up.

Thanks.
I think the problem is that the literal number you want to assign to n is too large (which is why you are using GMP).

By that I mean the compiler can't handle the actual number 48112959837082048697 that you want to assign. I think the GMP library allows you to assign it as a string rather than a number:
 
mpz_class n("48112959837082048697", 10); // try this 

http://gmplib.org/manual/C_002b_002b-Interface-Integers.html#C_002b_002b-Interface-Integers
Well, I notice immediately without even reading the documentation very closely that you are not using the documented way to declare/initialize a GMP integer:

http://gmplib.org/manual/Integer-Functions.html#Integer-Functions

This says "GMP integers are stored in objects of type mpz_t" -- not mpz_class which you have used in your code. Now, I don't know GMP, and for all I know this "mpz_class" type may be a fine substitute, but it's not what the documentation says to do. If you are trying to do some unconventional (but okay) manner of utilizing this library then you might want to say a bit more about exactly how you're trying to use it.

http://gmplib.org/manual/Initializing-Integers.html#Initializing-Integers

This page says to use functions like mpz_init() for initializing, not the standard "=" operator.

http://gmplib.org/manual/Assigning-Integers.html#Assigning-Integers

If I understand this page right, it's saying that if you want to drop a massive value into your mpz_t integer, you use the function mpz_set_str and pass in a char array which contains a textual representation of the number you want to assign it to. It makes sense that you must do this, because the compiler cannot, on its own, grasp such a large number in true numerical form (if it could, there would be no need for this library).
Thanks. I didn't find those pages on initializing and assigning integers earlier. Now I see them.

Thank you!
Topic archived. No new replies allowed.