problem with complex numbers

Discussion in 'C++' started by vj, Apr 13, 2006.

  1. vj

    vj Guest

    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();
    }
     
    vj, Apr 13, 2006
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Apr 13, 2006
    #2
    1. Advertising

  3. vj

    osmium Guest

    "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::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();
    }
     
    osmium, Apr 14, 2006
    #3
  4. vj

    vj Guest

    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.
     
    vj, Apr 14, 2006
    #4
  5. vj

    dan2online Guest

    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!
     
    dan2online, Apr 14, 2006
    #5
  6. 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::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


    --
    FAQ: http://ma.rtij.nl/acllc-c .FAQ.html
     
    Ulrich Eckhardt, Apr 14, 2006
    #6
  7. vj

    dan2online Guest

    > 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.
     
    dan2online, Apr 14, 2006
    #7
  8. vj

    P.J. Plauger Guest

    "dan2online" <> wrote in message
    news:...

    >> 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
     
    P.J. Plauger, Apr 14, 2006
    #8
  9. vj

    dan2online Guest

    I agree with you. The code itself needs clearing.
     
    dan2online, Apr 14, 2006
    #9
  10. vj

    Ben Measures Guest

    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/@/
     
    Ben Measures, Apr 14, 2006
    #10
  11. vj

    Default User Guest

    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.
     
    Default User, Apr 14, 2006
    #11
  12. On 13 Apr 2006 20:34:31 -0700, "vj" <> 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
     
    Robert W Hand, Apr 16, 2006
    #12
  13. On 13 Apr 2006 21:51:40 -0700, "dan2online" <>
    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
     
    Robert W Hand, Apr 16, 2006
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.amnet.net.au
    Replies:
    1
    Views:
    585
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Stanimir Stamenkov
    Replies:
    2
    Views:
    755
    Stanimir Stamenkov
    Oct 25, 2005
  3. Robert Mark Bram
    Replies:
    0
    Views:
    694
    Robert Mark Bram
    Feb 4, 2007
  4. Christian Heimes

    Re: Problem with complex numbers

    Christian Heimes, Mar 22, 2008, in forum: Python
    Replies:
    1
    Views:
    310
    David C. Ullrich
    Mar 23, 2008
  5. universeee
    Replies:
    0
    Views:
    482
    universeee
    Nov 9, 2008
Loading...

Share This Page