]> www.ginac.de Git - cln.git/blob - src/integer/division/cl_I_round1.cc
* include/cln/number.h (As): Fix it in namespace by suffixing `_As'
[cln.git] / src / integer / division / cl_I_round1.cc
1 // round1().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cln/integer.h"
8
9
10 // Implementation.
11
12 #include "cl_I.h"
13
14 namespace cln {
15
16 const cl_I round1 (const cl_I& x, const cl_I& y)
17 {
18 // Methode:
19 // (round x y) :==
20 // (DIVIDE (abs x) (abs y)) -> q,r
21 // Setze s:=abs(y)-r.
22 // Falls (r>s) oder (r=s und q ungerade),
23 //   (d.h. falls r>abs(y)/2 oder r=abs(y)/2 und q ungerade),
24 //   setze q:=q+1 und r:=-s (d.h. r:=r-abs(y)).
25 // {Nun ist abs(r) <= abs(y)/2, bei abs(r)=abs(y)/2 ist q gerade.}
26 // Falls x<0, setze r:=-r.
27 // Falls x,y verschiedene Vorzeichen haben, setze q:=-q.
28 // Liefere nur q.
29   var cl_I abs_y = abs(y);
30   var cl_I_div_t q_r = cl_divide(abs(x),abs_y);
31   var cl_I& q = q_r.quotient;
32   var cl_I& r = q_r.remainder;
33   var cl_I s = abs_y - r;
34   if ((r > s) || ((r == s) && oddp(q)))
35     { q = q + 1; }
36   if (minusp(x) != minusp(y))
37     { q = -q; }
38   return q;
39 }
40
41 }  // namespace cln