上下文只是一个解决输入二次方程的函数。以下是代码发生故障的部分:
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.发生了什么?
答案 0 :(得分:0)
忘记将保存这些值的变量赋值为复数。即使用float而不是float _Complex。