Hallo, ich habe ein problem bei der Potenzbildung m.h.e Rekursion. Die postivien Potenzen funktionieren schon, jedoch wenn ich z.B 3.0^(-6) rechnen will klappt es nicht. Kann mir jemnad helfen? Bisher habe ich folgenden Code:
1 | #include <iostream> |
2 | #include <cmath> |
3 | |
4 | using namespace std; |
5 | |
6 | double foo (double,int ); |
7 | |
8 | int main() { |
9 | double d = 3.0; |
10 | int e = -2; |
11 | int rwert = foo(d,e); |
12 | cout << rwert; |
13 | cin.get(); |
14 | return 0; |
15 | }
|
16 | |
17 | double foo (double a, int b) { |
18 | if (b == 0) return 1.0; |
19 | double c = 1.0; |
20 | |
21 | if (b >0 ) { |
22 | double c = foo (a,b/2); |
23 | c =c*c; |
24 | if (b % 2 != 0) c= a*c; |
25 | return c; |
26 | }
|
27 | if (b <0 ) { |
28 | double c = foo (a,b/2); |
29 | if (b % 2 != 0) c= 1/(a*c); |
30 | return c; |
31 | }
|
32 | }
|