BUPT-homework/pset6/5-exp.c

50 lines
786 B
C
Raw Normal View History

2021-11-02 10:50:36 +08:00
#include <math.h>
#include <stdio.h>
// 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;
}
}