java - problem with a data structure

I created a code in my spare time that prints out the entire fibonacci sequence. But, at the ninety-third number, it turns negative. I think that this is because I'm declaring the number as a long. Is there any other primative data structure that can hold REALLY large numbers?

Comments

  • airbornflghtairbornflght Houston, TX Icrontian
    edited June 2006
    I created a code in my spare time that prints out the entire fibonacci sequence. But, at the ninety-third number, it turns negative. I think that this is because I'm declaring the number as a long. Is there any other primative data structure that can hold REALLY large numbers?

    *Breaks out Java Book*

    you could use double, it is good to ~1.79xxx+308. You could use that and truncate the decimal early on if you do not need/want it.
  • Bad_KarmaBad_Karma The Great White North
    edited June 2006
    There are 2 other primitive type.

    float it's approximate range is + or - 3.4 x 10^38
    float's precision is at least 6 decimal digits.

    double it's approximate range is + or - 1.8 x 10^308
    double's precision is at least 15 decimal digits.

    Those should help you out with what you need.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited June 2006
    yeh, in the past 4 months, I have learned 3 languages, and I'm just starting java, but I thought javas types were like c++'s so I didnt want to tell him a non existent data type.
  • edited June 2006
    Those are all great ways to solve my problem, but I'm trying to do it so that it isn't in scientific notation. Does anybody know how to do it that way?
  • edited June 2006
    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

    list of all java data types.

    You could output as a string. or write to a text file and grab it from there and display that way.

    btw above solutions would not make it in sci notation automatically
  • airbornflghtairbornflght Houston, TX Icrontian
    edited June 2006
    yeh, you could even move it all to a string and format it.

    are you using a gui, or just the console?
  • edited June 2006
    I'm just using the console in Textpad
  • edited June 2006
    you should use the newer BigDecimal class.

    [PHP]
    import java.math.*;

    public class TestBig {

    private static final int Fractional_Digits = 50; //
    private static final int N_Terms = 200; //

    static final BigDecimal golden_ratio = new BigDecimal("1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391138");

    public static void main(String[] args)
    {
    BigInteger lo = BigInteger.valueOf(1L);
    BigInteger hi = BigInteger.valueOf(1L);
    int n = 1;

    System.out.print(n);
    System.out.print("\t");
    System.out.println(lo);

    while (n < N_Terms)
    {
    n++;
    System.out.print(n);
    System.out.print("\t");
    System.out.print(hi);
    System.out.print("\t");
    BigDecimal ratio = new BigDecimal(hi);
    BigDecimal den = new BigDecimal(lo);
    ratio = ratio.divide(den, Fractional_Digits, ratio.ROUND_HALF_DOWN);
    System.out.print(ratio);
    System.out.print("\t");
    System.out.println(ratio.subtract(golden_ratio).setScale(Fractional_Digits,
    ratio.ROUND_HALF_DOWN));
    hi = lo.add(hi);
    lo = hi.subtract(lo);
    }
    }
    }
    [/PHP]


    --edited

    sorry, posted the wrong sample earlier. I realize this prints sceintific notation and i forgot you didn't want that. hmm. check the API, perhaps there's a different format or you could simple use a pad function.
  • edited June 2006
    or use BigInteger

    [PHP]
    import java.math.BigInteger;

    public class TestBig {

    public static void main(String[] args) {

    BigInteger low = BigInteger.ONE;
    BigInteger high = BigInteger.ONE;
    for (int i = 1; i <= 200; i++) {
    System.out.println(low);
    BigInteger temp = high;
    high = high.add(low);
    low = temp;
    }

    }

    }
    [/PHP]
  • edited June 2006
    you arent going to get far using primitives simply because of memory allocation.. java supports 64 bits for doubles (and ints i think) and 32 bits for floats, so naturally you're gonna run into storage issues pretty quickly (as you've found). so you'll have to use an Object of some sort that is designed for large numbers and precision.
Sign In or Register to comment.