更新时间:2023-02-26 20:26:44
1月24日,2:03 * pm,jacob navia< ja ... @ nospam .comwrote:
On Jan 24, 2:03*pm, jacob navia <ja...@nospam.comwrote:
hi
我正在尝试使用复杂的数据类型。
考虑这段代码,取自cclib库:
struct complex csqrt(Cpx z)
{double r;
* * r = sqrt(z .re * z.re + z.im * z.im);
* * r = sqrt(ldexp(r + fabs(z.re), - 1));
* * if(r == 0。)z.re = z.im = 0 .;
* * else {
* * * if( z.re> = 0。){z.re = r; z.im = ldexp(z.im/r,-1);}
* * * else {z.re = ldexp(fabs(z.im)/ r,-1);
* * * * if(z.im> = 0。)z.im = r;否则z.im = -r;
* * *}
* *}
* *返回z;
}
现在,这很容易在lcc-win中运行,因为lcc-win
允许访问实际和虚部复杂
数据类型轻松,使用
z.re(真实部分)
和
ze.im
假想部分是
。
我看不出用标准C写的怎么样。
Has有人有想法吗?
hi
I am trying to use the complex data type.
Consider this code, taken from the cclib library:
struct complex csqrt(Cpx z)
{ double r;
* *r=sqrt(z.re*z.re+z.im*z.im);
* *r=sqrt(ldexp(r+fabs(z.re),-1));
* *if(r==0.) z.re=z.im=0.;
* *else{
* * *if(z.re>=0.){ z.re=r; z.im=ldexp(z.im/r,-1);}
* * *else{ z.re=ldexp(fabs(z.im)/r,-1);
* * * *if(z.im>=0.) z.im=r; else z.im= -r;
* * * }
* * }
* *return z;
}
Now, this works in lcc-win easily because lcc-win
allows access to the real and imaginary parts of the complex
data type easily, using
z.re (real part)
and
ze.im
for the imaginary part.
I do not see how can be this written in standard C.
Has anyone an idea?
获取想象中的部分:
#include< complex.h>
double cimag(double complex z);
float cimagf(float complex z);
long double cimagl(long double complex z);
但是再一次,要获得平方根,你只需要调用
函数:
#include< complex.h>
double complex csqrt(double complex z);
float complex csqrtf(float complex z);
long double complex csqrtl(long double complex z );
所以我猜你没有标题complex.h?
在这种情况下,也许这有用:
http://www.moshier.net/#Complex_variables
1月24日,2:03 * pm,jacob navia< ja ... @ nospam.comwrote:
On Jan 24, 2:03*pm, jacob navia <ja...@nospam.comwrote:
hi
我正在尝试使用复杂的da ta类型。
考虑这段代码,取自cclib库:
struct complex csqrt(Cpx z)
{double r;
* * r = sqrt(z.re * z.re + z.im * z.im);
* * r = sqrt (ldexp(r + fabs(z.re), - 1));
* * if(r == 0。)z.re = z.im = 0 .;
* * else {
* * * if(z.re> = 0。){z.re = r; z.im = ldexp(z.im/r,-1);}
* * * else {z.re = ldexp(fabs(z.im)/ r,-1);
* * * * if(z.im> = 0。)z.im = r;否则z.im = -r;
* * *}
* *}
* *返回z;
}
现在,这很容易在lcc-win中运行,因为lcc-win
允许访问实际和虚部复杂
数据类型轻松,使用
z.re(真实部分)
和
ze.im
假想部分是
。
我看不出用标准C写的怎么样。
Has有人有想法吗?
hi
I am trying to use the complex data type.
Consider this code, taken from the cclib library:
struct complex csqrt(Cpx z)
{ double r;
* *r=sqrt(z.re*z.re+z.im*z.im);
* *r=sqrt(ldexp(r+fabs(z.re),-1));
* *if(r==0.) z.re=z.im=0.;
* *else{
* * *if(z.re>=0.){ z.re=r; z.im=ldexp(z.im/r,-1);}
* * *else{ z.re=ldexp(fabs(z.im)/r,-1);
* * * *if(z.im>=0.) z.im=r; else z.im= -r;
* * * }
* * }
* *return z;
}
Now, this works in lcc-win easily because lcc-win
allows access to the real and imaginary parts of the complex
data type easily, using
z.re (real part)
and
ze.im
for the imaginary part.
I do not see how can be this written in standard C.
Has anyone an idea?
只是猜测,因为我现在没有方便的C99编译器,但
我会说:
#include< complex.h>
double complex my_csqrt(double complex z)
{
double r;
r = sqrt(creal(z)* creal(z)+(cimag(z)* cimag(z))* I;
r = sqrt(ldexp(r +) fabs(creal(z)), - 1));
if(r == 0。)
z = 0.0 + 0.0 * I;
else {
if(creal(z)> = 0.){
z = r + ldexp(cimag(z)/ r,-1)* I ;
}否则{
z = ldexp(fabs(cimag(z))/ r,-1)+ cimag(z)> = 0?r *
我:-r *我;
}
}
返回z;
}
Just guessing, since I do not have a C99 compiler handy right now, but
I would say:
#include <complex.h>
double complex my_csqrt(double complex z)
{
double r;
r = sqrt(creal(z) * creal(z) + (cimag(z) * cimag(z)) * I;
r = sqrt(ldexp(r + fabs(creal(z)), -1));
if (r == 0.)
z= 0.0 + 0.0 * I;
else {
if (creal(z) >= 0.) {
z = r + ldexp(cimag(z) / r, -1) * I;
} else {
z = ldexp(fabs(cimag(z)) / r, -1) + cimag(z) >= 0 ? r *
I : -r * I;
}
}
return z;
}
jacob navia写道:
jacob navia wrote:
hi
I我试图使用复杂的数据类型。
考虑这个代码,取自cclib库:
struct complex csqrt(Cpx z)
{double r;
r = sqrt(z.re * z.re + z.im * z.im);
r = sqrt(ldexp(r +) fabs(z.re), - 1));
if(r == 0。)z.re = z.im = 0 .;
else {
if(z.re> = 0。){z.re = r; z.im = ldexp(z.im/r,-1);}
else {z.re = ldexp(fabs(z.im)/ r,-1);
if(z.im> = 0。)z.im = r;否则z.im = -r;
}
}
返回z;
}
现在,这很容易在lcc-win中运行,因为lcc-win
允许访问复杂的实部和虚部
数据类型轻松,使用
z.re(真实部分)
和
ze.im
对于想象中的部分。
我看不出用标准C写的怎么样。
有人有想法吗?
hi
I am trying to use the complex data type.
Consider this code, taken from the cclib library:
struct complex csqrt(Cpx z)
{ double r;
r=sqrt(z.re*z.re+z.im*z.im);
r=sqrt(ldexp(r+fabs(z.re),-1));
if(r==0.) z.re=z.im=0.;
else{
if(z.re>=0.){ z.re=r; z.im=ldexp(z.im/r,-1);}
else{ z.re=ldexp(fabs(z.im)/r,-1);
if(z.im>=0.) z.im=r; else z.im= -r;
}
}
return z;
}
Now, this works in lcc-win easily because lcc-win
allows access to the real and imaginary parts of the complex
data type easily, using
z.re (real part)
and
ze.im
for the imaginary part.
I do not see how can be this written in standard C.
Has anyone an idea?
你的功能在标准C下工作正常。检查以下内容:
#include< math.h>
#include< stdio.h>
/ *如果您没有< complex.h>,删除或注释下面的行* /
#define USE_COMPLEX_H
typedef struct cmplx
{
double re,im;
} Cpx;
/ *''cmplx''选择结构名称,因为''complex''应该是
< complex.h * /
Cpx Csqrt
/ *已重命名,因为csqrt应位于< complex.h * /
(Cpx z )
{
double r;
r = sqrt(z.re * z.re + z.im * z.im);
r = sqrt(ldexp(r + fabs(z.re), - 1));
if(r == 0.)
z.re = z.im = 0 .;
else {
if(z.re> = 0.){
z .re = r;
z.im = ldexp(z.im / r,-1);
}
else {
z.re = ldexp(fabs(z.im) / r,-1);
if(z.im> = 0.)
z.im = r;
else
z.im = -r;
}
}
返回z;
}
Cpx Csquare(Cpx z)
{
Cpx r;
r。 re = z.re * z.re - z.im * z.im;
r.im = 2 * z.re * z.im;
return r ;
}
#if定义(USE_COMPLEX_H)
#include< complex.h>
双复数Csqrt2(双复z)
{
双r;
r = sqrt(creal(z)* creal(z)+ cimag(z)* cimag(z));
r = sqrt(ldexp(r + fabs(creal(z)), - 1));
if(r == 0。)
z = 0 .;
else {
if(creal(z)> = 0 。){
z = r + ldexp(cimag(z)/ r,-1)* I;
}
else {
z = ldexp(fabs(cimag(z))/ r,
-1)+(cimag(z)> = 0? r:-r)*我;
}
}
返回z;
}
#endif
int main(无效)
{
Cpx x,y,z;
#if定义(USE_COMPLEX_H)
复数复数cx,cy,cz;
#endif
int i,j;
printf("使用Jacob Navia提供的函数,\ n"
"带有复杂类型的结构。\ nn \ n");
for(i = -2; i< 3; i ++)
for(j = -2; j< 3; j ++){
x.re = i;
x.im = j;
y = Csqrt(x);
z = Csquare(y);
printf(" y = Csqrt(%g +%gi))=%g +%gi,y * y =%g +%gi \ n",
x .re,x.im,y.re,y.im,z.re,z.im);
}
#if定义(USE_COMPLEX_H )
printf(\ n使用Jacob Navia提供的函数,\ n"
重写为使用复杂类型。\ nn \ n") ;
for(i = -2; i< 3; i ++)
for(j = - 2; j< 3; j ++){
cx = i + j * I;
cy = Csqrt2(cx);
cz = cy * cy;
printf(" y = Csqrt2(%g +%gi))=%g +%gi,y * y =%g +%gi \ n,
creal(cx),cimag(cx),
creal(cy),cimag(cy),
creal(cz),cimag(cz)) ;
}
#endif
返回0;
}
[输出]
使用Jacob Navia提供的函数,
带有复杂类型的结构。
y = Csqrt(-2 + -2i))= 0.643594 + -1.55377i,y * y = -2 + -2i
y = Csqrt(-2 + - 1i))= 0.343561 + -1.45535i,y * y = -2 + -1i
y = Csqrt(-2 + 0i))= 0 + 1.41421i,y * y = -2 + 0i
y = Csqrt(-2 + 1i))= 0.343561 + 1.45535i,y * y = -2 + 1i
y = Csqrt(-2 + 2i))= 0.643594 + 1.55377i,y * y = -2 + 2i
y = Csqrt(-1 + -2i))= 0.786151 + -1.27202i,y * y = -1 + -2i
y = Csqrt(-1 + -1i))= 0.45509 + -1.09868i,y * y = -1 + -1i
y = Csqrt(-1 + 0i))= 0+ 1i,y * y = -1 + 0i
y = Csqrt(-1 + 1i))= 0.4 5509 + 1.09868i,y * y = -1 + 1i
y = Csqrt(-1 + 2i))= 0.786151 + 1.27202i,y * y = -1 + 2i
y = Csqrt(0 + -2i))= 1 + -1i,y * y = 0 + -2i
y = Csqrt(0 + -1i))= 0.707107 + -0.707107i,y * y = 1.5702e-16 + -1i
y = Csqrt(0 + 0i))= 0 + 0i,y * y = 0 + 0i
y = Csqrt(0 + 1i))= 0.707107 + 0.707107i,y * y = 1.5702e-16 + 1i
y = Csqrt(0 + 2i))= 1 + 1i,y * y = 0 + 2i
y = Csqrt(1 + -2i))= 1.27202 + -0.786151i,y * y = 1 + -2i
y = Csqrt(1 + -1i))= 1.09868+ -0.45509i,y * y = 1 + -1i
y = Csqrt(1 + 0i))= 1 + 0i,y * y = 1 + 0i
y = Csqrt (1 + 1i))= 1.09868 + 0.45509i,y * y = 1 + 1i
y = Csqrt(1 + 2i))= 1.27202 + 0.786151i,y * y = 1 + 2i
y = Csqrt(2 + -2i))= 1.55377 + -0.643594i,y * y = 2 + -2i
y = Csqrt(2 + -1i))= 1.45535+ -0.343561i,y * y = 2 + -1i
y = Csqrt(2 + 0i))= 1.41421 + 0i,y * y = 2 + 0i
y = Csqrt (2 + 1i))= 1.45535 + 0.343561i,y * y = 2 + 1i
y = Csqrt(2 + 2i))= 1.55377 + 0.643594i,y * y = 2 + 2i
使用Jacob Navia提供的功能,
改写为使用__complex__类型。
y = Csqrt2(-2 + -2i))= 0.643594 + -1.55377i,y * y = -2 + -2i
y = Csqrt2(-2 + -1i))= 0.343561 + -1.45535i,y * y = -2 + -1i
y = Csqrt2(-2 + 0i))= 0 + 1.41421 i,y * y = -2 + 0i
y = Csqrt2(-2 + 1i))= 0.343561 + 1.45535i,y * y = -2 + 1i
y = Csqrt2(-2 + 2i))= 0.643594 + 1.55377i,y * y = -2 + 2i
y = Csqrt2(-1 + -2i))= 0.786151 + -1.27202i,y * y = -1 + -2i
y = Csqrt2(-1 + -1i))= 0.45509 + -1.09868i,y * y = -1 + -1i
y = Csqrt2 (-1 + 0i))= 0 + 1i,y * y = -1 + 0i
y = Csqrt2(-1 + 1i))= 0.45509 + 1.09868i,y * y = -1 + 1i
y = Csqrt2(-1 + 2i))= 0.786151 + 1.27202i,y * y = -1 + 2i
y = Csqrt2(0 + -2i))= 1 + -1i,y * y = 0 + -2i
y = Csqrt2(0 + -1i))= 0.707107 + -0.707107i,y * y = 1.5702e-16 + -1i
y = Csqrt2(0 + 0i))= 0 + 0i,y * y = 0 + 0i
y = Csqrt2(0 + 1i))= 0.707107 + 0.707107i,y * y = 1.5702e-16 + 1i
y = Csqrt2(0 + 2i))= 1 + 1i,y * y = 0 + 2i
y = Csqrt2(1 + - 2i))= 1.27202 + -0.786151i,y * y = 1 + -2i
y = Csqrt2(1 + -1i))= 1.09868 + -0.45509i,y * y = 1 + -1i
y = Csqrt2(1 + 0i))= 1 + 0i,y * y = 1 + 0i
y = Csqrt2(1 + 1i))= 1.09868 + 0.45509i,y * y = 1 + 1i
y = Csqrt2(1 + 2i))= 1.27202 + 0.786151i,y * y = 1 + 2i
y = Csqrt2(2 + -2i))= 1.55377 + -0.643594i,y * y = 2 + -2i
y = Csqrt2(2 + -1i))= 1.45535 + -0.343561i,y * y = 2 + -1i
y = Csqrt2(2 + 0i))= 1.41421 + 0i,y * y = 2 + 0i
y = Csqrt2(2 + 1i))= 1.45535 + 0.343561i,y * y = 2 + 1i
y = Csqrt2(2 + 2i))= 1.55377 + 0.643594i,y * y = 2 + 2i
Your function works just fine in standard C. Check the following:
#include <math.h>
#include <stdio.h>
/* if you don''t have <complex.h>, remove or comment out the line below */
#define USE_COMPLEX_H
typedef struct cmplx
{
double re, im;
} Cpx;
/* ''cmplx'' chosen for the name of the struct since ''complex'' should be
<complex.h*/
Cpx Csqrt
/* renamed because csqrt should be in <complex.h*/
(Cpx z)
{
double r;
r = sqrt(z.re * z.re + z.im * z.im);
r = sqrt(ldexp(r + fabs(z.re), -1));
if (r == 0.)
z.re = z.im = 0.;
else {
if (z.re >= 0.) {
z.re = r;
z.im = ldexp(z.im / r, -1);
}
else {
z.re = ldexp(fabs(z.im) / r, -1);
if (z.im >= 0.)
z.im = r;
else
z.im = -r;
}
}
return z;
}
Cpx Csquare(Cpx z)
{
Cpx r;
r.re = z.re * z.re - z.im * z.im;
r.im = 2 * z.re * z.im;
return r;
}
#if defined(USE_COMPLEX_H)
#include <complex.h>
double complex Csqrt2(double complex z)
{
double r;
r = sqrt(creal(z) * creal(z) + cimag(z) * cimag(z));
r = sqrt(ldexp(r + fabs(creal(z)), -1));
if (r == 0.)
z = 0.;
else {
if (creal(z) >= 0.) {
z = r + ldexp(cimag(z) / r, -1) * I;
}
else {
z = ldexp(fabs(cimag(z)) / r,
-1) + (cimag(z) >= 0. ? r : -r) * I;
}
}
return z;
}
#endif
int main(void)
{
Cpx x, y, z;
#if defined(USE_COMPLEX_H)
double complex cx, cy, cz;
#endif
int i, j;
printf("Using the function Jacob Navia provided,\n"
"with a struct for complex types.\n\n");
for (i = -2; i < 3; i++)
for (j = -2; j < 3; j++) {
x.re = i;
x.im = j;
y = Csqrt(x);
z = Csquare(y);
printf("y = Csqrt(%g+%gi)) = %g+%gi, y*y=%g+%gi\n",
x.re, x.im, y.re, y.im, z.re, z.im);
}
#if defined(USE_COMPLEX_H)
printf("\nUsing the function Jacob Navia provided,\n"
"rewritten to use complex types.\n\n");
for (i = -2; i < 3; i++)
for (j = -2; j < 3; j++) {
cx = i + j * I;
cy = Csqrt2(cx);
cz = cy * cy;
printf("y = Csqrt2(%g+%gi)) = %g+%gi, y*y=%g+%gi\n",
creal(cx), cimag(cx),
creal(cy), cimag(cy),
creal(cz), cimag(cz));
}
#endif
return 0;
}
[Output]
Using the function Jacob Navia provided,
with a struct for complex types.
y = Csqrt(-2+-2i)) = 0.643594+-1.55377i, y*y=-2+-2i
y = Csqrt(-2+-1i)) = 0.343561+-1.45535i, y*y=-2+-1i
y = Csqrt(-2+0i)) = 0+1.41421i, y*y=-2+0i
y = Csqrt(-2+1i)) = 0.343561+1.45535i, y*y=-2+1i
y = Csqrt(-2+2i)) = 0.643594+1.55377i, y*y=-2+2i
y = Csqrt(-1+-2i)) = 0.786151+-1.27202i, y*y=-1+-2i
y = Csqrt(-1+-1i)) = 0.45509+-1.09868i, y*y=-1+-1i
y = Csqrt(-1+0i)) = 0+1i, y*y=-1+0i
y = Csqrt(-1+1i)) = 0.45509+1.09868i, y*y=-1+1i
y = Csqrt(-1+2i)) = 0.786151+1.27202i, y*y=-1+2i
y = Csqrt(0+-2i)) = 1+-1i, y*y=0+-2i
y = Csqrt(0+-1i)) = 0.707107+-0.707107i, y*y=1.5702e-16+-1i
y = Csqrt(0+0i)) = 0+0i, y*y=0+0i
y = Csqrt(0+1i)) = 0.707107+0.707107i, y*y=1.5702e-16+1i
y = Csqrt(0+2i)) = 1+1i, y*y=0+2i
y = Csqrt(1+-2i)) = 1.27202+-0.786151i, y*y=1+-2i
y = Csqrt(1+-1i)) = 1.09868+-0.45509i, y*y=1+-1i
y = Csqrt(1+0i)) = 1+0i, y*y=1+0i
y = Csqrt(1+1i)) = 1.09868+0.45509i, y*y=1+1i
y = Csqrt(1+2i)) = 1.27202+0.786151i, y*y=1+2i
y = Csqrt(2+-2i)) = 1.55377+-0.643594i, y*y=2+-2i
y = Csqrt(2+-1i)) = 1.45535+-0.343561i, y*y=2+-1i
y = Csqrt(2+0i)) = 1.41421+0i, y*y=2+0i
y = Csqrt(2+1i)) = 1.45535+0.343561i, y*y=2+1i
y = Csqrt(2+2i)) = 1.55377+0.643594i, y*y=2+2i
Using the function Jacob Navia provided,
rewritten to use __complex__ types.
y = Csqrt2(-2+-2i)) = 0.643594+-1.55377i, y*y=-2+-2i
y = Csqrt2(-2+-1i)) = 0.343561+-1.45535i, y*y=-2+-1i
y = Csqrt2(-2+0i)) = 0+1.41421i, y*y=-2+0i
y = Csqrt2(-2+1i)) = 0.343561+1.45535i, y*y=-2+1i
y = Csqrt2(-2+2i)) = 0.643594+1.55377i, y*y=-2+2i
y = Csqrt2(-1+-2i)) = 0.786151+-1.27202i, y*y=-1+-2i
y = Csqrt2(-1+-1i)) = 0.45509+-1.09868i, y*y=-1+-1i
y = Csqrt2(-1+0i)) = 0+1i, y*y=-1+0i
y = Csqrt2(-1+1i)) = 0.45509+1.09868i, y*y=-1+1i
y = Csqrt2(-1+2i)) = 0.786151+1.27202i, y*y=-1+2i
y = Csqrt2(0+-2i)) = 1+-1i, y*y=0+-2i
y = Csqrt2(0+-1i)) = 0.707107+-0.707107i, y*y=1.5702e-16+-1i
y = Csqrt2(0+0i)) = 0+0i, y*y=0+0i
y = Csqrt2(0+1i)) = 0.707107+0.707107i, y*y=1.5702e-16+1i
y = Csqrt2(0+2i)) = 1+1i, y*y=0+2i
y = Csqrt2(1+-2i)) = 1.27202+-0.786151i, y*y=1+-2i
y = Csqrt2(1+-1i)) = 1.09868+-0.45509i, y*y=1+-1i
y = Csqrt2(1+0i)) = 1+0i, y*y=1+0i
y = Csqrt2(1+1i)) = 1.09868+0.45509i, y*y=1+1i
y = Csqrt2(1+2i)) = 1.27202+0.786151i, y*y=1+2i
y = Csqrt2(2+-2i)) = 1.55377+-0.643594i, y*y=2+-2i
y = Csqrt2(2+-1i)) = 1.45535+-0.343561i, y*y=2+-1i
y = Csqrt2(2+0i)) = 1.41421+0i, y*y=2+0i
y = Csqrt2(2+1i)) = 1.45535+0.343561i, y*y=2+1i
y = Csqrt2(2+2i)) = 1.55377+0.643594i, y*y=2+2i