1
\$\begingroup\$

I'm writing a C++ high precision library based on GMP, sample code(files which have to be added to project):

//biginteger.cc
#include "biginteger.h"
#include <cstdlib>
#include <iostream>
#include <tuple>
#include <gmp.h>

void biginteger::deleteBiginteger(){
std::cout << "sprzatam bigintegera"; std::cout << std::endl;
mpz_clear(x);
}
void biginteger::printbiginteger(){
gmp_printf("%Zd\n", this->x); //std::cout <<std::endl;
   }
   // overloaded += operators 
    biginteger& biginteger::operator += (const unsigned long long int& a){
    mpz_add_ui(this->x, x, a);
    return *this;
    }
biginteger& biginteger::operator += (const signed long int& a){
    if (a < 0) {
    signed long int tmp1 = -a;
    unsigned long long int tmp2 = tmp1;
    mpz_sub_ui(this->x, x, tmp2);
    return *this;
}
else{
    unsigned long long int t1 = (unsigned long long int) a;
    mpz_add_ui(this->x, x, t1);
    return *this;
}
}
biginteger& biginteger::operator += (const biginteger& a){
mpz_add(this->x, this->x, a.x);
return *this;
}
//biginteger.h
#ifndef biginteger_h
#define biginteger_h

#include <cstdlib>
#include <iostream>
#include <tuple>
#include <gmp.h>
 class biginteger{
    public:
    mpz_t x;
    biginteger(mpz_t n){
    mpz_init(x);
    mpz_set(x, n); 
    }
    biginteger(unsigned long long int a){
    mpz_init(x);
    mpz_set_ui(x, a);
    }
    biginteger(signed long int a){
        mpz_init(x);
        mpz_set_si(x, a);
    }
biginteger(int a){
    mpz_init(x);
    mpz_set_si(x, a);
}
biginteger(const char *str, int base){
    mpz_init_set_str(x, str, base);
}   

void deleteBiginteger();
void printbiginteger();
biginteger& operator += (const unsigned long long int& a);
biginteger& operator += (const signed long int& a);
biginteger& operator += (const biginteger& a);

Those two files are just sample code, is there any way to improve performance of this?

\$\endgroup\$
  • \$\begingroup\$ Your indentation looks wrong. The easiest way to post code is to paste it, highlight it, and press Ctrl-K to mark it as a code block. \$\endgroup\$ – 200_success Mar 24 '17 at 5:36
  • \$\begingroup\$ I don’t see the reinventing the wheel tag. Are you aware that GMP already has a C++ interface? \$\endgroup\$ – Tom Zych Oct 6 '17 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.