-2

i have code like this, i try to get number from henon formula

public class henon 
{
    public static void main(String[] args) 
    {         
        double a = 0.3;
        double b = 1.4;    
        double k[] = new double[1026];
        k[0] = 0.01;
        k[1] = 0.02;              

        for(int i = 0; i < 1024; i++)
        {                       
            k[i+2] = 1 - (a * Math.pow(k[i+1], 2) + b * k[i]);          
            System.out.println( "nilai ke" + i +" adalah " + k[i]);
        }
    }               
}

how i can get infinity valuew in 24, 25 ...how to solve it?

user1209555
  • 47
  • 2
  • 5

2 Answers2

0

You're getting "infinite" values because your calculations are going beyond the range of a double data type.

If you need more range, you'll have to resort to something like a bignum library, such as BigDecimal.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • @Marko, you're right of course, I mistakenly thought this was C hence my reference to MPIR. Changed to BigDecimal since it's more relevant to Java. – paxdiablo Apr 24 '12 at 12:42
  • Your proposal wasn't all wrong because people do crave the C library for speed. BigDecimal is crap in that category, but it's much easier to start with. – Marko Topolnik Apr 24 '12 at 12:45
  • can you give me donwload link for GMP – user1209555 Apr 24 '12 at 12:49
  • MPIR is at http://www.mpir.org/ and GMP is at http://gmplib.org/ - I've always gotten a better response to problems and suggestions from MPIR (a fork of GMP) so that's my preference. – paxdiablo Apr 24 '12 at 13:38
0

BigDecimal is better for your requirement.

double and float are not useful for accurate results as Joshua bloch said in Effective Java.

BigDecimal k[] = new BigDecimal[1026];
        k[0] = new BigDecimal(0.01);
        k[1] = new BigDecimal(0.02);  
        for (int i = 2; i < k.length; i++) {
            k[i]=new BigDecimal(0.0);
        }

if you want double value of Bigdecimal ,you can use doubleValue() method.

Balaswamy Vaddeman
  • 8,360
  • 3
  • 30
  • 40