Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Fibonacci and Java

  • 02-10-2009 1:31pm
    #1
    Registered Users, Registered Users 2 Posts: 439 ✭✭


    Hi Guys,
    I'm looking for some help with a very simple problem, well simple to others not me :)
    I have the below code for Java and when it runs it generates minus numbers after a certain value. How can i stop the minus numbers, sorry but i've just starting in this field!

    public class Fibonacci
    {
    public static void main (String[] args)
    {
    int current , prev = 1, prevprev = 0;
    for( int i = 0; i < 50; i++)
    {
    current= prev+prevprev; // Next number is sum of the previous two
    System.out.print( current + " "); // print out the value of current and a space
    prevprev=prev; // first value now becomes second value
    prev=current; // makes first value takes on curent value
    }
    System.out.println ();
    }
    }

    Oh and thanks for any help


Comments

  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Welcome to the wondeful world of signed and unsigned data types.

    When data is stored as a 32-bit integer, the maximum number which can be stored in this data type is 4,294,967,295. However in this case, we are making no distinction between positive and negative. In order to be able to store negative integers, we need to steal one bit from the 32 which tells us whether this integer is positive or negative.

    This means that we only have 31 bits with which to represent our number, so the maximum we can store is 2,147,483,647 (and the last bit tells us if this is positive or negative).

    Data types where one bit represents the positive or negative aspect are known as signed, and those which can only represent positive numbers are unsigned

    By default, all ints in Java are signed, meaning that the largest positive value you can store in an int is 2,147,483,647.

    The 46th number in the Fibonacci sequence is 1,836,311,903. This is fine, and it fits in the int data type. However, the next number (47) is 2,971,215,073 - this won't fit in a signed int. Rather than dumping you out of bounds, Java performs the addition anyway. This causes the integer to "roll over" into the negative side (basically the bit which indicates the sign of the integer has now been "set", causing the number to become negative).

    Since you're getting Fibonacci numbers up to 49, even a standard unsigned integer can't accomodate the size of the last Integer. Instead you can use the data type long int, which is a 64-bit integer, which can accomodate positive/negative integers up to 9,223,372,036,854,775,807. Or if unsigned, positive integers up to 18,446,744,073,709,551,615.

    This is actually a good lesson about data and memory management and thinking about what kind of variables you are using to store your data.


  • Registered Users, Registered Users 2 Posts: 1,636 ✭✭✭henbane


    Your last two numbers are 1134903170 and 1836311903 = 2971215073

    So -2,147,483,648+(2971215073-2,147,483,647) = -1323752223


    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
    int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.

    Or what Seamus with far more clarity


  • Registered Users, Registered Users 2 Posts: 439 ✭✭zep


    Hi Guys,
    Thanks for the help with this, much appreciated.
    Zep:D


Advertisement