34 #ifdef SOPLEX_WITH_GMP 39 #define SOPLEX_PERFALT_1 42 #define SOPLEX_PERFALT_2a 45 #define SOPLEX_PERFALT_2b 48 #define SOPLEX_PERFALT_3 51 #define SOPLEX_PERFALT_4 54 #define SOPLEX_PERFALT_5a 57 #define SOPLEX_PERFALT_5b 62 #ifdef SOPLEX_WITH_GMP 76 #ifdef SOPLEX_NOLISTMEM 87 #ifdef SOPLEX_WITH_GMP 105 mpq_init(privatevalue);
115 mpq_init(privatevalue);
124 mpq_init(privatevalue);
126 if(r == (
long double)(1.0))
128 else if(r == (
long double)(-1.0))
130 else if(r == (
long double)(0.0))
135 mpq_set_d(privatevalue,
double(r));
143 mpq_init(privatevalue);
154 mpq_set_d(privatevalue, r);
162 mpq_init(privatevalue);
173 mpq_set_si(privatevalue, i, 1);
181 mpq_init(privatevalue);
182 mpq_set(privatevalue, q);
188 mpq_clear(privatevalue);
194 #ifdef SOPLEX_PERFALT_4 210 if(r == (
long double)(0.0))
212 #ifdef SOPLEX_PERFALT_5a 213 #ifdef SOPLEX_PERFALT_1 215 if(mpq_sgn(privatevalue) != 0)
222 else if(r == (
long double)(1.0))
224 #ifdef SOPLEX_PERFALT_5b 230 else if(r == (
long double)(-1.0))
232 #ifdef SOPLEX_PERFALT_5b 239 mpq_set_d(this->privatevalue,
double(r));
250 #ifdef SOPLEX_PERFALT_5a 251 #ifdef SOPLEX_PERFALT_1 253 if(mpq_sgn(privatevalue) != 0)
262 #ifdef SOPLEX_PERFALT_5b 270 #ifdef SOPLEX_PERFALT_5b 277 mpq_set_d(privatevalue, r);
288 #ifdef SOPLEX_PERFALT_5a 289 #ifdef SOPLEX_PERFALT_1 291 if(mpq_sgn(privatevalue) != 0)
300 #ifdef SOPLEX_PERFALT_5b 308 #ifdef SOPLEX_PERFALT_5b 315 mpq_set_si(privatevalue, i, 1);
323 #ifdef SOPLEX_PERFALT_4 325 if(mpq_equal(this->privatevalue, q) != 0)
331 mpq_set(this->privatevalue, q);
379 if(Rational::useListMem)
411 if(Rational::useListMem)
444 if(Rational::useListMem)
477 if(Rational::useListMem)
510 if(Rational::useListMem)
543 if(Rational::useListMem)
578 if(!Rational::useListMem ||
this == &Rational::ZERO ||
this == &Rational::POSONE
579 ||
this == &Rational::NEGONE)
600 Rational::useListMem =
true;
623 Rational::useListMem =
false;
674 Rational::operator double()
const 676 #ifdef SOPLEX_PERFALT_3 677 #ifdef SOPLEX_PERFALT_1 697 Rational::operator
long double()
const 699 #ifdef SOPLEX_PERFALT_3 700 #ifdef SOPLEX_PERFALT_1 754 #ifdef SOPLEX_PERFALT_2a 755 #ifdef SOPLEX_PERFALT_1 782 #ifdef SOPLEX_PERFALT_2a 783 #ifdef SOPLEX_PERFALT_1 813 #ifdef SOPLEX_PERFALT_2a 814 #ifdef SOPLEX_PERFALT_1 839 return (*
this += Rational::POSONE);
841 return (*
this += Rational::NEGONE);
844 #ifdef SOPLEX_PERFALT_2a 845 #ifdef SOPLEX_PERFALT_1 874 #ifdef SOPLEX_PERFALT_2a 875 #ifdef SOPLEX_PERFALT_1 900 return (*
this += Rational::POSONE);
902 return (*
this += Rational::NEGONE);
905 #ifdef SOPLEX_PERFALT_2a 906 #ifdef SOPLEX_PERFALT_1 931 #ifdef SOPLEX_PERFALT_2a 932 #ifdef SOPLEX_PERFALT_1 959 #ifdef SOPLEX_PERFALT_2a 960 #ifdef SOPLEX_PERFALT_1 998 #ifdef SOPLEX_PERFALT_2a 999 #ifdef SOPLEX_PERFALT_1 1025 return (*
this -= Rational::POSONE);
1027 return (*
this -= Rational::NEGONE);
1030 #ifdef SOPLEX_PERFALT_2a 1031 #ifdef SOPLEX_PERFALT_1 1034 return (*
this = -d);
1039 return (*
this = -d);
1060 #ifdef SOPLEX_PERFALT_2a 1061 #ifdef SOPLEX_PERFALT_1 1087 return (*
this -= Rational::POSONE);
1089 return (*
this -= Rational::NEGONE);
1092 #ifdef SOPLEX_PERFALT_2a 1093 #ifdef SOPLEX_PERFALT_1 1096 return (*
this = -d);
1101 return (*
this = -d);
1118 #ifdef SOPLEX_PERFALT_2b 1119 #ifdef SOPLEX_PERFALT_1 1155 #ifdef SOPLEX_PERFALT_2b 1156 #ifdef SOPLEX_PERFALT_1 1159 return (*
this = Rational::ZERO);
1166 return (*
this = Rational::ZERO);
1209 #ifdef SOPLEX_PERFALT_2b 1210 #ifdef SOPLEX_PERFALT_1 1240 return (*
this = Rational::ZERO);
1250 #ifdef SOPLEX_PERFALT_2b 1251 #ifdef SOPLEX_PERFALT_1 1296 #ifdef SOPLEX_PERFALT_2b 1297 #ifdef SOPLEX_PERFALT_1 1327 return (*
this = Rational::ZERO);
1337 #ifdef SOPLEX_PERFALT_2b 1338 #ifdef SOPLEX_PERFALT_1 1371 #ifdef SOPLEX_PERFALT_2b 1372 #ifdef SOPLEX_PERFALT_1 1413 #ifdef SOPLEX_PERFALT_2b 1414 #ifdef SOPLEX_PERFALT_1 1461 #ifdef SOPLEX_PERFALT_2b 1462 #ifdef SOPLEX_PERFALT_1 1509 #ifdef SOPLEX_PERFALT_2b 1510 #ifdef SOPLEX_PERFALT_1 1553 #ifdef SOPLEX_PERFALT_2b 1554 #ifdef SOPLEX_PERFALT_1 1601 #ifdef SOPLEX_PERFALT_2b 1602 #ifdef SOPLEX_PERFALT_1 1639 #ifdef SOPLEX_PERFALT_2b 1662 #if 0 // currently, SoPlex calls this method only with nonzero r and s, hence we do not check this case 1663 #ifdef SOPLEX_PERFALT_1 1690 #ifdef SOPLEX_PERFALT_2b 1713 #if 0 // currently, SoPlex calls this method only with nonzero r and s, hence we do not check this case 1714 #ifdef SOPLEX_PERFALT_1 1743 #ifdef SOPLEX_PERFALT_2b 1744 #ifdef SOPLEX_PERFALT_1 1780 #ifdef SOPLEX_PERFALT_2b 1781 #ifdef SOPLEX_PERFALT_1 1829 " to power of two" <<
"\n");
1834 mpz_sub_ui(roundval, roundval, 1);
1836 MSG_DEBUG(std::cout <<
" --> " << mpz_get_str(0, 10, roundval) <<
"\n");
1838 size_t binlog = mpz_sizeinbase(roundval, 2);
1840 MSG_DEBUG(std::cout <<
" --> 2^" << binlog <<
"\n");
1842 mpz_ui_pow_ui(roundval, 2, binlog);
1844 MSG_DEBUG(std::cout <<
" --> " << mpz_get_str(0, 10, roundval) <<
"\n");
1847 mpz_clear(roundval);
1915 return ((a == d) || (b == d));
1937 #define MAX_STR_LEN 10000 1948 if(strchr(s,
'/') != 0 || strpbrk(s,
".eE") == 0)
1950 pos = (*s ==
'+') ? s + 1 : s;
1964 bool has_exponent =
false;
1965 bool has_dot =
false;
1967 bool has_digits =
false;
1968 bool has_emptyexponent =
false;
1969 long int exponent = 0;
1970 long int decshift = 0;
1972 mpz_init(shiftpower);
1973 mpq_t shiftpowerRational;
1974 mpq_init(shiftpowerRational);
1981 if((*pos ==
'+') || (*pos ==
'-'))
1985 while((*pos >=
'0') && (*pos <=
'9'))
2000 while((*pos >=
'0') && (*pos <=
'9'))
2008 if(tolower(*pos) ==
'e')
2011 has_exponent =
true;
2013 has_emptyexponent =
true;
2017 if((*pos ==
'+') || (*pos ==
'-'))
2021 while((*pos >=
'0') && (*pos <=
'9'))
2023 has_emptyexponent =
false;
2028 if(has_emptyexponent || !has_digits)
2031 assert(has_exponent || has_dot);
2040 while(((*pos >=
'0') && (*pos <=
'9')) || *pos ==
'+' || *pos ==
'-')
2054 while((*pos >=
'0') && (*pos <=
'9'))
2070 exponent = -decshift;
2072 if(tolower(*pos) ==
'e')
2075 assert(has_exponent);
2077 for(t = tmp; *pos !=
'\0'; pos++)
2081 exponent += atol(tmp);
2086 mpz_ui_pow_ui(shiftpower, 10, exponent);
2087 mpq_set_z(shiftpowerRational, shiftpower);
2090 else if(exponent < 0)
2092 mpz_ui_pow_ui(shiftpower, 10, -exponent);
2093 mpq_set_z(shiftpowerRational, shiftpower);
2099 mpz_clear(shiftpower);
2100 mpq_clear(shiftpowerRational);
2111 #if defined(_WIN32) || defined(_WIN64) || defined(__APPLE__) 2112 std::stringstream sstream;
2114 return sstream.str();
2119 std::stringstream sstream;
2121 return sstream.str();
2129 tmpStream = fmemopen(tmpString, 63,
"w");
2130 mpf_init2(tmpFloat, 256);
2131 mpf_set_q(tmpFloat, r.dpointer->privatevalue);
2132 mpf_out_str(tmpStream, 10, precision, tmpFloat);
2133 mpf_clear(tmpFloat);
2136 std::string retString = std::string(tmpString);
2155 if(strchr(s,
'/') != 0 || strpbrk(s,
".eE") == 0)
2157 pos = (*s ==
'+') ? s + 1 : s;
2170 bool has_exponent =
false;
2171 bool has_dot =
false;
2173 bool has_digits =
false;
2174 bool has_emptyexponent =
false;
2175 long int exponent = 0;
2176 long int decshift = 0;
2178 mpz_init(shiftpower);
2179 mpq_t shiftpowerRational;
2180 mpq_init(shiftpowerRational);
2187 if((*pos ==
'+') || (*pos ==
'-'))
2191 while((*pos >=
'0') && (*pos <=
'9'))
2206 while((*pos >=
'0') && (*pos <=
'9'))
2214 if(tolower(*pos) ==
'e')
2217 has_exponent =
true;
2219 has_emptyexponent =
true;
2223 if((*pos ==
'+') || (*pos ==
'-'))
2227 while((*pos >=
'0') && (*pos <=
'9'))
2229 has_emptyexponent =
false;
2234 if(has_emptyexponent || !has_digits)
2237 assert(has_exponent || has_dot);
2246 while(((*pos >=
'0') && (*pos <=
'9')) || *pos ==
'+' || *pos ==
'-')
2260 while((*pos >=
'0') && (*pos <=
'9'))
2276 exponent = -decshift;
2278 if(tolower(*pos) ==
'e')
2281 assert(has_exponent);
2283 for(t = tmp; *pos !=
'\0'; pos++)
2287 exponent += atol(tmp);
2292 mpz_ui_pow_ui(shiftpower, 10, exponent);
2293 mpq_set_z(shiftpowerRational, shiftpower);
2296 else if(exponent < 0)
2298 mpz_ui_pow_ui(shiftpower, 10, -exponent);
2299 mpq_set_z(shiftpowerRational, shiftpower);
2304 mpz_clear(shiftpower);
2305 mpq_clear(shiftpowerRational);
2385 #ifdef SOPLEX_PERFALT_1 2405 #ifdef SOPLEX_PERFALT_1 2425 #ifdef SOPLEX_PERFALT_1 2445 #ifdef SOPLEX_PERFALT_1 2465 #ifdef SOPLEX_PERFALT_1 2485 #ifdef SOPLEX_PERFALT_1 2553 #ifdef SOPLEX_PERFALT_1 2573 #ifdef SOPLEX_PERFALT_1 2593 #ifdef SOPLEX_PERFALT_1 2613 #ifdef SOPLEX_PERFALT_1 2633 #ifdef SOPLEX_PERFALT_1 2653 #ifdef SOPLEX_PERFALT_1 2787 #ifdef SOPLEX_PERFALT_1 2807 #ifdef SOPLEX_PERFALT_1 2827 #ifdef SOPLEX_PERFALT_1 2847 #ifdef SOPLEX_PERFALT_1 2867 #ifdef SOPLEX_PERFALT_1 2887 #ifdef SOPLEX_PERFALT_1 3048 assert(vector != 0);
3049 assert(length >= 0);
3054 for(
int i = 0; i < length; i++)
3065 assert(vector != 0);
3066 assert(length >= 0);
3071 mpz_init_set_ui(lcm, 1);
3073 for(
int i = 0; i < length; i++)
3074 mpz_lcm(lcm, lcm, mpq_denref(vector[i].
getMpqRef()));
3076 int size = (int)mpz_sizeinbase(lcm, base);
3088 assert(vector != 0);
3089 assert(length >= 0);
3094 for(
int i = 0; i < length; i++)
3096 size_t dsize = mpz_sizeinbase(mpq_denref(vector[i].
getMpqRef()), base);
3160 this->privatevalue = r;
3167 this->privatevalue = (
long double)(r);
3174 this->privatevalue = (
long double)(i);
3304 Rational::operator double()
const 3312 Rational::operator
long double()
const 3630 return sizeof(
long double);
3647 std::stringstream sstream;
3648 sstream << std::setprecision(precision <= 0 ? 16 : precision) << r;
3649 return sstream.str();
4135 assert(vector != 0);
4136 assert(length >= 0);
4141 for(
int i = 0; i < length; i++)
4152 assert(vector != 0);
4153 assert(length >= 0);
4164 assert(vector != 0);
4165 assert(length >= 0);
4173 #endif // SOPLEX_WITH_GMP Rational & addProduct(const Rational &r, const Rational &s)
add product of two rationals
bool isAdjacentTo(const double &d) const
checks if d is exactly equal to the Rational and if not, if it is one of the two adjacent doubles ...
bool isNextTo(const double &d)
checks if d is the closest number that can be represented by double
THREADLOCAL const Real infinity
friend Rational spxAbs(const Rational &r)
Absolute.
Memory allocation routines.
friend std::string rationalToString(const Rational &r, const int precision)
convert rational number to string
int dlcmSizeRational(const Rational *vector, const int length, const int base)
Size of least common multiple of denominators in rational vector.
static const Rational ZERO
rational zero (GMP only)
friend bool operator<(const Rational &r, const Rational &s)
less than operator
static THREADLOCAL bool useListMem
list of unused Private objects; note that this cannot be used if SOPLEX_WITH_GMP is not defined...
Private & operator=(const int &i)
assignment operator from int
Rational operator*(const Rational &r) const
multiplication operator
Private *const & prev() const
previous Private element
int sizeInBase(const int base=2) const
Size in specified base (bit size for base 2)
friend bool operator<=(const Rational &r, const Rational &s)
less than or equal to operator
Rational operator-(const Rational &r) const
subtraction operator
friend bool operator!=(const Rational &r, const Rational &s)
inequality operator
Rational & subProduct(const Rational &r, const Rational &s)
subtract product of two rationals
Rational & invert()
inversion
friend bool operator>=(const Rational &r, const Rational &s)
greater than or equal to operator
Private *& prev()
previous Private element
int totalSizeRational(const Rational *vector, const int length, const int base)
Total size of rational vector.
Wrapper for GMP type mpq_class.We wrap mpq_class so that we can replace it by a double type if GMP is...
static void disableListMem()
disables list memory
Private *& next()
next Private element
Private & operator=(const long double &r)
assignment operator from long double
static const Rational NEGONE
rational minus one (GMP only)
Private & operator=(const double &r)
assignment operator from double
Rational & operator/=(const Rational &r)
division assignment operator
Rational & operator-=(const Rational &r)
subtraction assignment operator
void spx_alloc(T &p, int n=1)
Allocate memory.
static void freeListMem()
frees the unused rational elements in the memory list
int dmaxSizeRational(const Rational *vector, const int length, const int base)
Size of largest denominator in rational vector.
Private * theprev
pointer to the previous element in the list
Private()
default constructor
Rational & operator*=(const Rational &r)
multiplication assignment operator operator
Private & operator=(const Private &p)
assignment operator
Private(const double &r)
constructor from double
bool readString(const char *s)
read Rational from string
static int precision()
returns precision of Rational implementation, i.e., number of bits used to store Rational numbers (IN...
#define DEFAULT_EPS_ZERO
default allowed additive zero: 1.0 + EPS_ZERO == 1.0
friend int sign(const Rational &r)
Sign function; returns 1 if r > 0, 0 if r = 0, and -1 if r < 0.
Defines the "Pimpl"-class Private.
friend std::ostream & operator<<(std::ostream &os, const Rational &q)
print Rational
mpq_t privatevalue
actual value of the Rational object
Private(const int &i)
constructor from int
static void enableListMem()
enables list memory
const mpq_t & getMpqRef() const
provides read-only access to underlying mpq_t
Private(const long double &r)
constructor from long double
static const Rational POSONE
rational plus one (GMP only)
Debugging, floating point type and parameter definitions.
friend bool operator==(const Rational &r, const Rational &s)
equality operator
friend int compareRational(const Rational &r, const Rational &s)
comparison operator returning a positive value if r > s, zero if r = s, and a negative value if r < s...
Rational operator+(const Rational &r) const
addition operator
Everything should be within this namespace.
Private * thenext
pointer to the next element in the list
const mpq_t * getMpqPtr() const
provides read-only access to underlying mpq_t
Rational operator/(const Rational &r) const
division operator
Real spxNextafter(Real x, Real y)
friend bool readStringRational(const char *s, Rational &value)
read Rational from string
Rational & operator=(const Rational &)
assignment operator
static THREADLOCAL IdList< Private > unusedPrivateList
list of unused Private objects
Rational & subQuotient(const Rational &r, const Rational &s)
subtract quotient of two rationals, r divided by s
friend bool operator>(const Rational &r, const Rational &s)
greater than operator
mpq_t * getMpqPtr_w() const
provides write access to underlying mpq_t; use with care
Private & operator=(const mpq_t &q)
assignment operator from mpq_t
Private(const Private &p)
copy constructor
Private(const mpq_t &q)
constructor from mpq_t
Rational()
default constructor
Private *const & next() const
next Private element
mpq_t & getMpqRef_w() const
provides write access to underlying mpq_t; use with care
#define THREADLOCAL
SOPLEX_DEBUG.
void spx_free(T &p)
Release memory.
Rational & addQuotient(const Rational &r, const Rational &s)
add quotient of two rationals, r divided by s
Rational & operator+=(const Rational &r)
addition assignment operator
Rational & powRound()
round up to next power of two