C++で複素計算をするプログラムです。
コンパイルはエラーなくできるのですが、なぜか.exeを実行できません。教授に訊いたところ、初期値設定に問題があるとのことですが、そこを弄っても直りませんでした。
#ソースコード
class complex { public: double re, im; complex(double r = 0.0, double i = 0.0) { re = r; im = i; } }; complex operator+(const complex& a, const complex& b) { return complex(a.re + b.re, a.im + b.im); } complex operator-(const complex& a, const complex& b) { return complex(a.re - b.re, a.im - b.im); } complex operator*(const complex& a, const complex& b) { return complex(a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re); } complex operator/(const complex& a, const complex& b) { double d = b.re * b.re + b.im * b.im; return complex((a.re * b.re + a.im * b.im) / d, (a.im * b.re - a.re * b.im) / d); } double fabs(const complex& a) { return sqrt(a.re * a.re + a.im * a.im); } complex f(const complex& x) { complex s = (1.0, 0.0); double a[3] = {1.0, 1.0, 1.0}; int i; for(i = 0; i < 3; i++) s = s * x + a[i]; return s; } complex df(const complex& x) { complex s = (3.0, 0.0); double a[2] = {2.0, 1.0}; int i; for(i = 0; i < 2; i++) s = s * x + a[i]; return s; } int main(void) { complex xn, xn1; xn = (0.5, 0.5); while(1){ xn1 = xn - f(xn) / df(xn); if(fabs(xn1 - xn) < 1.0e-10)break; xn=xn1; } printf("ans=%lf\n", xn1); return 0; }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/22 09:47 編集
2018/11/22 09:37
2018/11/22 09:46 編集
2018/11/22 10:07
2018/11/22 11:10