Added more operations

This commit is contained in:
Robert 2020-07-19 17:01:23 +02:00
parent 971f13a59b
commit 6d15af8b86
3 changed files with 56 additions and 38 deletions

View file

@ -6,6 +6,9 @@
namespace gm3p
{
/**
* A wrapper for mpz_t
*/
class gInt
{
public:
@ -14,10 +17,8 @@ namespace gm3p
gInt(mpz_t init);
gInt(mpq_t init);
gInt(mpf_t init);
gInt(unsigned long int init);
gInt(long int init);
gInt(const char* init, int base);
gInt(double init);
gInt(const char* init, int base = 10);
// DESTRUCTOR
~gInt();
@ -28,6 +29,8 @@ namespace gm3p
gInt(gInt&& other);
// ARITHMETIC OPERATORS
friend gInt operator-(const gInt& value);
friend gInt operator+(const gInt& left, const gInt& right);
friend gInt operator-(const gInt& left, const gInt& right);
friend gInt operator*(const gInt& left, const gInt& right);
@ -38,8 +41,13 @@ namespace gm3p
gInt& operator*=(const gInt& right);
gInt& operator/=(const gInt& right);
friend gInt Mul2Exp(const gInt& value, const mp_bitcnt_t& exp);
friend gInt Neg(const gInt& value);
friend gInt Abs(const gInt& value);
// CONVERSION OPERATORS
operator long int() const;
operator mpz_t&();
unsigned long int ToUnsigned() const;
char* ToString(char* buf, int base) const;

View file

@ -24,12 +24,6 @@ namespace gm3p
mpz_set_f(value, init);
}
gInt::gInt(unsigned long int init) :
gInt::gInt()
{
mpz_init_set_ui(value, init);
}
gInt::gInt(long int init) :
gInt::gInt()
{
@ -42,77 +36,69 @@ namespace gm3p
mpz_init_set_str(value, init, base);
}
gInt::gInt(double init)
{
mpz_init_set_d(value, init);
}
gInt::~gInt()
{
mpz_clear(value);
}
gInt& gInt::operator=(const gInt& other)
{
mpz_set(this->value, other.value);
mpz_set(*this, const_cast<gInt&>(other));
return *this;
}
gInt::gInt(const gInt& other)
{
mpz_set(this->value, other.value);
mpz_set(*this, const_cast<gInt&>(other));
}
gInt::gInt(gInt&& other)
{
mpz_set(this->value, other.value);
mpz_set(*this, const_cast<gInt&>(other));
}
gInt operator-(const gInt& value)
{
return Neg(value);
}
gInt operator+(const gInt& left, const gInt& right)
{
gInt ret;
mpz_add(ret.value, left.value, right.value);
mpz_add(ret, const_cast<gInt&>(left), const_cast<gInt&>(right));
return ret;
}
gInt operator-(const gInt& left, const gInt& right)
{
gInt ret;
mpz_sub(ret.value, left.value, right.value);
mpz_sub(ret, const_cast<gInt&>(left), const_cast<gInt&>(right));
return ret;
}
gInt operator*(const gInt& left, const gInt& right)
{
gInt ret;
mpz_mul(ret.value, left.value, right.value);
mpz_mul(ret, const_cast<gInt&>(left), const_cast<gInt&>(right));
return ret;
}
gInt operator/(const gInt& left, const gInt& right)
{
gInt ret;
mpz_div(ret.value, left.value, right.value);
mpz_div(ret, const_cast<gInt&>(left), const_cast<gInt&>(right));
return ret;
}
gInt& gInt::operator+=(const gInt& right)
{
mpz_add(value, value, right.value);
mpz_add(value, value, const_cast<gInt&>(right));
return *this;
}
gInt& gInt::operator-=(const gInt& right)
{
mpz_sub(value, value, right.value);
mpz_sub(value, value, const_cast<gInt&>(right));
return *this;
}
@ -124,15 +110,41 @@ namespace gm3p
gInt& gInt::operator/=(const gInt& right)
{
mpz_div(value, value, right.value);
mpz_div(value, value, const_cast<gInt&>(right));
return *this;
}
gInt Mul2Exp(const gInt& value, const mp_bitcnt_t& exp)
{
gInt ret;
mpz_mul_2exp(ret, const_cast<gInt&>(value), exp);
return ret;
}
gInt Neg(const gInt& value)
{
gInt ret;
mpz_neg(ret, const_cast<gInt&>(value));
return ret;
}
gInt Abs(const gInt& value)
{
gInt ret;
mpz_abs(ret, const_cast<gInt&>(value));
return ret;
}
gInt::operator long int() const
{
return mpz_get_si(value);
}
gInt::operator mpz_t&()
{
return value;
}
unsigned long int gInt::ToUnsigned() const
{
return mpz_get_ui(value);

View file

@ -7,17 +7,15 @@ using namespace gm3p;
int main(int argc, char** argv)
{
gInt a("12234234234095780349859034869034635235", 10);
gInt b("123534622305820985092875207398734234753", 10);
gInt a("12");
gInt b = -5;
a *= b;
gInt c = a * b;
std::cout << std::dec << a << " * " << b << " = " << c << std::endl;
std::cout << std::hex << a << " * " << b << " = " << c << std::endl;
std::cout << std::oct << a << " * " << b << " = " << c << std::endl << std::endl << std::dec;
std::cout << c << std::endl << std::endl;
std::cout << -c << std::endl << std::endl;
int d = c;
int d = Abs(c);
std::cout << d << std::endl;
std::cout << c.ToString(NULL, 2) << std::endl;