diff --git a/pset6/5-exp.c b/pset6/5-exp.c new file mode 100644 index 0000000..4342b9c --- /dev/null +++ b/pset6/5-exp.c @@ -0,0 +1,49 @@ +#include +#include + +// Function prototypes +double expe(double x); +double my_pow(double x, double l); +double get_sing(double x, int l); + +int main(void) { + // Initial variables + double x; + scanf("%lf", &x); + + printf("%.4lf\n", expe(x)); + return 0; +} + +double expe(double x) { + double ans = 0; + double addup; + for (int i = 0;; i++) { + addup = (get_sing(x, i)); + if (fabs(addup) < 1e-8) { + break; + } + ans += addup; + } + return ans; +} + +double my_pow(double x, double l) { + double ans = 1; + for (long long i = 0; i < l; i++) { + ans *= x; + } + return ans; +} + +double get_sing(double x, int l) { + if (l <= 0) { + return 1; + } else { + double ans = my_pow(x, l); + for (int i = l; i > 1; i--) { + ans /= i; + } + return ans; + } +}