Classify Beziers, use DeCasteljau for CPU renderer
[ipdf/code.git] / src / rational.h
1 #ifndef _RATIONAL_H
2 #define _RATIONAL_H
3
4 /**
5  * A really shoddy implementation of Rational numbers
6  */
7
8 #include "common.h"
9 #include <cmath>
10 #include <cassert>
11 #include "arbint.h"
12 #include "gmpint.h"
13 #include <climits>
14 #include <values.h>
15
16 namespace IPDF
17 {
18         
19 template <class T> T Tabs(const T & a)
20 {
21         return abs(a);
22 }
23 template <> Arbint Tabs(const Arbint & a);
24 template <> Gmpint Tabs(const Gmpint & a);
25
26 /* Recursive version  of GCD
27 template <class T>
28 T gcd(const T & a, const T & b)
29 {
30         Debug("Called on %li/%li", int64_t(a), int64_t(b));
31         if (a == T(1) || a == T(0)) return T(1);
32         if (b == T(0)) return a;
33         if (b == a) 
34         {
35                 Debug("Equal!");
36                 return a;
37         }
38         Debug("Not equal!");
39         
40         if (a > b) return gcd(a-b,b);
41         return gcd(a, b-a);
42 }
43 */
44
45 /** Greatest Common Divisor of p and q **/
46 template <class T>
47 T gcd(const T & p, const T & q)
48 {
49
50
51         T g(1);
52         T big(p);
53         T small(q);
54         if (p < q)
55         {
56                 big = q;
57                 small = p;
58         }
59         if (small == T(0))
60                 return g;
61         while ((g = big % small) > T(0))
62         {
63                 //Debug("big = %li, small = %li", int64_t(big), int64_t(small));
64                 big = small;
65                 small = g;
66                 //Debug("Loop %u", ++count);
67         }
68         return small;
69 }       
70
71
72 template <class T = int64_t>
73 struct Rational
74 {
75         /** Construct from a double.**/
76         Rational(double d=0) : P(d*1e6), Q(1e6) // Possibly the worst thing ever...
77         {
78                 Simplify();
79         }
80         
81
82
83         Rational(const T & _P, const T & _Q) : P(_P), Q(_Q)
84         {
85                 Simplify();
86         }
87
88         Rational(const Rational & cpy) : P(cpy.P), Q(cpy.Q)
89         {
90                 Simplify();
91         }
92
93         void Simplify()
94         {
95                 if (Q < T(0)) 
96                 {
97                         P = -P;
98                         Q = -Q;
99                 }
100                 if (P == T(0))
101                 {
102                         Q = T(1);
103                         return;
104                 }
105                 if (P == Q)
106                 {
107                         P = Q = T(1);
108                         return;
109                 }
110                 T g = gcd(Tabs(P), Tabs(Q));
111                 //Debug("Got gcd!");
112                 P /= g;
113                 Q /= g;
114         }
115
116         bool operator==(const Rational & r)  const
117         {
118                 if (P == r.P && Q == r.Q) return true;
119                 return ToDouble() == r.ToDouble();
120         }
121
122
123         bool operator<(const Rational & r) const {return (P*r.Q < r.P * Q);}
124         bool operator>(const Rational & r) const {return !(*this < r);}
125         bool operator<=(const Rational & r) const {return *this == r || *this < r;}
126         bool operator>=(const Rational & r) const {return *this == r || *this > r;}
127         bool operator!=(const Rational & r) const {return !(*this == r);}
128
129         Rational operator+(const Rational & r) const 
130         {
131                 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
132                 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"+"))
133                 //{
134                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
135                 //}
136                 return result;
137         }
138         Rational operator-(const Rational & r) const 
139         {
140                 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q - r.P*Q, Q*r.Q);
141                 //result.CheckAccuracy(ToDouble() - r.ToDouble(),"-");
142                 return result;
143         }
144         Rational operator*(const Rational & r) const 
145         {
146                 Rational result(P * r.P, Q * r.Q);
147                 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
148                 //{
149                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
150                 //}
151                 return result;
152         }
153         Rational operator/(const Rational & r) const 
154         {
155                 Rational result(P * r.Q, Q*r.P);
156                 //if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
157                 //{
158                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
159                 //}
160                 return result;
161         }       
162
163         /** To cheat, use these **/
164         //Rational operator+(const Rational & r) const {return Rational(ToDouble()+r.ToDouble());}
165         //Rational operator-(const Rational & r) const {return Rational(ToDouble()-r.ToDouble());}
166         //Rational operator*(const Rational & r) const {return Rational(ToDouble()*r.ToDouble());}
167         //Rational operator/(const Rational & r) const {return Rational(ToDouble()/r.ToDouble());}
168
169         Rational operator-() const {Rational r(*this); r.P = -r.P; return r;}
170         Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; Simplify(); return *this;}
171         Rational & operator+=(const Rational & r) {this->operator=(*this+r); return *this;}
172         Rational & operator-=(const Rational & r) {this->operator=(*this-r); return *this;}
173         Rational & operator*=(const Rational & r) {this->operator=(*this*r); return *this;}
174         Rational & operator/=(const Rational & r) {this->operator=(*this/r); return *this;}
175
176         double ToDouble() const 
177         {
178                 T num = P, denom = Q;
179                 while (Tabs(num) > T(1e10))
180                 {
181                         num /= T(16);
182                         denom /= T(16);
183                 }
184                 return ((double)(num))/((double)(denom));
185         }
186         bool CheckAccuracy(double d, const char * msg, double threshold = 1e-3) const
187         {
188                 double result = fabs(ToDouble() - d);
189                 if (d != 0e0) result /= d;
190                 if (result > threshold)
191                 {
192                         Warn("(%s) : Rational %s (%f) is not close enough at representing %f (%f vs %f)", msg, Str().c_str(), ToDouble(), d, result, threshold);
193                         Backtrace();
194                         return false;
195                 }
196                 return true;
197         }
198         std::string Str() const
199         {
200                 std::stringstream s;
201                 s << int64_t(P) << "/" << int64_t(Q);
202                 return s.str();
203         }
204         
205         T P;
206         T Q;
207 };
208
209
210
211 }
212
213 #endif //_RATIONAL_H
214

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