update pi in each iteration. threshold set to be 0.01
handle small number when calculating next pi