static bool relstop(double vold, double vnew, double reltol, double abstol)
{
if (Double.IsInfinity(vold))
return false;
if (Double.IsNaN(vold) || Double.IsNaN(vnew))
return true;
return (Math.Abs(vnew - vold) < abstol
|| Math.Abs(vnew - vold) < reltol * (Math.Abs(vnew) + Math.Abs(vold)) * 0.5
|| (reltol > 0 && vnew == vold)); // catch vnew == vold == 0
}