2

Running on Widows 7 with MSVS 2010

I am following this tutorial to understand how to use MPIR library for adding two big integers

I understand this library should help me in adding very big numbers as shown in the program below:

#include < stdio.h>
#include < stdlib.h>
#include < gmpxx.h>
#include < iostream>

using namespace std;

void main(int argc, char *argv[])
{

   mpz_class answer_a = 111111111111111111111111111111111111111111111111;
   mpz_class answer_b = 111111111111111111111111111111111111111111111111;


   mpz_class answer_c; 

   answer_c= answer_b + answer_a ;   

   cout << answer_c<<"\n";


} 

But still I get error C2177: constant too big. Did I misunderstand MPIR ?

user3891236
  • 607
  • 1
  • 9
  • 23

1 Answers1

4

Such constant is (very likely) too big for standard integer types. You should use char * constructor instead:

void mpz_class::mpz_class (const char *s)

For example:

mpz_class answer_a("111111111111111111111111111111111111111111111111");

to make this work you need to include suitable MPIR C++ interface header (notice that <gmpxx.h> is from C++ interface of GNU MP library):

#include <mpirxx.h>

See 12.2 C++ Interface Integers chapter in MPIR documentation for more details.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • 1
    You beat me, I almost wrote the same answer :) – Iharob Al Asimi Dec 19 '14 at 12:02
  • @Grzegorz Szpetkowski Thanks. Tried this but says: error C2440: 'initializing' : cannot convert from 'const char [49]' to '__gmp_expr' – user3891236 Dec 19 '14 at 12:14
  • @Grzegorz Szpetkowski But where do I put void mpz_class::mpz_class (const char *s)? I am reading the manual. – user3891236 Dec 19 '14 at 12:16
  • @user3891236: I found it, you need to include `<#include ` as well. Don't declare constructor by yourself, it should be always in header file. – Grzegorz Szpetkowski Dec 19 '14 at 12:32
  • @GrzegorzSzpetkowski Now it gives additional error: error C2440: 'initializing' : cannot convert from 'const char [49]' to '__gmp_expr' AND thsi error 1> Constructor for class '__gmp_expr' is declared 'explicit' – user3891236 Dec 19 '14 at 12:36
  • @user3891236: Yes, I confirm that (on Visual Studio 2013 with version 2.7.0), you might report it to MPIR developers as a bug if you like. As a workaround declare `answer_a` without initializer (i.e. `mpz_class answer_a;`), then assign `answer_a = "111....";` in separate statement. – Grzegorz Szpetkowski Dec 19 '14 at 13:02
  • @GrzegorzSzpetkowski The work around worked. But why? – user3891236 Dec 19 '14 at 13:12
  • @GrzegorzSzpetkowski Your code is using the `char *` *assignment operator*, not the `char *` *constructor* . In order to use that constructor, you'd have to write this code: `mpz_class answer_a("111111111111111111111111111111111111111111111111");` (which, technically speaking, avoids a useless assignment) – Mihai Todor Dec 19 '14 at 18:07
  • @MihaiTodor: Of course you're right, fixed my answer to use proper form. – Grzegorz Szpetkowski Dec 19 '14 at 18:37