]> www.ginac.de Git - cln.git/blob - src/real/misc/cl_R_expt.cc
Use paths relative the `src' directory in the #include directives.
[cln.git] / src / real / misc / cl_R_expt.cc
1 // expt().
2
3 // General includes.
4 #include "base/cl_sysdep.h"
5
6 // Specification.
7 #include "cln/real.h"
8
9
10 // Implementation.
11
12 #include "real/cl_R.h"
13 #include "cln/rational.h"
14 #include "cln/integer.h"
15
16 namespace cln {
17
18 // Methode:
19 // Für y>0:
20 //   a:=x, b:=y.
21 //   Solange b gerade, setze a:=a*a, b:=b/2. [a^b bleibt invariant, = x^y.]
22 //   c:=a.
23 //   Solange b:=floor(b/2) >0 ist,
24 //     setze a:=a*a, und falls b ungerade, setze c:=a*c.
25 //   Ergebnis c.
26 // Für y=0: Ergebnis 1.
27 // Für y<0: (/ (expt x (- y))).
28
29 // Assume y>0.
30 inline const cl_R expt_pos (const cl_R& x, uintL y)
31 {
32         if (rationalp(x)) {
33                 DeclareType(cl_RA,x);
34                 return expt(x,y); // x rational -> schnellere Routine
35         } else {
36                 DeclareType(cl_F,x);
37                 var cl_F a = x;
38                 var uintL b = y;
39                 while (!(b % 2)) { a = square(a); b = b >> 1; }
40                 var cl_F c = a;
41                 until (b == 1)
42                   { b = b >> 1;
43                     a = square(a);
44                     if (b % 2) { c = a * c; }
45                   }
46                 return c;
47         }
48 }
49
50 const cl_R expt (const cl_R& x, sintL y)
51 {
52         if (y==0) { return 1; } // y=0 -> Ergebnis 1
53         var uintL abs_y = (y<0 ? (uintL)(-y) : y); // Betrag von y nehmen
54         var cl_R z = expt_pos(x,abs_y); // (expt x (abs y))
55         return (y<0 ? recip(z) : z); // evtl. noch Kehrwert nehmen
56 }
57
58 }  // namespace cln