PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/cln-1.3.2/src/rational/division/cl_RA_ceil22.cc

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