41#ifdef SOPLEX_WITH_BOOST
42#include <boost/multiprecision/number.hpp>
45#include <boost/multiprecision/gmp.hpp>
50using namespace boost::multiprecision;
55 mpz_lcm(result.backend().data(), a.backend().data(), b.backend().data());
59 mpz_gcd(result.backend().data(), a.backend().data(), b.backend().data());
64#include <boost/multiprecision/cpp_int.hpp>
65#include <boost/multiprecision/detail/default_ops.hpp>
70using namespace boost::multiprecision;
75 result = boost::multiprecision::lcm(a, b);
79 result = boost::multiprecision::gcd(a, b);
90 std::cout << r << std::endl;
95 std::cout << r << std::endl;
99 double x = (double) r;
111 b = (double)nextafter(a, 1e100);
117 a = (double)nextafter(b, -1e100);
120 return ((a == d) || (b == d));
125 r =
Rational(denominator(r), numerator(r));
136 den = denominator(r);
137 roundval = num / den;
139 size_t binlog = roundval == 0 ? 1 : msb(roundval) + 1;
142 roundval = boost::multiprecision::pow(base, (
unsigned int)binlog);
150 if(numerator(r) == 0 || (
int) log10((
double)numerator(r)) == log10((
double)denominator(r)))
153 return (
int) log10((
double)numerator(r)) - (int) log10((
double)denominator(r));
158std::string::const_iterator
findSubStringIC(
const std::string& substr,
const std::string& str)
160 auto it = std::search(
161 str.begin(), str.end(),
162 substr.begin(), substr.end(),
163 [](
char ch1,
char ch2)
165 return std::toupper(ch1) == std::toupper(ch2);
175 if(0 == strcmp(desc,
"inf"))
179 else if(0 == strcmp(desc,
"-inf"))
188 if(s.find_first_of(
".Ee") == std::string::npos)
203 int exponentidx = int(it - s.begin());
204 mult = std::stoi(s.substr(exponentidx + 1, s.length()));
205 s = s.substr(0, exponentidx);
212 size_t pos = s.find(
'.');
215 if(pos != std::string::npos)
217 size_t exp = s.length() - 1 - pos;
218 std::string den(
"1");
220 for(
size_t i = 0; i < exp; ++i)
224 assert(std::all_of(s.begin() + 1, s.end(), ::isdigit));
228 s.erase(1,
SOPLEX_MIN(s.substr(1).find_first_not_of(
'0'), s.size() - 1));
230 s.erase(0,
SOPLEX_MIN(s.find_first_not_of(
'0'), s.size() - 1));
241 res *= pow(10, mult);
262 inline void rationalErrorMessage()
const
264 SPX_MSG_ERROR(std::cerr <<
"Using rational methods without linking boost is not supported" <<
277 inline Rational(
const long double& r)
292#ifdef SOPLEX_WITH_GMP
298#ifdef SOPLEX_WITH_BOOST
300 inline template <
typename T, boost::multiprecision::expression_
template_option eto>
301 Rational(
const boost::multiprecision::number<T, eto>& q)
317 inline Rational& operator=(
const long double& r)
322 inline Rational& operator=(
const double& r)
327 inline Rational& operator=(
const int& i)
331#ifdef SOPLEX_WITH_GMP
333 inline Rational& operator=(
const mpq_t& q)
341 rationalErrorMessage();
343 inline void assign(
const long double& r)
345 rationalErrorMessage();
347 inline void assign(
const double& r)
349 rationalErrorMessage();
351 inline void assign(
const int& i)
353 rationalErrorMessage();
359 inline operator double()
const
363 inline operator long double()
const
367 inline operator float()
const
371#ifdef SOPLEX_WITH_BOOST
372#ifndef SOPLEX_WITH_CPPMPF
374 inline template <
typename T, boost::multiprecision::expression_
template_option eto>
375 operator boost::multiprecision::number<T, eto>()
const
377 rationalErrorMessage();
382 inline template <
unsigned bits, boost::multiprecision::expression_
template_option eto>
383 operator boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<bits>, eto>()
386 rationalErrorMessage();
404 rationalErrorMessage();
410 rationalErrorMessage();
414 inline Rational operator+(
const double& r)
const
416 rationalErrorMessage();
420 inline Rational operator+=(
const double& r)
422 rationalErrorMessage();
426 inline Rational operator+(
const int& r)
const
428 rationalErrorMessage();
432 inline Rational operator+=(
const int& r)
434 rationalErrorMessage();
440 rationalErrorMessage();
446 rationalErrorMessage();
452 rationalErrorMessage();
456 inline Rational operator-=(
const double& r)
458 rationalErrorMessage();
464 rationalErrorMessage();
468 inline Rational operator-=(
const int& r)
470 rationalErrorMessage();
476 rationalErrorMessage();
482 rationalErrorMessage();
488 rationalErrorMessage();
492 inline Rational operator*=(
const double& r)
494 rationalErrorMessage();
500 rationalErrorMessage();
504 inline Rational operator*=(
const int& r)
506 rationalErrorMessage();
512 rationalErrorMessage();
518 rationalErrorMessage();
522 inline Rational operator/(
const double& r)
const
524 rationalErrorMessage();
528 inline Rational operator/=(
const double& r)
530 rationalErrorMessage();
534 inline Rational operator/(
const int& r)
const
536 rationalErrorMessage();
540 inline Rational operator/=(
const int& r)
542 rationalErrorMessage();
548 rationalErrorMessage();
555 rationalErrorMessage();
562 rationalErrorMessage();
569 rationalErrorMessage();
582 rationalErrorMessage();
595 rationalErrorMessage();
606 r.rationalErrorMessage();
609 inline std::string str()
const
611 this->rationalErrorMessage();
612 return std::string(
"");
621 r.rationalErrorMessage();
626 r.rationalErrorMessage();
631 r.rationalErrorMessage();
636 r.rationalErrorMessage();
641 r.rationalErrorMessage();
646 r.rationalErrorMessage();
651 r.rationalErrorMessage();
655 inline friend bool operator!=(
const Rational& r,
const double& s)
657 r.rationalErrorMessage();
660 inline friend bool operator==(
const Rational& r,
const double& s)
662 r.rationalErrorMessage();
665 inline friend bool operator<(
const Rational& r,
const double& s)
667 r.rationalErrorMessage();
670 inline friend bool operator<=(
const Rational& r,
const double& s)
672 r.rationalErrorMessage();
675 inline friend bool operator>(
const Rational& r,
const double& s)
677 r.rationalErrorMessage();
680 inline friend bool operator>=(
const Rational& r,
const double& s)
682 r.rationalErrorMessage();
686 inline friend bool operator!=(
const double& r,
const Rational& s)
688 s.rationalErrorMessage();
691 inline friend bool operator==(
const double& r,
const Rational& s)
693 s.rationalErrorMessage();
696 inline friend bool operator<(
const double& r,
const Rational& s)
698 s.rationalErrorMessage();
701 inline friend bool operator<=(
const double& r,
const Rational& s)
703 s.rationalErrorMessage();
706 inline friend bool operator>(
const double& r,
const Rational& s)
708 s.rationalErrorMessage();
711 inline friend bool operator>=(
const double& r,
const Rational& s)
713 s.rationalErrorMessage();
717 inline friend bool operator!=(
const Rational& r,
const long double& s)
719 r.rationalErrorMessage();
722 inline friend bool operator==(
const Rational& r,
const long double& s)
724 r.rationalErrorMessage();
727 inline friend bool operator<(
const Rational& r,
const long double& s)
729 r.rationalErrorMessage();
732 inline friend bool operator<=(
const Rational& r,
const long double& s)
734 r.rationalErrorMessage();
737 inline friend bool operator>(
const Rational& r,
const long double& s)
739 r.rationalErrorMessage();
742 inline friend bool operator>=(
const Rational& r,
const long double& s)
744 r.rationalErrorMessage();
748 inline friend bool operator!=(
const long double& r,
const Rational& s)
750 s.rationalErrorMessage();
753 inline friend bool operator==(
const long double& r,
const Rational& s)
755 s.rationalErrorMessage();
758 inline friend bool operator<(
const long double& r,
const Rational& s)
760 s.rationalErrorMessage();
763 inline friend bool operator<=(
const long double& r,
const Rational& s)
765 s.rationalErrorMessage();
768 inline friend bool operator>(
const long double& r,
const Rational& s)
770 s.rationalErrorMessage();
773 inline friend bool operator>=(
const long double& r,
const Rational& s)
775 s.rationalErrorMessage();
779 inline friend bool operator!=(
const Rational& r,
const float& s)
781 r.rationalErrorMessage();
784 inline friend bool operator==(
const Rational& r,
const float& s)
786 r.rationalErrorMessage();
789 inline friend bool operator<(
const Rational& r,
const float& s)
791 r.rationalErrorMessage();
794 inline friend bool operator<=(
const Rational& r,
const float& s)
796 r.rationalErrorMessage();
799 inline friend bool operator>(
const Rational& r,
const float& s)
801 r.rationalErrorMessage();
804 inline friend bool operator>=(
const Rational& r,
const float& s)
806 r.rationalErrorMessage();
810 inline friend bool operator!=(
const float& r,
const Rational& s)
812 s.rationalErrorMessage();
815 inline friend bool operator==(
const float& r,
const Rational& s)
817 s.rationalErrorMessage();
820 inline friend bool operator<(
const float& r,
const Rational& s)
822 s.rationalErrorMessage();
825 inline friend bool operator<=(
const float& r,
const Rational& s)
827 s.rationalErrorMessage();
830 inline friend bool operator>(
const float& r,
const Rational& s)
832 s.rationalErrorMessage();
835 inline friend bool operator>=(
const float& r,
const Rational& s)
837 s.rationalErrorMessage();
843 r.rationalErrorMessage();
848 r.rationalErrorMessage();
853 r.rationalErrorMessage();
858 r.rationalErrorMessage();
862 inline friend bool operator!=(
const Rational& r,
const int& s)
864 r.rationalErrorMessage();
867 inline friend bool operator==(
const Rational& r,
const int& s)
869 r.rationalErrorMessage();
872 inline friend bool operator<(
const Rational& r,
const int& s)
874 r.rationalErrorMessage();
877 inline friend bool operator<=(
const Rational& r,
const int& s)
879 r.rationalErrorMessage();
882 inline friend bool operator>(
const Rational& r,
const int& s)
884 r.rationalErrorMessage();
887 inline friend bool operator>=(
const Rational& r,
const int& s)
889 r.rationalErrorMessage();
893 inline friend bool operator!=(
const int& r,
const Rational& s)
895 s.rationalErrorMessage();
898 inline friend bool operator==(
const int& r,
const Rational& s)
900 s.rationalErrorMessage();
903 inline friend bool operator<(
const int& r,
const Rational& s)
905 s.rationalErrorMessage();
908 inline friend bool operator<=(
const int& r,
const Rational& s)
910 s.rationalErrorMessage();
913 inline friend bool operator>(
const int& r,
const Rational& s)
915 s.rationalErrorMessage();
918 inline friend bool operator>=(
const int& r,
const Rational& s)
920 s.rationalErrorMessage();
926 r.rationalErrorMessage();
931 r.rationalErrorMessage();
936 r.rationalErrorMessage();
941 r.rationalErrorMessage();
947 r.rationalErrorMessage();
950 inline friend int sign(
const Rational& r)
952 r.rationalErrorMessage();
957 q.rationalErrorMessage();
965 r.rationalErrorMessage();
970 r.rationalErrorMessage();
979 r.rationalErrorMessage();
987 r.rationalErrorMessage();
991 r.rationalErrorMessage();
1001#ifndef SOPLEX_WITH_BOOST
1002 SPX_MSG_ERROR(std::cerr <<
"ERROR: rational solve without Boost not defined!" << std::endl;)
1011 size_t numsize, densize;
1013#ifdef SOPLEX_WITH_GMP
1014 densize = mpz_sizeinbase(den.backend().data(), base);
1015 numsize = mpz_sizeinbase(num.backend().data(), base);
1020 densize = (size_t)(log2(den.convert_to<
double>()) / log2(
double(base))) + 1;
1021 numsize = (size_t)(log2(num.convert_to<
double>()) / log2(
double(base))) + 1;
1025 densize = msb(den) + 1;
1026 numsize = msb(num) + 1;
1031 return (
int)(densize + numsize);
1037 assert(vector !=
nullptr);
1038 assert(length >= 0);
1043 for(
int i = 0; i < length; i++)
1052 assert(vector !=
nullptr);
1053 assert(length >= 0);
1055#ifndef SOPLEX_WITH_BOOST
1056 SPX_MSG_ERROR(std::cerr <<
"ERROR: rational solve without Boost not defined!" << std::endl;)
1062 for(
int i = 0; i < length; i++)
1063 SpxLcm(lcm, lcm, denominator(vector[i]));
1074 assert(vector !=
nullptr);
1075 assert(length >= 0);
1076#ifndef SOPLEX_WITH_BOOST
1077 SPX_MSG_ERROR(std::cerr <<
"ERROR: rational solve without Boost not defined!" << std::endl;)
1083 for(
int i = 0; i < length; i++)
Everything should be within this namespace.
Rational ratFromString(const char *desc)
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)
number< gmp_int, et_off > Integer
VectorBase< R > operator-(const SVectorBase< R > &v, const VectorBase< R > &w)
Subtraction.
bool isAdjacentTo(const Rational &r, const double &d)
std::ostream & operator<<(std::ostream &s, const VectorBase< R > &vec)
Output operator.
int totalSizeRational(const Rational *vector, const int length, const int base)
Total size of rational vector.
static std::string::const_iterator findSubStringIC(const std::string &substr, const std::string &str)
number< gmp_rational, et_off > Rational
DSVectorBase< R > operator*(const SVectorBase< R > &v, R x)
Scaling.
void powRound(Rational &r)
round up to next power of two
void printInteger(Integer r)
void printRational(Rational r)
int sizeInBase(const Rational R, const int base)
Size in specified base (bit size for base 2)
int orderOfMagnitude(Rational &r)
returns the order of magnitude of the given rational
int dmaxSizeRational(const Rational *vector, const int length, const int base)
Size of largest denominator in rational vector.
void SpxLcm(Integer &result, Integer a, Integer b)
Debugging, floating point type and parameter definitions.
#define SPX_MSG_ERROR(x)
Prints out message x if the verbosity level is at least SPxOut::ERROR.