Created
October 20, 2011 00:43
-
-
Save pokutuna/1300117 to your computer and use it in GitHub Desktop.
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> | |
#include <math.h> | |
double calc_clause(double x, int k) | |
{ | |
return k == 0 ? 1 : pow(x, k) / (double)fact(k); | |
} | |
int fact(int i) | |
{ | |
return i == 1 ? 1 : i * fact(i - 1); | |
} | |
double myexp(double x) | |
{ | |
int k = 0; | |
double threshold = pow(10, -6); | |
double result = 0.0; | |
while (1) { | |
double num = calc_clause(x, k); | |
if (num < threshold) break; | |
result += num; | |
k++; | |
} | |
return result; | |
} | |
int main(void) | |
{ | |
int i; | |
printf("x\tmyexp(x)\n"); | |
for (i = 1; i < 11; i++) { | |
printf("%4.1f\t%13.5f\n", (double)i, myexp((double)i)); | |
} | |
return 0; | |
} |
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
x myexp(x) | |
1.0 2.71828 | |
2.0 7.38912 | |
3.0 20.11841 | |
4.0 57.50654 | |
5.0 244.87811 | |
6.0 2110.09358 | |
7.0 20597.88081 | |
8.0 164511.80663 | |
9.0 1053597.01267 | |
10.0 5589274.20360 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
階乗がオーヴァーフロゥしている例ナリ