1 // floor2().
2 
3 // General includes.
4 #include "base/cl_sysdep.h"
5 
6 // Specification.
7 #include "cln/rational.h"
8 
9 
10 // Implementation.
11 
12 #include "rational/cl_RA.h"
13 #include "cln/integer.h"
14 
15 namespace cln {
16 
floor2(const cl_RA & x,const cl_RA & y)17 const cl_RA_div_t floor2 (const cl_RA& x, const cl_RA& y)
18 {
19 #if 1 // Ist das wirklich schneller??
20 // Methode:
21 // x = a/b, y = c/d ->
22 //   (floor (* a d) (* b c)) liefert q und r.
23 //   Liefere q und r/(b*d).
24 // x Integer -> dito mit b=1.
25 // y Integer -> dito mit d=1.
26 // x und y Integer -> bekannt.
27 	if (integerp(x)) {
28 		DeclareType(cl_I,x);
29 		if (integerp(y)) {
30 			DeclareType(cl_I,y);
31 			var cl_I_div_t q_r = floor2(x,y);
32 			var cl_I& q = q_r.quotient;
33 			var cl_I& r = q_r.remainder;
34 			return cl_RA_div_t(q,r);
35 		} else {
36 			DeclareType(cl_RT,y);
37 			var const cl_I& c = numerator(y);
38 			var const cl_I& d = denominator(y);
39 			var cl_I_div_t q_r = floor2(x*d,c);
40 			var cl_I& q = q_r.quotient;
41 			var cl_I& r = q_r.remainder;
42 			return cl_RA_div_t(q,I_posI_div_RA(r,d));
43 		}
44 	} else {
45 		DeclareType(cl_RT,x);
46 		var const cl_I& a = numerator(x);
47 		var const cl_I& b = denominator(x);
48 		if (integerp(y)) {
49 			DeclareType(cl_I,y);
50 			var cl_I_div_t q_r = floor2(a,b*y);
51 			var cl_I& q = q_r.quotient;
52 			var cl_I& r = q_r.remainder;
53 			return cl_RA_div_t(q,I_posI_div_RA(r,b));
54 		} else {
55 			DeclareType(cl_RT,y);
56 			var const cl_I& c = numerator(y);
57 			var const cl_I& d = denominator(y);
58 			var cl_I_div_t q_r = floor2(a*d,b*c);
59 			var cl_I& q = q_r.quotient;
60 			var cl_I& r = q_r.remainder;
61 			return cl_RA_div_t(q,I_posI_div_RA(r,b*d));
62 		}
63 	}
64 #else
65 // Methode:
66 // floor2(x/y) -> (q,r). Liefere q und x-y*q=y*r.
67 	var cl_RA_div_t q_r = floor2(x/y);
68 	var cl_I& q = q_r.quotient;
69 	var cl_RA& r = q_r.remainder;
70 	return cl_RA_div_t(q,y*r);
71 #endif
72 }
73 
74 }  // namespace cln
75