package tutorial.client;

import tutorial.compute.Task;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * @author HyunSub Shim
 * @since 1.0
 */
public class Pi implements Task<BigDecimal>, Serializable {

    private static final long serialVersionUID = 5969259572282270500L;

    private static final BigDecimal FOUR = BigDecimal.valueOf(4);

    private static final int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;

    private final int digits;

    public Pi(int digits) {
        this.digits = digits;
    }

    public BigDecimal execute() {
        System.out.println("executing pi");
        return computePi(digits);
    }

    public static BigDecimal computePi(int digits) {
        int scale = digits + 5;
        BigDecimal arctan1_5 = arctan(5, scale);
        BigDecimal arctan1_239 = arctan(239, scale);
        BigDecimal pi = arctan1_5.multiply(FOUR).subtract(arctan1_239).multiply(FOUR);
        return pi.setScale(digits, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal arctan(int inverseX, int scale) {
        BigDecimal result, numer, term;
        BigDecimal invX = BigDecimal.valueOf(inverseX);
        BigDecimal invX2 = BigDecimal.valueOf(inverseX * inverseX);
        numer = BigDecimal.ONE.divide(invX, scale, ROUNDING_MODE);
        result = numer;
        int i = 1;
        do {
            numer = numer.divide(invX2, scale, ROUNDING_MODE);
            int denom = 2 * i + 1;
            term = numer.divide(BigDecimal.valueOf(denom), scale, ROUNDING_MODE);
            if ((i % 2) != 0) {
                result = result.subtract(term);
            } else {
                result = result.add(term);
            }
            i++;
        } while (term.compareTo(BigDecimal.ZERO) != 0);
        return result;
    }
}
