problem with complex numbers

V

vj

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::eek:ut);
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();
}
 
V

Victor Bazarov

vj said:
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>
[...multiple inclusion of non-standard headers snipped...]
main()

You use non-standard language constructs here. You need to make those
things standard or ask in the newsgroup that deals with your particular
compiler (borland.public.cppbuilder.language, IIRC).

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
 
O

osmium

vj said:
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).

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::eek:ut);
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();
}
 
V

vj

Thankyou Victor and Osmium for your replies.

Victor, after getting your reply that formula might be incorrect, I
made investigations on the formula. Now I can pretty surely say that
the formula was correct and there is some other problem somewhere in
the code which stops it from running I say this based on following
observations:

(1) If I replace the complex expression for 'r', the formula which
allegedly is causing problem, by a simple expression such as r =
1.0+j*2.0; even then the code doesn't run, giving the same error.
(2) The formula has been taken from a reputed research paper and is
authentic, therefore less likely to be wrong.

Osmium, I will work on your suggestion. But even if I am taking sqrt of
a negative number, the result will be a complex number, and the 'r' has
been declared as a complex number, so shouldn't the code be able to
handle this?

Any suggestions are welcome.
 
D

dan2online

sqrt is a template function here. So you need to convert double to
double_complex.

r =
-cc/(sqrt(doule_complex(cc*cc-detuning*detuning))*(1.0/tanh(sqrt(double_complex((cc*cc-detuning*detuning)*L)))-j*detuning);

try it!
 
U

Ulrich Eckhardt

vj said:
#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>

You are including
- iostream.h three times
- fstream.h three times
- stdio.h two times
- math.h two times
- stdlib.h two times
- stddef.h two times

Of those, the first two are not even part of standard C++ and the others
are deprecated. Several modern compilers don't even provide these
pre-standard IOStreams. This means two things:
1. It's hard to diagnose what's wrong without even knowing what is going on
and we don't know that because it is not standardised.
2. You need to update your knowledge about C++. Go to accu.org and pick a
good book from the reviews section.
#define PI 3.14159265
#define c 2.997924591e8

double const pi = 3.14159265;
double const c = 2.997924591e8;

Using #define might accidentally change other code that uses these to e.g.
define function parameters or local variables. On a cursory glance, this
doesn't even use 'c' then...
#define double_complex complex<double>

Dito, but here you rather use a typedef.


No even halfway modern C++ compilers accepts main() without a returnvalue
(which is int, btw).
ofstream outfile("MyProg.dat", ios::eek:ut);
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);

I guess somewhere in this code it fails, not in any stream operations,
right? If so, please remove all that is not necessary (in particular the
redundant headers and the stream operations) and then post a *_minimal_*
example. Also, please tell us where exactly your program fails.
outfile.close();
}

You know that streams are closed automatically when the stream object goes
out of scope?

Uli
 
D

dan2online

You use non-standard language constructs here. You need to make those

<complex.h> is a feature in C99 standard. Now these constructs are OK
for most popular compilers compatible with C99.
 
P

P.J. Plauger

<complex.h> is a feature in C99 standard. Now these constructs are OK
for most popular compilers compatible with C99.

That may be, but it doesn't mix well with notation like:

#define double_complex complex<double>

For that you need the C++ header <complex> (and a using declaration).

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
B

Ben Measures

if I am taking sqrt of a negative number, the result will be a complex
number

Mathematically, yes. Programmatically, no.

What Osmium was trying to hint at (and would have been immediately
apparent if you actually took a proper look at his code) was that the
function sqrt(x) from math.h yields NAN if x < 0.
 
R

Robert W Hand

r = -cc/(sqrt(cc*cc-detuning*detuning)* // more code ...
... But even if I am taking sqrt of
a negative number, the result will be a complex number, and the 'r' has
been declared as a complex number, so shouldn't the code be able to
handle this?

Overloading is a great strength of C++, but it can lead the unwary
astray. sqrt is an identifier that refers to a small family of
overloaded functions. The process of overload resolution picks one of
these functions for the operation in your code.

First all the possible candidate functions for the overload are
identified. Inclusion of proper headers may affect such
identification. In this case, the list should include sqrt(double),
sqrt(float), sqrt(long double), sqrt(complex<double>), etc.

Then of those candidates, the best function (if unique) is chosen by a
series of rules based on the number and types of the arguments. Note
that the return type of the function is of no importance in this
process.

In your case, the argument is of type double, so the real-valued
sqrt(double) will be chosen. To chose the complex valued sqrt, you
need to finagle the argument type.

As I see it, there are three ways to handle this problem.

First, if mixing doubles and complex types is a recurring theme in the
program, I would probably write an inline function to cover the
conversion, say, something like this:

inline std::complex<double> mysqrt(double t)
{
return sqrt(static_cast<std::complex<double> >(t));
}

Call it what you like, but mysqrt(-4) will invoke the complex form.

Second, if the answer is a complex number, make all the variables
complex from the start by changing all the double variables to
complex. Overload resolution works best by sticking to one type of
number.

Third, if mixing doubles and complex types is rare, then explicitly
cast the double when a complex function needs to be invoked.

HTH.
 
R

Robert W Hand

sqrt is a template function here.

<Nit>sqrt is overloaded, but it is not explicitly a template function
or a function template, is it? sqrt<double> is not allowed.</Nit>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,048
Members
48,769
Latest member
Clifft

Latest Threads

Top