I forgor💀
This commit is contained in:
parent
235484859e
commit
d4ef5f38b9
49
pset6/5-exp.c
Normal file
49
pset6/5-exp.c
Normal file
|
@ -0,0 +1,49 @@
|
|||
#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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue