Use Gmprat for Path bounds with TRANSFORM_BEZIERS_TO_PATH
[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                 double ToDouble() const {return mpq_get_d(m_op);}
25                 std::string Str(int base = 10) const
26                 {
27                         //TODO: Make less hacky, if we care.
28                         char * buff = mpq_get_str(NULL, 10, m_op);
29                         std::string result(buff);
30                         free(buff);
31                         return result;
32                 }
33                 
34                 Gmprat & operator=(const Gmprat & equ) {mpq_set(m_op, equ.m_op); return *this;}
35                 Gmprat & operator=(const double & equ) {mpq_set_d(m_op, equ); return *this;}
36                 Gmprat & operator+=(const Gmprat & add) {mpq_add(m_op, m_op, add.m_op); return *this;}
37                 Gmprat & operator-=(const Gmprat & sub) {mpq_sub(m_op, m_op, sub.m_op); return *this;}
38                 Gmprat & operator*=(const Gmprat & mul) {mpq_mul(m_op, m_op, mul.m_op); return *this;}
39                 Gmprat & operator/=(const Gmprat & div) {mpq_div(m_op, m_op, div.m_op); return *this;}
40                 
41                 Gmprat operator+(const Gmprat & add) const {Gmprat a(*this); a += add; return a;}
42                 Gmprat operator-(const Gmprat & sub) const {Gmprat a(*this); a -= sub; return a;}
43                 Gmprat operator*(const Gmprat & mul) const {Gmprat a(*this); a *= mul; return a;}
44                 Gmprat operator/(const Gmprat & div) const {Gmprat a(*this); a /= div; return a;}
45                 //Gmprat operator%(const Gmprat & div) const {Gmprat a(*this); mpq_mod(a.m_op, a.m_op, div.m_op); return a;}
46                 Gmprat operator-() const {return (Gmprat(0L)-*this);}
47                 
48                 
49                 bool operator==(const Gmprat & cmp) const {return mpq_cmp(m_op, cmp.m_op) == 0;}
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                 
56                 Gmprat Abs() const {Gmprat a(*this); mpq_abs(a.m_op, a.m_op); return a;}
57                 
58                 
59         private:
60                 mpq_t m_op;
61 };      
62
63
64
65
66 #endif //_GMPRAT_H
67

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