#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Why is this array of Fibonacci Numbers returning negative values?

edited October 2017

Hello! I am new to this forum and I just created this account to ask this question: Why is the code below returning negative values in the log of the array?

``````float AmountOfFibonacciNumbers = 47;
int[] FibonacciNumbers = new int[ceil(AmountOfFibonacciNumbers / 2) * 2];
int j = 1;
int k = 1;

void setup(){
AmountOfFibonacciNumbers = ceil(AmountOfFibonacciNumbers / 2) * 2;
for (int i = 0; i < AmountOfFibonacciNumbers; i = i + 2){
FibonacciNumbers[i] = j;
FibonacciNumbers[i + 1] = k;
j = j + k;
k = k + j;
}
printArray(FibonacciNumbers);
}``````

Here is what it returns:

``` 1
 1
 2
 3
 5
 8
 13
 21
 34
 55
 89
 144
 233
 377
 610
 987
 1597
 2584
 4181
 6765
 10946
 17711
 28657
 46368
 75025
 121393
 196418
 317811
 514229
 832040
 1346269
 2178309
 3524578
 5702887
 9227465
 14930352
 24157817
 39088169
 63245986
 102334155
 165580141
 267914296
 433494437
 701408733
 1134903170
 1836311903
 -1323752223
 512559680```

If you extend the array further, even more negative numbers pop up. What is going on here?

Any help would be appreciated, thanks! -Gav

Tagged:

## Answers

• i think you leave the allowed range for int

try double or so in line 2

• Hello, I changed the code a little bit and I'm coming across a similar problem that appears around the same part of the array:

``````int NumberOfFibonacciNumbers = 49;
int[] FibonacciNumbers = new int[NumberOfFibonacciNumbers];
float j = 1;
float k = 1;

void setup() {
for (int i = 0; i < NumberOfFibonacciNumbers; i = i + 2) {
FibonacciNumbers[i] = int(j);
if(i < NumberOfFibonacciNumbers - 1) {
FibonacciNumbers[i + 1] = int(k);
} else {
if (NumberOfFibonacciNumbers % 2 == 0) {
FibonacciNumbers[i + 1] = int(k);
}
}
j = j + k;
k = k + j;
}
printArray(FibonacciNumbers);
}``````

Here is what it returns:

`````` 1
 1
 2
 3
 5
 8
 13
 21
 34
 55
 89
 144
 233
 377
 610
 987
 1597
 2584
 4181
 6765
 10946
 17711
 28657
 46368
 75025
 121393
 196418
 317811
 514229
 832040
 1346269
 2178309
 3524578
 5702887
 9227465
 14930352
 24157816
 39088168
 63245984
 102334152
 165580128
 267914272
 433494400
 701408640
 1134903040
 1836311680
 2147483647
 2147483647
 2147483647``````
• Answer ✓

It has to do with the way the computer handles numbers. When the number is gets too high it rolls over to its lowest value.

In this code it shows what happens when the number rolls over.

```int x = 2147483647; println(x); println(x + 1);```

• Thanks for answering my question! I tried the code you provided and it logged the following:

``````2147483647
-2147483648``````

This bug seems like it could cause problems with all sorts of applications. Do you know what causes this to happen? It's sad that such a powerful API can be hampered by a simple problem like this. If Processing is just a beginner API that isn't designed to handle heavy loads, then I assume I should be learning about a more serious API that is capable of handling numbers larger than 2147483647. What should I do about this?

Thanks!
-Gav

• Answer ✓

It has nothing to do with how processing handles numbers, but it has everything to do with how computers handle numbers. A different datatype (such as long) may give you better results.

• Ah, I see. Each integer exists as part of its own array with a max of 2147483647, and minimum of -2147483648. If I define the numbers as 'long' and convert them to 'int', then I can extend outside of that range.

Edit: Converting back into integers defeats the purpose of using long numbers.

Sign In or Register to comment.