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 110 if(Rational::useListMem)
142 if(Rational::useListMem)
175 if(Rational::useListMem)
208 if(Rational::useListMem)
241 if(Rational::useListMem)
274 if(Rational::useListMem)
309 if(!Rational::useListMem ||
this == &Rational::ZERO ||
this == &Rational::POSONE
310 ||
this == &Rational::NEGONE)
331 Rational::useListMem =
true;
354 Rational::useListMem =
false;
407 Rational::operator double()
const 409 #ifdef SOPLEX_PERFALT_3 410 #ifdef SOPLEX_PERFALT_1 430 Rational::operator
long double()
const 432 #ifdef SOPLEX_PERFALT_3 433 #ifdef SOPLEX_PERFALT_1 451 Rational::operator float()
const 453 #ifdef SOPLEX_PERFALT_3 454 #ifdef SOPLEX_PERFALT_1 506 #ifdef SOPLEX_PERFALT_2a 507 #ifdef SOPLEX_PERFALT_1 534 #ifdef SOPLEX_PERFALT_2a 535 #ifdef SOPLEX_PERFALT_1 565 #ifdef SOPLEX_PERFALT_2a 566 #ifdef SOPLEX_PERFALT_1 591 return (*
this += Rational::POSONE);
593 return (*
this += Rational::NEGONE);
596 #ifdef SOPLEX_PERFALT_2a 597 #ifdef SOPLEX_PERFALT_1 626 #ifdef SOPLEX_PERFALT_2a 627 #ifdef SOPLEX_PERFALT_1 652 return (*
this += Rational::POSONE);
654 return (*
this += Rational::NEGONE);
657 #ifdef SOPLEX_PERFALT_2a 658 #ifdef SOPLEX_PERFALT_1 683 #ifdef SOPLEX_PERFALT_2a 684 #ifdef SOPLEX_PERFALT_1 711 #ifdef SOPLEX_PERFALT_2a 712 #ifdef SOPLEX_PERFALT_1 750 #ifdef SOPLEX_PERFALT_2a 751 #ifdef SOPLEX_PERFALT_1 777 return (*
this -= Rational::POSONE);
779 return (*
this -= Rational::NEGONE);
782 #ifdef SOPLEX_PERFALT_2a 783 #ifdef SOPLEX_PERFALT_1 812 #ifdef SOPLEX_PERFALT_2a 813 #ifdef SOPLEX_PERFALT_1 839 return (*
this -= Rational::POSONE);
841 return (*
this -= Rational::NEGONE);
844 #ifdef SOPLEX_PERFALT_2a 845 #ifdef SOPLEX_PERFALT_1 870 #ifdef SOPLEX_PERFALT_2b 871 #ifdef SOPLEX_PERFALT_1 907 #ifdef SOPLEX_PERFALT_2b 908 #ifdef SOPLEX_PERFALT_1 911 return (*
this = Rational::ZERO);
918 return (*
this = Rational::ZERO);
961 #ifdef SOPLEX_PERFALT_2b 962 #ifdef SOPLEX_PERFALT_1 992 return (*
this = Rational::ZERO);
1002 #ifdef SOPLEX_PERFALT_2b 1003 #ifdef SOPLEX_PERFALT_1 1048 #ifdef SOPLEX_PERFALT_2b 1049 #ifdef SOPLEX_PERFALT_1 1079 return (*
this = Rational::ZERO);
1089 #ifdef SOPLEX_PERFALT_2b 1090 #ifdef SOPLEX_PERFALT_1 1123 #ifdef SOPLEX_PERFALT_2b 1124 #ifdef SOPLEX_PERFALT_1 1165 #ifdef SOPLEX_PERFALT_2b 1166 #ifdef SOPLEX_PERFALT_1 1213 #ifdef SOPLEX_PERFALT_2b 1214 #ifdef SOPLEX_PERFALT_1 1261 #ifdef SOPLEX_PERFALT_2b 1262 #ifdef SOPLEX_PERFALT_1 1305 #ifdef SOPLEX_PERFALT_2b 1306 #ifdef SOPLEX_PERFALT_1 1353 #ifdef SOPLEX_PERFALT_2b 1354 #ifdef SOPLEX_PERFALT_1 1391 #ifdef SOPLEX_PERFALT_2b 1414 #if 0 // currently, SoPlex calls this method only with nonzero r and s, hence we do not check this case 1415 #ifdef SOPLEX_PERFALT_1 1442 #ifdef SOPLEX_PERFALT_2b 1465 #if 0 // currently, SoPlex calls this method only with nonzero r and s, hence we do not check this case 1466 #ifdef SOPLEX_PERFALT_1 1495 #ifdef SOPLEX_PERFALT_2b 1496 #ifdef SOPLEX_PERFALT_1 1532 #ifdef SOPLEX_PERFALT_2b 1533 #ifdef SOPLEX_PERFALT_1 1581 " to power of two" <<
"\n");
1586 mpz_sub_ui(roundval, roundval, 1);
1588 MSG_DEBUG(std::cout <<
" --> " << mpz_get_str(0, 10, roundval) <<
"\n");
1590 size_t binlog = mpz_sizeinbase(roundval, 2);
1592 MSG_DEBUG(std::cout <<
" --> 2^" << binlog <<
"\n");
1594 mpz_ui_pow_ui(roundval, 2, binlog);
1596 MSG_DEBUG(std::cout <<
" --> " << mpz_get_str(0, 10, roundval) <<
"\n");
1599 mpz_clear(roundval);
1665 return ((a == d) || (b == d));
1687 #define MAX_STR_LEN 10000 1698 if(strchr(s,
'/') != 0 || strpbrk(s,
".eE") == 0)
1700 pos = (*s ==
'+') ? s + 1 : s;
1714 bool has_exponent =
false;
1715 bool has_dot =
false;
1717 bool has_digits =
false;
1718 bool has_emptyexponent =
false;
1719 long int exponent = 0;
1720 long int decshift = 0;
1722 mpz_init(shiftpower);
1723 mpq_t shiftpowerRational;
1724 mpq_init(shiftpowerRational);
1731 if((*pos ==
'+') || (*pos ==
'-'))
1735 while((*pos >=
'0') && (*pos <=
'9'))
1750 while((*pos >=
'0') && (*pos <=
'9'))
1758 if(tolower(*pos) ==
'e')
1761 has_exponent =
true;
1763 has_emptyexponent =
true;
1767 if((*pos ==
'+') || (*pos ==
'-'))
1771 while((*pos >=
'0') && (*pos <=
'9'))
1773 has_emptyexponent =
false;
1778 if(has_emptyexponent || !has_digits)
1781 assert(has_exponent || has_dot);
1790 while(((*pos >=
'0') && (*pos <=
'9')) || *pos ==
'+' || *pos ==
'-')
1804 while((*pos >=
'0') && (*pos <=
'9'))
1820 exponent = -decshift;
1822 if(tolower(*pos) ==
'e')
1825 assert(has_exponent);
1827 for(t = tmp; *pos !=
'\0'; pos++)
1831 exponent += atol(tmp);
1836 mpz_ui_pow_ui(shiftpower, 10, exponent);
1837 mpq_set_z(shiftpowerRational, shiftpower);
1840 else if(exponent < 0)
1842 mpz_ui_pow_ui(shiftpower, 10, -exponent);
1843 mpq_set_z(shiftpowerRational, shiftpower);
1849 mpz_clear(shiftpower);
1850 mpq_clear(shiftpowerRational);
1861 #if defined(_WIN32) || defined(_WIN64) || defined(__APPLE__) 1862 std::stringstream sstream;
1864 return sstream.str();
1869 std::stringstream sstream;
1871 return sstream.str();
1879 tmpStream = fmemopen(tmpString, 63,
"w");
1880 mpf_init2(tmpFloat, 256);
1881 mpf_set_q(tmpFloat, r.dpointer->privatevalue);
1882 mpf_out_str(tmpStream, 10, precision, tmpFloat);
1883 mpf_clear(tmpFloat);
1886 std::string retString = std::string(tmpString);
1905 if(strchr(s,
'/') != 0 || strpbrk(s,
".eE") == 0)
1907 pos = (*s ==
'+') ? s + 1 : s;
1920 bool has_exponent =
false;
1921 bool has_dot =
false;
1923 bool has_digits =
false;
1924 bool has_emptyexponent =
false;
1925 long int exponent = 0;
1926 long int decshift = 0;
1928 mpz_init(shiftpower);
1929 mpq_t shiftpowerRational;
1930 mpq_init(shiftpowerRational);
1937 if((*pos ==
'+') || (*pos ==
'-'))
1941 while((*pos >=
'0') && (*pos <=
'9'))
1956 while((*pos >=
'0') && (*pos <=
'9'))
1964 if(tolower(*pos) ==
'e')
1967 has_exponent =
true;
1969 has_emptyexponent =
true;
1973 if((*pos ==
'+') || (*pos ==
'-'))
1977 while((*pos >=
'0') && (*pos <=
'9'))
1979 has_emptyexponent =
false;
1984 if(has_emptyexponent || !has_digits)
1987 assert(has_exponent || has_dot);
1996 while(((*pos >=
'0') && (*pos <=
'9')) || *pos ==
'+' || *pos ==
'-')
2010 while((*pos >=
'0') && (*pos <=
'9'))
2026 exponent = -decshift;
2028 if(tolower(*pos) ==
'e')
2031 assert(has_exponent);
2033 for(t = tmp; *pos !=
'\0'; pos++)
2037 exponent += atol(tmp);
2042 mpz_ui_pow_ui(shiftpower, 10, exponent);
2043 mpq_set_z(shiftpowerRational, shiftpower);
2046 else if(exponent < 0)
2048 mpz_ui_pow_ui(shiftpower, 10, -exponent);
2049 mpq_set_z(shiftpowerRational, shiftpower);
2054 mpz_clear(shiftpower);
2055 mpq_clear(shiftpowerRational);
2135 #ifdef SOPLEX_PERFALT_1 2155 #ifdef SOPLEX_PERFALT_1 2175 #ifdef SOPLEX_PERFALT_1 2195 #ifdef SOPLEX_PERFALT_1 2215 #ifdef SOPLEX_PERFALT_1 2235 #ifdef SOPLEX_PERFALT_1 2303 #ifdef SOPLEX_PERFALT_1 2323 #ifdef SOPLEX_PERFALT_1 2343 #ifdef SOPLEX_PERFALT_1 2363 #ifdef SOPLEX_PERFALT_1 2383 #ifdef SOPLEX_PERFALT_1 2403 #ifdef SOPLEX_PERFALT_1 2470 #ifdef SOPLEX_PERFALT_1 2490 #ifdef SOPLEX_PERFALT_1 2510 #ifdef SOPLEX_PERFALT_1 2530 #ifdef SOPLEX_PERFALT_1 2550 #ifdef SOPLEX_PERFALT_1 2570 #ifdef SOPLEX_PERFALT_1 2703 #ifdef SOPLEX_PERFALT_1 2723 #ifdef SOPLEX_PERFALT_1 2743 #ifdef SOPLEX_PERFALT_1 2763 #ifdef SOPLEX_PERFALT_1 2783 #ifdef SOPLEX_PERFALT_1 2803 #ifdef SOPLEX_PERFALT_1 2964 assert(vector != 0);
2965 assert(length >= 0);
2970 for(
int i = 0; i < length; i++)
2981 assert(vector != 0);
2982 assert(length >= 0);
2987 mpz_init_set_ui(lcm, 1);
2989 for(
int i = 0; i < length; i++)
2990 mpz_lcm(lcm, lcm, mpq_denref(vector[i].
getMpqRef()));
2992 int size = (int)mpz_sizeinbase(lcm, base);
3004 assert(vector != 0);
3005 assert(length >= 0);
3010 for(
int i = 0; i < length; i++)
3012 size_t dsize = mpz_sizeinbase(mpq_denref(vector[i].
getMpqRef()), base);
3153 Rational::operator double()
const 3161 Rational::operator
long double()
const 3479 return sizeof(
long double);
3496 std::stringstream sstream;
3497 sstream << std::setprecision(precision <= 0 ? 16 : precision) << r;
3498 return sstream.str();
3652 return (r < s.dpointer->privatevalue);
3660 return (r <= s.dpointer->privatevalue);
3753 return (r < s.dpointer->privatevalue);
3761 return (r <= s.dpointer->privatevalue);
3984 assert(vector != 0);
3985 assert(length >= 0);
3990 for(
int i = 0; i < length; i++)
4001 assert(vector != 0);
4002 assert(length >= 0);
4013 assert(vector != 0);
4014 assert(length >= 0);
4022 #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...
Rational operator*(const Rational &r) const
multiplication operator
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
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
static const Rational NEGONE
rational minus one (GMP only)
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.
Rational & operator*=(const Rational &r)
multiplication assignment operator operator
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
static void enableListMem()
enables list memory
const mpq_t & getMpqRef() const
provides read-only access to underlying mpq_t
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()
constructor from boost number
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
Rational()
default constructor
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