Complex number prints wierd

Discussion in 'C++' started by Protoman, Nov 8, 2005.

  1. Protoman

    Protoman Guest

    I'm trying to print a complex number and i get this as output: Phi:
    (1.618034,0.000000).
    What does it mean? How do I get it to print normally? Here's the code:

    ----------fib.hpp-------------
    #ifndef FIB_HPP
    #define FIB_HPP
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <complex>
    using namespace std;

    namespace
    {
    template <long N>
    class Fib
    {
    public:
    static const long double val=Fib<N-2>::val+Fib<N-1>::val;
    };

    template<>
    class Fib<2>
    {
    public:
    static const long double val=1;
    };

    template<>
    class Fib<1>
    {
    public:
    static const long double val=1;
    };

    const complex<long double> phi= Fib<32>::val/Fib<31>::val;
    }
    #endif
    --------------------------

    ------Main.cpp------
    #include "fib.hpp"

    int main()
    {
    cout << "Phi: " << fixed << phi << endl;
    system("PAUSE");
    return 0;
    }
    ------------------------

    Can you help me?
    Protoman, Nov 8, 2005
    #1
    1. Advertising

  2. Protoman

    Kai-Uwe Bux Guest

    Protoman wrote:

    > I'm trying to print a complex number and i get this as output: Phi:
    > (1.618034,0.000000).
    > What does it mean?


    It means that your complex number has a real part of 1.618034 and an
    imaginary part of 0.0.


    > How do I get it to print normally?


    This is the way complex numbers print *normally*. What did you expect?


    [code snipped]


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Nov 8, 2005
    #2
    1. Advertising

  3. Protoman

    Protoman Guest

    OK. Is this a fast way to get fibonacci numbers?
    Protoman, Nov 8, 2005
    #3
  4. Protoman

    Chris Goller Guest

    You are calculating the fibonacci of 32 and 31 at compile time. So,
    when the program runs, it doesn't have to calculate the sequence.

    >From the executable's standpoint: yes, this is fast.
    Chris Goller, Nov 8, 2005
    #4
  5. Protoman

    Protoman Guest

    Is there anyway I can make it faster? Do you know if there's a name for
    what I'm doing for Fib?
    Protoman, Nov 8, 2005
    #5
  6. Protoman

    Greg Guest

    Chris Goller wrote:
    > You are calculating the fibonacci of 32 and 31 at compile time. So,
    > when the program runs, it doesn't have to calculate the sequence.


    There is no such thing as the Fibonacci of a number.

    Granted, there is 32nd and 31st Fibonacci number, but this program is
    certainly not calculating either of them. Nor is it successfully
    calculating the golden mean.

    This program is simply dividing the sum of all integers from 1 to 32 by
    the sum of all integers from 1 to 31.

    Greg
    Greg, Nov 8, 2005
    #6
  7. Protoman

    Kai-Uwe Bux Guest

    Greg wrote:

    >
    > Chris Goller wrote:
    >> You are calculating the fibonacci of 32 and 31 at compile time. So,
    >> when the program runs, it doesn't have to calculate the sequence.

    >
    > There is no such thing as the Fibonacci of a number.
    >
    > Granted, there is 32nd and 31st Fibonacci number, but this program is
    > certainly not calculating either of them. Nor is it successfully
    > calculating the golden mean.


    Your assesment is a little off, at least if you are referring to the program
    in the original post: if the program compiled, it would compute Fibonacci
    numbers and the golden mean; the problem with the code is that it uses
    in-place initialization for non-integral types. Here is a version that
    compiles. The algorithm is completely unchanged.

    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <complex>
    using namespace std;

    template <long N>
    class Fib
    {
    public:
    static const unsigned long val=Fib<N-2>::val+Fib<N-1>::val;
    };

    template<>
    class Fib<2>
    {
    public:
    static const unsigned long val=1;
    };

    template<>
    class Fib<1>
    {
    public:
    static const unsigned long val=1;
    };

    const double phi= double(Fib<32>::val)/double(Fib<31>::val);

    int main()
    {
    cout << "Phi: " << fixed << phi << endl;
    return 0;
    }

    Guess what it prints:

    Phi: 1.618034

    That passes for an approximation of the golden mean. Moreover, you can print
    the first few terms of the sequence:

    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <complex>
    using namespace std;

    template <long N>
    class Fib
    {
    public:
    static const unsigned long val=Fib<N-2>::val+Fib<N-1>::val;
    };

    template<>
    class Fib<2>
    {
    public:
    static const unsigned long val=1;
    };

    template<>
    class Fib<1>
    {
    public:
    static const unsigned long val=1;
    };

    const double phi= double(Fib<32>::val)/double(Fib<31>::val);

    int main()
    {
    cout << Fib<1>::val << " "
    << Fib<2>::val << " "
    << Fib<3>::val << " "
    << Fib<4>::val << " "
    << Fib<5>::val << " "
    << Fib<6>::val << " "
    << Fib<7>::val << "\n";
    return 0;
    }


    And that prints:

    1 1 2 3 5 8 13

    Looks like Fibonacci numbers to me.


    Also, it is actually clear from the code that the program computes the
    Fibonacci sequence.


    > This program is simply dividing the sum of all integers from 1 to 32 by
    > the sum of all integers from 1 to 31.


    No it does not. The sum of all integers from 1 to 32 is 16*31. The sum of
    all integers from 1 to 31 is 15*31. The quotient is 16/15.



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Nov 8, 2005
    #7
  8. Protoman

    Protoman Guest

    So, how does the compiler execute the metaprogram?
    Protoman, Nov 8, 2005
    #8
  9. Protoman wrote:
    >
    > So, how does the compiler execute the metaprogram?


    Well. If *you* were the compiler. What would *you* need to
    do in order to create an executable program.
    (Actually: A surprisingly large number of so called 'clever'
    programming is just an adoption of what people do in real
    life. So if you direct your thinking into 'What would I do
    if all I have is paper and pencil?' is surprisingly often
    an excellent tool in understanding what is going on.)

    The compiler comes to compiling:

    const complex<long double> phi= Fib<32>::val/Fib<31>::val;

    This is a declaration of a complex<double> variable called phi.
    The variable is initialized with the value of
    Fib<32>::val/Fib<31>::val

    For this the compiler needs to know
    Fib<32>::val
    and Fib<31>::val

    Fib<32> is the instantiation of a template. Thus the compiler
    looks up the Fib template and substitutes 32 for N

    template <long N>
    class Fib
    {
    public:
    static const long double val=Fib<N-2>::val+Fib<N-1>::val;
    };

    becomes

    class Fib
    {
    public:
    static const long double val=Fib<30>::val+Fib<31>::val;
    };

    In order to make this compilable the compiler has to come up with
    the initial value for val. For this it has to evaluate the initialization
    part:

    Fib<30>::val+Fib<31>::val

    Again: The compiler is looking for an instantiation of Fib<30> and since
    there is none it creates one, substituting 30 for N

    class Fib
    {
    public:
    static const long double val=Fib<28>::val+Fib<29>::val;
    };

    Part of compiling that one, makes the compiler look for an instatiation
    of Fib<28>. Since there is none, the compiler creates one, using 28 for N

    class Fib
    {
    public:
    static const long double val=Fib<26>::val+Fib<27>::val;
    };

    And so on, and so on.
    Finally the compiler will have the request to intialize one of the
    generated classes with:

    class Fib
    {
    public:
    static const long double val=Fib<1>::val+Fib<2>::val;
    };

    Looking for Fib<1> the compiler figures out, that the programmer specified
    that one:

    template<>
    class Fib<1>
    {
    public:
    static const long double val=1;
    };

    so it doesn't need to create one on its own. Same for Fib<2>.

    Since those 2 classes are 'complete' the compiler now can use
    them to continue working on

    class Fib
    {
    public:
    static const long double val=Fib<1>::val+Fib<2>::val;
    };

    (which was the template instatiation for Fib<3>).
    Fib<1>::val is known, it equals 1.
    Fib<2>::val is known, it equals 1
    Thus the initialization value for Fib<3>::val thus must be 2

    Having this value, the compiler can continue on finishing
    Fib<4> and so on, and so on, until finally Fib<32>::val can
    be calculated by the compiler.
    Now the whole story starts again for Fib<31>.

    If both values Fib<32>::val and Fib<31>::val are known to the
    copmiler, it can easily calculate Fib<32>::val / Fib<31>::val
    and assign that value to phi.


    However why one would want to do all of that with complex numbers
    is bejond my imagination.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Nov 8, 2005
    #9
  10. Protoman

    Protoman Guest

    So that's how metaprogramming works. Thanks!!!
    Protoman, Nov 9, 2005
    #10
    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:
    560
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Stanimir Stamenkov
    Replies:
    2
    Views:
    734
    Stanimir Stamenkov
    Oct 25, 2005
  3. Robert Mark Bram
    Replies:
    0
    Views:
    673
    Robert Mark Bram
    Feb 4, 2007
  4. Kottiyath

    How complex is complex?

    Kottiyath, Mar 18, 2009, in forum: Python
    Replies:
    22
    Views:
    742
  5. SiWi
    Replies:
    3
    Views:
    806
    geremy condra
    Dec 9, 2009
Loading...

Share This Page