Last active
May 12, 2017 17:14
-
-
Save lefirea/ffd70d948966eea3d3a8a768e7d154b7 to your computer and use it in GitHub Desktop.
This file contains 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> | |
#define PI 3.141592653589793 | |
int menu(); | |
void msin(); | |
void mcos(); | |
void mexp(); | |
double fact(int x); //階乗計算。念のため返り値は少数型 | |
double rad; //複数関数で共通の変数 | |
int i; //複数関数内のループで使用 | |
double M1,M2,M3; //誤差計算用変数。複数関数内で使用 | |
void main(void) { | |
int num; | |
setbuf(stdout, 0); | |
do { | |
switch (num = menu()) { | |
case 1: | |
msin(); | |
break; | |
case 2: | |
mcos(); | |
break; | |
case 3: | |
mexp(); | |
break; | |
case 4: | |
printf("fin.\n"); | |
break; | |
default: | |
printf("pls input 1-4\n"); | |
break; | |
} | |
} while (num != 4); | |
} | |
int menu() { | |
int n; | |
printf("input:"); | |
scanf("%d", &n); | |
return n; | |
} | |
void msin() { | |
double s5, s6, s7; | |
double sine; | |
for (i = 0;i <= 180;i += 5) { //0(deg)~180(deg)まで | |
rad = i*PI / 180.0; | |
sine = sin(rad); | |
s5 = rad - (pow(rad, 3) / fact(3)) + (pow(rad, 5) / fact(5)) - (pow(rad, 7) / fact(7)) + (pow(rad, 9) / fact(9)); | |
s6 = s5 - (pow(rad, 11) / fact(11)); | |
s7 = s6 + (pow(rad, 13) / fact(13)); | |
if (i != 0 && i != 180) { | |
M1 = fabs((sine - s5) / sine); | |
M2 = fabs((sine - s6) / sine); | |
M3 = fabs((sine - s7) / sine); | |
} | |
else { | |
M1 = 0; M2 = 0; M3 = 0; //sin(0)=0,sin(180)=0 | |
} | |
printf("deg=%d\tsin()=%f\n", i,sine); | |
printf("s5=%f\tM1=%f\t",s5,M1); //第5項までの結果と誤差 | |
printf("s6=%f\tM2=%f\t", s6, M2); //第6項までの結果と誤差 | |
printf("s7=%f\tM3=%f\n", s7, M3); //第7項までの結果と誤差 | |
} | |
} | |
void mcos() { | |
double c5, c6, c7; | |
double cosin; | |
for (i = 0;i <= 180;i += 5) { //0(deg)~180(deg)まで | |
rad = i*PI / 180.0; | |
cosin = cos(rad); | |
c5 = 1.0 - (pow(rad, 2) / fact(2)) + (pow(rad, 4) / fact(4)) - (pow(rad, 6) / fact(6)) + (pow(rad, 8) / fact(8)); | |
c6 = c5 - (pow(rad, 10) / fact(10)); | |
c7 = c6 + (pow(rad, 12) / fact(12)); | |
if (i != 90) { | |
M1 = fabs((cosin - c5) / cosin); | |
M2 = fabs((cosin - c6) / cosin); | |
M3 = fabs((cosin - c7) / cosin); | |
} | |
else { | |
M1 = 0; M2 = 0; M3 = 0; //cos(90)=0 | |
} | |
printf("deg=%d\tcos()=%f\n", i,cosin); | |
printf("c5=%f\tM1=%f\t", c5, M1); //第5項までの結果と誤差 | |
printf("c6=%f\tM2=%f\t", c6, M2); //第6項までの結果と誤差 | |
printf("c7=%f\tM3=%f\n", c7, M3); //第7項までの結果と誤差 | |
} | |
} | |
void mexp() { //eのx乗 | |
/* | |
誤差計算の時、kがマイナスの時ほど誤差が大きい。関数電卓でも計算してみたが、結果は同じだった。 | |
つまり仕様のようなものである可能性が高い。 | |
*/ | |
double e5, e6, e7; | |
double k; | |
double expo; | |
for (k = -4.0;k <= 4.0;k += 0.25) { | |
expo = exp(k); | |
e5 = 1 + k + (pow(k, 2) / fact(2)) + (pow(k, 3) / fact(3)) + (pow(k, 4) / fact(4)); | |
e6 = e5 + (pow(k, 5) / fact(5)); | |
e7 = e6 + (pow(k, 6) / fact(6)); | |
M1 = fabs((expo - e5) / expo); | |
M2 = fabs((expo - e6) / expo); | |
M3 = fabs((expo - e7) / expo); | |
printf("x=%.2f\texp()=%f\n", k,expo); | |
printf("e5=%f\tM1=%f\t", e5, M1); //第5項までの結果と誤差 | |
printf("e6=%f\tM2=%f\t", e6, M2); //第6項までの結果と誤差 | |
printf("e7=%f\tM3=%f\n", e7, M3); //第7項までの結果と誤差 | |
} | |
} | |
double fact(int x) { | |
double ans=1.0; | |
int j; | |
for (j = x;j >= 1;j--) { | |
ans *= double(j); //念のため少数型に変換して計算 | |
} | |
return ans; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment