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
97 class Rational::Private
130 if( r == (
long double)(1.0) )
132 else if( r == (
long double)(-1.0) )
134 else if( r == (
long double)(0.0) )
196 #ifdef SOPLEX_PERFALT_4
197 if( mpq_equal(this->
privatevalue, p.privatevalue) != 0 )
210 if( r == (
long double)(0.0) )
212 #ifdef SOPLEX_PERFALT_5a
213 #ifdef SOPLEX_PERFALT_1
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
247 #ifdef SOPLEX_PERFALT_5a
248 #ifdef SOPLEX_PERFALT_1
258 #ifdef SOPLEX_PERFALT_5b
265 #ifdef SOPLEX_PERFALT_5b
282 #ifdef SOPLEX_PERFALT_5a
283 #ifdef SOPLEX_PERFALT_1
293 #ifdef SOPLEX_PERFALT_5b
300 #ifdef SOPLEX_PERFALT_5b
314 #ifdef SOPLEX_PERFALT_4
414 new (
dpointer) Private(*(r.dpointer));
422 new (
dpointer) Private(*(r.dpointer));
567 if( !
Rational::useListMem ||
this == &Rational::ZERO ||
this == &Rational::POSONE ||
this == &Rational::NEGONE )
662 Rational::operator double()
const
664 #ifdef SOPLEX_PERFALT_3
665 #ifdef SOPLEX_PERFALT_1
666 if( mpq_sgn(this->dpointer->privatevalue) == 0 )
668 if( mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0 )
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 )
677 return mpq_get_d(this->dpointer->privatevalue);
683 Rational::operator
long double()
const
685 #ifdef SOPLEX_PERFALT_3
686 #ifdef SOPLEX_PERFALT_1
687 if( mpq_sgn(this->dpointer->privatevalue) == 0 )
689 if( mpq_equal(this->dpointer->privatevalue, Rational::ZERO.dpointer->privatevalue) != 0 )
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 )
698 return (
long double)mpq_get_d(this->dpointer->privatevalue);
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);
2144 std::ostream&
operator<<(std::ostream& os,
const Rational& r)
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);
2164 bool operator==(
const Rational& r,
const Rational& s)
2166 return (mpq_equal(r.dpointer->privatevalue, s.dpointer->privatevalue) != 0);
2172 bool operator!=(
const Rational& r,
const Rational& s)
2174 return (mpq_equal(r.dpointer->privatevalue, s.dpointer->privatevalue) == 0);
2180 bool operator<(
const Rational& r,
const Rational& s)
2182 return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) < 0);
2188 bool operator<=(
const Rational& r,
const Rational& s)
2190 return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) <= 0);
2196 bool operator>(
const Rational& r,
const Rational& s)
2198 return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) > 0);
2204 bool operator>=(
const Rational& r,
const Rational& s)
2206 return (mpq_cmp(r.dpointer->privatevalue, s.dpointer->privatevalue) >= 0);
2212 bool operator==(
const Rational& r,
const double& s)
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);
2225 return (r == Rational(s));
2231 bool operator!=(
const Rational& r,
const double& s)
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);
2244 return (r != Rational(s));
2250 bool operator<(
const Rational& r,
const double& s)
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);
2263 return (r < Rational(s));
2269 bool operator<=(
const Rational& r,
const double& s)
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);
2282 return (r <= Rational(s));
2288 bool operator>(
const Rational& r,
const double& s)
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);
2301 return (r > Rational(s));
2307 bool operator>=(
const Rational& r,
const double& s)
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);
2320 return (r >= Rational(s));
2326 bool operator==(
const double& r,
const Rational& s)
2334 bool operator!=(
const double& r,
const Rational& s)
2342 bool operator<(
const double& r,
const Rational& s)
2350 bool operator<=(
const double& r,
const Rational& s)
2358 bool operator>(
const double& r,
const Rational& s)
2366 bool operator>=(
const double& r,
const Rational& s)
2374 bool operator==(
const Rational& r,
const long double& s)
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);
2387 return (r == Rational(s));
2393 bool operator!=(
const Rational& r,
const long double& s)
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);
2406 return (r != Rational(s));
2412 bool operator<(
const Rational& r,
const long double& s)
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);
2425 return (r < Rational(s));
2431 bool operator<=(
const Rational& r,
const long double& s)
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);
2444 return (r <= Rational(s));
2450 bool operator>(
const Rational& r,
const long double& s)
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);
2463 return (r > Rational(s));
2469 bool operator>=(
const Rational& r,
const long double& s)
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);
2482 return (r >= Rational(s));
2488 bool operator==(
const long double& r,
const Rational& s)
2496 bool operator!=(
const long double& r,
const Rational& s)
2504 bool operator<(
const long double& r,
const Rational& s)
2512 bool operator<=(
const long double& r,
const Rational& s)
2520 bool operator>(
const long double& r,
const Rational& s)
2528 bool operator>=(
const long double& r,
const Rational& s)
2536 Rational
operator+(
const double& d,
const Rational& r)
2544 Rational
operator-(
const double& d,
const Rational& r)
2547 mpq_neg(res.dpointer->privatevalue, res.dpointer->privatevalue);
2555 Rational
operator*(
const double& d,
const Rational& r)
2561 else if( d == -1.0 )
2566 mpq_mul(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
2574 Rational
operator/(
const double& d,
const Rational& r)
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);
2602 bool operator==(
const Rational& r,
const int& s)
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);
2615 return (r == Rational(s));
2621 bool operator!=(
const Rational& r,
const int& s)
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);
2634 return (r != Rational(s));
2640 bool operator<(
const Rational& r,
const int& s)
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);
2653 return (r < Rational(s));
2659 bool operator<=(
const Rational& r,
const int& s)
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);
2672 return (r <= Rational(s));
2678 bool operator>(
const Rational& r,
const int& s)
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);
2691 return (r > Rational(s));
2697 bool operator>=(
const Rational& r,
const int& s)
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);
2710 return (r >= Rational(s));
2716 bool operator==(
const int& r,
const Rational& s)
2724 bool operator!=(
const int& r,
const Rational& s)
2732 bool operator<(
const int& r,
const Rational& s)
2740 bool operator<=(
const int& r,
const Rational& s)
2748 bool operator>(
const int& r,
const Rational& s)
2756 bool operator>=(
const int& r,
const Rational& s)
2764 Rational
operator+(
const int& d,
const Rational& r)
2772 Rational
operator-(
const int& d,
const Rational& r)
2775 mpq_neg(res.dpointer->privatevalue, res.dpointer->privatevalue);
2783 Rational
operator*(
const int& d,
const Rational& r)
2794 mpq_mul(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
2802 Rational
operator/(
const int& d,
const Rational& r)
2816 mpq_neg(retval.dpointer->privatevalue, retval.dpointer->privatevalue);
2822 mpq_div(retval.dpointer->privatevalue, retval.dpointer->privatevalue, r.dpointer->privatevalue);
2830 Rational
spxAbs(
const Rational& r)
2833 mpq_abs(res.dpointer->privatevalue, r.dpointer->privatevalue);
2840 int sign(
const Rational& r)
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++ )
2867 size += vector[i].sizeInBase(base);
2875 int dlcmSizeRational(
const Rational* vector,
const int length,
const int base)
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);
2898 int dmaxSizeRational(
const Rational* vector,
const int length,
const int 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);
3127 Rational::operator double()
const
3135 Rational::operator
long double()
const
3138 return this->dpointer->privatevalue;
3453 return sizeof(
long double);
3470 std::stringstream sstream;
3471 sstream << std::setprecision(precision <= 0 ? 16 : precision) << r;
3472 return sstream.str();
3626 return (r < s.dpointer->privatevalue);
3634 return (r <= s.dpointer->privatevalue);
3727 return (r < s.dpointer->privatevalue);
3735 return (r <= s.dpointer->privatevalue);
3958 assert(vector != 0);
3959 assert(length >= 0);
3964 for(
int i = 0; i < length; i++ )
3965 size += vector[i].sizeInBase(base);
3975 assert(vector != 0);
3976 assert(length >= 0);
3987 assert(vector != 0);
3988 assert(length >= 0);
3996 #endif // SOPLEX_WITH_GMP