Recursive function to calculate x to the power n
| 19 | |
| 20 | // Recursive function to calculate x to the power n |
| 21 | long double power(double x, int n) |
| 22 | { |
| 23 | if (n == 0) return 1.0; |
| 24 | else if (n < 0) return 1.0 / power(x, -n); |
| 25 | else if (n % 2) return mult(x, power(x, n - 1)); // x is odd |
| 26 | |
| 27 | // If we make it this far, x > 0 and even |
| 28 | const auto y{ power(x, n / 2) }; |
| 29 | return mult(y, y); |
| 30 | } |