Use boost::multiprecision::number API (WIP)

This commit is contained in:
Andreas Fabri 2021-10-07 17:29:09 +01:00
parent ef94cd588d
commit f5367709ad
7 changed files with 365 additions and 279 deletions

View File

@ -736,6 +736,7 @@ void test_Type_functions( const CGAL::Euclidean_ring_tag&) {
b = AS(5);
r = CGAL_NTS mod(a,b);
q = CGAL_NTS div(a,b);
std::cout << r << std::endl << q << std::endl;
assert( a == b*q+r);
CGAL_NTS div_mod(a,b,q,r);
assert( a == b*q+r);
@ -876,7 +877,9 @@ void test_algebraic_structure(){
c = b * AS (-3);
assert( c == AS (-15));
c = a;
c += b;
assert( c == AS (6));
c = a;
c -= b;

View File

@ -69,6 +69,7 @@ public:
: RCBigFloat(new BigFloatRep(i)) {}
BigFloat(long& x, const extLong& /*r*/, const extLong& /*a*/)
: RCBigFloat(new BigFloatRep(x)) {}
/// constructor from <tt>BigInt</tt>, error and exponent values
BigFloat(const BigInt& I, unsigned long er, long ex)
: RCBigFloat(new BigFloatRep(I, er, ex)) {}
@ -77,6 +78,7 @@ public:
: RCBigFloat(new BigFloatRep(I, ex)) {}
BigFloat(const BigInt& I)
: RCBigFloat(new BigFloatRep(I)) {}
/// constructor for <tt>BigRat</tt>
BigFloat(const BigRat& R, const extLong& r = get_static_defRelPrec(),
const extLong& a = get_static_defAbsPrec())
@ -214,10 +216,12 @@ public:
double doubleValue() const {
return rep->toDouble();
}
/// return BigInt value
BigInt BigIntValue() const {
return rep->toBigInt();
}
/// return BigRat value
BigRat BigRatValue() const {
return rep->BigRatize();
@ -262,6 +266,7 @@ public:
const BigInt& m() const {
return rep->m;
}
/// get error bits
unsigned long err() const {
return rep->err;
@ -359,10 +364,12 @@ public:
/// \name Utility Functions
//@{
/// approximate BigInt number
void approx(const BigInt& I, const extLong& r, const extLong& a) {
makeCopy();
rep->trunc(I, r, a);
}
/// approximate BigFloat number
void approx(const BigFloat& B, const extLong& r, const extLong& a) {
makeCopy();

View File

@ -24,6 +24,7 @@
#ifndef _CORE_BIGINT_H_
#define _CORE_BIGINT_H_
#include <boost/multiprecision/cpp_int.hpp>
#include <CGAL/CORE/Gmp.h>
#include <CGAL/CORE/RefCount.h>
#include <CGAL/CORE/MemoryPool.h>
@ -31,79 +32,104 @@
namespace CORE {
typedef boost::multiprecision::cpp_int Z;
class BigIntRep : public RCRepImpl<BigIntRep> {
class BigIntRep : public RCRepImpl<BigIntRep > {
public:
BigIntRep() {
mpz_init(mp);
}
// Note : should the copy-ctor be alloed at all ? [Sylvain Pion]
BigIntRep(const BigIntRep& z) : RCRepImpl<BigIntRep>() {
mpz_init_set(mp, z.mp);
}
BigIntRep(signed char c) {
mpz_init_set_si(mp, c);
}
BigIntRep(unsigned char c) {
mpz_init_set_ui(mp, c);
}
BigIntRep(signed int i) {
mpz_init_set_si(mp, i);
}
BigIntRep(unsigned int i) {
mpz_init_set_ui(mp, i);
}
BigIntRep(signed short int s) {
mpz_init_set_si(mp, s);
}
BigIntRep(unsigned short int s) {
mpz_init_set_ui(mp, s);
}
BigIntRep(signed long int l) {
mpz_init_set_si(mp, l);
}
BigIntRep(unsigned long int l) {
mpz_init_set_ui(mp, l);
}
BigIntRep(float f) {
mpz_init_set_d(mp, f);
}
BigIntRep(double d) {
mpz_init_set_d(mp, d);
}
BigIntRep(const char* s, int base=0) {
mpz_init_set_str(mp, s, base);
}
BigIntRep(const std::string& s, int base=0) {
mpz_init_set_str(mp, s.c_str(), base);
}
explicit BigIntRep(mpz_srcptr z) {
mpz_init_set(mp, z);
}
BigIntRep()
: mp()
{}
// Note : should the copy-ctor be allowed at all ? [Sylvain Pion]
BigIntRep(const BigIntRep& z) : RCRepImpl<BigIntRep >(), mp(z.mp)
{}
BigIntRep(signed char c)
: mp(c)
{}
BigIntRep(unsigned char c)
: mp(c)
{}
BigIntRep(signed int i)
: mp(i)
{}
BigIntRep(unsigned int i)
: mp(i)
{}
BigIntRep(signed short int s)
: mp(s)
{}
BigIntRep(unsigned short int s)
: mp(s)
{}
BigIntRep(signed long int l)
: mp(l)
{}
BigIntRep(unsigned long int l)
: mp(l)
{}
BigIntRep(float f)
: mp(f)
{}
BigIntRep(double d)
: mp(d)
{}
BigIntRep(const char* s, int base=0)
: mp(s)
{}
BigIntRep(const std::string& s, int base=0)
: mp(s)
{}
explicit BigIntRep(const Z& z)
: mp(z)
{}
/*
~BigIntRep() {
mpz_clear(mp);
}
*/
CGAL_CORE_EXPORT CORE_NEW(BigIntRep)
CGAL_CORE_EXPORT CORE_DELETE(BigIntRep)
//CGAL_CORE_EXPORT CORE_NEW(BigIntRep)
//CGAL_CORE_EXPORT CORE_DELETE(BigIntRep)
mpz_srcptr get_mp() const {
const Z& get_mp() const {
return mp;
}
mpz_ptr get_mp() {
Z& get_mp() {
return mp;
}
private:
mpz_t mp;
Z mp;
};
typedef RCImpl<BigIntRep> RCBigInt;
class CGAL_CORE_EXPORT BigInt : public RCBigInt {
//typedef RCImpl<BigIntRep> RCBigInt;
class CGAL_CORE_EXPORT BigInt : public RCImpl<BigIntRep> {
public:
typedef RCImpl<BigIntRep> RCBigInt;
/// \name Constructors
//@{
/// default constructor
BigInt() : RCBigInt(new BigIntRep()) {}
BigInt(const Z& z) : RCBigInt(new BigIntRep(z)) {}
/// constructor for <tt>signed char</tt>
BigInt(signed char x) : RCBigInt(new BigIntRep(x)) {}
/// constructor for <tt>unsigned char</tt>
@ -128,8 +154,9 @@ public:
BigInt(const char* s, int base=0) : RCBigInt(new BigIntRep(s, base)) {}
/// constructor for <tt>std::string</tt> with base
BigInt(const std::string& s, int base=0) : RCBigInt(new BigIntRep(s, base)) {}
/// constructor for <tt>mpz_srcptr</tt>
explicit BigInt(mpz_srcptr z) : RCBigInt(new BigIntRep(z)) {}
// explicit BigInt(mpz_srcptr z) : RCBigInt(new BigIntRep(z)) {}
//@}
/// \name Copy-Assignment-Destructor
@ -157,52 +184,53 @@ public:
//@{
BigInt& operator +=(const BigInt& rhs) {
makeCopy();
mpz_add(get_mp(), get_mp(), rhs.get_mp());
get_mp() += rhs.get_mp();
return *this;
}
BigInt& operator -=(const BigInt& rhs) {
makeCopy();
mpz_sub(get_mp(), get_mp(), rhs.get_mp());
get_mp() -= rhs.get_mp();
return *this;
}
BigInt& operator *=(const BigInt& rhs) {
makeCopy();
mpz_mul(get_mp(), get_mp(), rhs.get_mp());
get_mp() *= rhs.get_mp();
return *this;
}
BigInt& operator /=(const BigInt& rhs) {
makeCopy();
mpz_tdiv_q(get_mp(), get_mp(), rhs.get_mp());
get_mp() /= rhs.get_mp();
return *this;
}
BigInt& operator %=(const BigInt& rhs) {
makeCopy();
mpz_tdiv_r(get_mp(), get_mp(), rhs.get_mp());
get_mp() %= rhs.get_mp();
return *this;
}
BigInt& operator &=(const BigInt& rhs) {
makeCopy();
mpz_and(get_mp(), get_mp(), rhs.get_mp());
get_mp() &= rhs.get_mp();
return *this;
}
BigInt& operator |=(const BigInt& rhs) {
makeCopy();
mpz_ior(get_mp(), get_mp(), rhs.get_mp());
get_mp() |= rhs.get_mp();
return *this;
}
BigInt& operator ^=(const BigInt& rhs) {
makeCopy();
mpz_xor(get_mp(), get_mp(), rhs.get_mp());
get_mp() ^= rhs.get_mp();
return *this;
}
BigInt& operator <<=(unsigned long ul) {
makeCopy();
mpz_mul_2exp(get_mp(), get_mp(), ul);
get_mp() <<= ul;
return *this;
}
BigInt& operator >>=(unsigned long ul) {
makeCopy();
mpz_tdiv_q_2exp(get_mp(), get_mp(), ul);
get_mp() >>= ul;
return *this;
}
//@}
@ -214,17 +242,17 @@ public:
}
BigInt operator-() const {
BigInt r;
mpz_neg(r.get_mp(), get_mp());
r.get_mp() = -get_mp();
return r;
}
BigInt& operator++() {
makeCopy();
mpz_add_ui(get_mp(), get_mp(), 1);
++get_mp();
return *this;
}
BigInt& operator--() {
makeCopy();
mpz_sub_ui(get_mp(), get_mp(), 1);
--get_mp();
return *this;
}
BigInt operator++(int) {
@ -246,11 +274,11 @@ public:
return false;
}
/// get mpz pointer (const)
mpz_srcptr get_mp() const {
const Z& get_mp() const {
return rep->get_mp();
}
/// get mpz pointer
mpz_ptr get_mp() {
Z& get_mp() {
return rep->get_mp();
}
//@}
@ -260,16 +288,12 @@ public:
/// set value from <tt>const char*</tt>
int set_str(const char* s, int base = 0) {
makeCopy();
return mpz_set_str(get_mp(), s, base);
get_mp() = Z(s);
return 0; // should be -1 if not correct in the base (we ignore)
}
/// convert to <tt>std::string</tt>
std::string get_str(int base = 10) const {
int n = mpz_sizeinbase (get_mp(), base) + 2;
char *buffer = new char[n];
mpz_get_str(buffer, base, get_mp());
std::string result(buffer);
delete [] buffer;
return result;
return get_mp().convert_to<std::string>();
}
//@}
@ -277,130 +301,152 @@ public:
//@{
/// intValue
int intValue() const {
return static_cast<int>(mpz_get_si(get_mp()));
return get_mp().convert_to<int>();
}
/// longValue
long longValue() const {
return mpz_get_si(get_mp());
return get_mp().convert_to<long>();
}
/// ulongValue
unsigned long ulongValue() const {
return mpz_get_ui(get_mp());
return get_mp().convert_to<unsigned long>();
}
/// doubleValue
double doubleValue() const {
return mpz_get_d(get_mp());
return get_mp().convert_to<double>();
}
//@}
};
inline BigInt operator+(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_add(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator-(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_sub(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator*(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_mul(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator/(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_tdiv_q(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator%(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_tdiv_r(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator&(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_and(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator|(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_ior(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator^(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_xor(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator<<(const BigInt& a, unsigned long ul) {
BigInt r;
mpz_mul_2exp(r.get_mp(), a.get_mp(), ul);
return r;
}
inline BigInt operator>>(const BigInt& a, unsigned long ul) {
BigInt r;
mpz_tdiv_q_2exp(r.get_mp(), a.get_mp(), ul);
r.get_mp() = a.get_mp() + b.get_mp();
return r;
}
inline int cmp(const BigInt& x, const BigInt& y) {
return mpz_cmp(x.get_mp(), y.get_mp());
inline BigInt operator-(const BigInt& a, const BigInt& b) {
BigInt r;
r.get_mp() = a.get_mp() - b.get_mp();
return r;
}
inline BigInt operator*(const BigInt& a, const BigInt& b) {
BigInt r;
r.get_mp() = a.get_mp() * b.get_mp();
return r;
}
inline BigInt operator/(const BigInt& a, const BigInt& b) {
BigInt r;
r.get_mp() = a.get_mp() / b.get_mp();
return r;
}
inline BigInt operator%(const BigInt& a, const BigInt& b) {
BigInt r;
r.get_mp() = a.get_mp() % b.get_mp();
return r;
}
inline BigInt operator&(const BigInt& a, const BigInt& b) {
BigInt r;
r.get_mp() = a.get_mp() & b.get_mp();
return r;
}
inline BigInt operator|(const BigInt& a, const BigInt& b) {
BigInt r;
r.get_mp() = a.get_mp()| b.get_mp();
return r;
}
inline BigInt operator^(const BigInt& a, const BigInt& b) {
BigInt r;
r.get_mp() = a.get_mp() ^ b.get_mp();
return r;
}
inline BigInt operator<<(const BigInt& a, unsigned long ul) {
BigInt r;
r.get_mp() = a.get_mp() << ul;
return r;
}
inline BigInt operator>>(const BigInt& a, unsigned long ul) {
BigInt r;
r.get_mp() = a.get_mp() >> ul;
return r;
}
inline int cmp(const BigInt& x, const BigInt& y) {
return x.get_mp().compare(y.get_mp());
}
inline bool operator==(const BigInt& a, const BigInt& b) {
return cmp(a, b) == 0;
}
inline bool operator!=(const BigInt& a, const BigInt& b) {
return cmp(a, b) != 0;
}
inline bool operator>=(const BigInt& a, const BigInt& b) {
return cmp(a, b) >= 0;
}
inline bool operator>(const BigInt& a, const BigInt& b) {
return cmp(a, b) > 0;
}
inline bool operator<=(const BigInt& a, const BigInt& b) {
return cmp(a, b) <= 0;
}
inline bool operator<(const BigInt& a, const BigInt& b) {
return cmp(a, b) < 0;
}
inline std::ostream& operator<<(std::ostream& o, const BigInt& x) {
//return CORE::operator<<(o, x.get_mp());
return CORE::io_write(o, x.get_mp());
return o <<x.get_mp();
}
inline std::istream& operator>>(std::istream& i, BigInt& x) {
x.makeCopy();
//return CORE::operator>>(i, x.get_mp());
return CORE::io_read(i, x.get_mp());
return i >> x.get_mp();
}
/// sign
inline int sign(const BigInt& a) {
return mpz_sgn(a.get_mp());
return sign(a.get_mp());
}
/// abs
inline BigInt abs(const BigInt& a) {
BigInt r;
mpz_abs(r.get_mp(), a.get_mp());
r.get_mp() = abs(a.get_mp());
return r;
}
/// neg
inline BigInt neg(const BigInt& a) {
BigInt r;
mpz_neg(r.get_mp(), a.get_mp());
r.get_mp() = - a.get_mp();
return r;
}
/// negate
inline void negate(BigInt& a) {
a.makeCopy();
mpz_neg(a.get_mp(), a.get_mp());
a.get_mp() = - a.get_mp();
}
/// cmpabs
inline int cmpabs(const BigInt& a, const BigInt& b) {
return mpz_cmpabs(a.get_mp(), b.get_mp());
assert(false);
// return mpz_cmpabs(a.get_mp(), b.get_mp()); AF: todo
return 0;
}
/// \name Conversion Functions
@ -409,63 +455,80 @@ inline int cmpabs(const BigInt& a, const BigInt& b) {
inline long longValue(const BigInt& a) {
return a.longValue();
}
/// ulongValue
inline unsigned long ulongValue(const BigInt& a) {
return a.ulongValue();
}
/// doubleValue
inline double doubleValue(const BigInt& a) {
return a.doubleValue();
}
//@}
/*
/// \name File I/O Functions
//@{
/// read from file
void readFromFile(BigInt& z, std::istream& in, long maxLength = 0);
/// write to file
void writeToFile(const BigInt& z, std::ostream& in, int base=10, int charsPerLine=80);
//@}
*/
/// \name Misc Functions
//@{
/// isEven
inline bool isEven(const BigInt& z) {
return mpz_even_p(z.get_mp());
return bit_test(z.get_mp(),0) == 0;
}
/// isOdd
inline bool isOdd(const BigInt& z) {
return mpz_odd_p(z.get_mp());
return bit_test(z.get_mp(),0) == 1;
}
/// get exponent of power 2
inline unsigned long getBinExpo(const BigInt& z) {
return mpz_scan1(z.get_mp(), 0);
return lsb(abs(z.get_mp()));
}
/// get exponent of power k
inline void getKaryExpo(const BigInt& z, BigInt& m, int& e, unsigned long k) {
assert(false); // AF: todo
/*
mpz_t f;
mpz_init_set_ui(f, k);
m.makeCopy();
e = mpz_remove(m.get_mp(), z.get_mp(), f);
mpz_clear(f);
*/
}
/// divisible(x,y) = "x | y"
inline bool isDivisible(const BigInt& x, const BigInt& y) {
return mpz_divisible_p(x.get_mp(), y.get_mp()) != 0;
assert(false);
return true; // AF mpz_divisible_p(x.get_mp(), y.get_mp()) != 0;
}
inline bool isDivisible(int x, int y) {
return x % y == 0;
}
inline bool isDivisible(long x, long y) {
return x % y == 0;
}
/// exact div
inline void divexact(BigInt& z, const BigInt& x, const BigInt& y) {
z.makeCopy();
mpz_divexact(z.get_mp(), x.get_mp(), y.get_mp());
assert(false);
// AF: todo mpz_divexact(z.get_mp(), x.get_mp(), y.get_mp());
}
// Chee (1/12/2004) The definition of div_exact(x,y) next
// ensure that in Polynomials<NT> works with both NT=BigInt and NT=int:
inline BigInt div_exact(const BigInt& x, const BigInt& y) {
@ -473,9 +536,11 @@ inline BigInt div_exact(const BigInt& x, const BigInt& y) {
divexact(z, x, y); // z is set to x/y;
return z;
}
inline int div_exact(int x, int y) {
return x/y; // precondition: isDivisible(x,y)
}
inline long div_exact(long x, long y) {
return x/y; // precondition: isDivisible(x,y)
}
@ -484,19 +549,21 @@ inline long div_exact(long x, long y) {
/// gcd
inline BigInt gcd(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_gcd(r.get_mp(), a.get_mp(), b.get_mp());
r.get_mp() = gcd(a.get_mp(), b.get_mp());
return r;
}
/// div_rem
inline void div_rem(BigInt& q, BigInt& r, const BigInt& a, const BigInt& b) {
q.makeCopy();
r.makeCopy();
mpz_tdiv_qr(q.get_mp(), r.get_mp(), a.get_mp(), b.get_mp());
divide_qr(a.get_mp(), b.get_mp(), q.get_mp(), r.get_mp());
}
/// power
inline void power(BigInt& c, const BigInt& a, unsigned long ul) {
c.makeCopy();
mpz_pow_ui(c.get_mp(), a.get_mp(), ul);
c.get_mp() = pow(a.get_mp(), ul);
}
// pow
@ -508,8 +575,9 @@ inline BigInt pow(const BigInt& a, unsigned long ui) {
// bit length
inline int bitLength(const BigInt& a) {
return mpz_sizeinbase(a.get_mp(), 2);
return msb(abs(a.get_mp())); /// AF todo was mpz_sizeinbase(a.get_mp(), 2);
}
/// floorLg -- floor of log_2(a)
/** Convention: a=0, floorLg(a) returns -1.
* This makes sense for integer a.
@ -517,6 +585,7 @@ inline int bitLength(const BigInt& a) {
inline long floorLg(const BigInt& a) {
return (sign(a) == 0) ? (-1) : (bitLength(a)-1);
}
/// ceilLg -- ceiling of log_2(a) where a=BigInt, int or long
/** Convention: a=0, ceilLg(a) returns -1.
* This makes sense for integer a.
@ -525,11 +594,14 @@ inline long ceilLg(const BigInt& a) {
if (sign(a) == 0)
return -1;
unsigned long len = bitLength(a);
return (mpz_scan1(a.get_mp(), 0) == len-1) ? (len-1) : len;
return (lsb(a.get_mp()) == len - 1) ? (len - 1) : len;
}
inline long ceilLg(long a) { // need this for Polynomial<long>
return ceilLg(BigInt(a));
}
inline long ceilLg(int a) { // need this for Polynomial<int>
return ceilLg(BigInt(a));
}

View File

@ -29,93 +29,87 @@
namespace CORE {
typedef boost::multiprecision::cpp_rational Q;
class BigRatRep : public RCRepImpl<BigRatRep> {
public:
BigRatRep() {
mpq_init(mp);
}
BigRatRep()
: mp()
{}
// Note : should the copy-ctor be alloed at all ? [Sylvain Pion]
BigRatRep(const BigRatRep& z) : RCRepImpl<BigRatRep>() {
mpq_init(mp);
mpq_set(mp, z.mp);
}
BigRatRep(signed char c) {
mpq_init(mp);
mpq_set_si(mp, c, 1);
}
BigRatRep(unsigned char c) {
mpq_init(mp);
mpq_set_ui(mp, c, 1);
}
BigRatRep(signed int i) {
mpq_init(mp);
mpq_set_si(mp, i, 1);
}
BigRatRep(unsigned int i) {
mpq_init(mp);
mpq_set_ui(mp, i, 1);
}
BigRatRep(signed short int s) {
mpq_init(mp);
mpq_set_si(mp, s, 1);
}
BigRatRep(unsigned short int s) {
mpq_init(mp);
mpq_set_ui(mp, s, 1);
}
BigRatRep(signed long int l) {
mpq_init(mp);
mpq_set_si(mp, l, 1);
}
BigRatRep(unsigned long int l) {
mpq_init(mp);
mpq_set_ui(mp, l, 1);
}
BigRatRep(float f) {
mpq_init(mp);
mpq_set_d(mp, f);
}
BigRatRep(double d) {
mpq_init(mp);
mpq_set_d(mp, d);
}
BigRatRep(const char* s) {
mpq_init(mp);
mpq_set_str(mp, s, 0);
}
BigRatRep(const std::string& s) {
mpq_init(mp);
mpq_set_str(mp, s.c_str(), 0);
}
explicit BigRatRep(mpq_srcptr q) {
mpq_init(mp);
mpq_set(mp, q);
}
BigRatRep(mpz_srcptr z) {
mpq_init(mp);
mpq_set_z(mp, z);
}
BigRatRep(mpz_srcptr n, mpz_srcptr d) {
mpq_init(mp);
mpz_set(mpq_numref(mp), n);
mpz_set(mpq_denref(mp), d);
mpq_canonicalize(mp);
}
~BigRatRep() {
mpq_clear(mp);
}
BigRatRep(const BigRatRep& z) : RCRepImpl<BigRatRep>(), mp(z.mp)
{}
CGAL_CORE_EXPORT CORE_NEW(BigRatRep)
CGAL_CORE_EXPORT CORE_DELETE(BigRatRep)
mpq_srcptr get_mp() const {
BigRatRep(signed char c)
: mp(c)
{}
BigRatRep(unsigned char c)
: mp(c)
{}
BigRatRep(signed int i)
: mp(i)
{}
BigRatRep(unsigned int i)
: mp(i)
{}
BigRatRep(signed short int s)
: mp(s)
{}
BigRatRep(unsigned short int s)
: mp(s)
{}
BigRatRep(signed long int l)
: mp(l)
{}
BigRatRep(unsigned long int l)
: mp(l)
{}
BigRatRep(float f)
: mp(f)
{}
BigRatRep(double d)
: mp(d)
{}
BigRatRep(const char* s)
: mp(s)
{}
BigRatRep(const std::string& s)
: mp(s)
{}
explicit BigRatRep(const Z& q)
: mp(q)
{}
BigRatRep(const Z& n, const Z& d)
: mp(n,d)
{}
//CGAL_CORE_EXPORT CORE_NEW(BigRatRep)
// CGAL_CORE_EXPORT CORE_DELETE(BigRatRep)
const Q& get_mp() const {
return mp;
}
mpq_ptr get_mp() {
Q& get_mp() {
return mp;
}
private:
mpq_t mp;
Q mp;
}; //BigRatRep
class BigFloat;
@ -152,7 +146,7 @@ public:
/// constructor for <tt>std::string</tt> with base
BigRat(const std::string& s) : RCBigRat(new BigRatRep(s)) {}
/// constructor for <tt>mpq_srcptr</tt>
explicit BigRat(mpq_srcptr z) : RCBigRat(new BigRatRep(z)) {}
explicit BigRat(const Z& z) : RCBigRat(new BigRatRep(z)) {}
/// constructor for <tt>BigInt</tt>
BigRat(const BigInt& z) : RCBigRat(new BigRatRep(z.get_mp())) {}
/// constructor for two <tt>BigInts</tt>
@ -187,32 +181,34 @@ public:
//@{
BigRat& operator +=(const BigRat& rhs) {
makeCopy();
mpq_add(get_mp(), get_mp(), rhs.get_mp());
get_mp() += rhs.get_mp();
return *this;
}
BigRat& operator -=(const BigRat& rhs) {
makeCopy();
mpq_sub(get_mp(), get_mp(), rhs.get_mp());
get_mp() -= rhs.get_mp();
return *this;
}
BigRat& operator *=(const BigRat& rhs) {
makeCopy();
mpq_mul(get_mp(), get_mp(), rhs.get_mp());
get_mp() *= rhs.get_mp();
return *this;
}
BigRat& operator /=(const BigRat& rhs) {
makeCopy();
mpq_div(get_mp(), get_mp(), rhs.get_mp());
get_mp() /= rhs.get_mp();
return *this;
}
BigRat& operator <<=(unsigned long ul) {
makeCopy();
mpq_mul_2exp(get_mp(), get_mp(), ul);
assert(false);
// AF no shift for Q get_mp() <<= ul;
return *this;
}
BigRat& operator >>=(unsigned long ul) {
makeCopy();
mpq_div_2exp(get_mp(), get_mp(), ul);
assert(false);
// AF no >> for Q get_mp() >>= ul;
return *this;
}
//@}
@ -223,7 +219,7 @@ public:
/// exact division by 2 (this method is provided for compatibility)
BigRat div2() const {
BigRat r; BigRat t(2); // probably not most efficient way
mpq_div(r.get_mp(), get_mp(), t.get_mp());
r.get_mp() /= t.get_mp();
return r;
}
BigRat operator+() const {
@ -231,17 +227,17 @@ public:
}
BigRat operator-() const {
BigRat r;
mpq_neg(r.get_mp(), get_mp());
r.get_mp() = - get_mp();
return r;
}
BigRat& operator++() {
makeCopy();
mpz_add(get_num_mp(),get_num_mp(),get_den_mp());
get_num_mp() += get_den_mp();
return *this;
}
BigRat& operator--() {
makeCopy();
mpz_sub(get_num_mp(),get_num_mp(),get_den_mp());
get_num_mp() -= get_den_mp();
return *this;
}
BigRat operator++(int) {
@ -261,7 +257,8 @@ public:
/// Canonicalize
void canonicalize() {
makeCopy();
mpq_canonicalize(get_mp());
assert(false);
// AF todo mpq_canonicalize(get_mp());
}
/// Has Exact Division
static bool hasExactDivision() {
@ -269,28 +266,28 @@ public:
}
/// return mpz pointer of numerator (const)
mpz_srcptr get_num_mp() const {
return mpq_numref(get_mp());
Z get_num_mp() const {
return numerator(get_mp());
}
/// return mpz pointer of numerator
mpz_ptr get_num_mp() {
return mpq_numref(get_mp());
/// return mpz pointer of numerator // no references as numerator() returns a copy
Z get_num_mp() {
return numerator(get_mp());
}
/// return mpz pointer of denominator
mpz_srcptr get_den_mp() const {
return mpq_denref(get_mp());
Z get_den_mp() const {
return denominator(get_mp());
}
/// return mpz pointer of denominator
mpz_ptr get_den_mp() {
return mpq_denref(get_mp());
Z get_den_mp() {
return denominator(get_mp());
}
/// get mpq pointer (const)
mpq_srcptr get_mp() const {
const Q& get_mp() const {
return rep->get_mp();
}
/// get mpq pointer
mpq_ptr get_mp() {
Q& get_mp() {
return rep->get_mp();
}
//@}
@ -300,16 +297,12 @@ public:
/// set value from <tt>const char*</tt>
int set_str(const char* s, int base = 0) {
makeCopy();
return mpq_set_str(get_mp(), s, base);
get_mp() = Q(s);
return 0;
}
/// convert to <tt>std::string</tt>
std::string get_str(int base = 10) const {
int n = mpz_sizeinbase(mpq_numref(get_mp()), base) + mpz_sizeinbase(mpq_denref(get_mp()), base)+ 3;
char *buffer = new char[n];
mpq_get_str(buffer, base, get_mp());
std::string result(buffer);
delete [] buffer;
return result;
return get_mp().convert_to<std::string>();
}
//@}
@ -317,20 +310,21 @@ public:
//@{
/// intValue
int intValue() const {
return static_cast<int>(doubleValue());
return get_mp().convert_to<int>();
}
/// longValue
long longValue() const {
return static_cast<long>(doubleValue());
return get_mp().convert_to<long>();
}
/// doubleValue
double doubleValue() const {
return mpq_get_d(get_mp());
return get_mp().convert_to<double>();
}
/// BigIntValue
BigInt BigIntValue() const {
BigInt r;
mpz_tdiv_q(r.get_mp(), get_num_mp(), get_den_mp());
r.get_mp() = Z( get_num_mp() / get_den_mp()); // AF check that this is the integer divsion without rest
return r;
}
//@}
@ -338,22 +332,22 @@ public:
inline BigRat operator+(const BigRat& a, const BigRat& b) {
BigRat r;
mpq_add(r.get_mp(), a.get_mp(), b.get_mp());
r.get_mp() = a.get_mp() + b.get_mp();
return r;
}
inline BigRat operator-(const BigRat& a, const BigRat& b) {
BigRat r;
mpq_sub(r.get_mp(), a.get_mp(), b.get_mp());
r.get_mp() = a.get_mp() - b.get_mp();
return r;
}
inline BigRat operator*(const BigRat& a, const BigRat& b) {
BigRat r;
mpq_mul(r.get_mp(), a.get_mp(), b.get_mp());
r.get_mp() = a.get_mp() * b.get_mp();
return r;
}
inline BigRat operator/(const BigRat& a, const BigRat& b) {
BigRat r;
mpq_div(r.get_mp(), a.get_mp(), b.get_mp());
r.get_mp() = a.get_mp() / b.get_mp();
return r;
}
// Chee (3/19/2004):
@ -362,7 +356,7 @@ inline BigRat operator/(const BigRat& a, const BigRat& b) {
/// divisible(x,y) = "x | y"
inline BigRat div_exact(const BigRat& x, const BigRat& y) {
BigRat z;
mpq_div(z.get_mp(), x.get_mp(), y.get_mp());
z.get_mp() = x.get_mp() / y.get_mp();
return z;
}
/// numerator
@ -396,22 +390,22 @@ inline bool isInteger(const BigRat& x) {
}
inline bool isDivisible(const BigRat& x, const BigRat& y) {
BigRat r;
mpq_div(r.get_mp(), x.get_mp(), y.get_mp());
r.get_mp() = x.get_mp() / y.get_mp();
return isInteger(r);
}
inline BigRat operator<<(const BigRat& a, unsigned long ul) {
BigRat r;
mpq_mul_2exp(r.get_mp(), a.get_mp(), ul);
//AF todo no << for Q r.get_mp() = a.get_mp() << int(ul) ;
return r;
}
inline BigRat operator>>(const BigRat& a, unsigned long ul) {
BigRat r;
mpq_div_2exp(r.get_mp(), a.get_mp(), ul);
// AF todo no >> for Q r.get_mp() = a.get_mp() >> ul;
return r;
}
inline int cmp(const BigRat& x, const BigRat& y) {
return mpq_cmp(x.get_mp(), y.get_mp());
return x.get_mp().compare(y.get_mp());
}
inline bool operator==(const BigRat& a, const BigRat& b) {
return cmp(a, b) == 0;
@ -433,29 +427,27 @@ inline bool operator<(const BigRat& a, const BigRat& b) {
}
inline std::ostream& operator<<(std::ostream& o, const BigRat& x) {
//return CORE::operator<<(o, x.get_mp());
return CORE::io_write(o, x.get_mp());
return o << x.get_mp();
}
inline std::istream& operator>>(std::istream& i, BigRat& x) {
x.makeCopy();
//return CORE::operator>>(i, x.get_mp());
return CORE::io_read(i, x.get_mp());
return i >> x.get_mp();
}
/// sign
inline int sign(const BigRat& a) {
return mpq_sgn(a.get_mp());
return sign(a.get_mp());
}
/// abs
inline BigRat abs(const BigRat& a) {
BigRat r;
mpq_abs(r.get_mp(), a.get_mp());
r.get_mp() = abs( a.get_mp());
return r;
}
/// neg
inline BigRat neg(const BigRat& a) {
BigRat r;
mpq_neg(r.get_mp(), a.get_mp());
r.get_mp() = - a.get_mp();
return r;
}
/// div2

View File

@ -268,6 +268,9 @@ void readFromFile(BigInt& z, std::istream& in, long maxLength) {
CGAL_INLINE_FUNCTION
void writeToFile(const BigInt& z, std::ostream& out, int base, int charsPerLine) {
assert(false);
/*
BigInt c = abs(z);
// get the absoulte value string
@ -286,10 +289,14 @@ void writeToFile(const BigInt& z, std::ostream& out, int base, int charsPerLine)
write_base_number(out, buffer, length, base, charsPerLine);
out << "\n";
delete[] buffer;
*/
}
CGAL_INLINE_FUNCTION
void readFromFile(BigFloat& bf, std::istream& in, long maxLength) {
assert(false);
/*
char *buffer;
long length;
long exponent;
@ -326,10 +333,14 @@ void readFromFile(BigFloat& bf, std::istream& in, long maxLength) {
// construct BigFloat
bf = BigFloat(mantissa, 0, exponent);
*/
}
CGAL_INLINE_FUNCTION
void writeToFile(const BigFloat& bf, std::ostream& out, int base, int charsPerLine) {
assert(false);
/*
BigInt c(CORE::abs(bf.m()));
// get the absoulte value string
@ -351,6 +362,7 @@ void writeToFile(const BigFloat& bf, std::ostream& out, int base, int charsPerLi
write_base_number(out, buffer, length, base, charsPerLine);
out << '\n';
delete[] buffer;
*/
}
/* Underconstruction ----

View File

@ -1215,8 +1215,8 @@ void BinOpRep::debugTree(int level, int indent, int depthLimit) const {
second->debugTree(level, indent + 2, depthLimit - 1);
}
CORE_MEMORY_IMPL(BigIntRep)
CORE_MEMORY_IMPL(BigRatRep)
// AF CORE_MEMORY_IMPL(BigIntRep)
// AF CORE_MEMORY_IMPL(BigRatRep)
CORE_MEMORY_IMPL(ConstDoubleRep)
CORE_MEMORY_IMPL(ConstRealRep)

View File

@ -48,7 +48,7 @@ template <> class Algebraic_structure_traits< CORE::BigInt >
//! computes the largest NT not larger than the square root of \a a.
Type operator()( const Type& x) const {
Type result;
mpz_sqrt(result.get_mp(), x.get_mp());
result.get_mp() = sqrt( x.get_mp());
return result;
}
};
@ -63,7 +63,7 @@ template <> class Algebraic_structure_traits< CORE::BigInt >
if ( x == Type(0) && y == Type(0) )
return Type(0);
Type result;
mpz_gcd(result.get_mp(), x.get_mp(), y.get_mp());
result.get_mp() = gcd( x.get_mp(), y.get_mp());
return result;
}
};