473,663 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:Domai n 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*detuni ng)*(1.0/tanh(sqrt(cc*cc-detuning*detuni ng)*L))-j*detuning);

outfile<<x<<"\t "<<abs(r)<< endl;
}

outfile.close() ;
}

Apr 13 '06 #1
12 2758
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:Domai n 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.lan guage, 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:Domai n 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*detuni ng;
double b = sqrt(a);
cout << "\n a, b " << a << ' ' << b << endl;

r =
-cc/(sqrt(cc*cc-detuning*detuni ng)*(1.0/tanh(sqrt(cc*cc-detuning*detuni ng)*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_com plex(cc*cc-detuning*detuni ng))*(1.0/tanh(sqrt(doubl e_complex((cc*c c-detuning*detuni ng)*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*detuni ng)
*(1.0/tanh(sqrt(cc*cc-detuning*detuni ng)*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********@gma il.com> wrote in message
news:11******** *************@i 39g2000cwa.goog legroups.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. Programmaticall y, 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1967
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 details. In the company details are phone numbers. The phone numbers with the formats I need have the following structure. <xs:complexType name="PHONENO"> <xs:sequence maxOccurs="unbounded"> <xs:element name="COUNTRY" type="xs:string"/>
21
4113
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 cmplx, temp;
34
6438
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 operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public: Complex( float = 0.0, float = 0.0 );
5
5402
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 either "float" or "complex" types. These numbers are so large that I either get an overflow error, or some funky code like #INF or 1.#INDj. However I really need these numbers to be calculated (although precision isn't key). Is there a way to get...
6
2248
by: gc | last post by:
Hi, Why didn't the committee propose a new type for complex numbers with integer components? thanks, gc
1
3670
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 complex numbers. I want to declare an array of complex numbers(with 8 elements) of this type(Cx), such that even and odd indices of array X are loaded as real
3
2054
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 instance for "x" above, I'd like to make the format string apply to an element or elements of the instance. Can I somehow overload the "%" operator for that? Thanks.
11
7352
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
1644
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
8436
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8771
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.