e0472ca3f79047b4654d54d45ef9d951c2f78f75
[ipdf/code.git] / src / gmprat.h
1 /**
2  * @file gmpint.h
3  * @brief Wraps to GMP mpq_t type using inlines
4  */
5
6 #ifndef _GMPRAT_H
7 #define _GMPRAT_H
8
9 #include <gmp.h>
10 #include <string>
11
12 class Gmprat
13 {
14         public:
15                 Gmprat(double d=0) {mpq_init(m_op); mpq_set_d(m_op, d);}
16                 //Gmprat(int64_t p = 0, int64_t q = 1) {mpq_init(m_op); mpq_set_si(m_op, p, q);}
17                 //Gmprat(const std::string & str, int base=10) {mpq_init_set_str(m_op, str.c_str(), base);}
18                 Gmprat(const Gmprat & cpy) {mpq_init(m_op); mpq_set(m_op, cpy.m_op);}
19                 virtual ~Gmprat() {mpq_clear(m_op);}
20                 
21                 //operator int64_t() const {return mpq_get_si(m_op);}
22                 //operator uint64_t() const {return mpq_get_ui(m_op);}
23                 //operator double() const {return mpq_get_d(m_op);}
24                 //operator float() const {return (float)ToDouble();}
25                 double ToDouble() const {return mpq_get_d(m_op);}
26                 std::string Str(int base = 10) const
27                 {
28                         //TODO: Make less hacky, if we care.
29                         char * buff = mpq_get_str(NULL, 10, m_op);
30                         std::string result(buff);
31                         free(buff);
32                         return result;
33                 }
34                 
35                 Gmprat & operator=(const Gmprat & equ) {mpq_set(m_op, equ.m_op); return *this;}
36                 Gmprat & operator=(const double & equ) {mpq_set_d(m_op, equ); return *this;}
37                 Gmprat & operator+=(const Gmprat & add) {mpq_add(m_op, m_op, add.m_op); return *this;}
38                 Gmprat & operator-=(const Gmprat & sub) {mpq_sub(m_op, m_op, sub.m_op); return *this;}
39                 Gmprat & operator*=(const Gmprat & mul) {mpq_mul(m_op, m_op, mul.m_op); return *this;}
40                 Gmprat & operator/=(const Gmprat & div) {mpq_div(m_op, m_op, div.m_op); return *this;}
41                 
42                 Gmprat operator+(const Gmprat & add) const {Gmprat a(*this); a += add; return a;}
43                 Gmprat operator-(const Gmprat & sub) const {Gmprat a(*this); a -= sub; return a;}
44                 Gmprat operator*(const Gmprat & mul) const {Gmprat a(*this); a *= mul; return a;}
45                 Gmprat operator/(const Gmprat & div) const {Gmprat a(*this); a /= div; return a;}
46                 //Gmprat operator%(const Gmprat & div) const {Gmprat a(*this); mpq_mod(a.m_op, a.m_op, div.m_op); return a;}
47                 Gmprat operator-() const {return (Gmprat(0L)-*this);}
48                 
49                 
50                 bool operator==(const Gmprat & cmp) const {return mpq_cmp(m_op, cmp.m_op) == 0;}
51                 bool operator!=(const Gmprat & cmp) const {return mpq_cmp(m_op, cmp.m_op) != 0;}
52                 bool operator<(const Gmprat & cmp) const {return mpq_cmp(m_op, cmp.m_op) < 0;}
53                 bool operator>(const Gmprat & cmp) const {return mpq_cmp(m_op, cmp.m_op) > 0;}
54                 bool operator<=(const Gmprat & cmp) const {return mpq_cmp(m_op, cmp.m_op) <= 0;}
55                 bool operator>=(const Gmprat & cmp) const {return mpq_cmp(m_op, cmp.m_op) >= 0;}
56                 
57                 Gmprat Abs() const {Gmprat a(*this); mpq_abs(a.m_op, a.m_op); return a;}
58                 
59                 
60         private:
61                 friend std::ostream& operator<<(std::ostream& os, const Gmprat & fith);
62                 mpq_t m_op;
63 };      
64
65 std::ostream & operator<<(std::ostream & os, const Gmprat & fith)
66 {
67         os << fith.Str();
68         return os;
69 }
70
71
72
73
74 #endif //_GMPRAT_H
75

UCC git Repository :: git.ucc.asn.au