C - Loop using double variable won't terminate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jombloboy
    New Member
    • Mar 2008
    • 1

    C - Loop using double variable won't terminate

    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?
    Last edited by sicarie; Mar 3 '08, 02:53 PM. Reason: Code tags are [code=cpp] and after your code [/code]. Please use them. They want to be used. They like to be used.
  • hsn
    New Member
    • Sep 2007
    • 237

    #2
    it is not a rule that a variable will become negative if it arrived to its limit
    it will become a garbadge value. negative or positive

    Comment

    Working...