Added a set of conditions to prevent infinite loops due to numerical inaccuracies when
working with double precision.