#include <stdio.h>
#include <math.h>
double pow2(double x, int n) {
double ans = 1;
if(n == 0) return 1;
if(n < 0) x = 1 / x;
while(n) {
if(n % 2) ans *= x;
x *= x;
n /= 2;
}
return ans;
}
double pow2_rec(double x, int n) {
double t;
if(n == 0) return 1;
t = pow2_rec(x, n / 2);
if( (n & 1) == 0) return t * t;
//if( n % 2 == 0) return t * t;
else {
if(n > 0) return x * t * t;
else
return (t * t) / x;
}
}
int main(int argc, char const *argv[])
{
printf("%f\n", pow2
(3.14, -5));
printf("%f\n", pow2_rec
(3.14, -5));
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+Cgpkb3VibGUgcG93Mihkb3VibGUgeCwgaW50IG4pIHsKCiAgICAgICBkb3VibGUgYW5zID0gMTsgCgogICAgICAgaWYobiA9PSAwKSByZXR1cm4gMTsKICAgICAgCiAgICAgICBpZihuIDwgMCkgeCA9IDEgLyB4OwoKICAgICAgIHdoaWxlKG4pIHsKCiAgICAgICAgICBpZihuICUgMikgYW5zICo9IHg7CgogICAgICAgICAgeCAqPSB4OwoKICAgICAgICAgIG4gLz0gMjsKICAgICAgIH0KCiAgICAgICByZXR1cm4gYW5zOwp9Cgpkb3VibGUgcG93Ml9yZWMoZG91YmxlIHgsIGludCBuKSB7CgogICAgICAgZG91YmxlIHQ7CgogICAgICAgaWYobiA9PSAwKSByZXR1cm4gMTsgICAgICAgCgogICAgICAgdCA9IHBvdzJfcmVjKHgsIG4gLyAyKTsKCiAgICAgICBpZiggKG4gJiAxKSA9PSAwKSByZXR1cm4gdCAqIHQ7CiAgICAgICAvL2lmKCBuICUgMiA9PSAwKSByZXR1cm4gdCAqIHQ7CgogICAgICAgICAgICAgZWxzZSB7CiAgCiAgICAgICAgICAgICAgICAgICBpZihuID4gMCkgcmV0dXJuIHggKiB0ICogdDsgCgogICAgICAgICAgICAgICAgICAgZWxzZSAKCiAgICAgICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gICh0ICogdCkgLyB4OwogICAgICAgICAgICAgIH0KfQkKCmludCBtYWluKGludCBhcmdjLCBjaGFyIGNvbnN0ICphcmd2W10pCnsKCXByaW50ZigiJWZcbiIsIHBvdygzLjE0LCAtNSkpOwoKCXByaW50ZigiJWZcbiIsIHBvdzIoMy4xNCwgLTUpKTsKCglwcmludGYoIiVmXG4iLCBwb3cyX3JlYygzLjE0LCAtNSkpOwoKCXJldHVybiAwOwp9