473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

problem with complex numbers

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
12 2726
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
"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
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
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
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
> 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
"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
I agree with you. The code itself needs clearing.

Apr 14 '06 #9
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Clarence | last post by:
Hi - I have a problem and here is the verbose version of what I am trying to do (better too much info than not enough). I am searching through about 4,700 XML files containing company contact...
21
by: Blair | last post by:
could someone PLEASE tell me why this doesn't work... ----------------------------------------- #include <complex> using namespace std; typedef complex<long double> cld; void main() { cld...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
5
by: Todd Steury | last post by:
Greetings Python'ers: I'm just an amature who occasionally uses Python for complex mathematical models. The current model I'm working with occasionally generates really large numbers that are...
6
by: gc | last post by:
Hi, Why didn't the committee propose a new type for complex numbers with integer components? thanks, gc
1
by: seia0106 | last post by:
Hello, I have an array X=, whose even and odd indices should represent real and imaginary parts of complex numbers. This I want to use in a routine that uses the typedef double Cx; for storing...
3
by: Russ | last post by:
I'd like to get output formatting for my own classes that mimics the built-in output formatting. For example, >>> x = 4.54 >>> print "%4.2f" % x 4.54 In other words, if I substitute a class...
11
by: Sambo | last post by:
I have the following module: ------------------------------- import math def ac_add_a_ph( amp1, ph1, amp2, ph2 ): amp3 = 0.0 ph3 = 0.0 ac1 = ( 0, 0j ) ac2 = ( 0, 0j )
9
by: tiwarinitin.3108 | last post by:
An interactive program that reads 3 list of numbers, which are stored in three seperate files, and creates one sorted list. Each file should contain not more than 15 numbers.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.