问题描述
#include< stdio.h>
#include< stdlib.h>
#include< complex.h>
int main(void)
{
_Complex z1,z2,z3;
z1 = .4 + .7i;
z2 = pow(z1,2);
z3 = z1 * z1;
printf("%f,%f \ n",z1 );
printf("%f,%f \ n",z2);
printf("%f,%f \ n",z3) );
系统(PAUSE);
返回0;
}
在这个程序中, z2不等于z3。 z3看起来正确。在我的
实现(devcpp在其首次航行中),z2成为真正的部分
平方,复杂的部分为零并且可能未定义。
Q1)对于C99的新类型,如何使用旧的数学函数?
Q2)是复数还是_Complex更好的类型定义? LS
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
int main(void)
{
_Complex z1, z2, z3;
z1 = .4 + .7i;
z2 = pow(z1, 2);
z3 = z1 * z1;
printf("%f, %f \n", z1);
printf("%f, %f \n", z2);
printf("%f, %f \n", z3);
system("PAUSE");
return 0;
}
In this program, z2 is not equal to z3 . z3 looks correct. On my
implementation (devcpp on its maiden voyage), z2 becomes the real part
squared, and the complex part zeros out and is probably undefined.
Q1)With C99''s new types, how does one use the old math functions?
Q2)Is complex or _Complex a better type defn? LS
推荐答案
您应该使用专为复杂操作数设计的数学函数。
例如,使用cpow代替pow。
You should use math functions designed to work with complex operands.
For example, instead of pow, use cpow.
前者。
尝试类似:
#include < stdio.h>
#include< stdlib.h>
#include< complex.h>
int main(void)
{
双复数z1,z2,z3;
z1 = .4 + .7I;
z2 = cpow(z1,2.0);
z3 = z1 * z1;
printf("%lf%lf \ n",creal( z1),cimag(z1));
printf("%lf%lf \ n",creal(z2),cimag(z2));
printf ("%lf%lf \ n",creal(z3),cimag(z3));
返回EXIT_SUCCESS;
}
-
希望这会有所帮助,
史蒂文
The former.
Try something like:
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
int main (void)
{
double complex z1, z2, z3;
z1 = .4 + .7I;
z2 = cpow(z1, 2.0);
z3 = z1 * z1;
printf("%lf %lf\n", creal(z1), cimag(z1));
printf("%lf %lf\n", creal(z2), cimag(z2));
printf("%lf %lf\n", creal(z3), cimag(z3));
return EXIT_SUCCESS;
}
--
Hope this helps,
Steven
您应该使用专为复杂操作数设计的数学函数。
例如,使用cpow代替pow。
You should use math functions designed to work with complex operands.
For example, instead of pow, use cpow.
感谢您的回复。我完全不知道cpow的存在。
Thanks for your reply. I was completely ignorant of cpow''s existence.
前者。
The former.
我也这么认为。
I think so too.
#include< stdio.h>
#include< stdlib.h>
#include< complex.h>
#include< stdbool.h>
#define N 41
#ifdef __bool_true_false_are_defined
#define N 42
#endif
int main(无效)
{
双复数z1,z2,z3;
bool flag;
z1 = .4 + .7I;
z2 = cpow(z1,2.0);
z3 = z1 * z1;
flag = false;
flag = true;
if(flag)
{
printf("%lf%lf \ n",creal(z1),cimag(z1));
printf("%lf%lf \ n",creal(z2),cimag(z2));
printf("%lf%lf \ n" ;,creal(z3),cimag(z3));
printf("%d \ n",N);
}
system(PAUSE);
返回EXIT_SUCCESS;
}
这个程序没有太大的深刻性;我不知道有什么方法可以用来语法的新方面,而不是简单地从
基本的东西开始。这只是一个窍门,只是为了获得标题
包括在内。 LS
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
#include <stdbool.h>
#define N 41
#ifdef __bool_true_false_are_defined
#define N 42
#endif
int main (void)
{
double complex z1, z2, z3;
bool flag;
z1 = .4 + .7I;
z2 = cpow(z1, 2.0);
z3 = z1 * z1;
flag = false;
flag = true;
if (flag)
{
printf("%lf %lf\n", creal(z1), cimag(z1));
printf("%lf %lf\n", creal(z2), cimag(z2));
printf("%lf %lf\n", creal(z3), cimag(z3));
printf("%d\n", N);
}
system("PAUSE");
return EXIT_SUCCESS;
}
This program is of no great profoundness; I don''t know of any way to get
used to new aspects of syntax without simply plodding ahead beginning with
elementary-looking stuff. It''s a bit of a trick just to get the headers
included. LS
你不能在< stdlib.h>中使用任何东西。
You don''t use anything in <stdlib.h>.
_Complex不是一种类型;复杂类型是float _Complex,
" double _Complex"和long double _Complex。如果您的编译器
本身允许_Complex,则可能是由于其作者的b / b
的误解;标准中有一个不正确的例子。 C99 6.7.8
例1是:
int i = 3.5;
复数c = 5 + 3 * I;
这是非法的。 N1124将此更正为:
int i = 3.5;
double complex c = 5 + 3 * I;
_Complex is not a type; the complex types are "float _Complex",
"double _Complex", and "long double _Complex". If your compiler
allows _Complex by itself, it may be due to a misunderstanding by its
authors; there''s an incorrect example in the standard. C99 6.7.8
Example 1 is:
int i = 3.5;
complex c = 5 + 3 * I;
which is illegal. N1124 corrects this to:
int i = 3.5;
double complex c = 5 + 3 * I;
你想要cpow,而不是pow。
You want cpow, not pow.
格式为%f,%f \ n需要两个double类型的参数。你是
给它一个复杂类型的单个参数,它调用未定义的
行为。复杂的数字有printf格式;你需要使用creal()和
cimag()函数分别打印真实和复杂的部分。
A format of "%f, %f \n" requires two arguments of type double. You''re
giving it a single argument of a complex type, which invokes undefined
behavior. There''s are printf formats for complex numbers; you need to
print the real and complex parts separately, using the creal() and
cimag() functions.
我得到:
sh:PAUSE:找不到命令
I get:
sh: PAUSE: command not found
< complex.hdeclares复杂类型的数学函数。
<complex.hdeclares math functions for complex types.
新关键字拼写为_Complex只是为了避免破坏现有的
代码。如果您#include< complex.h>",则会出现复杂字样。作为一个宏,
扩展为_Complex。 " _Complex"几乎是故意的丑陋;
没有必要在用户代码中使用它。只需使用复杂即可 - 或者
而不是双重复杂。
这是你的程序的更正版本:
#include< stdio.h>
#include< complex.h>
int main(无效)
{
双复数z1,z2,z3;
z1 = .4 + .7i;
z2 = cpow(z1,2);
z3 = z1 * z1;
printf("%f,%f \ n",creal(z1),cimag(z1));
printf("%f,%f \ n",creal(z2),cimag(z1));
printf("%f,%f \ n",creal) (z3),cimag(z1));
返回0;
}
-
Keith Thompson(The_Other_Keith)< http://www.ghoti.net/~kst> ;
圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
The new keyword is spelled "_Complex" only to avoid breaking existing
code. If you #include <complex.h>", you get "complex" as a macro that
expands to "_Complex". "_Complex" is almost deliberately ugly;
there''s no need to use it in user code. Just use "complex" -- or
rather "double complex".
Here''s a corrected version of your program:
#include <stdio.h>
#include <complex.h>
int main(void)
{
double complex z1, z2, z3;
z1 = .4 + .7i;
z2 = cpow(z1, 2);
z3 = z1 * z1;
printf("%f, %f \n", creal(z1), cimag(z1));
printf("%f, %f \n", creal(z2), cimag(z1));
printf("%f, %f \n", creal(z3), cimag(z1));
return 0;
}
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
这篇关于math.h函数用于_Complex类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!