Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ program using GMP library

Tags:

c++

gmp

I have installed GMP using the instruction on this website: http://www.cs.nyu.edu/exact/core/gmp/ Then I looked for an example program using the library:

    #include <iostream>
#include <gmpxx.h>
using namespace std;
int main (void) {
mpz_class a, b, c;
a = 1234;
b = "-5678";
c = a+b;
cout << "sum is " << c << "\n";
cout << "absolute value is " << abs(c) << "\n";
cin >> a;
return 0;
}

But if I compile this using the command: g++ test.cpp -o test.exe, it says gmpxx.h: no such file or directory. How can I fix this? I am kind of new to this. And I am using MinGW.

like image 827
Badshah Avatar asked Jan 02 '13 17:01

Badshah


People also ask

What is GMP library used for?

GMP is used for integer arithmetic in many computer algebra systems such as Mathematica and Maple. It is also used in the Computational Geometry Algorithms Library (CGAL). GMP is needed to build the GNU Compiler Collection (GCC).

What is GMP programming?

Good Manufacturing Practice (GMP) is a system for ensuring that products are consistently produced and controlled according to quality standards. It is designed to minimize the risks involved in any pharmaceutical production that cannot be eliminated through testing the final product.

What is the latest version of GMP?

The current stable release is 6.2. 1, released 2020-11-14.


1 Answers

Get the actual version here GNU GMP Library. Make sure you configure it to be installed in /usr/lib (pass --prefix=/usr to configure).

Here you have documentation: GNU GMP Manual.

You are not using the lib correctly. I don't know if you can directly access mpx values with C++ functions but, here you have a working example of what you wanted to achieve:

#include<iostream>
#include<gmp.h>

using namespace std;

int main (int argc, char **argv) {

    mpz_t a,b,c;
    mpz_inits(a,b,c,NULL);

    mpz_set_str(a, "1234", 10);
    mpz_set_str(b,"-5678", 10); //Decimal base

    mpz_add(c,a,b);

    cout<<"\nThe exact result is:";
    mpz_out_str(stdout, 10, c); //Stream, numerical base, var
    cout<<endl;

    mpz_abs(c, c);
    cout<<"The absolute value result is:";
    mpz_out_str(stdout, 10, c);
    cout<<endl;

    cin.get();

    return 0;
}

Compile with:

g++ -lgmp file.cpp -o file
like image 153
someoneigna Avatar answered Oct 18 '22 19:10

someoneigna