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>::RE T/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!!!! 6 4657
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>::RE T/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.
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>::RE T/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
OK, I replaced it, and it works:
long double phi=static_cast <long double>(FIB<11> ::RET)/static_cast<lon g
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!!!!
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<lon g 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<dou ble>(fib<11>::r esult)/fib<10>::result ;
translates into something like this:
double phi = 1.618181818; // maybe more precision
What did you expect?
Thomas
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>::RE T/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
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<lon g 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<dou ble>(fib<11>::r esult)/fib<10>::result ;
translates into something like this:
double phi = 1.618181818; // maybe more precision
What did you expect?
Thomas
A mov instruction. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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, and
when the numbers get too large it maxes out the int and I can't count
any higher. I am trying to use extremely large numbers, I would like
to use up to 10^50 or so. So my question is how do I do this? I'm just
learning the language and I...
|
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 of the code produced by various compilers.
===========================================================
Windows 2000 Professional Ver 5.0 Build 2195 Service Pack 2
Intel(R) Celeron(R) CPU 1.70 GHz
GNU time 1.7 (to get the real time used)
|
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 newsgroup there is an ongoing discussion about the famous
fibonacci sequences, and a fine program written by a poster got my
attention so just for fun I wrote one myself and put it on the web just
about two days ago. It can be found on the site
...
|
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;
std::cout << a/b << std::endl;
|
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 example if the user gives 7 terms of the series to be displayed,
then the display of the fibonacci series is 0, 1,1, 2, 3, 5, 8.
But the natural numbers not involved are 4, 6 and 7. That's what my teacher wants.
But I am struggling to write a...
| |
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
|
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 creates a character string containing
the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) =
F(1) = 1 -, separated by comma. n should be given as an argument to
the program. The recursive function should only take one parameter, n,
and...
|
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 creates a character string containing
the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) =
F(1) = 1 -, separated by comma. n should be given as an argument to
the program. The recursive function should only take one parameter, n,
and...
|
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:
|
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 program should prompt the user to enter a limit and indicate whether the last number in the sequence printed is either even or odd.
|
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...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
|
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...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |