473,799 Members | 2,941 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Complex number prints wierd

I'm trying to print a complex number and i get this as output: Phi:
(1.618034,0.000 000).
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?

Nov 8 '05 #1
9 1842
Protoman wrote:
I'm trying to print a complex number and i get this as output: Phi:
(1.618034,0.000 000).
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
Nov 8 '05 #2
OK. Is this a fast way to get fibonacci numbers?

Nov 8 '05 #3
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.


Nov 8 '05 #4
Is there anyway I can make it faster? Do you know if there's a name for
what I'm doing for Fib?

Nov 8 '05 #5

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

Nov 8 '05 #6
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
Nov 8 '05 #7
So, how does the compiler execute the metaprogram?

Nov 8 '05 #8
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>::va l+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+Fi b<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>::va l+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>::va l+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
kb******@gascad .at
Nov 8 '05 #9
So that's how metaprogramming works. Thanks!!!

Nov 9 '05 #10

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

Similar topics

13
1519
by: Chris Cioffi | last post by:
Hello all! I'm trying to subclass int such that once it reaches a certain value, it flips back to the starting count. This is for use as an X12 control number. Subclassing int and getting the display like I want it was trivial, however what's the best way to make it such that: 990 + 1 = 991 991 + 1 = 992 .... 998 + 1 = 999
116
7584
by: Mike MacSween | last post by:
S**t for brains strikes again! Why did I do that? When I met the clients and at some point they vaguely asked whether eventually would it be possible to have some people who could read the data and some who couldn't but that it wasn't important right now. And I said, 'sure, we can do that later'. So now I've developed an app without any thought to security and am trying to apply it afterwards. Doh!, doh! and triple doh!
6
4152
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the end is found is less efficient than using sizeof operator , since size of the array is completely determined at compile time. i don't quite understand this. Could anyone explain to me in detail ?
3
2066
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.
12
2768
by: vj | last post by:
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...
2
4235
by: wudoug119 | last post by:
This is my code and it will take any number that I input and say it is a prime number. Please help me... int Prime(int prime) //declares isPrime as a function using integers { int primeCount = 2; //declares primeCount as an int variable and sets it equal to 2 while(primeCount < prime) //checks if primeCount is less than prime {
4
5227
by: SweetLeftFoot | last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime and 0 if it isn't. I have spent ages just getting this code to work and i'm worried i need to rip it to bit to get this function to work ! The function is: int prime(int number) here is my code so far: #include...
7
8617
by: Tina | last post by:
Dear all, I'm looking for a routine which calculates the Gamma Function for a complex valued variable. I'm using #include <complex> to work with complex numbers. I define a complex number as complex <doublealpha(3.0, 1.0)
25
9732
by: jacob navia | last post by:
The C99 standard forgot to define the printf equivalent for complex numbers Since I am revising the lcc-win implementation of complex numbers I decided to fill this hole with "Z" for instance double _Complex m = 2+3*I; printf("%Zg\n",m);
0
9686
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
10475
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
10250
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...
1
10222
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9068
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
7564
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
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
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...
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.