aboutsummaryrefslogtreecommitdiff
path: root/pow_xn.c
blob: 9baec1bf194b8997b950efcd2b948705412e11d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 50. Pow(x, n)50. Pow(x, n)
#include <limits.h>
#include <stdio.h>

/*
 * implement 'pow(x, n)', which calculates 'x' raised to the power 'n' (i.e.
 * x^n)
 */

double myPow(double x, int n) {
  if (x == 1.0)
    return x;
  if (x == -1.0)
    return (n % 2 ? -1.0 : 1.0);
  if (n == INT_MIN)
    return 0;
  double ans = 1;
  if (n < 0) {
    x = 1 / x;
    n = -n;
  }
  for (int i = 0; i < n && ans * x != ans; i++)
    ans *= x;
  return ans;
}

int main() {
  printf("%f\n", myPow(2, 10));  // expect: 1024.00000
  printf("%f\n", myPow(2.1, 3)); // expect: 9.26100
  printf("%f\n", myPow(2, -2));  // expect: 0.25000
}