rational_cln_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 <sstream>
#include <cassert>
#include <cln/rational.h>
#include <cln/input.h>
#include <cln/io.h>
#include <cln/output.h>
#include <cln/rational_io.h>
#include <cln/number_io.h>
#include <cln/dfloat.h>
#include <cln/real.h>
#include "util/exception.h"
#include "util/integer.h"
namespace CVC4 {
class CVC4_PUBLIC RationalFromDoubleException : public Exception {
public:
RationalFromDoubleException(double d) throw();
};
class CVC4_PUBLIC Rational {
private:
cln::cl_RA d_value;
//Rational(const mpq_class& val) : d_value(val) { }
Rational(const cln::cl_RA& val) : d_value(val) { }
public:
static Rational fromDecimal(const std::string& dec);
Rational() : d_value(0){
}
explicit Rational(const char* s, unsigned base = 10) throw (std::invalid_argument){
cln::cl_read_flags flags;
flags.syntax = cln::syntax_rational;
flags.lsyntax = cln::lsyntax_standard;
flags.rational_base = base;
try{
d_value = read_rational(flags, s, NULL, NULL);
}catch(...){
std::stringstream ss;
ss << "Rational() failed to parse value \"" <<s << "\" in base=" <<base;
throw std::invalid_argument(ss.str());
}
}
Rational(const std::string& s, unsigned base = 10) throw (std::invalid_argument){
cln::cl_read_flags flags;
flags.syntax = cln::syntax_rational;
flags.lsyntax = cln::lsyntax_standard;
flags.rational_base = base;
try{
d_value = read_rational(flags, s.c_str(), NULL, NULL);
}catch(...){
std::stringstream ss;
ss << "Rational() failed to parse value \"" <<s << "\" in base=" <<base;
throw std::invalid_argument(ss.str());
}
}
Rational(const Rational& q) : d_value(q.d_value) { }
Rational(signed int n) : d_value((signed long int)n) { }
Rational(unsigned int n) : d_value((unsigned long int)n) { }
Rational(signed long int n) : d_value(n) { }
Rational(unsigned long int n) : d_value(n) { }
#ifdef CVC4_NEED_INT64_T_OVERLOADS
Rational(int64_t n) : d_value(static_cast<long>(n)) { }
Rational(uint64_t n) : d_value(static_cast<unsigned long>(n)) { }
#endif /* CVC4_NEED_INT64_T_OVERLOADS */
Rational(signed int n, signed int d) : d_value((signed long int)n) {
d_value /= cln::cl_I(d);
}
Rational(unsigned int n, unsigned int d) : d_value((unsigned long int)n) {
d_value /= cln::cl_I(d);
}
Rational(signed long int n, signed long int d) : d_value(n) {
d_value /= cln::cl_I(d);
}
Rational(unsigned long int n, unsigned long int d) : d_value(n) {
d_value /= cln::cl_I(d);
}
#ifdef CVC4_NEED_INT64_T_OVERLOADS
Rational(int64_t n, int64_t d) : d_value(static_cast<long>(n)) {
d_value /= cln::cl_I(d);
}
Rational(uint64_t n, uint64_t d) : d_value(static_cast<unsigned long>(n)) {
d_value /= cln::cl_I(d);
}
#endif /* CVC4_NEED_INT64_T_OVERLOADS */
Rational(const Integer& n, const Integer& d) :
d_value(n.get_cl_I())
{
d_value /= d.get_cl_I();
}
Rational(const Integer& n) : d_value(n.get_cl_I()){ }
~Rational() {}
Integer getNumerator() const {
return Integer(cln::numerator(d_value));
}
Integer getDenominator() const {
return Integer(cln::denominator(d_value));
}
static Rational fromDouble(double d) throw(RationalFromDoubleException);
double getDouble() const {
return cln::double_approx(d_value);
}
Rational inverse() const {
return Rational(cln::recip(d_value));
}
int cmp(const Rational& x) const {
//Don't use mpq_class's cmp() function.
//The name ends up conflicting with this function.
return cln::compare(d_value, x.d_value);
}
int sgn() const {
if(cln::zerop(d_value)){
return 0;
}else if(cln::minusp(d_value)){
return -1;
}else{
assert(cln::plusp(d_value));
return 1;
}
}
bool isZero() const {
return cln::zerop(d_value);
}
bool isOne() const {
return d_value == 1;
}
bool isNegativeOne() const {
return d_value == -1;
}
Rational abs() const {
if(sgn() < 0){
return -(*this);
}else{
return *this;
}
}
bool isIntegral() const{
return getDenominator() == 1;
}
Integer floor() const {
return Integer(cln::floor1(d_value));
}
Integer ceiling() const {
return Integer(cln::ceiling1(d_value));
}
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);
}
std::string toString(int base = 10) const {
cln::cl_print_flags flags;
flags.rational_base = base;
flags.rational_readably = false;
std::stringstream ss;
print_rational(ss, flags, d_value);
return ss.str();
}
size_t hash() const {
return equal_hashcode(d_value);
}
uint32_t complexity() const {
return getNumerator().length() + getDenominator().length();
}
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::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_cln_imp.h:335
CVC4::Rational::inverse
Rational inverse() const
Definition rational_cln_imp.h:208
CVC4::Rational::isOne
bool isOne() const
Definition rational_cln_imp.h:234
CVC4::Rational::sgn
int sgn() const
Definition rational_cln_imp.h:219
CVC4::Rational::getDenominator
Integer getDenominator() const
Returns the value of denominator of the Rational.
Definition rational_cln_imp.h:192
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_cln_imp.h:272
CVC4::Rational::operator*
Rational operator*(const Rational &y) const
Definition rational_cln_imp.h:307
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_cln_imp.h:242
CVC4::Rational::operator!=
bool operator!=(const Rational &y) const
Definition rational_cln_imp.h:280
CVC4::Rational::hash
size_t hash() const
Computes the hash of the rational from hashes of the numerator and the denominator.
Definition rational_cln_imp.h:348
CVC4::Rational::Rational
Rational(const Integer &n, const Integer &d)
Definition rational_cln_imp.h:170
CVC4::Rational::fromDouble
static Rational fromDouble(double d)
Return an exact rational for a double d.
CVC4::Rational::~Rational
~Rational()
Definition rational_cln_imp.h:177
CVC4::Rational::floor
Integer floor() const
Definition rational_cln_imp.h:254
CVC4::Rational::Rational
Rational(signed int n, signed int d)
Constructs a canonical Rational from a numerator and denominator.
Definition rational_cln_imp.h:148
CVC4::Rational::operator-=
Rational & operator-=(const Rational &y)
Definition rational_cln_imp.h:319
CVC4::Rational::Rational
Rational(unsigned int n)
Definition rational_cln_imp.h:136
CVC4::Rational::operator/=
Rational & operator/=(const Rational &y)
Definition rational_cln_imp.h:329
CVC4::Rational::ceiling
Integer ceiling() const
Definition rational_cln_imp.h:258
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_cln_imp.h:98
CVC4::Rational::cmp
int cmp(const Rational &x) const
Definition rational_cln_imp.h:212
CVC4::Rational::operator+=
Rational & operator+=(const Rational &y)
Definition rational_cln_imp.h:314
CVC4::Rational::getDouble
double getDouble() const
Get a double representation of this Rational, which is approximate: truncation may occur,...
Definition rational_cln_imp.h:204
CVC4::Rational::getNumerator
Integer getNumerator() const
Returns the value of numerator of the Rational.
Definition rational_cln_imp.h:184
CVC4::Rational::Rational
Rational(signed long int n)
Definition rational_cln_imp.h:137
CVC4::Rational::Rational
Rational()
Constructs a rational with the value 0/1.
Definition rational_cln_imp.h:89
CVC4::Rational::Rational
Rational(unsigned int n, unsigned int d)
Definition rational_cln_imp.h:151
CVC4::Rational::Rational
Rational(signed long int n, signed long int d)
Definition rational_cln_imp.h:154
CVC4::Rational::operator+
Rational operator+(const Rational &y) const
Definition rational_cln_imp.h:300
CVC4::Rational::isNegativeOne
bool isNegativeOne() const
Definition rational_cln_imp.h:238
CVC4::Rational::operator=
Rational & operator=(const Rational &x)
Definition rational_cln_imp.h:266
CVC4::Rational::operator-
Rational operator-(const Rational &y) const
Definition rational_cln_imp.h:303
CVC4::Rational::operator/
Rational operator/(const Rational &y) const
Definition rational_cln_imp.h:310
CVC4::Rational::Rational
Rational(const Integer &n)
Definition rational_cln_imp.h:175
CVC4::Rational::Rational
Rational(unsigned long int n)
Definition rational_cln_imp.h:138
CVC4::Rational::Rational
Rational(const Rational &q)
Creates a Rational from another Rational, q, by performing a deep copy.
Definition rational_cln_imp.h:130
CVC4::Rational::Rational
Rational(signed int n)
Constructs a canonical Rational from a numerator.
Definition rational_cln_imp.h:135
CVC4::Rational::isIntegral
bool isIntegral() const
Definition rational_cln_imp.h:250
CVC4::Rational::floor_frac
Rational floor_frac() const
Definition rational_cln_imp.h:262
CVC4::Rational::Rational
Rational(const std::string &s, unsigned base=10)
Definition rational_cln_imp.h:112
CVC4::Rational::operator==
bool operator==(const Rational &y) const
Definition rational_cln_imp.h:276
CVC4::Rational::Rational
Rational(unsigned long int n, unsigned long int d)
Definition rational_cln_imp.h:157
CVC4::Rational::isZero
bool isZero() const
Definition rational_cln_imp.h:230
CVC4::Rational::complexity
uint32_t complexity() const
Definition rational_cln_imp.h:352
CVC4::Rational::operator*=
Rational & operator*=(const Rational &y)
Definition rational_cln_imp.h:324
CVC4::Rational::operator<=
bool operator<=(const Rational &y) const
Definition rational_cln_imp.h:288
CVC4::Rational::operator>=
bool operator>=(const Rational &y) const
Definition rational_cln_imp.h:296
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::operator<<
std::ostream & operator<<(std::ostream &out, const TypeCheckingException &e)
std
STL namespace.
CVC4::RationalHashFunction
Definition rational_gmp_imp.h:339
CVC4::RationalHashFunction::operator()
size_t operator()(const CVC4::Rational &r) const
Definition rational_cln_imp.h:362