Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > problem with complex numbers

Reply
Thread Tools

problem with complex numbers

 
 
vj
Guest
Posts: n/a
 
      04-13-2006
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 "SQRTomain 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: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();
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-13-2006
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 "SQRTomain 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


 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      04-14-2006
"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 "SQRTomain 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: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();
}


 
Reply With Quote
 
vj
Guest
Posts: n/a
 
      04-14-2006
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.

 
Reply With Quote
 
dan2online
Guest
Posts: n/a
 
      04-14-2006
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!

 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      04-14-2006
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: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


--
FAQ: http://ma.rtij.nl/acllc-c++.FAQ.html
 
Reply With Quote
 
dan2online
Guest
Posts: n/a
 
      04-14-2006
> 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.

 
Reply With Quote
 
P.J. Plauger
Guest
Posts: n/a
 
      04-14-2006
"dan2online" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
dan2online
Guest
Posts: n/a
 
      04-14-2006
I agree with you. The code itself needs clearing.

 
Reply With Quote
 
Ben Measures
Guest
Posts: n/a
 
      04-14-2006
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/@/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How complex is complex? Kottiyath Python 22 03-28-2009 10:11 PM
Re: Problem with complex numbers Christian Heimes Python 1 03-23-2008 02:25 PM
wsdl2java: method parameter a complex type that extends another complex type Robert Mark Bram Java 0 02-04-2007 10:06 AM
[XML Schema] Content type of complex type definition with complex content Stanimir Stamenkov XML 2 10-25-2005 10:16 AM
For expert on complex loops (reposted) - complex looping problem news.amnet.net.au Java 1 04-13-2004 07:10 AM



Advertisments