8 #ifndef __SOPLEX_MULTIPRECISION_HPP_ 9 #define __SOPLEX_MULTIPRECISION_HPP_ 16 #ifdef SOPLEX_WITH_GMP 20 #ifdef SOPLEX_WITH_BOOST 21 #include <boost/multiprecision/number.hpp> 23 #ifdef SOPLEX_WITH_GMP 24 #include <boost/multiprecision/gmp.hpp> 34 mpz_lcm(result.backend().data(), a.backend().data(), b.backend().data());
38 mpz_gcd(result.backend().data(), a.backend().data(), b.backend().data());
43 #include <boost/multiprecision/cpp_int.hpp> 44 #include <boost/multiprecision/detail/default_ops.hpp> 54 result = boost::multiprecision::lcm(a, b);
58 result = boost::multiprecision::gcd(a, b);
69 std::cout << r << std::endl;
74 std::cout << r << std::endl;
78 double x = (double) r;
90 b = (double)nextafter(a, 1e100);
96 a = (double)nextafter(b, -1e100);
99 return ((a == d) || (b == d));
104 r =
Rational(denominator(r), numerator(r));
114 MSG_DEBUG(std::cout <<
"rounding " << str(r) <<
115 " to power of two" <<
"\n");
118 den = denominator(r);
119 roundval = num / den;
121 MSG_DEBUG(std::cout <<
" --> " << str(roundval) <<
"\n");
123 size_t binlog = roundval == 0 ? 1 : msb(roundval) + 1;
126 MSG_DEBUG(std::cout <<
" --> 2^" << binlog <<
"\n");
128 roundval = boost::multiprecision::pow(base, (
unsigned int)binlog);
130 MSG_DEBUG(std::cout <<
" --> " << str(roundval) <<
"\n");
134 MSG_DEBUG(std::cout <<
" --> " << str(r) <<
"\n");
139 std::string::const_iterator
findSubStringIC(
const std::string& substr,
const std::string& str)
141 auto it = std::search(
142 str.begin(), str.end(),
143 substr.begin(), substr.end(),
144 [](
char ch1,
char ch2)
146 return std::toupper(ch1) == std::toupper(ch2);
156 if(0 == strcmp(desc,
"inf"))
160 else if(0 == strcmp(desc,
"-inf"))
169 if(s.find(
'.') == std::string::npos)
184 int exponentidx = int(it - s.begin());
185 mult = std::stoi(s.substr(exponentidx + 1, s.length()));
186 s = s.substr(0, exponentidx);
193 size_t pos = s.find(
'.');
194 size_t exp = s.length() - 1 - pos;
195 std::string den(
"1");
197 for(
size_t i = 0; i < exp; ++i)
201 assert(std::all_of(s.begin() + 1, s.end(), ::isdigit));
205 s.erase(1, std::min(s.substr(1).find_first_not_of(
'0'), s.size() - 1));
207 s.erase(0, std::min(s.find_first_not_of(
'0'), s.size() - 1));
212 res *= pow(10, mult);
222 #ifndef SOPLEX_WITH_GMP 236 inline void rationalErrorMessage()
const 238 MSG_ERROR(std::cerr <<
"Using rational methods without linking boost is not supported" << std::endl;
251 inline Rational(
const long double& r)
270 #ifdef SOPLEX_WITH_BOOST 272 inline template <
typename T, boost::multiprecision::expression_
template_option eto>
273 Rational(
const boost::multiprecision::number<T, eto>& q)
288 inline Rational& operator=(
const long double& r)
293 inline Rational& operator=(
const double& r)
298 inline Rational& operator=(
const int& i)
303 inline Rational& operator=(
const mpq_t& q)
310 rationalErrorMessage();
312 inline void assign(
const long double& r)
314 rationalErrorMessage();
316 inline void assign(
const double& r)
318 rationalErrorMessage();
320 inline void assign(
const int& i)
322 rationalErrorMessage();
328 inline operator double()
const 332 inline operator long double()
const 336 inline operator float()
const 340 #ifdef SOPLEX_WITH_BOOST 341 #ifndef SOPLEX_WITH_CPPMPF 343 inline template <
typename T, boost::multiprecision::expression_
template_option eto>
344 operator boost::multiprecision::number<T, eto>()
const 346 rationalErrorMessage();
351 inline template <
unsigned bits, boost::multiprecision::expression_
template_option eto>
352 operator boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<bits>, eto>()
355 rationalErrorMessage();
373 rationalErrorMessage();
379 rationalErrorMessage();
383 inline Rational operator+(
const double& r)
const 385 rationalErrorMessage();
389 inline Rational operator+=(
const double& r)
391 rationalErrorMessage();
395 inline Rational operator+(
const int& r)
const 397 rationalErrorMessage();
401 inline Rational operator+=(
const int& r)
403 rationalErrorMessage();
409 rationalErrorMessage();
415 rationalErrorMessage();
421 rationalErrorMessage();
425 inline Rational operator-=(
const double& r)
427 rationalErrorMessage();
433 rationalErrorMessage();
437 inline Rational operator-=(
const int& r)
439 rationalErrorMessage();
445 rationalErrorMessage();
451 rationalErrorMessage();
457 rationalErrorMessage();
461 inline Rational operator*=(
const double& r)
463 rationalErrorMessage();
469 rationalErrorMessage();
473 inline Rational operator*=(
const int& r)
475 rationalErrorMessage();
481 rationalErrorMessage();
487 rationalErrorMessage();
491 inline Rational operator/(
const double& r)
const 493 rationalErrorMessage();
497 inline Rational operator/=(
const double& r)
499 rationalErrorMessage();
503 inline Rational operator/(
const int& r)
const 505 rationalErrorMessage();
509 inline Rational operator/=(
const int& r)
511 rationalErrorMessage();
517 rationalErrorMessage();
524 rationalErrorMessage();
531 rationalErrorMessage();
538 rationalErrorMessage();
551 rationalErrorMessage();
564 rationalErrorMessage();
575 r.rationalErrorMessage();
578 inline std::string str()
const 580 this->rationalErrorMessage();
581 return std::string(
"");
590 r.rationalErrorMessage();
595 r.rationalErrorMessage();
600 r.rationalErrorMessage();
605 r.rationalErrorMessage();
610 r.rationalErrorMessage();
615 r.rationalErrorMessage();
620 r.rationalErrorMessage();
624 inline friend bool operator!=(
const Rational& r,
const double& s)
626 r.rationalErrorMessage();
629 inline friend bool operator==(
const Rational& r,
const double& s)
631 r.rationalErrorMessage();
634 inline friend bool operator<(
const Rational& r,
const double& s)
636 r.rationalErrorMessage();
639 inline friend bool operator<=(
const Rational& r,
const double& s)
641 r.rationalErrorMessage();
644 inline friend bool operator>(
const Rational& r,
const double& s)
646 r.rationalErrorMessage();
649 inline friend bool operator>=(
const Rational& r,
const double& s)
651 r.rationalErrorMessage();
655 inline friend bool operator!=(
const double& r,
const Rational& s)
657 s.rationalErrorMessage();
660 inline friend bool operator==(
const double& r,
const Rational& s)
662 s.rationalErrorMessage();
665 inline friend bool operator<(
const double& r,
const Rational& s)
667 s.rationalErrorMessage();
670 inline friend bool operator<=(
const double& r,
const Rational& s)
672 s.rationalErrorMessage();
675 inline friend bool operator>(
const double& r,
const Rational& s)
677 s.rationalErrorMessage();
680 inline friend bool operator>=(
const double& r,
const Rational& s)
682 s.rationalErrorMessage();
686 inline friend bool operator!=(
const Rational& r,
const long double& s)
688 r.rationalErrorMessage();
691 inline friend bool operator==(
const Rational& r,
const long double& s)
693 r.rationalErrorMessage();
696 inline friend bool operator<(
const Rational& r,
const long double& s)
698 r.rationalErrorMessage();
701 inline friend bool operator<=(
const Rational& r,
const long double& s)
703 r.rationalErrorMessage();
706 inline friend bool operator>(
const Rational& r,
const long double& s)
708 r.rationalErrorMessage();
711 inline friend bool operator>=(
const Rational& r,
const long double& s)
713 r.rationalErrorMessage();
717 inline friend bool operator!=(
const long double& r,
const Rational& s)
719 s.rationalErrorMessage();
722 inline friend bool operator==(
const long double& r,
const Rational& s)
724 s.rationalErrorMessage();
727 inline friend bool operator<(
const long double& r,
const Rational& s)
729 s.rationalErrorMessage();
732 inline friend bool operator<=(
const long double& r,
const Rational& s)
734 s.rationalErrorMessage();
737 inline friend bool operator>(
const long double& r,
const Rational& s)
739 s.rationalErrorMessage();
742 inline friend bool operator>=(
const long double& r,
const Rational& s)
744 s.rationalErrorMessage();
748 inline friend bool operator!=(
const Rational& r,
const float& s)
750 r.rationalErrorMessage();
753 inline friend bool operator==(
const Rational& r,
const float& s)
755 r.rationalErrorMessage();
758 inline friend bool operator<(
const Rational& r,
const float& s)
760 r.rationalErrorMessage();
763 inline friend bool operator<=(
const Rational& r,
const float& s)
765 r.rationalErrorMessage();
768 inline friend bool operator>(
const Rational& r,
const float& s)
770 r.rationalErrorMessage();
773 inline friend bool operator>=(
const Rational& r,
const float& s)
775 r.rationalErrorMessage();
779 inline friend bool operator!=(
const float& r,
const Rational& s)
781 s.rationalErrorMessage();
784 inline friend bool operator==(
const float& r,
const Rational& s)
786 s.rationalErrorMessage();
789 inline friend bool operator<(
const float& r,
const Rational& s)
791 s.rationalErrorMessage();
794 inline friend bool operator<=(
const float& r,
const Rational& s)
796 s.rationalErrorMessage();
799 inline friend bool operator>(
const float& r,
const Rational& s)
801 s.rationalErrorMessage();
804 inline friend bool operator>=(
const float& r,
const Rational& s)
806 s.rationalErrorMessage();
812 r.rationalErrorMessage();
817 r.rationalErrorMessage();
822 r.rationalErrorMessage();
827 r.rationalErrorMessage();
831 inline friend bool operator!=(
const Rational& r,
const int& s)
833 r.rationalErrorMessage();
836 inline friend bool operator==(
const Rational& r,
const int& s)
838 r.rationalErrorMessage();
841 inline friend bool operator<(
const Rational& r,
const int& s)
843 r.rationalErrorMessage();
846 inline friend bool operator<=(
const Rational& r,
const int& s)
848 r.rationalErrorMessage();
851 inline friend bool operator>(
const Rational& r,
const int& s)
853 r.rationalErrorMessage();
856 inline friend bool operator>=(
const Rational& r,
const int& s)
858 r.rationalErrorMessage();
862 inline friend bool operator!=(
const int& r,
const Rational& s)
864 s.rationalErrorMessage();
867 inline friend bool operator==(
const int& r,
const Rational& s)
869 s.rationalErrorMessage();
872 inline friend bool operator<(
const int& r,
const Rational& s)
874 s.rationalErrorMessage();
877 inline friend bool operator<=(
const int& r,
const Rational& s)
879 s.rationalErrorMessage();
882 inline friend bool operator>(
const int& r,
const Rational& s)
884 s.rationalErrorMessage();
887 inline friend bool operator>=(
const int& r,
const Rational& s)
889 s.rationalErrorMessage();
895 r.rationalErrorMessage();
900 r.rationalErrorMessage();
905 r.rationalErrorMessage();
910 r.rationalErrorMessage();
916 r.rationalErrorMessage();
919 inline friend int sign(
const Rational& r)
921 r.rationalErrorMessage();
926 q.rationalErrorMessage();
934 r.rationalErrorMessage();
939 r.rationalErrorMessage();
951 r.rationalErrorMessage();
955 r.rationalErrorMessage();
967 #ifndef SOPLEX_WITH_BOOST 968 MSG_ERROR(std::cerr <<
"ERROR: rational solve without Boost not defined!" << std::endl;)
971 size_t densize = msb(denominator(R)) + 1;
972 size_t numsize = msb(numerator(R)) + 1;
974 return (
int)(densize + numsize);
986 for(
int i = 0; i < length; i++)
999 #ifndef SOPLEX_WITH_BOOST 1000 MSG_ERROR(std::cerr <<
"ERROR: rational solve without Boost not defined!" << std::endl;)
1006 for(
int i = 0; i < length; i++)
1007 SpxLcm(lcm, lcm, denominator(vector[i]));
1009 int size = msb(lcm) + 1;
1018 assert(vector != 0);
1019 assert(length >= 0);
1021 #ifndef SOPLEX_WITH_BOOST 1022 MSG_ERROR(std::cerr <<
"ERROR: rational solve without Boost not defined!" << std::endl;)
1028 for(
int i = 0; i < length; i++)
1030 size_t dsize = msb(denominator(vector[i])) + 1;
void printInteger(Integer r)
number< gmp_rational, et_off > Rational
void powRound(Rational &r)
round up to next power of two
bool isAdjacentTo(const Rational &r, const double &d)
VectorBase< R > operator-(const SVectorBase< R > &v, const VectorBase< R > &w)
Subtraction.
Rational ratFromString(const char *desc)
DSVectorBase< R > operator*(const SVectorBase< R > &v, R x)
Scaling.
number< gmp_int, et_off > Integer
std::ostream & operator<<(std::ostream &s, const VectorBase< R > &vec)
Output operator.
void SpxLcm(Integer &result, Integer a, Integer b)
#define MSG_ERROR(x)
Prints out message x if the verbosity level is at least SPxOut::ERROR.
static std::string::const_iterator findSubStringIC(const std::string &substr, const std::string &str)
Debugging, floating point type and parameter definitions.
Everything should be within this namespace.
int totalSizeRational(const Rational *vector, const int length, const int base)
Total size of rational vector.
int sizeInBase(const Rational R, const int base)
Size in specified base (bit size for base 2)
void printRational(Rational r)
int dmaxSizeRational(const Rational *vector, const int length, const int base)
Size of largest denominator in rational vector.
int dlcmSizeRational(const Rational *vector, const int length, const int base)
Size of least common multiple of denominators in rational vector.
void SpxGcd(Integer &result, Integer a, Integer b)