By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,084 Members | 1,956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,084 IT Pros & Developers. It's quick & easy.

problem with complex numbers

P: n/a
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::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();
}

Apr 13 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
vj wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 13 '06 #2

P: n/a
"vj" wrote:
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::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();
}
Apr 14 '06 #3

P: n/a
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.

Apr 14 '06 #4

P: n/a
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!

Apr 14 '06 #5

P: n/a
vj wrote:
#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.


main()
No even halfway modern C++ compilers accepts main() without a returnvalue
(which is int, btw).
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);
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
--
FAQ: http://ma.rtij.nl/acllc-c++.FAQ.html
Apr 14 '06 #6

P: n/a
> 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.

Apr 14 '06 #7

P: n/a
"dan2online" <da********@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
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.


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
Apr 14 '06 #8

P: n/a
I agree with you. The code itself needs clearing.

Apr 14 '06 #9

P: n/a
On Thu, 13 Apr 2006 20:34:31 -0700, vj wrote:
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.

--
Ben Measures
$email =~ s/is@silly/@/

Apr 14 '06 #10

P: n/a
dan2online wrote:
I agree with you. The code itself needs clearing.


With who? About what?

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 14 '06 #11

P: n/a
On 13 Apr 2006 20:34:31 -0700, "vj" <vi**********@gmail.com> wrote:
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.
--

Best wishes,

Bob
Apr 16 '06 #12

P: n/a
On 13 Apr 2006 21:51:40 -0700, "dan2online" <da********@gmail.com>
wrote:
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>
--

Best wishes,

Bob
Apr 16 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.