## java提高精度问题（斐波那契数列）

```public class fibonacci2 {
public static void main(String args[])
{
double n = 1000;
double nums = fibonacci(n);
double nums_1 = fibonacci(n-1);
double nums_2 = nums_1/nums;
System.out.println(nums_2);
}
static double fibonacci(double n)
{
double a=1,b=1,c;
if(n==0) return 0;
if(n==1) return 1;
if(n==2) return 1;
else
{
for(int i=3;i<=n;i++)
{
c = a+b;
a = b;
b = c;
}
}
return (double) b;
}
} ```

0
```import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;

public class Fib {
public static void main(String[] args) {

int n = 1000;
BigDecimal d1 = new BigDecimal(fibonacci(n));
BigDecimal d2 = new BigDecimal(fibonacci(n - 1));
System.out.println(d2.divide(d1, new MathContext(100)));
}

static BigInteger fibonacci(int n) {
BigInteger a = BigInteger.ONE;
BigInteger b = BigInteger.ONE;
BigInteger c = BigInteger.ONE;

if (n < 3)
return BigInteger.ONE;

for (int i = 3; i <= n; i++) {
a = b;
b = c;
}
return c;
}

}```

Output: 0.6180339887498948482045868343656381177203091798057628621354486227
052604628189024497072072041893911375

0

0

BigDecimal nums_2 = nums_1.divide(nums,new MathContext(70));

0

0
BigDecimal
0

0