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 65 const Rational Rational::ZERO(0,
true);
68 const Rational Rational::POSONE(1,
true);
71 const Rational Rational::NEGONE(-1,
true);
76 #ifdef SOPLEX_NOLISTMEM 87 #ifdef SOPLEX_WITH_GMP 88 THREADLOCAL IdList< Rational::Private > Rational::unusedPrivateList(0, 0,
true);
92 class Rational::Private
105 mpq_init(privatevalue);
115 mpq_init(privatevalue);
116 mpq_set(this->privatevalue, p.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 196 if(mpq_equal(this->privatevalue, p.privatevalue) != 0)
202 mpq_set(this->privatevalue, p.privatevalue);
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)
381 dpointer = unusedPrivateList.last();
385 assert(unusedPrivateList.first() != 0);
390 assert(unusedPrivateList.first() == 0);
397 assert(unusedPrivateList.length() == 0);
411 if(Rational::useListMem)
413 dpointer = unusedPrivateList.last();
417 assert(unusedPrivateList.first() != 0);
423 assert(unusedPrivateList.first() == 0);
430 assert(unusedPrivateList.length() == 0);
444 if(Rational::useListMem)
446 dpointer = unusedPrivateList.last();
450 assert(unusedPrivateList.first() != 0);
456 assert(unusedPrivateList.first() == 0);
463 assert(unusedPrivateList.length() == 0);
477 if(Rational::useListMem)
479 dpointer = unusedPrivateList.last();
483 assert(unusedPrivateList.first() != 0);
489 assert(unusedPrivateList.first() == 0);
496 assert(unusedPrivateList.length() == 0);
510 if(Rational::useListMem)
512 dpointer = unusedPrivateList.last();
516 assert(unusedPrivateList.first() != 0);
522 assert(unusedPrivateList.first() == 0);
529 assert(unusedPrivateList.length() == 0);
543 if(Rational::useListMem)
545 dpointer = unusedPrivateList.last();
549 assert(unusedPrivateList.first() != 0);
555 assert(unusedPrivateList.first() == 0);
562 assert(unusedPrivateList.length() == 0);
576 assert(Rational::useListMem || unusedPrivateList.length() == 0);
578 if(!Rational::useListMem ||
this == &Rational::ZERO ||
this == &Rational::POSONE
579 ||
this == &Rational::NEGONE)
599 assert(Rational::useListMem || unusedPrivateList.length() == 0);
600 Rational::useListMem =
true;
613 unusedPrivateList.clear(
true);
614 assert(unusedPrivateList.length() == 0);
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 720 const mpq_t* Rational::getMpqPtr()
const 728 const mpq_t& Rational::getMpqRef()
const 736 mpq_t* Rational::getMpqPtr_w()
const 744 mpq_t& Rational::getMpqRef_w()
const 754 #ifdef SOPLEX_PERFALT_2a 755 #ifdef SOPLEX_PERFALT_1 757 if(mpq_sgn(r.dpointer->privatevalue) == 0)
764 if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
773 mpq_add(retval.dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
782 #ifdef SOPLEX_PERFALT_2a 783 #ifdef SOPLEX_PERFALT_1 785 if(mpq_sgn(r.dpointer->privatevalue) == 0)
792 if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
813 #ifdef SOPLEX_PERFALT_2a 814 #ifdef SOPLEX_PERFALT_1 828 mpq_add(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
839 return (*
this += Rational::POSONE);
841 return (*
this += Rational::NEGONE);
844 #ifdef SOPLEX_PERFALT_2a 845 #ifdef SOPLEX_PERFALT_1 859 mpq_add(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
874 #ifdef SOPLEX_PERFALT_2a 875 #ifdef SOPLEX_PERFALT_1 889 mpq_add(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
900 return (*
this += Rational::POSONE);
902 return (*
this += Rational::NEGONE);
905 #ifdef SOPLEX_PERFALT_2a 906 #ifdef SOPLEX_PERFALT_1 920 mpq_add(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
931 #ifdef SOPLEX_PERFALT_2a 932 #ifdef SOPLEX_PERFALT_1 934 if(mpq_sgn(r.dpointer->privatevalue) == 0)
941 if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
950 mpq_sub(retval.dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
959 #ifdef SOPLEX_PERFALT_2a 960 #ifdef SOPLEX_PERFALT_1 962 if(mpq_sgn(r.dpointer->privatevalue) == 0)
973 if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
998 #ifdef SOPLEX_PERFALT_2a 999 #ifdef SOPLEX_PERFALT_1 1013 mpq_sub(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
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);
1045 mpq_sub(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1060 #ifdef SOPLEX_PERFALT_2a 1061 #ifdef SOPLEX_PERFALT_1 1075 mpq_sub(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
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);
1107 mpq_sub(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1118 #ifdef SOPLEX_PERFALT_2b 1119 #ifdef SOPLEX_PERFALT_1 1121 if(mpq_sgn(r.dpointer->privatevalue) == 0)
1122 return Rational::ZERO;
1124 return Rational::ZERO;
1128 if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1129 return Rational::ZERO;
1131 return Rational::ZERO;
1134 else if(mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1138 else if(mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1146 mpq_mul(retval.dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1155 #ifdef SOPLEX_PERFALT_2b 1156 #ifdef SOPLEX_PERFALT_1 1158 if(mpq_sgn(r.dpointer->privatevalue) == 0)
1159 return (*
this = Rational::ZERO);
1165 if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1166 return (*
this = Rational::ZERO);
1171 else if(mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1175 else if(mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1198 return Rational::ZERO;
1204 mpq_neg(retval.dpointer->privatevalue, this->dpointer->privatevalue);
1209 #ifdef SOPLEX_PERFALT_2b 1210 #ifdef SOPLEX_PERFALT_1 1213 return Rational::ZERO;
1218 return Rational::ZERO;
1229 mpq_mul(retval.dpointer->privatevalue, retval.dpointer->privatevalue, this->dpointer->privatevalue);
1240 return (*
this = Rational::ZERO);
1250 #ifdef SOPLEX_PERFALT_2b 1251 #ifdef SOPLEX_PERFALT_1 1274 mpq_mul(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1285 return Rational::ZERO;
1291 mpq_neg(retval.dpointer->privatevalue, this->dpointer->privatevalue);
1296 #ifdef SOPLEX_PERFALT_2b 1297 #ifdef SOPLEX_PERFALT_1 1300 return Rational::ZERO;
1305 return Rational::ZERO;
1316 mpq_mul(retval.dpointer->privatevalue, retval.dpointer->privatevalue, this->dpointer->privatevalue);
1327 return (*
this = Rational::ZERO);
1337 #ifdef SOPLEX_PERFALT_2b 1338 #ifdef SOPLEX_PERFALT_1 1361 mpq_mul(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1371 #ifdef SOPLEX_PERFALT_2b 1372 #ifdef SOPLEX_PERFALT_1 1375 return Rational::ZERO;
1380 return Rational::ZERO;
1383 else if(mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1391 else if(mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1397 mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
1404 mpq_div(retval.dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1413 #ifdef SOPLEX_PERFALT_2b 1414 #ifdef SOPLEX_PERFALT_1 1425 else if(mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1427 else if(mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1461 #ifdef SOPLEX_PERFALT_2b 1462 #ifdef SOPLEX_PERFALT_1 1465 return Rational::ZERO;
1470 return Rational::ZERO;
1483 mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
1490 mpq_div(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1509 #ifdef SOPLEX_PERFALT_2b 1510 #ifdef SOPLEX_PERFALT_1 1537 mpq_div(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1553 #ifdef SOPLEX_PERFALT_2b 1554 #ifdef SOPLEX_PERFALT_1 1557 return Rational::ZERO;
1562 return Rational::ZERO;
1575 mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
1582 mpq_div(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1601 #ifdef SOPLEX_PERFALT_2b 1602 #ifdef SOPLEX_PERFALT_1 1629 mpq_div(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1639 #ifdef SOPLEX_PERFALT_2b 1641 if(mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1646 else if(mpq_equal(s.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1651 else if(mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1656 else if(mpq_equal(s.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
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 1664 else if(mpq_sgn(r.dpointer->privatevalue) == 0)
1666 else if(mpq_sgn(s.dpointer->privatevalue) == 0)
1670 else if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1672 else if(mpq_equal(s.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1680 mpq_mul(product.dpointer->privatevalue, product.dpointer->privatevalue, s.dpointer->privatevalue);
1681 mpq_add(this->
dpointer->
privatevalue, this->dpointer->privatevalue, product.dpointer->privatevalue);
1690 #ifdef SOPLEX_PERFALT_2b 1692 if(mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1697 else if(mpq_equal(s.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1702 else if(mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1707 else if(mpq_equal(s.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
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 1716 if(mpq_sgn(r.dpointer->privatevalue) == 0)
1718 else if(mpq_sgn(s.dpointer->privatevalue) == 0)
1723 if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1725 else if(mpq_equal(s.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1733 mpq_mul(product.dpointer->privatevalue, product.dpointer->privatevalue, s.dpointer->privatevalue);
1734 mpq_sub(this->
dpointer->
privatevalue, this->dpointer->privatevalue, product.dpointer->privatevalue);
1743 #ifdef SOPLEX_PERFALT_2b 1744 #ifdef SOPLEX_PERFALT_1 1746 if(mpq_sgn(r.dpointer->privatevalue) == 0)
1751 if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1755 else if(mpq_equal(s.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1760 else if(mpq_equal(s.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1769 mpq_div(quotient.dpointer->privatevalue, quotient.dpointer->privatevalue, s.dpointer->privatevalue);
1771 quotient.dpointer->privatevalue);
1780 #ifdef SOPLEX_PERFALT_2b 1781 #ifdef SOPLEX_PERFALT_1 1783 if(mpq_sgn(r.dpointer->privatevalue) == 0)
1788 if(mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0)
1792 else if(mpq_equal(s.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0)
1797 else if(mpq_equal(s.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0)
1806 mpq_div(quotient.dpointer->privatevalue, quotient.dpointer->privatevalue, s.dpointer->privatevalue);
1808 quotient.dpointer->privatevalue);
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 1942 assert(strlen(s) <= MAX_STR_LEN);
1948 if(strchr(s,
'/') != 0 || strpbrk(s,
".eE") == 0)
1950 pos = (*s ==
'+') ? s + 1 : s;
1952 if(mpq_set_str(value.dpointer->privatevalue, pos, 10) == 0)
1954 mpq_canonicalize(value.dpointer->privatevalue);
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);
1976 char tmp[MAX_STR_LEN];
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'))
2064 if(mpq_set_str(value.dpointer->privatevalue, tmp, 10) != 0)
2067 mpq_canonicalize(value.dpointer->privatevalue);
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);
2088 mpq_mul(value.dpointer->privatevalue, value.dpointer->privatevalue, shiftpowerRational);
2090 else if(exponent < 0)
2092 mpz_ui_pow_ui(shiftpower, 10, -exponent);
2093 mpq_set_z(shiftpowerRational, shiftpower);
2094 mpq_div(value.dpointer->privatevalue, value.dpointer->privatevalue, shiftpowerRational);
2097 mpq_canonicalize(value.dpointer->privatevalue);
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);
2150 assert(strlen(s) <= MAX_STR_LEN);
2155 if(strchr(s,
'/') != 0 || strpbrk(s,
".eE") == 0)
2157 pos = (*s ==
'+') ? s + 1 : s;
2159 if(mpq_set_str(value.dpointer->privatevalue, pos, 10) == 0)
2161 mpq_canonicalize(value.dpointer->privatevalue);
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);
2182 char tmp[MAX_STR_LEN];
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'))
2270 if(mpq_set_str(value.dpointer->privatevalue, tmp, 10) != 0)
2273 mpq_canonicalize(value.dpointer->privatevalue);
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);
2294 mpq_mul(value.dpointer->privatevalue, value.dpointer->privatevalue, shiftpowerRational);
2296 else if(exponent < 0)
2298 mpz_ui_pow_ui(shiftpower, 10, -exponent);
2299 mpq_set_z(shiftpowerRational, shiftpower);
2300 mpq_div(value.dpointer->privatevalue, value.dpointer->privatevalue, shiftpowerRational);
2303 mpq_canonicalize(value.dpointer->privatevalue);
2304 mpz_clear(shiftpower);
2305 mpq_clear(shiftpowerRational);
2316 buffer = (
char*) malloc(mpz_sizeinbase(mpq_numref(r.dpointer->privatevalue),
2317 10) + mpz_sizeinbase(mpq_denref(r.dpointer->privatevalue), 10) + 3);
2318 os << mpq_get_str(buffer, 10, r.dpointer->privatevalue);
2328 return mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue);
2336 return (mpq_equal(r.dpointer->privatevalue, s.dpointer->privatevalue) != 0);
2344 return (mpq_equal(r.dpointer->privatevalue, s.dpointer->privatevalue) == 0);
2352 return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) < 0);
2360 return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) <= 0);
2368 return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) > 0);
2376 return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) >= 0);
2385 #ifdef SOPLEX_PERFALT_1 2386 return (mpq_sgn(r.dpointer->privatevalue) == 0);
2389 return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0);
2392 return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0);
2394 return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0);
2405 #ifdef SOPLEX_PERFALT_1 2406 return (mpq_sgn(r.dpointer->privatevalue) != 0);
2409 return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) == 0);
2412 return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) == 0);
2414 return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) == 0);
2425 #ifdef SOPLEX_PERFALT_1 2426 return (mpq_sgn(r.dpointer->privatevalue) == -1);
2429 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) < 0);
2432 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) < 0);
2434 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) < 0);
2445 #ifdef SOPLEX_PERFALT_1 2446 return (mpq_sgn(r.dpointer->privatevalue) <= 0);
2449 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) <= 0);
2452 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) <= 0);
2454 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) <= 0);
2465 #ifdef SOPLEX_PERFALT_1 2466 return (mpq_sgn(r.dpointer->privatevalue) == 1);
2469 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) > 0);
2472 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) > 0);
2474 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) > 0);
2485 #ifdef SOPLEX_PERFALT_1 2486 return (mpq_sgn(r.dpointer->privatevalue) >= 0);
2489 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) >= 0);
2492 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) >= 0);
2494 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) >= 0);
2553 #ifdef SOPLEX_PERFALT_1 2554 return (mpq_sgn(r.dpointer->privatevalue) == 0);
2557 return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0);
2560 return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0);
2562 return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0);
2573 #ifdef SOPLEX_PERFALT_1 2574 return (mpq_sgn(r.dpointer->privatevalue) != 0);
2577 return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) == 0);
2580 return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) == 0);
2582 return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) == 0);
2593 #ifdef SOPLEX_PERFALT_1 2594 return (mpq_sgn(r.dpointer->privatevalue) == -1);
2597 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) < 0);
2600 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) < 0);
2602 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) < 0);
2613 #ifdef SOPLEX_PERFALT_1 2614 return (mpq_sgn(r.dpointer->privatevalue) <= 0);
2617 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) <= 0);
2620 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) <= 0);
2622 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) <= 0);
2633 #ifdef SOPLEX_PERFALT_1 2634 return (mpq_sgn(r.dpointer->privatevalue) == 1);
2637 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) > 0);
2640 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) > 0);
2642 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) > 0);
2653 #ifdef SOPLEX_PERFALT_1 2654 return (mpq_sgn(r.dpointer->privatevalue) >= 0);
2657 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) >= 0);
2660 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) >= 0);
2662 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) >= 0);
2729 mpq_neg(res.dpointer->privatevalue, res.dpointer->privatevalue);
2740 return Rational::ZERO;
2748 mpq_mul(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
2759 return Rational::ZERO;
2770 mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
2776 mpq_div(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
2787 #ifdef SOPLEX_PERFALT_1 2788 return (mpq_sgn(r.dpointer->privatevalue) == 0);
2791 return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0);
2794 return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0);
2796 return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0);
2807 #ifdef SOPLEX_PERFALT_1 2808 return (mpq_sgn(r.dpointer->privatevalue) != 0);
2811 return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) == 0);
2814 return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) == 0);
2816 return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) == 0);
2827 #ifdef SOPLEX_PERFALT_1 2828 return (mpq_sgn(r.dpointer->privatevalue) == -1);
2831 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) < 0);
2834 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) < 0);
2836 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) < 0);
2847 #ifdef SOPLEX_PERFALT_1 2848 return (mpq_sgn(r.dpointer->privatevalue) <= 0);
2851 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) <= 0);
2854 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) <= 0);
2856 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) <= 0);
2867 #ifdef SOPLEX_PERFALT_1 2868 return (mpq_sgn(r.dpointer->privatevalue) == 1);
2871 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) > 0);
2874 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) > 0);
2876 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) > 0);
2887 #ifdef SOPLEX_PERFALT_1 2888 return (mpq_sgn(r.dpointer->privatevalue) >= 0);
2891 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) >= 0);
2894 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) >= 0);
2896 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) >= 0);
2963 mpq_neg(res.dpointer->privatevalue, res.dpointer->privatevalue);
2974 return Rational::ZERO;
2982 mpq_mul(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
2993 return Rational::ZERO;
3004 mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
3010 mpq_div(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
3021 mpq_abs(res.dpointer->privatevalue, r.dpointer->privatevalue);
3030 return mpq_sgn(r.dpointer->privatevalue);
3039 mpq_neg(res.dpointer->privatevalue, r.dpointer->privatevalue);
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.
long double privatevalue
value
friend bool operator<(const Rational &r, const Rational &s)
less than operator
Private & operator=(const int &i)
assignment operator from int
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
Private & operator=(const long double &r)
assignment operator from long double
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()
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
Private(const int &i)
constructor from int
static void enableListMem()
enables list memory
Private(const long double &r)
constructor from long double
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.
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
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
Private(const Private &p)
copy constructor
Rational()
default constructor
#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