Created
August 24, 2020 13:03
-
-
Save harieamjari/0bd33fddf3c774e7f902cfb8e5e339d1 to your computer and use it in GitHub Desktop.
Compute the product of base and exponent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define INF 0x7F800000 | |
int inf = INF; | |
long double factorial(const int temp) { | |
if (temp == 0) | |
return 1.0; | |
long double fact = 1; | |
for (int i = 1; i <= temp; i++) { | |
fact *= i; | |
} | |
return fact; | |
} | |
long double raise_int(const long double base, const int exp) { | |
long double ans = 0.0; | |
if (exp == 0) | |
return 1.0; | |
else if (exp < 0) | |
return 1.0 / raise_int(base, -exp); | |
ans = base; | |
for (int i = 1; i < exp; i++) { | |
ans *= base; | |
} | |
return ans; | |
} | |
long double e_raise(const long double temp) { | |
long double ans = 0.0; | |
long double ctr = 0.0; | |
for (int i = 0; i < 1000; i++) { | |
ctr = raise_int(temp, i) / factorial(i); | |
if (ctr != ctr || ctr == (long double) *(float *)&inf) break; | |
ans += ctr; | |
} | |
return ans; | |
} | |
long double mylog(const long double temp) { | |
long double ret = 0.0; | |
long double ctr = 0.0; | |
for (int i = 1; i < 1000; i++) { | |
ctr = raise_int(temp, i) * raise_int(-1.0, i - 1.0) / i; | |
if (ctr != ctr) break; | |
ret += ctr; | |
} | |
return ret; | |
} | |
int main() { | |
long double base = 0; | |
long double exp = 0; | |
scanf("%Lf %Lf", &base, &exp); | |
long double ans = e_raise(exp * (-mylog((1.0 / base) - 1.0))); | |
printf("%Lf\n\n", ans); | |
printf("%Le\n", ans); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment