41#ifdef SOPLEX_WITH_BOOST
42#include <boost/multiprecision/number.hpp>
45#include <boost/multiprecision/gmp.hpp>
50using namespace boost::multiprecision;
51using Rational = number<gmp_rational, et_off>;
52using Integer = number<gmp_int, et_off>;
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;
93inline void printInteger(
Integer r)
95 std::cout << r << std::endl;
97inline bool isAdjacentTo(
const Rational& r,
const double& d)
99 double x = (double) r;
111 b = (double)nextafter(a, 1e100);
117 a = (double)nextafter(b, -1e100);
120 return ((a == d) || (b == d));
137 roundval = num / den;
139 size_t binlog = roundval == 0 ? 1 : msb(roundval) + 1;
142 roundval = boost::multiprecision::pow(base, (
unsigned int)binlog);
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)
198 std::string::const_iterator it = findSubStringIC(
"e", s);
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);
264 SPX_MSG_ERROR(std::cerr <<
"Using rational methods without linking boost is not supported" <<
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)
331#ifdef SOPLEX_WITH_GMP
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
382 inline template <
unsigned bits, boost::multiprecision::expression_
template_option eto>
383 operator boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<bits>, eto>()
609 inline std::string
str()
const
612 return std::string(
"");
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++)
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++)
friend bool operator<=(const long double &r, const Rational &s)
friend bool operator!=(const Rational &r, const float &s)
Rational operator*=(const Rational &r)
multiplication assignment operator operator
friend Rational operator/(const double &d, const Rational &r)
friend int sign(const Rational &r)
friend Rational operator*(const int &d, const Rational &r)
Rational operator/(const Rational &r) const
division operator
Rational operator-=(const int &r)
subtraction assignment operator for ints
friend bool operator!=(const int &r, const Rational &s)
friend bool operator<=(const Rational &r, const float &s)
void assign(const int &i)
Rational(const Rational &r)
copy constructor
int sizeInBase(const int base=2) const
Size in specified base (bit size for base 2)
Rational & addProduct(const Rational &r, const Rational &s)
add product of two rationals
friend bool operator==(const Rational &r, const Rational &s)
friend bool operator>=(const long double &r, const Rational &s)
Rational(const Integer &num, const Integer &den)
constructor from Integer fraction pair
Rational(const long double &r)
constructor from long double
friend bool operator<=(const float &r, const Rational &s)
friend bool operator>=(const float &r, const Rational &s)
Rational operator*(const int &r) const
multiplication operator for ints
friend bool operator>=(const double &r, const Rational &s)
friend bool operator<(const Rational &r, const long double &s)
friend bool operator>=(const Rational &r, const long double &s)
friend bool operator<=(const Rational &r, const int &s)
Rational operator/=(const double &r)
division assignment operator for doubles
friend bool operator<(const Rational &r, const Rational &s)
friend bool operator<(const double &r, const Rational &s)
friend bool operator==(const double &r, const Rational &s)
friend bool operator>(const Rational &r, const long double &s)
Rational & operator=(const mpq_t &q)
assignment from mpq_t (GMP only)
void assign(const long double &r)
friend bool operator!=(const Rational &r, const int &s)
Rational & operator=(const double &r)
assignment from double
friend bool operator<(const float &r, const Rational &s)
Rational operator-(const Rational &r) const
subtraction operator
friend Rational operator+(const double &d, const Rational &r)
friend bool operator<=(const double &r, const Rational &s)
Rational operator+=(const Rational &r)
addition assignment operator
friend bool operator>(const double &r, const Rational &s)
Rational(const mpq_t &q)
constructor from mpq_t (GMP only)
void assign(const Rational &)
Rational & subProduct(const Rational &r, const Rational &s)
subtract product of two rationals
Rational operator*=(const int &r)
multiplication assignment operator for ints
friend int compareRational(const Rational &r, const Rational &s)
Rational & subQuotient(const Rational &r, const Rational &s)
subtract quotient of two rationals, r divided by s
friend bool operator==(const long double &r, const Rational &s)
friend bool operator<(const Rational &r, const float &s)
Rational operator-(const int &r) const
subtraction operator for ints
friend bool operator>(const long double &r, const Rational &s)
friend bool operator<(const int &r, const Rational &s)
friend bool operator!=(const long double &r, const Rational &s)
friend bool operator!=(const Rational &r, const long double &s)
Rational operator+(const double &r) const
addition operator for doubles
friend std::ostream & operator<<(std::ostream &os, const Rational &r)
friend bool operator>=(const Rational &r, const int &s)
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
friend Rational operator-(const int &d, const Rational &r)
friend bool operator>=(const int &r, const Rational &s)
Rational & operator=(const Rational &)
copy assignment
friend bool operator<=(const int &r, const Rational &s)
friend bool operator<=(const Rational &r, const double &s)
friend bool operator!=(const double &r, const Rational &s)
friend bool operator>=(const Rational &r, const double &s)
Rational operator/(const int &r) const
division operator for ints
Rational(const int &i)
constructor from int
Rational operator-=(const double &r)
subtraction assignment operator for doubles
friend bool operator>(const int &r, const Rational &s)
Rational(const double &r)
constructor from double
friend bool operator>(const float &r, const Rational &s)
friend bool operator==(const Rational &r, const float &s)
friend Rational operator*(const double &d, const Rational &r)
Rational operator-=(const Rational &r)
subtraction assignment operator
friend bool operator<=(const Rational &r, const long double &s)
friend bool operator!=(const Rational &r, const Rational &s)
Rational operator+=(const int &r)
addition assignment operator for ints
Rational & operator=(const int &i)
assignment from int
Rational operator+=(const double &r)
addition assignment operator for doubles
friend bool operator!=(const float &r, const Rational &s)
Rational operator*(const Rational &r) const
multiplication operator
friend bool operator>=(const Rational &r, const Rational &s)
friend Rational spxAbs(const Rational &r)
friend bool operator==(const float &r, const Rational &s)
Rational operator/=(const int &r)
division assignment operator for ints
friend bool operator==(const int &r, const Rational &s)
Rational operator*=(const double &r)
multiplication assignment operator for doubles
friend bool operator>(const Rational &r, const Rational &s)
friend bool operator>(const Rational &r, const float &s)
friend bool operator!=(const Rational &r, const double &s)
friend bool operator<(const long double &r, const Rational &s)
friend bool operator<=(const Rational &r, const Rational &s)
friend bool operator>=(const Rational &r, const float &s)
friend bool operator==(const Rational &r, const double &s)
Rational & operator=(const long double &r)
assignment from long double
friend bool operator<(const Rational &r, const double &s)
friend bool operator==(const Rational &r, const long double &s)
friend bool operator>(const Rational &r, const double &s)
Rational operator/(const double &r) const
division operator for doubles
void assign(const double &r)
friend bool operator==(const Rational &r, const int &s)
friend Rational operator/(const int &d, const Rational &r)
Rational operator+(const Rational &r) const
addition operator
Rational operator-(const double &r) const
subtraction operator for doubles
Rational()
default constructor
Rational operator*(const double &r) const
multiplication operator for doubles
Rational operator+(const int &r) const
addition operator for ints
friend Rational operator-(const double &d, const Rational &r)
friend bool operator>(const Rational &r, const int &s)
friend Rational operator-(const Rational &q)
void rationalErrorMessage() const
Rational & addQuotient(const Rational &r, const Rational &s)
add quotient of two rationals, r divided by s
friend bool operator<(const Rational &r, const int &s)
friend Rational operator+(const int &d, const Rational &r)
Rational operator/=(const Rational &r)
division assignment operator
Everything should be within this namespace.
int dlcmSizeRational(const Rational *vector, const int length, const int base)
Size of least common multiple of denominators in rational vector.
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)
int dmaxSizeRational(const Rational *vector, const int length, const int base)
Size of largest denominator in rational vector.
int orderOfMagnitude(Rational &r)
void powRound(Rational &r)
void divide_qr(Integer &result, Integer &result2, Integer a, Integer b)
Integer numerator(const Rational &r)
void SpxLcm(Integer &result, Integer a, Integer b)
void SpxGcd(Integer &result, Integer a, Integer b)
Integer denominator(const Rational &r)
Rational ratFromString(const char *desc)
Debugging, floating point type and parameter definitions.
#define SPX_MSG_ERROR(x)
Prints out message x if the verbosity level is at least SPxOut::VERB_ERROR.