/usr/include/cvc3/rational.h is in libcvc3-dev 2.4.1-5ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | /*****************************************************************************/
/*!
* \file rational.h
*
* Author: Sergey Berezin
*
* Created: Dec 12 22:00:18 GMT 2002
*
* <hr>
*
* License to use, copy, modify, sell and/or distribute this software
* and its documentation for any purpose is hereby granted without
* royalty, subject to the terms and conditions defined in the \ref
* LICENSE file provided with this distribution.
*
* <hr>
*
*/
/*****************************************************************************/
// Class: Rational
// Author: Sergey Berezin, 12/12/2002 (adapted from Bignum)
//
// Description: This is an abstration of a rational with arbitrary
// precision. It provides a constructor from a pair of ints and
// strings, overloaded operator{+,-,*,/}, assignment, etc. The
// current implementation uses GMP mpq_class.
///////////////////////////////////////////////////////////////////////////////
#ifndef _cvc3__rational_h_
#define _cvc3__rational_h_
// Do not include <gmpxx.h> here; it contains some depricated C++
// headers. We only include it in the .cpp file.
#include <vector>
#include "debug.h"
// To be defined only in bignum.cpp
namespace CVC3 {
class Unsigned;
class CVC_DLL Rational {
friend class Unsigned;
private:
class Impl;
Impl *d_n;
// Debugging
#ifdef _DEBUG_RATIONAL_
// Encapsulate static values in a function to guarantee
// initialization when we need it
int& getCreated() {
static int num_created = 0;
return(num_created);
}
int& getDeleted() {
static int num_deleted = 0;
return(num_deleted);
}
void printStats();
#endif
// Private constructor (for internal consumption only)
Rational(const Impl& t);
public:
// Constructors
Rational();
// Copy constructor
Rational(const Rational &n);
Rational(const Unsigned& n);
Rational(int n, int d = 1);
Rational(const char* n, int base = 10);
Rational(const std::string& n, int base = 10);
Rational(const char* n, const char* d, int base = 10);
Rational(const std::string& n, const std::string& d, int base = 10);
// Destructor
~Rational();
// Assignment
Rational& operator=(const Rational& n);
std::string toString(int base = 10) const;
// Compute hash value (for DAG expression representation)
size_t hash() const;
friend CVC_DLL bool operator==(const Rational &n1, const Rational &n2);
friend CVC_DLL bool operator<(const Rational &n1, const Rational &n2);
friend CVC_DLL bool operator<=(const Rational &n1, const Rational &n2);
friend CVC_DLL bool operator>(const Rational &n1, const Rational &n2);
friend CVC_DLL bool operator>=(const Rational &n1, const Rational &n2);
friend CVC_DLL bool operator!=(const Rational &n1, const Rational &n2);
friend CVC_DLL Rational operator+(const Rational &n1, const Rational &n2);
friend CVC_DLL Rational operator-(const Rational &n1, const Rational &n2);
friend CVC_DLL Rational operator*(const Rational &n1, const Rational &n2);
friend CVC_DLL Rational operator/(const Rational &n1, const Rational &n2);
// 'mod' operator, defined only for integer values of n1 and n2
friend CVC_DLL Rational operator%(const Rational &n1, const Rational &n2);
// Unary minus
Rational operator-() const;
Rational &operator+=(const Rational &n2);
Rational &operator-=(const Rational &n2);
Rational &operator*=(const Rational &n2);
Rational &operator/=(const Rational &n2);
//! Prefix increment
const Rational& operator++() { *this = (*this)+1; return *this; }
//! Postfix increment
Rational operator++(int) { Rational x(*this); *this = x+1; return x; }
//! Prefix decrement
const Rational& operator--() { *this = (*this)-1; return *this; }
//! Postfix decrement
Rational operator--(int) { Rational x(*this); *this = x-1; return x; }
// Result is integer
Rational getNumerator() const;
Rational getDenominator() const;
// Equivalent to (getDenominator() == 1), but possibly more efficient
bool isInteger() const;
// Convert to int; defined only on integer values
int getInt() const;
// Equivalent to (*this >= 0 && isInteger())
bool isUnsigned() const { return (isInteger() && (*this) >= 0); }
// Convert to unsigned int; defined only on non-negative integer values
unsigned int getUnsigned() const;
Unsigned getUnsignedMP() const;
friend std::ostream &operator<<(std::ostream &os, const Rational &n);
friend std::ostream &operator<<(std::ostream &os, const Impl &n);
/* Computes gcd and lcm on *integer* values. Result is always a
positive integer. */
friend CVC_DLL Rational gcd(const Rational &x, const Rational &y);
friend CVC_DLL Rational gcd(const std::vector<Rational> &v);
friend CVC_DLL Rational lcm(const Rational &x, const Rational &y);
friend CVC_DLL Rational lcm(const std::vector<Rational> &v);
friend CVC_DLL Rational abs(const Rational &x);
//! Compute the floor of x (result is an integer)
friend CVC_DLL Rational floor(const Rational &x);
//! Compute the ceiling of x (result is an integer)
friend CVC_DLL Rational ceil(const Rational &x);
//! Compute non-negative remainder for *integer* x,y.
friend CVC_DLL Rational mod(const Rational &x, const Rational &y);
//! nth root: return 0 if no exact answer (base should be nonzero)
friend CVC_DLL Rational intRoot(const Rational& base, unsigned long int n);
// For debugging, to be able to print in gdb
void print() const;
}; // Rational class
//! Raise 'base' into the power of 'pow' (pow must be an integer)
inline Rational pow(Rational pow, const Rational& base) {
DebugAssert(pow.isInteger(), "pow("+pow.toString()
+", "+base.toString()+")");
FatalAssert(base != 0 || pow >= 0, "Attempt to divide by zero");
bool neg(pow < 0);
if(neg) pow = -pow;
Rational res(1);
for(; pow > 0; --pow) res *= base;
if(neg) res = 1/res;
return res;
}
//! take nth root of base, return result if it is exact, 0 otherwise
// base should not be 0
inline Rational ratRoot(const Rational& base, unsigned long int n)
{
DebugAssert(base != 0, "Expected nonzero base");
Rational num = base.getNumerator();
num = intRoot(num, n);
if (num != 0) {
Rational den = base.getDenominator();
den = intRoot(den, n);
if (den != 0) {
return num / den;
}
}
return 0;
}
// Methods creating new Rational values, similar to the
// constructors, but can be nested
inline Rational newRational(int n, int d = 1) { return Rational(n, d); }
inline Rational newRational(const char* n, int base = 10)
{ return Rational(n, base); }
inline Rational newRational(const std::string& n, int base = 10)
{ return Rational(n, base); }
inline Rational newRational(const char* n, const char* d, int base = 10)
{ return Rational(n, d, base); }
inline Rational newRational(const std::string& n, const std::string& d,
int base = 10)
{ return Rational(n, d, base); }
// Debugging print
void printRational(const Rational &x);
class CVC_DLL Unsigned {
private:
friend class Rational::Impl;
class Impl;
Impl *d_n;
// Private constructor (for internal consumption only)
Unsigned(const Impl& t);
public:
// Constructors
Unsigned();
// Copy constructor
Unsigned(const Unsigned &n);
Unsigned(int n);
Unsigned(unsigned n);
Unsigned(const char* n, int base = 10);
Unsigned(const std::string& n, int base = 10);
// Destructor
~Unsigned();
// Assignment
Unsigned& operator=(const Unsigned& n);
std::string toString(int base = 10) const;
// Compute hash value (for DAG expression representation)
size_t hash() const;
friend CVC_DLL bool operator==(const Unsigned &n1, const Unsigned &n2);
friend CVC_DLL bool operator<(const Unsigned &n1, const Unsigned &n2);
friend CVC_DLL bool operator<=(const Unsigned &n1, const Unsigned &n2);
friend CVC_DLL bool operator>(const Unsigned &n1, const Unsigned &n2);
friend CVC_DLL bool operator>=(const Unsigned &n1, const Unsigned &n2);
friend CVC_DLL bool operator!=(const Unsigned &n1, const Unsigned &n2);
friend CVC_DLL Unsigned operator+(const Unsigned &n1, const Unsigned &n2);
friend CVC_DLL Unsigned operator-(const Unsigned &n1, const Unsigned &n2);
friend CVC_DLL Unsigned operator*(const Unsigned &n1, const Unsigned &n2);
friend CVC_DLL Unsigned operator/(const Unsigned &n1, const Unsigned &n2);
// 'mod' operator, defined only for integer values of n1 and n2
friend CVC_DLL Unsigned operator%(const Unsigned &n1, const Unsigned &n2);
friend CVC_DLL Unsigned operator<<(const Unsigned& n1, unsigned n2);
friend CVC_DLL Unsigned operator&(const Unsigned& n1, const Unsigned& n2);
Unsigned &operator+=(const Unsigned &n2);
Unsigned &operator-=(const Unsigned &n2);
Unsigned &operator*=(const Unsigned &n2);
Unsigned &operator/=(const Unsigned &n2);
//! Prefix increment
const Unsigned& operator++() { *this = (*this)+1; return *this; }
//! Postfix increment
Unsigned operator++(int) { Unsigned x(*this); *this = x+1; return x; }
//! Prefix decrement
const Unsigned& operator--() { *this = (*this)-1; return *this; }
//! Postfix decrement
Unsigned operator--(int) { Unsigned x(*this); *this = x-1; return x; }
// Convert to unsigned int if possible
unsigned long getUnsigned() const;
friend std::ostream &operator<<(std::ostream &os, const Unsigned &n);
/* Computes gcd and lcm on *integer* values. Result is always a
positive integer. */
friend CVC_DLL Unsigned gcd(const Unsigned &x, const Unsigned &y);
friend CVC_DLL Unsigned gcd(const std::vector<Unsigned> &v);
friend CVC_DLL Unsigned lcm(const Unsigned &x, const Unsigned &y);
friend CVC_DLL Unsigned lcm(const std::vector<Unsigned> &v);
//! Compute non-negative remainder for *integer* x,y.
friend CVC_DLL Unsigned mod(const Unsigned &x, const Unsigned &y);
//! nth root: return 0 if no exact answer (base should be nonzero)
friend CVC_DLL Unsigned intRoot(const Unsigned& base, unsigned long int n);
// For debugging, to be able to print in gdb
void print() const;
}; // Unsigned class
//! Raise 'base' into the power of 'pow' (pow must be an integer)
inline Unsigned pow(Unsigned pow, const Unsigned& base) {
Unsigned res(1);
for(; pow > (unsigned)0; --pow) res *= base;
return res;
}
// Methods creating new Unsigned values, similar to the
// constructors, but can be nested
inline Unsigned newUnsigned(int n) { return Unsigned(n); }
inline Unsigned newUnsigned(const char* n, int base = 10)
{ return Unsigned(n, base); }
inline Unsigned newUnsigned(const std::string& n, int base = 10)
{ return Unsigned(n, base); }
// Debugging print
void printUnsigned(const Unsigned &x);
} // end of namespace CVC3
#endif
|