0
上下文只是一個解決輸入二次方程的函數。下面是其失靈的代碼段:複數算術運算不正確
case NEGATIVE:
printf("\n\n beforehand sqrt(discriminant) is %f%+fi",creal(csqrt(eqn->discriminant)), cimag(csqrt(eqn->discriminant)));
eqn->complex_root = (-(eqn->b)+csqrt(eqn->discriminant))/(2*eqn->a);
printf("\n\n result after full formula is %f%+fi", creal(eqn->complex_root),cimag(eqn->complex_root));
break;
和輸出文本我x^2+5 = 0
作爲試探方程得到。中間的三條線正在調試文本,其中類型僅僅是指程序應該期待什麼樣的解決方案,爲二次(0手段當然2個複雜的解決方案):
Please enter the coefficients of the quadratic separated by spaces: 1 0 5
The coefficients entered are a=1, b=0 and c=5.
TYPE RETURNED: 0
beforehand sqrt(discriminant) is 0.000000+4.472136i
result after full formula is 0.000000+0.000000i
The equation defined by 1x^2 +0x +5=0 has two complex solutions, x = 0+0i and x = 0-0i.
我根本不知道爲什麼結果減少到0.發生了什麼事?
你能告訴你的函數'creal','cimag'和'csqrt'嗎? – haccks
他們在 –
FireGarden
沒關係,我發現這個問題..事實證明,你需要聲明變量具有複雜的價值,然後才能正確地分配它們。我有一個浮動,而不是浮動_Complex。對不起浪費人們的時間! – FireGarden