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