calculate sqrt safely