I have a method that takes n and returns nth Fibonacci number. Inside the method implementation I use BigDecimal
to get the nth Fibonacci number then I use method toBigInteger()
to get the number as a BigInteger
object and that's surely because I am working with huge numbers in my application.
I keep getting correct results until I pass 1475 as an argument for my method. I get NumberFormatException: Infinite or NaN
in this case without any clear reason for me.
Could you please explain me why am I getting this exception?
Here's my method:
BigInteger getFib(int n){
double phi = (1 + Math.sqrt(5))/2;
double squareRoot = (Math.sqrt(5)) + (1/2);
BigDecimal bd = new BigDecimal(Math.floor(Math.pow(phi, n)/(squareRoot)));
return bd.toBigInteger();
}