35 #ifdef SOPLEX_WITH_GMP 40 #define SOPLEX_PERFALT_1 43 #define SOPLEX_PERFALT_2a 46 #define SOPLEX_PERFALT_2b 49 #define SOPLEX_PERFALT_3 52 #define SOPLEX_PERFALT_4 55 #define SOPLEX_PERFALT_5a 58 #define SOPLEX_PERFALT_5b 78 #ifdef SOPLEX_WITH_GMP 83 #ifdef SOPLEX_NOLISTMEM 86 bool Rational::useListMem =
true;
97 class Rational::Private
110 mpq_init(privatevalue);
120 mpq_init(privatevalue);
121 mpq_set(this->privatevalue, p.privatevalue);
129 mpq_init(privatevalue);
130 if( r == (
long double)(1.0) )
132 else if( r == (
long double)(-1.0) )
134 else if( r == (
long double)(0.0) )
139 mpq_set_d(privatevalue,
double(r));
147 mpq_init(privatevalue);
157 mpq_set_d(privatevalue, r);
165 mpq_init(privatevalue);
175 mpq_set_si(privatevalue, i, 1);
183 mpq_init(privatevalue);
184 mpq_set(privatevalue, q);
190 mpq_clear(privatevalue);
196 #ifdef SOPLEX_PERFALT_4 197 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 214 if( mpq_sgn(privatevalue) != 0 )
221 else if( r == (
long double)(1.0) )
223 #ifdef SOPLEX_PERFALT_5b 228 else if( r == (
long double)(-1.0) )
230 #ifdef SOPLEX_PERFALT_5b 236 mpq_set_d(this->privatevalue,
double(r));
247 #ifdef SOPLEX_PERFALT_5a 248 #ifdef SOPLEX_PERFALT_1 249 if( mpq_sgn(privatevalue) != 0 )
258 #ifdef SOPLEX_PERFALT_5b 265 #ifdef SOPLEX_PERFALT_5b 271 mpq_set_d(privatevalue, r);
282 #ifdef SOPLEX_PERFALT_5a 283 #ifdef SOPLEX_PERFALT_1 284 if( mpq_sgn(privatevalue) != 0 )
293 #ifdef SOPLEX_PERFALT_5b 300 #ifdef SOPLEX_PERFALT_5b 306 mpq_set_si(privatevalue, i, 1);
314 #ifdef SOPLEX_PERFALT_4 315 if( mpq_equal(this->privatevalue, q) != 0 )
320 mpq_set(this->privatevalue, q);
368 if( Rational::useListMem )
400 if( Rational::useListMem )
433 if( Rational::useListMem )
466 if( Rational::useListMem )
499 if( Rational::useListMem )
532 if( Rational::useListMem )
567 if( !Rational::useListMem ||
this == &Rational::ZERO ||
this == &Rational::POSONE ||
this == &Rational::NEGONE )
588 Rational::useListMem =
true;
611 Rational::useListMem =
false;
662 Rational::operator double()
const 664 #ifdef SOPLEX_PERFALT_3 665 #ifdef SOPLEX_PERFALT_1 671 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
673 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
683 Rational::operator
long double()
const 685 #ifdef SOPLEX_PERFALT_3 686 #ifdef SOPLEX_PERFALT_1 692 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
694 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
704 const mpq_t* Rational::getMpqPtr()
const 712 const mpq_t& Rational::getMpqRef()
const 720 mpq_t* Rational::getMpqPtr_w()
const 728 mpq_t& Rational::getMpqRef_w()
const 738 #ifdef SOPLEX_PERFALT_2a 739 #ifdef SOPLEX_PERFALT_1 740 if( mpq_sgn(r.dpointer->privatevalue) == 0 )
745 if( mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0 )
753 mpq_add(retval.dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
762 #ifdef SOPLEX_PERFALT_2a 763 #ifdef SOPLEX_PERFALT_1 764 if( mpq_sgn(r.dpointer->privatevalue) == 0 )
769 if( mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0 )
789 #ifdef SOPLEX_PERFALT_2a 790 #ifdef SOPLEX_PERFALT_1 800 mpq_add(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
811 return (*
this += Rational::POSONE);
813 return (*
this += Rational::NEGONE);
816 #ifdef SOPLEX_PERFALT_2a 817 #ifdef SOPLEX_PERFALT_1 827 mpq_add(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
842 #ifdef SOPLEX_PERFALT_2a 843 #ifdef SOPLEX_PERFALT_1 853 mpq_add(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
864 return (*
this += Rational::POSONE);
866 return (*
this += Rational::NEGONE);
869 #ifdef SOPLEX_PERFALT_2a 870 #ifdef SOPLEX_PERFALT_1 880 mpq_add(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
891 #ifdef SOPLEX_PERFALT_2a 892 #ifdef SOPLEX_PERFALT_1 893 if( mpq_sgn(r.dpointer->privatevalue) == 0 )
898 if( mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0 )
906 mpq_sub(retval.dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
915 #ifdef SOPLEX_PERFALT_2a 916 #ifdef SOPLEX_PERFALT_1 917 if( mpq_sgn(r.dpointer->privatevalue) == 0 )
926 if( mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0 )
950 #ifdef SOPLEX_PERFALT_2a 951 #ifdef SOPLEX_PERFALT_1 961 mpq_sub(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
973 return (*
this -= Rational::POSONE);
975 return (*
this -= Rational::NEGONE);
978 #ifdef SOPLEX_PERFALT_2a 979 #ifdef SOPLEX_PERFALT_1 989 mpq_sub(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1004 #ifdef SOPLEX_PERFALT_2a 1005 #ifdef SOPLEX_PERFALT_1 1015 mpq_sub(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1027 return (*
this -= Rational::POSONE);
1029 return (*
this -= Rational::NEGONE);
1032 #ifdef SOPLEX_PERFALT_2a 1033 #ifdef SOPLEX_PERFALT_1 1035 return (*
this = -d);
1038 return (*
this = -d);
1043 mpq_sub(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1054 #ifdef SOPLEX_PERFALT_2b 1055 #ifdef SOPLEX_PERFALT_1 1056 if( mpq_sgn(r.dpointer->privatevalue) == 0 )
1061 if( mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0 )
1066 else if( mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1068 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1070 else if( mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1072 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1077 mpq_mul(retval.dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1086 #ifdef SOPLEX_PERFALT_2b 1087 #ifdef SOPLEX_PERFALT_1 1088 if( mpq_sgn(r.dpointer->privatevalue) == 0 )
1089 return (*
this = Rational::ZERO);
1093 if( mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0 )
1094 return (*
this = Rational::ZERO);
1098 else if( mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1100 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1102 else if( mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1107 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1127 else if( d == -1.0 )
1130 mpq_neg(retval.dpointer->privatevalue, this->dpointer->privatevalue);
1135 #ifdef SOPLEX_PERFALT_2b 1136 #ifdef SOPLEX_PERFALT_1 1143 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1145 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1150 mpq_mul(retval.dpointer->privatevalue, retval.dpointer->privatevalue, this->dpointer->privatevalue);
1161 return (*
this = Rational::ZERO);
1164 else if( d == -1.0 )
1171 #ifdef SOPLEX_PERFALT_2b 1172 #ifdef SOPLEX_PERFALT_1 1179 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1181 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1190 mpq_mul(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1207 mpq_neg(retval.dpointer->privatevalue, this->dpointer->privatevalue);
1212 #ifdef SOPLEX_PERFALT_2b 1213 #ifdef SOPLEX_PERFALT_1 1220 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1222 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1227 mpq_mul(retval.dpointer->privatevalue, retval.dpointer->privatevalue, this->dpointer->privatevalue);
1238 return (*
this = Rational::ZERO);
1248 #ifdef SOPLEX_PERFALT_2b 1249 #ifdef SOPLEX_PERFALT_1 1256 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1258 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1267 mpq_mul(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1277 #ifdef SOPLEX_PERFALT_2b 1278 #ifdef SOPLEX_PERFALT_1 1285 else if( mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1287 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1293 else if( mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1295 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1299 mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
1305 mpq_div(retval.dpointer->privatevalue, this->dpointer->privatevalue, r.dpointer->privatevalue);
1314 #ifdef SOPLEX_PERFALT_2b 1315 #ifdef SOPLEX_PERFALT_1 1322 else if( mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1324 else if( mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1329 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1334 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1353 else if( d == -1.0 )
1357 #ifdef SOPLEX_PERFALT_2b 1358 #ifdef SOPLEX_PERFALT_1 1365 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1371 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1375 mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
1381 mpq_div(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1393 else if( d == -1.0 )
1400 #ifdef SOPLEX_PERFALT_2b 1401 #ifdef SOPLEX_PERFALT_1 1408 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1414 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1423 mpq_div(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1439 #ifdef SOPLEX_PERFALT_2b 1440 #ifdef SOPLEX_PERFALT_1 1447 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1453 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1457 mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
1463 mpq_div(retval.dpointer->privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1482 #ifdef SOPLEX_PERFALT_2b 1483 #ifdef SOPLEX_PERFALT_1 1490 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1496 else if( mpq_equal(this->
dpointer->
privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1505 mpq_div(this->
dpointer->
privatevalue, this->dpointer->privatevalue, retval.dpointer->privatevalue);
1515 #ifdef SOPLEX_PERFALT_2b 1516 if( mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1521 else if( mpq_equal(s.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1526 else if( mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1531 else if( mpq_equal(s.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1536 #if 0 // currently, SoPlex calls this method only with nonzero r and s, hence we do not check this case 1537 #ifdef SOPLEX_PERFALT_1 1538 else if( mpq_sgn(r.dpointer->privatevalue) == 0 )
1540 else if( mpq_sgn(s.dpointer->privatevalue) == 0 )
1543 else if( mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0 )
1545 else if( mpq_equal(s.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0 )
1552 mpq_mul(product.dpointer->privatevalue, product.dpointer->privatevalue, s.dpointer->privatevalue);
1553 mpq_add(this->
dpointer->
privatevalue, this->dpointer->privatevalue, product.dpointer->privatevalue);
1562 #ifdef SOPLEX_PERFALT_2b 1563 if( mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1568 else if( mpq_equal(s.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1573 else if( mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1578 else if( mpq_equal(s.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1583 #if 0 // currently, SoPlex calls this method only with nonzero r and s, hence we do not check this case 1584 #ifdef SOPLEX_PERFALT_1 1585 if( mpq_sgn(r.dpointer->privatevalue) == 0 )
1587 else if( mpq_sgn(s.dpointer->privatevalue) == 0 )
1590 if( mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0 )
1592 else if( mpq_equal(s.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0 )
1599 mpq_mul(product.dpointer->privatevalue, product.dpointer->privatevalue, s.dpointer->privatevalue);
1600 mpq_sub(this->
dpointer->
privatevalue, this->dpointer->privatevalue, product.dpointer->privatevalue);
1609 #ifdef SOPLEX_PERFALT_2b 1610 #ifdef SOPLEX_PERFALT_1 1611 if( mpq_sgn(r.dpointer->privatevalue) == 0 )
1614 if( mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0 )
1617 else if( mpq_equal(s.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1622 else if( mpq_equal(s.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1630 mpq_div(quotient.dpointer->privatevalue, quotient.dpointer->privatevalue, s.dpointer->privatevalue);
1631 mpq_add(this->
dpointer->
privatevalue, this->dpointer->privatevalue, quotient.dpointer->privatevalue);
1640 #ifdef SOPLEX_PERFALT_2b 1641 #ifdef SOPLEX_PERFALT_1 1642 if( mpq_sgn(r.dpointer->privatevalue) == 0 )
1645 if( mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0 )
1648 else if( mpq_equal(s.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0 )
1653 else if( mpq_equal(s.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0 )
1661 mpq_div(quotient.dpointer->privatevalue, quotient.dpointer->privatevalue, s.dpointer->privatevalue);
1662 mpq_sub(this->
dpointer->
privatevalue, this->dpointer->privatevalue, quotient.dpointer->privatevalue);
1686 mpz_sub_ui(roundval, roundval, 1);
1688 MSG_DEBUG( std::cout <<
" --> " << mpz_get_str(0, 10, roundval) <<
"\n" );
1690 size_t binlog = mpz_sizeinbase(roundval, 2);
1692 MSG_DEBUG( std::cout <<
" --> 2^" << binlog <<
"\n" );
1694 mpz_ui_pow_ui(roundval, 2, binlog);
1696 MSG_DEBUG( std::cout <<
" --> " << mpz_get_str(0, 10, roundval) <<
"\n" );
1699 mpz_clear(roundval);
1767 return ((a == d) || (b == d));
1789 #define MAX_STR_LEN 10000 1794 assert(strlen(s) <= MAX_STR_LEN);
1800 if( strchr(s,
'/') != 0 || strpbrk(s,
".eE") == 0 )
1802 pos = (*s ==
'+') ? s + 1 : s;
1803 if( mpq_set_str(value.dpointer->privatevalue, pos, 10) == 0 )
1805 mpq_canonicalize(value.dpointer->privatevalue);
1815 bool has_exponent =
false;
1816 bool has_dot =
false;
1818 bool has_digits =
false;
1819 bool has_emptyexponent =
false;
1820 long int exponent = 0;
1821 long int decshift = 0;
1823 mpz_init(shiftpower);
1824 mpq_t shiftpowerRational;
1825 mpq_init(shiftpowerRational);
1827 char tmp[MAX_STR_LEN];
1832 if( (*pos ==
'+') || (*pos ==
'-') )
1836 while( (*pos >=
'0') && (*pos <=
'9') )
1851 while( (*pos >=
'0') && (*pos <=
'9') )
1859 if( tolower(*pos) ==
'e' )
1862 has_exponent =
true;
1864 has_emptyexponent =
true;
1868 if( (*pos ==
'+') || (*pos ==
'-') )
1872 while( (*pos >=
'0') && (*pos <=
'9') )
1874 has_emptyexponent =
false;
1879 if( has_emptyexponent || !has_digits )
1882 assert(has_exponent || has_dot);
1891 while( ((*pos >=
'0') && (*pos <=
'9') ) || *pos ==
'+' || *pos ==
'-' )
1902 while( (*pos >=
'0') && (*pos <=
'9') )
1911 if( mpq_set_str(value.dpointer->privatevalue, tmp, 10) != 0)
1913 mpq_canonicalize(value.dpointer->privatevalue);
1916 exponent = -decshift;
1917 if( tolower(*pos) ==
'e' )
1920 assert(has_exponent);
1921 for( t = tmp; *pos !=
'\0'; pos++ )
1924 exponent += atol(tmp);
1928 mpz_ui_pow_ui(shiftpower, 10, exponent);
1929 mpq_set_z(shiftpowerRational, shiftpower);
1930 mpq_mul(value.dpointer->privatevalue, value.dpointer->privatevalue, shiftpowerRational);
1932 else if( exponent < 0 )
1934 mpz_ui_pow_ui(shiftpower, 10, -exponent);
1935 mpq_set_z(shiftpowerRational, shiftpower);
1936 mpq_div(value.dpointer->privatevalue, value.dpointer->privatevalue, shiftpowerRational);
1939 mpq_canonicalize(value.dpointer->privatevalue);
1941 mpz_clear(shiftpower);
1942 mpq_clear(shiftpowerRational);
1953 #if defined(_WIN32) || defined(_WIN64) || defined(__APPLE__) 1954 std::stringstream sstream;
1956 return sstream.str();
1958 if( precision <= 0 )
1960 std::stringstream sstream;
1962 return sstream.str();
1970 tmpStream = fmemopen(tmpString, 63,
"w");
1971 mpf_init2(tmpFloat, 256);
1972 mpf_set_q(tmpFloat, r.dpointer->privatevalue);
1973 mpf_out_str(tmpStream, 10, precision, tmpFloat);
1974 mpf_clear(tmpFloat);
1977 std::string retString = std::string(tmpString);
1990 assert(strlen(s) <= MAX_STR_LEN);
1995 if( strchr(s,
'/') != 0 || strpbrk(s,
".eE") == 0 )
1997 pos = (*s ==
'+') ? s + 1 : s;
1998 if( mpq_set_str(value.dpointer->privatevalue, pos, 10) == 0 )
2000 mpq_canonicalize(value.dpointer->privatevalue);
2009 bool has_exponent =
false;
2010 bool has_dot =
false;
2012 bool has_digits =
false;
2013 bool has_emptyexponent =
false;
2014 long int exponent = 0;
2015 long int decshift = 0;
2017 mpz_init(shiftpower);
2018 mpq_t shiftpowerRational;
2019 mpq_init(shiftpowerRational);
2021 char tmp[MAX_STR_LEN];
2026 if( (*pos ==
'+') || (*pos ==
'-') )
2030 while( (*pos >=
'0') && (*pos <=
'9') )
2045 while( (*pos >=
'0') && (*pos <=
'9') )
2053 if( tolower(*pos) ==
'e' )
2056 has_exponent =
true;
2058 has_emptyexponent =
true;
2062 if( (*pos ==
'+') || (*pos ==
'-') )
2066 while( (*pos >=
'0') && (*pos <=
'9') )
2068 has_emptyexponent =
false;
2073 if( has_emptyexponent || !has_digits )
2076 assert(has_exponent || has_dot);
2085 while( ((*pos >=
'0') && (*pos <=
'9') ) || *pos ==
'+' || *pos ==
'-' )
2096 while( (*pos >=
'0') && (*pos <=
'9') )
2105 if( mpq_set_str(value.dpointer->privatevalue, tmp, 10) != 0)
2108 mpq_canonicalize(value.dpointer->privatevalue);
2111 exponent = -decshift;
2112 if( tolower(*pos) ==
'e' )
2115 assert(has_exponent);
2116 for( t = tmp; *pos !=
'\0'; pos++ )
2119 exponent += atol(tmp);
2123 mpz_ui_pow_ui(shiftpower, 10, exponent);
2124 mpq_set_z(shiftpowerRational, shiftpower);
2125 mpq_mul(value.dpointer->privatevalue, value.dpointer->privatevalue, shiftpowerRational);
2127 else if( exponent < 0 )
2129 mpz_ui_pow_ui(shiftpower, 10, -exponent);
2130 mpq_set_z(shiftpowerRational, shiftpower);
2131 mpq_div(value.dpointer->privatevalue, value.dpointer->privatevalue, shiftpowerRational);
2134 mpq_canonicalize(value.dpointer->privatevalue);
2135 mpz_clear(shiftpower);
2136 mpq_clear(shiftpowerRational);
2147 buffer = (
char*) malloc (mpz_sizeinbase(mpq_numref(r.dpointer->privatevalue), 10) + mpz_sizeinbase(mpq_denref(r.dpointer->privatevalue), 10) + 3);
2148 os << mpq_get_str(buffer, 10, r.dpointer->privatevalue);
2158 return mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue);
2166 return (mpq_equal(r.dpointer->privatevalue, s.dpointer->privatevalue) != 0);
2174 return (mpq_equal(r.dpointer->privatevalue, s.dpointer->privatevalue) == 0);
2182 return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) < 0);
2190 return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) <= 0);
2198 return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) > 0);
2206 return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) >= 0);
2215 #ifdef SOPLEX_PERFALT_1 2216 return (mpq_sgn(r.dpointer->privatevalue) == 0);
2218 return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0);
2221 return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0);
2222 else if( s == -1.0 )
2223 return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0);
2234 #ifdef SOPLEX_PERFALT_1 2235 return (mpq_sgn(r.dpointer->privatevalue) != 0);
2237 return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) == 0);
2240 return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) == 0);
2241 else if( s == -1.0 )
2242 return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) == 0);
2253 #ifdef SOPLEX_PERFALT_1 2254 return (mpq_sgn(r.dpointer->privatevalue) == -1);
2256 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) < 0);
2259 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) < 0);
2260 else if( s == -1.0 )
2261 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) < 0);
2272 #ifdef SOPLEX_PERFALT_1 2273 return (mpq_sgn(r.dpointer->privatevalue) <= 0);
2275 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) <= 0);
2278 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) <= 0);
2279 else if( s == -1.0 )
2280 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) <= 0);
2291 #ifdef SOPLEX_PERFALT_1 2292 return (mpq_sgn(r.dpointer->privatevalue) == 1);
2294 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) > 0);
2297 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) > 0);
2298 else if( s == -1.0 )
2299 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) > 0);
2310 #ifdef SOPLEX_PERFALT_1 2311 return (mpq_sgn(r.dpointer->privatevalue) >= 0);
2313 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) >= 0);
2316 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) >= 0);
2317 else if( s == -1.0 )
2318 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) >= 0);
2377 #ifdef SOPLEX_PERFALT_1 2378 return (mpq_sgn(r.dpointer->privatevalue) == 0);
2380 return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0);
2383 return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0);
2384 else if( s == -1.0 )
2385 return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0);
2396 #ifdef SOPLEX_PERFALT_1 2397 return (mpq_sgn(r.dpointer->privatevalue) != 0);
2399 return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) == 0);
2402 return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) == 0);
2403 else if( s == -1.0 )
2404 return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) == 0);
2415 #ifdef SOPLEX_PERFALT_1 2416 return (mpq_sgn(r.dpointer->privatevalue) == -1);
2418 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) < 0);
2421 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) < 0);
2422 else if( s == -1.0 )
2423 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) < 0);
2434 #ifdef SOPLEX_PERFALT_1 2435 return (mpq_sgn(r.dpointer->privatevalue) <= 0);
2437 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) <= 0);
2440 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) <= 0);
2441 else if( s == -1.0 )
2442 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) <= 0);
2453 #ifdef SOPLEX_PERFALT_1 2454 return (mpq_sgn(r.dpointer->privatevalue) == 1);
2456 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) > 0);
2459 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) > 0);
2460 else if( s == -1.0 )
2461 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) > 0);
2472 #ifdef SOPLEX_PERFALT_1 2473 return (mpq_sgn(r.dpointer->privatevalue) >= 0);
2475 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) >= 0);
2478 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) >= 0);
2479 else if( s == -1.0 )
2480 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) >= 0);
2547 mpq_neg(res.dpointer->privatevalue, res.dpointer->privatevalue);
2561 else if( d == -1.0 )
2566 mpq_mul(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
2584 else if( d == -1.0 )
2588 mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
2594 mpq_div(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
2605 #ifdef SOPLEX_PERFALT_1 2606 return (mpq_sgn(r.dpointer->privatevalue) == 0);
2608 return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0);
2611 return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) != 0);
2613 return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) != 0);
2624 #ifdef SOPLEX_PERFALT_1 2625 return (mpq_sgn(r.dpointer->privatevalue) != 0);
2627 return (mpq_equal(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) == 0);
2630 return (mpq_equal(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) == 0);
2632 return (mpq_equal(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) == 0);
2643 #ifdef SOPLEX_PERFALT_1 2644 return (mpq_sgn(r.dpointer->privatevalue) == -1);
2646 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) < 0);
2649 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) < 0);
2651 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) < 0);
2662 #ifdef SOPLEX_PERFALT_1 2663 return (mpq_sgn(r.dpointer->privatevalue) <= 0);
2665 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) <= 0);
2668 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) <= 0);
2670 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) <= 0);
2681 #ifdef SOPLEX_PERFALT_1 2682 return (mpq_sgn(r.dpointer->privatevalue) == 1);
2684 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) > 0);
2687 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) > 0);
2689 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) > 0);
2700 #ifdef SOPLEX_PERFALT_1 2701 return (mpq_sgn(r.dpointer->privatevalue) >= 0);
2703 return (mpq_cmp(r.dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) >= 0);
2706 return (mpq_cmp(r.dpointer->privatevalue, Rational::POSONE.dpointer->privatevalue) >= 0);
2708 return (mpq_cmp(r.dpointer->privatevalue, Rational::NEGONE.dpointer->privatevalue) >= 0);
2775 mpq_neg(res.dpointer->privatevalue, res.dpointer->privatevalue);
2794 mpq_mul(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
2816 mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
2822 mpq_div(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
2833 mpq_abs(res.dpointer->privatevalue, r.dpointer->privatevalue);
2842 return mpq_sgn(r.dpointer->privatevalue);
2851 mpq_neg(res.dpointer->privatevalue, r.dpointer->privatevalue);
2860 assert(vector != 0);
2861 assert(length >= 0);
2866 for(
int i = 0; i < length; i++ )
2877 assert(vector != 0);
2878 assert(length >= 0);
2883 mpz_init_set_ui(lcm, 1);
2885 for(
int i = 0; i < length; i++ )
2886 mpz_lcm(lcm, lcm, mpq_denref(vector[i].getMpqRef()));
2888 int size = (int)mpz_sizeinbase(lcm, base);
2900 assert(vector != 0);
2901 assert(length >= 0);
2906 for(
int i = 0; i < length; i++ )
2908 size_t dsize = mpz_sizeinbase(mpq_denref(vector[i].getMpqRef()), base);
2971 this->privatevalue = r;
2978 this->privatevalue = (
long double)(r);
2985 this->privatevalue = (
long double)(i);
3127 Rational::operator double()
const 3135 Rational::operator
long double()
const 3453 return sizeof(
long double);
3470 std::stringstream sstream;
3471 sstream << std::setprecision(precision <= 0 ? 16 : precision) << r;
3472 return sstream.str();
3958 assert(vector != 0);
3959 assert(length >= 0);
3964 for(
int i = 0; i < length; i++ )
3975 assert(vector != 0);
3976 assert(length >= 0);
3987 assert(vector != 0);
3988 assert(length >= 0);
3996 #endif // SOPLEX_WITH_GMP Rational & addProduct(const Rational &r, const Rational &s)
add product of two rationals
bool isNextTo(const double &d)
checks if d is the closest number that can be represented by double
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
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
friend bool operator<=(const Rational &r, const Rational &s)
less than or equal to 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
static const Rational NEGONE
rational minus one
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...
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 ...
#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
static const Rational POSONE
rational plus one
Debugging, floating point type and parameter definitions.
friend bool operator==(const Rational &r, const Rational &s)
equality operator
static IdList< Private > unusedPrivateList
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...
Everything should be within this namespace.
Rational operator-(const Rational &r) const
subtraction 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 operator*(const Rational &r) const
multiplication 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 operator+(const Rational &r) const
addition operator
Rational()
default constructor
int sizeInBase(const int base=2) const
Size in specified base (bit size for base 2)
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 operator/(const Rational &r) const
division operator
Rational & powRound()
round up to next power of two