I'm trying to find the biggest fibonacci number a double variable can handle, my loop looks like this:
[code=cpp]
double num1 = 1;
double num2 = 2;
double fib = 0;
{
while ( fib >= 0 )
{
fib = num1 + num2;
num1 = num2;
num2 = fib;
}
}
[/code]
From my understanding, after the variable hits its maximum positive value, it will revert to the negatives, thus the >= 0 condition for the loop. This works just fine with the int variable, however, for some reason it just won't terminate using the double variable. I tried putting a printf statement into the loop just to check the values, and for some reason, it seems to change from positives to negatives, and after hitting the max negatives, it reverts to 0 causing the loop to go indefinitely. I'm not sure why the loop will not terminate after fib hits the negatives. Anyone know what's causing this?
[code=cpp]
double num1 = 1;
double num2 = 2;
double fib = 0;
{
while ( fib >= 0 )
{
fib = num1 + num2;
num1 = num2;
num2 = fib;
}
}
[/code]
From my understanding, after the variable hits its maximum positive value, it will revert to the negatives, thus the >= 0 condition for the loop. This works just fine with the int variable, however, for some reason it just won't terminate using the double variable. I tried putting a printf statement into the loop just to check the values, and for some reason, it seems to change from positives to negatives, and after hitting the max negatives, it reverts to 0 causing the loop to go indefinitely. I'm not sure why the loop will not terminate after fib hits the negatives. Anyone know what's causing this?
Comment