Hello my problem is that I tried to write a program that sum up the Fibonacci number. It is a iterative solution. The problem is that the highest number the 47 Fibonacci number is. And I don't know why.
Here is my source code:
int n = 47;
if(n==1||n==2)
{
digitalWrite(ledr,LOW);
digitalWrite(ledg,LOW);
delay(2000);
}
if(n>=3)
{
float fib[n];
fib[0] = 1;
fib[1] = 1;
for(int i = 2; i<n; i++)
{
fib*=fib[i-2]+fib[i-1];*
_ Serial.println(fib*);*_ Thank you
Does your code compile without errors? The section of it you've copied and pasted here is incomplete so certainly wouldn't. Can you post the whole of your program, and use the [ code ] [ /code ] tags around it (it's the button with the # on it).
Why are you using a float? it is integer math so would be faster to use an 'unsigned long', plus would keep its accuracy. A float may be able to hold a bigger number, but it can't hold the same precision, and wont show you the correct value from the 41st onward (7dp using standard form give a maximum of 99,999,999)
Also, this line:
fib=fib[i-2]+fib[i-1];
should be:
fib[i ] = fib[i-2] + fib[i-1];
otherwise the value isn't saved anywhere. EDIT: I think you may have used that but because you didn't put it in `` markers, it took the [i ] to mean italic font.
Just as a side note, the seed numbers for the Fibonacci series are [0,1], not [1,1].
0,1,1,2,3,5,8,...
Beyond that (n>47), the number becomes larger than 32bits, so cant be stored in an unsigned long, and certainly can't be stored in a float with full precision.
If you want larger than this, you would need to use a 64bit variable. I think arduino supports "unsigned long long"? But Serial.print() doesnt.
Mammoth code. Can go up to 94. Beyond that you overflow even an unsigned long long (64bit). Now you'll be asking for an "unsigned long long long" (128bit) type, but they dont exist
The problem is that the function 'Serial.println()' doesn't work, because the highest number that 'println()' can describe is 2³².
So, how does that translate to "Serial.println() doesn't work"? It works as advertised. That it does something unexpected when you misuse it doesn't mean that it doesn't work.
I think what he means is println(unsigned long long) doesn't exist. Neither does %ull is sprintf. In order to go larger you have to do two or even three print()'s in order to convey the numbers.
I am Truly Curious as to why a Fib series is of any use to the OP beyond an Extensive math lesson... I found the whole thread truly fascinating from all the posters opinions and facts about math, the real value of floating point numbers and the limitations imposed by using an 8 Bit Chip as it is heroically being used here. My side thought train was 'interesting' to observe... A great lesson well worth reading.
1niklas1:
Hello my problem is that I tried to write a program that sum up the Fibonacci number. It is a iterative solution. The problem is that the highest number the 47 Fibonacci number is. And I don't know why.
Why are you doing this on an Arduino? For fun? Anyway, this is how to do it...
#include "BigNumber.h"
// function to display a big number and free it afterwards
void printBignum (BigNumber & n)
{
char * s = n.toString ();
Serial.println (s);
free (s);
} // end of printBignum
void setup()
{
Serial.begin(115200);
BigNumber::begin (); // initialize library
} // end of setup
void loop()
{
int n = 2;
BigNumber a = 0;
BigNumber b = 1;
BigNumber result;
while (n <= 1000)
{
result = a + b;
Serial.print (n++);
Serial.print (" = ");
printBignum (result);
a = b;
b = result;
} // end of while
} // end of loop
This is an 8-bit processor. Give it a bit of a break. To output long longs would make programs somewhat larger.
No larger than doing it manually.
The issue is that the println() method is overloaded. It has version that print ints, bytes, chars, floats, doubles, etc. It does not have a version that prints long longs.