问题描述
嗨!
我有一段涉及复杂数字的代码(如下所示)。
代码未运行并且出错(无效浮点
操作和SQRT:域错误)。我会非常感谢
如果有人能告诉我问题在哪里。我知道我的代码是高效且有条理的,而且还有许多额外的
#include语句,代码并不是真正需要的。我是新手
程序员,你可以看到!在这个时候,我只想通过做最小的更改/添加来获得代码来运行
建议,
而不是使代码生效或使用高级功能。
请帮助。
(我正在使用C ++ Builder5,如果您需要此信息)。
#include< iostream.h>
#include< fstream.h>
#include< stdio.h>
#include< math.h>
#include< stdlib.h>
#include< stddef.h>
#include < complex.h>
#include< iostream.h>
#include< fstream.h>
#include< iostream.h>
#include< fstream.h>
#include< stdio.h>
#include< math。 h>
#include< stdlib.h>
#include< stddef.h>
#define PI 3.14159265
#define c 2.997924591e8
#define double_complex complex< double>
main()
{
ofstream outfile(" MyProg.dat",ios :: out);
double_complex j(0,1);
double lambda_0 = 1.5e-6;
double L = 100000.0e-3;
double n = 1.5;
double dn = 0.001;
double prd = lambda_0 / 2.0 / n;
double cc = PI * dn /(2.0 * n * prd);
double_complex r;
for(double x = lambda_0-1.4e-6; X< = lambda_0; x = x +(2.0 * 1.4e-6 / 500.0)){
双失谐=(2.0 * PI * n / x) - (PI / prd);
r =
-cc /(sqrt(cc * cc-detuning * detuning)*(1.0 / tanh(sqrt(cc * cc-detuning * detuning)* L)) - j *失调);
outfile<< x<<" \t"<< abs(r)<< endl;
}
outfile.close();
}
Hi!
I have a piece of code (shown below) involving complex numbers. The
code is not running and giving error ("Invalid floating point
operation" and "SQRT:Domain error"). I would be very thankful
if someone can tell me where is the problem. I am aware that my code is
far from being efficient and organized, and also there are many extra
#include statements not really required for the code. I am a novice
programmer, as you can see ! At this time, I would just like to have
advice just to get the code running by doing minimum changes/additions,
and not on making the code efficient or on using advanced features.
Please help.
(I am using C++Builder5, if this info is required by you).
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stddef.h>
#include <complex.h>
#include <iostream.h>
#include <fstream.h>
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stddef.h>
#define PI 3.14159265
#define c 2.997924591e8
#define double_complex complex<double>
main()
{
ofstream outfile("MyProg.dat", ios::out);
double_complex j(0,1);
double lambda_0 = 1.5e-6;
double L = 100000.0e-3;
double n = 1.5;
double dn=0.001;
double prd=lambda_0/2.0/n;
double cc=PI*dn/(2.0*n*prd);
double_complex r;
for (double x=lambda_0-1.4e-6; x<=lambda_0; x=x+(2.0*1.4e-6/500.0)){
double detuning = (2.0*PI*n/x)-(PI/prd);
r =
-cc/(sqrt(cc*cc-detuning*detuning)*(1.0/tanh(sqrt(cc*cc-detuning*detuning)*L))-j*detuning);
outfile<<x<<"\t"<<abs(r)<< endl;
}
outfile.close();
}
推荐答案
我拿走了你的程序,删除了不必要的标题,将''int''添加到''main'' ,
并运行它。输出表明''r''用无效的
值计算。我建议你检查一下你的公式。
V
-
请在回复时删除资金''A'电子邮件
我没有回复最热门的回复,请不要问
I took your program, removed unnecessary headers, added ''int'' to ''main'',
and ran it. The output indicated that the ''r'' calculated with invalid
values. I suggest you check your formula.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
您正在尝试计算a的平方根负数。看看
这个,也许你可以了解我使用的调试技术。调试修改程序是你的程序。
#include< iostream.h>
#include< fstream.h>
#include< stdio.h>
#include< math.h>
#include< stdlib.h>
#include< stddef.h>
#include< complex.h>
#include< iostream.h>
#include< fstream.h>
#include< iostream.h>
#include< fstream.h>
#include< stdio.h>
#include< math.h>
#include< stdlib.h>
#include < stddef.h>
#define PI 3.14159265
#define c 2.997924591e8
#define double_complex complex< double>
main()
{
ofstream outfile(" MyProg.dat",ios :: out);
double_complex j(0,1);
double lambda_0 = 1.5e-6;
double L = 100000.0e-3;
双n = 1.5;
double dn = 0.001;
double prd = lambda_0 / 2.0 / n;
double cc = PI * dn /(2.0 * n * prd);
double_complex r;
for(double x = lambda_0-1.4e-6; X< = lambda_0; x = x +(2.0 * 1.4e-6 / 500.0)){
双失谐=(2.0 * PI * n / x) - (PI / prd);
double a = cc * cc - detuning * detuning;
double b = sqrt(a);
cout<< \ n a,b << a<< ''''<< b<<结束;
r =
-cc /(sqrt(cc * cc-detuning * detuning)*(1.0 / tanh(sqrt(cc * cc-detuning) *失谐)* L)) - j *失谐);
cout<< x<<" \t"<< abs(r)<< ;结束;
}
outfile.close();
cin.get();
}
You are trying to compute the square root of a negative number. Look at
this and maybe you can get a sense of the debugging technique I used. It is
your program with debugging modifications.
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stddef.h>
#include <complex.h>
#include <iostream.h>
#include <fstream.h>
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stddef.h>
#define PI 3.14159265
#define c 2.997924591e8
#define double_complex complex<double>
main()
{
ofstream outfile("MyProg.dat", ios::out);
double_complex j(0,1);
double lambda_0 = 1.5e-6;
double L = 100000.0e-3;
double n = 1.5;
double dn=0.001;
double prd=lambda_0/2.0/n;
double cc=PI*dn/(2.0*n*prd);
double_complex r;
for (double x=lambda_0-1.4e-6; x<=lambda_0; x=x+(2.0*1.4e-6/500.0)){
double detuning = (2.0*PI*n/x)-(PI/prd);
double a = cc*cc - detuning*detuning;
double b = sqrt(a);
cout << "\n a, b " << a << '' '' << b << endl;
r =
-cc/(sqrt(cc*cc-detuning*detuning)*(1.0/tanh(sqrt(cc*cc-detuning*detuning)*L))-j*detuning);
cout<<x<<"\t"<<abs(r)<< endl;
}
outfile.close();
cin.get();
}
这篇关于复数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!