rational_gmp_imp.h
Go to the documentation of this file.
/********************* */
#include "cvc4_public.h"
#ifndef __CVC4__RATIONAL_H
#define __CVC4__RATIONAL_H
#include <gmp.h>
#include <string>
#include "util/integer.h"
#include "util/exception.h"
namespace CVC4 {
class CVC4_PUBLIC RationalFromDoubleException : public Exception {
public:
RationalFromDoubleException(double d) throw();
};
class CVC4_PUBLIC Rational {
private:
mpq_class d_value;
Rational(const mpq_class& val) : d_value(val) { }
public:
static Rational fromDecimal(const std::string& dec);
Rational() : d_value(0){
d_value.canonicalize();
}
explicit Rational(const char* s, unsigned base = 10): d_value(s, base) {
d_value.canonicalize();
}
Rational(const std::string& s, unsigned base = 10) : d_value(s, base) {
d_value.canonicalize();
}
Rational(const Rational& q) : d_value(q.d_value) {
d_value.canonicalize();
}
Rational(signed int n) : d_value(n,1) {
d_value.canonicalize();
}
Rational(unsigned int n) : d_value(n,1) {
d_value.canonicalize();
}
Rational(signed long int n) : d_value(n,1) {
d_value.canonicalize();
}
Rational(unsigned long int n) : d_value(n,1) {
d_value.canonicalize();
}
#ifdef CVC4_NEED_INT64_T_OVERLOADS
Rational(int64_t n) : d_value(static_cast<long>(n), 1) {
d_value.canonicalize();
}
Rational(uint64_t n) : d_value(static_cast<unsigned long>(n), 1) {
d_value.canonicalize();
}
#endif /* CVC4_NEED_INT64_T_OVERLOADS */
Rational(signed int n, signed int d) : d_value(n,d) {
d_value.canonicalize();
}
Rational(unsigned int n, unsigned int d) : d_value(n,d) {
d_value.canonicalize();
}
Rational(signed long int n, signed long int d) : d_value(n,d) {
d_value.canonicalize();
}
Rational(unsigned long int n, unsigned long int d) : d_value(n,d) {
d_value.canonicalize();
}
#ifdef CVC4_NEED_INT64_T_OVERLOADS
Rational(int64_t n, int64_t d) : d_value(static_cast<long>(n), static_cast<long>(d)) {
d_value.canonicalize();
}
Rational(uint64_t n, uint64_t d) : d_value(static_cast<unsigned long>(n), static_cast<unsigned long>(d)) {
d_value.canonicalize();
}
#endif /* CVC4_NEED_INT64_T_OVERLOADS */
Rational(const Integer& n, const Integer& d) :
d_value(n.get_mpz(), d.get_mpz())
{
d_value.canonicalize();
}
Rational(const Integer& n) :
d_value(n.get_mpz())
{
d_value.canonicalize();
}
~Rational() {}
Integer getNumerator() const {
return Integer(d_value.get_num());
}
Integer getDenominator() const {
return Integer(d_value.get_den());
}
static Rational fromDouble(double d) throw(RationalFromDoubleException);
double getDouble() const {
return d_value.get_d();
}
Rational inverse() const {
return Rational(getDenominator(), getNumerator());
}
int cmp(const Rational& x) const {
//Don't use mpq_class's cmp() function.
//The name ends up conflicting with this function.
return mpq_cmp(d_value.get_mpq_t(), x.d_value.get_mpq_t());
}
int sgn() const {
return mpq_sgn(d_value.get_mpq_t());
}
bool isZero() const {
return sgn() == 0;
}
bool isOne() const {
return mpq_cmp_si(d_value.get_mpq_t(), 1, 1) == 0;
}
bool isNegativeOne() const {
return mpq_cmp_si(d_value.get_mpq_t(), -1, 1) == 0;
}
Rational abs() const {
if(sgn() < 0){
return -(*this);
}else{
return *this;
}
}
Integer floor() const {
mpz_class q;
mpz_fdiv_q(q.get_mpz_t(), d_value.get_num_mpz_t(), d_value.get_den_mpz_t());
return Integer(q);
}
Integer ceiling() const {
mpz_class q;
mpz_cdiv_q(q.get_mpz_t(), d_value.get_num_mpz_t(), d_value.get_den_mpz_t());
return Integer(q);
}
Rational floor_frac() const {
return (*this) - Rational(floor());
}
Rational& operator=(const Rational& x){
if(this == &x) return *this;
d_value = x.d_value;
return *this;
}
Rational operator-() const{
return Rational(-(d_value));
}
bool operator==(const Rational& y) const {
return d_value == y.d_value;
}
bool operator!=(const Rational& y) const {
return d_value != y.d_value;
}
bool operator< (const Rational& y) const {
return d_value < y.d_value;
}
bool operator<=(const Rational& y) const {
return d_value <= y.d_value;
}
bool operator> (const Rational& y) const {
return d_value > y.d_value;
}
bool operator>=(const Rational& y) const {
return d_value >= y.d_value;
}
Rational operator+(const Rational& y) const{
return Rational( d_value + y.d_value );
}
Rational operator-(const Rational& y) const {
return Rational( d_value - y.d_value );
}
Rational operator*(const Rational& y) const {
return Rational( d_value * y.d_value );
}
Rational operator/(const Rational& y) const {
return Rational( d_value / y.d_value );
}
Rational& operator+=(const Rational& y){
d_value += y.d_value;
return (*this);
}
Rational& operator-=(const Rational& y){
d_value -= y.d_value;
return (*this);
}
Rational& operator*=(const Rational& y){
d_value *= y.d_value;
return (*this);
}
Rational& operator/=(const Rational& y){
d_value /= y.d_value;
return (*this);
}
bool isIntegral() const{
return getDenominator() == 1;
}
std::string toString(int base = 10) const {
return d_value.get_str(base);
}
size_t hash() const {
size_t numeratorHash = gmpz_hash(d_value.get_num_mpz_t());
size_t denominatorHash = gmpz_hash(d_value.get_den_mpz_t());
return numeratorHash xor denominatorHash;
}
uint32_t complexity() const {
uint32_t numLen = getNumerator().length();
uint32_t denLen = getDenominator().length();
return numLen + denLen;
}
int absCmp(const Rational& q) const;
};/* class Rational */
struct RationalHashFunction {
inline size_t operator()(const CVC4::Rational& r) const {
return r.hash();
}
};/* struct RationalHashFunction */
CVC4_PUBLIC std::ostream& operator<<(std::ostream& os, const Rational& n);
}/* CVC4 namespace */
#endif /* __CVC4__RATIONAL_H */
CVC4::Exception
Definition exception.h:32
CVC4::Integer
Definition integer_gmp_imp.h:34
CVC4::RationalFromDoubleException
Definition rational_gmp_imp.h:31
CVC4::RationalFromDoubleException::RationalFromDoubleException
RationalFromDoubleException(double d)
CVC4::Rational
A multi-precision rational constant.
Definition rational_gmp_imp.h:51
CVC4::Rational::toString
std::string toString(int base=10) const
Returns a string representing the rational in the given base.
Definition rational_gmp_imp.h:313
CVC4::Rational::inverse
Rational inverse() const
Definition rational_gmp_imp.h:191
CVC4::Rational::isOne
bool isOne() const
Definition rational_gmp_imp.h:209
CVC4::Rational::sgn
int sgn() const
Definition rational_gmp_imp.h:201
CVC4::Rational::getDenominator
Integer getDenominator() const
Returns the value of denominator of the Rational.
Definition rational_gmp_imp.h:176
CVC4::Rational::absCmp
int absCmp(const Rational &q) const
Equivalent to calling (this->abs()).cmp(b.abs())
CVC4::Rational::operator-
Rational operator-() const
Definition rational_gmp_imp.h:247
CVC4::Rational::operator*
Rational operator*(const Rational &y) const
Definition rational_gmp_imp.h:282
CVC4::Rational::fromDecimal
static Rational fromDecimal(const std::string &dec)
Creates a rational from a decimal string (e.g., "1.5").
CVC4::Rational::abs
Rational abs() const
Definition rational_gmp_imp.h:217
CVC4::Rational::operator!=
bool operator!=(const Rational &y) const
Definition rational_gmp_imp.h:255
CVC4::Rational::hash
size_t hash() const
Computes the hash of the rational from hashes of the numerator and the denominator.
Definition rational_gmp_imp.h:321
CVC4::Rational::Rational
Rational(const Integer &n, const Integer &d)
Definition rational_gmp_imp.h:152
CVC4::Rational::fromDouble
static Rational fromDouble(double d)
CVC4::Rational::~Rational
~Rational()
Definition rational_gmp_imp.h:162
CVC4::Rational::floor
Integer floor() const
Definition rational_gmp_imp.h:225
CVC4::Rational::Rational
Rational(signed int n, signed int d)
Constructs a canonical Rational from a numerator and denominator.
Definition rational_gmp_imp.h:130
CVC4::Rational::operator-=
Rational & operator-=(const Rational &y)
Definition rational_gmp_imp.h:293
CVC4::Rational::Rational
Rational(unsigned int n)
Definition rational_gmp_imp.h:108
CVC4::Rational::operator/=
Rational & operator/=(const Rational &y)
Definition rational_gmp_imp.h:303
CVC4::Rational::ceiling
Integer ceiling() const
Definition rational_gmp_imp.h:231
CVC4::Rational::Rational
Rational(const char *s, unsigned base=10)
Constructs a Rational from a C string in a given base (defaults to 10).
Definition rational_gmp_imp.h:88
CVC4::Rational::cmp
int cmp(const Rational &x) const
Definition rational_gmp_imp.h:195
CVC4::Rational::operator+=
Rational & operator+=(const Rational &y)
Definition rational_gmp_imp.h:289
CVC4::Rational::getDouble
double getDouble() const
Get a double representation of this Rational, which is approximate: truncation may occur,...
Definition rational_gmp_imp.h:187
CVC4::Rational::getNumerator
Integer getNumerator() const
Returns the value of numerator of the Rational.
Definition rational_gmp_imp.h:168
CVC4::Rational::Rational
Rational(signed long int n)
Definition rational_gmp_imp.h:111
CVC4::Rational::Rational
Rational()
Constructs a rational with the value 0/1.
Definition rational_gmp_imp.h:78
CVC4::Rational::Rational
Rational(unsigned int n, unsigned int d)
Definition rational_gmp_imp.h:133
CVC4::Rational::Rational
Rational(signed long int n, signed long int d)
Definition rational_gmp_imp.h:136
CVC4::Rational::operator+
Rational operator+(const Rational &y) const
Definition rational_gmp_imp.h:275
CVC4::Rational::isNegativeOne
bool isNegativeOne() const
Definition rational_gmp_imp.h:213
CVC4::Rational::operator=
Rational & operator=(const Rational &x)
Definition rational_gmp_imp.h:241
CVC4::Rational::operator-
Rational operator-(const Rational &y) const
Definition rational_gmp_imp.h:278
CVC4::Rational::operator/
Rational operator/(const Rational &y) const
Definition rational_gmp_imp.h:285
CVC4::Rational::Rational
Rational(const Integer &n)
Definition rational_gmp_imp.h:157
CVC4::Rational::Rational
Rational(unsigned long int n)
Definition rational_gmp_imp.h:114
CVC4::Rational::Rational
Rational(const Rational &q)
Creates a Rational from another Rational, q, by performing a deep copy.
Definition rational_gmp_imp.h:98
CVC4::Rational::Rational
Rational(signed int n)
Constructs a canonical Rational from a numerator.
Definition rational_gmp_imp.h:105
CVC4::Rational::isIntegral
bool isIntegral() const
Definition rational_gmp_imp.h:308
CVC4::Rational::floor_frac
Rational floor_frac() const
Definition rational_gmp_imp.h:237
CVC4::Rational::Rational
Rational(const std::string &s, unsigned base=10)
Definition rational_gmp_imp.h:91
CVC4::Rational::operator==
bool operator==(const Rational &y) const
Definition rational_gmp_imp.h:251
CVC4::Rational::Rational
Rational(unsigned long int n, unsigned long int d)
Definition rational_gmp_imp.h:139
CVC4::Rational::isZero
bool isZero() const
Definition rational_gmp_imp.h:205
CVC4::Rational::complexity
uint32_t complexity() const
Definition rational_gmp_imp.h:328
CVC4::Rational::operator*=
Rational & operator*=(const Rational &y)
Definition rational_gmp_imp.h:298
CVC4::Rational::operator<=
bool operator<=(const Rational &y) const
Definition rational_gmp_imp.h:263
CVC4::Rational::operator>=
bool operator>=(const Rational &y) const
Definition rational_gmp_imp.h:271
cvc4_public.h
Macros that should be defined everywhere during the building of the libraries and driver binary,...
CVC4_PUBLIC
#define CVC4_PUBLIC
Definition cvc4_public.h:30
exception.h
CVC4's exception base class and some associated utilities.
CVC4
Definition expr.h:106
CVC4::gmpz_hash
size_t gmpz_hash(const mpz_t toHash)
Hashes the gmp integer primitive in a word by word fashion.
Definition gmp_util.h:28
CVC4::operator<<
std::ostream & operator<<(std::ostream &out, const TypeCheckingException &e)
CVC4::RationalHashFunction
Definition rational_gmp_imp.h:339
CVC4::RationalHashFunction::operator()
size_t operator()(const CVC4::Rational &r) const
Definition rational_gmp_imp.h:340