473,394 Members | 1,740 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,394 software developers and data experts.

Fibonacci Program

This Fibonacci program prints the wrong value for phi; it should be
1.618..., not 1. And it halts and closes as soon as I execute it.

Code:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#define setprec setprecision
using namespace std;

template <long long N> class FIB
{
public:
const static long long RET=FIB<N-1>::RET+FIB<N-2>::RET;
};
template<>class FIB<2>
{
public:
const static long long RET=1;
};
template<>class FIB<1>
{
public:
const static long long RET=1;
};
template<>class FIB<0>
{
public:
const static long long RET=0;
};
long double phi=FIB<10>::RET/FIB<9>::RET;
int main()
{
cout << "Fib(30)=" << setprec(10) << fixed << FIB<10>::RET << endl;
cout << "Fib(29)=" << setprec(9) << fixed << FIB<9>::RET << endl;
cout << "Phi= " << setprec(30) << fixed << phi << endl;
system ("PAUSED");
return 0;
}
Any guesses as to what's wrong, b/c I can't detect any errors?
Thanks!!!!

May 28 '06 #1
6 4649
Protoman wrote:
This Fibonacci program prints the wrong value for phi; it should be
1.618..., not 1. And it halts and closes as soon as I execute it.
long double phi=FIB<10>::RET/FIB<9>::RET;
You have an integer type divided by another. These should be converted
to long doubles.

double f10 = FIB<10>::RET;
double f9 = FIB<9>::RET;
long double phi= f10/f9;
int main()
{
cout << "Fib(30)=" << setprec(10) << fixed << FIB<10>::RET << endl;
cout << "Fib(29)=" << setprec(9) << fixed << FIB<9>::RET << endl;
cout << "Phi= " << setprec(30) << fixed << phi << endl;
system ("PAUSED");
What's this supposed to do?
return 0;
}


--
Ian Collins.
May 28 '06 #2
Protoman wrote:
This Fibonacci program prints the wrong value for phi; it should be
1.618..., not 1. And it halts and closes as soon as I execute it.

Code:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#define setprec setprecision
Don't do that, it gives you nothing, is dangerous and confuses other
programmers.
using namespace std;
http://www.parashift.com/c++-faq-lit....html#faq-27.5
template <long long N> class FIB
long long does not exist in standard C++.
{
public:
const static long long RET=FIB<N-1>::RET+FIB<N-2>::RET;
I am not sure whether this is legal or not and I found nothing in
c.l.c++ and c.std.c++ after a quick search. Comeau issues the warning
"storage class is not first", although the standard does not seem to
mandate any order for decl-specifier's. Perhaps someone could confirm
this.

In any case, I would recommend this:

static const long ...

instead.
};
template<>class FIB<2>
{
public:
const static long long RET=1;
};
template<>class FIB<1>
{
public:
const static long long RET=1;
};
template<>class FIB<0>
{
public:
const static long long RET=0;
};
long double phi=FIB<10>::RET/FIB<9>::RET;
FIB::RET is an integer, so this is an integer division, truncating the
fraction part. Therefore, it gives "1". Replace this by

double phi=static_cast<double>(FIB<10>::RET) / FIB<9>::RET;

And long double is also not standard.
int main()
{
cout << "Fib(30)=" << setprec(10) << fixed << FIB<10>::RET << endl;
cout << "Fib(29)=" << setprec(9) << fixed << FIB<9>::RET << endl;
cout << "Phi= " << setprec(30) << fixed << phi << endl;
system ("PAUSED");
system() is standard, but what goes in it is not. You seem to think
"PAUSED" on your system has some effect and that it is the effect you
desire. You also seem to be wrong. Perhaps

std::cin.get();

could help you more, if the problem is to keep a "console window"
opened.
Any guesses as to what's wrong, b/c I can't detect any errors?


This is not a chat room, try to type complete words.
Jonathan

May 28 '06 #3
OK, I replaced it, and it works:

long double phi=static_cast<long double>(FIB<11>::RET)/static_cast<long
double>(FIB<10>::RET);

And I made a spelling error in the system() call, which is why it kept
abending.

And, since this is a TMP program, could you tell me what the code the
compiler collaspses the metaprogram into is? Thanks!!!!

May 28 '06 #4
Protoman schrieb:
OK, I replaced it, and it works:
Was did you replace? What is _it_?
long double phi=static_cast<long double>(FIB<11>::RET)/static_cast<long
double>(FIB<10>::RET);

And I made a spelling error in the system() call, which is why it kept
abending.

And, since this is a TMP program, could you tell me what the code the
compiler collaspses the metaprogram into is? Thanks!!!!


Well, on my system, the line

double phi = static_cast<double>(fib<11>::result)/fib<10>::result;

translates into something like this:

double phi = 1.618181818; // maybe more precision

What did you expect?

Thomas
May 28 '06 #5
Jonathan Mcdougall wrote:
Protoman wrote:
This Fibonacci program prints the wrong value for phi; it should be
1.618..., not 1. And it halts and closes as soon as I execute it.

Code:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#define setprec setprecision


Don't do that, it gives you nothing, is dangerous and confuses other
programmers.
using namespace std;


http://www.parashift.com/c++-faq-lit....html#faq-27.5
template <long long N> class FIB


long long does not exist in standard C++.
{
public:
const static long long RET=FIB<N-1>::RET+FIB<N-2>::RET;


I am not sure whether this is legal or not and I found nothing in
c.l.c++ and c.std.c++ after a quick search. Comeau issues the warning
"storage class is not first", although the standard does not seem to
mandate any order for decl-specifier's. Perhaps someone could confirm
this.

In any case, I would recommend this:

static const long ...

instead.
};
template<>class FIB<2>
{
public:
const static long long RET=1;
};
template<>class FIB<1>
{
public:
const static long long RET=1;
};
template<>class FIB<0>
{
public:
const static long long RET=0;
};
long double phi=FIB<10>::RET/FIB<9>::RET;


FIB::RET is an integer, so this is an integer division, truncating the
fraction part. Therefore, it gives "1". Replace this by

double phi=static_cast<double>(FIB<10>::RET) / FIB<9>::RET;

And long double is also not standard.


The long double type is a standard C++ floating point type (double and
float are the other two). See §3.9.1/8. Furthermore, the long long
type has been added to the forthcoming C++ standard. But many C++
compilers support it already since long long is a standard type in C99.

Greg

May 28 '06 #6

Thomas J. Gritzan wrote:
Protoman schrieb:
OK, I replaced it, and it works:


Was did you replace? What is _it_?
long double phi=static_cast<long double>(FIB<11>::RET)/static_cast<long
double>(FIB<10>::RET);

And I made a spelling error in the system() call, which is why it kept
abending.

And, since this is a TMP program, could you tell me what the code the
compiler collaspses the metaprogram into is? Thanks!!!!


Well, on my system, the line

double phi = static_cast<double>(fib<11>::result)/fib<10>::result;

translates into something like this:

double phi = 1.618181818; // maybe more precision

What did you expect?

Thomas


A mov instruction.

May 28 '06 #7

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

Similar topics

28
by: dleecurt | last post by:
Hello, I have a small problem, I am trying to write a program that will calculate the Fibonacci number series, and I have the code complete with one problem. I used a long in to store the numbers,...
0
by: Alex Vinokur | last post by:
An algorithm which computes very long Fibonacci numbers http://groups.google.com/groups?selm=bnni5p%2412i47o%241%40ID-79865.news.uni-berlin.de was used as a performance testsuite to compare speed...
12
by: CII | last post by:
Hi everybody. I've been reading posts a year old about the fibonacci series right on this newsgroup, and while it's not directly games related, I'll share my own notes as well. On another...
14
by: felixnielsen | last post by:
Im actually kinda embarassed to ask this question... @code start #include <iostream> int main() { unsigned long long a = 1; unsigned long long b = 1; for (int i = 0; i < 45; i++) { a += b;...
8
by: srinpraveen | last post by:
I know to write a program to print the fibonacci series. But the problem is my teacher has asked us to write a program to print the natural numbers that are not involved in the fibonacci series. For...
3
by: veeru | last post by:
Hi All, Can anyone tell about how to create a FIBONACCI series in VB.Net and C# Thanks in Advance, Veeru
17
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
13
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
6
by: Andrew Tatum | last post by:
I'm having some problems with the below equation. I have no problems when it comes to positives. Negatives create the problem.. C 2 1 4 However, this doesn't work:
1
by: altaey | last post by:
Question Details: Write a program to find and print a Fibonacci sequence of numbers. The Fibonacci sequence is defined as follow: Fn = Fn-2 + Fn-1, n >= 0 F0 = 0, F1 = 1, F2 = 1 Your...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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
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,...
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...

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.