It is better to set the tolerance, 0.0001, here to a constant, e,g const double tol_z = 0.0001, or a large value.