简体   繁体   English

复数的算术无法正常工作

[英]Arithmetic in complex numbers isn't working properly

The context is simply a function that solves input quadratic equations. 上下文仅仅是解决输入二次方程的函数。 Here's the section of the code which malfunctions: 这是发生故障的代码部分:

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;

And the output text I get with x^2+5 = 0 as the trial equation. 我得到的输出文本是x^2+5 = 0作为试验方程式。 The middle three lines are debugging text, where type simply refers to what kind of solutions the program should expect for the quadratic (0 means 2 complex solutions of course): 中间的三行是调试文本,其中type仅仅表示程序应针对二次方提供什么样的解决方案(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.

I simply have no idea why the results reduce to 0. What's going on? 我根本不知道为什么结果减少到0。这是怎么回事?

Forgot to assign the variable holding these values as complex. 忘记分配包含这些值的变量为复杂变量。 ie used float instead of float _Complex. 即使用float而不是float _Complex。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM