Skip to content

Commit f0faad0

Browse files
add problem 50 and test function
1 parent 6c4b3c2 commit f0faad0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

C/1-50/50-Pow(x, n).c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Implement pow(x, n).
3+
* Created by supercoderx on 2017/8/15.
4+
*/
5+
#include
6+
#include
7+
8+
double myPow(double x, int n) {
9+
if (x == 0)
10+
return 0;
11+
if (n == 0)
12+
return 1;
13+
int index = n / 2;
14+
if (n < 0) {
15+
x = 1 / x;
16+
index *= -1;
17+
}
18+
double y = x * x;
19+
20+
return n % 2 == 0 ? myPow(y, index) : x * myPow(y, index);
21+
}
22+
23+
void testMyPow() {
24+
printf("%lf", myPow(22.14659, -2));
25+
printf("%lf", myPow(2.00000, -2147483648));
26+
}

0 commit comments

Comments
 (0)