Currently I am writing a program to calculate mathematical constants. However, I am having trouble with rounding BigDecimals. Once it hits a non-terminating decimal (1.6) it throws an error. I think the problem is rounding line, but it sets it too the right number of decimal places, but doesn't round it. I am new to BigDecimals so I probably forgot something. The code and output is shown below. Thanks!
This my code:
package goldenratio;
import java.math.*;
public class GoldenRatio {
static BigDecimal now;
static BigDecimal before;
static BigDecimal phi;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
for(int i=1;i<=100;i++)
{
if(i==1)
{
now=BigDecimal.valueOf(1);
before=BigDecimal.valueOf(1);
phi=BigDecimal.valueOf(1);
}else
{
phi=(now.divide(before));
phi = phi.setScale(5, RoundingMode.HALF_UP);
BigDecimal oldBefore = before;
before=now;
now=now.add(oldBefore);
}
System.out.println(phi);
}
}
This is my output:
run:
1.00000
2.00000
1.50000
Exception in thread "main" java.lang.ArithmeticException: Non-terminating
decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1690)
at goldenratio.GoldenRatio.main(GoldenRatio.java:30)