We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6c4b3c2 commit f0faad0Copy full SHA for f0faad0
C/1-50/50-Pow(x, n).c
@@ -0,0 +1,26 @@
1
+/*
2
+ * Implement pow(x, n).
3
+ * Created by supercoderx on 2017/8/15.
4
+ */
5
+#include
6
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