title: Efficient convergence check
body: <p>I have a grid with thousands of double precision reals.</p>
  
  <p>It's iterating through, and I need it to stop when it's reached convergence to 3 decimal places.</p>
  
  <p>The target is to have it run as fast as possible, but needs to give the same result every (to 3 dp) every time.</p>
  
  <p>At the minute I'm doing something like this</p>
  
  <pre><code>REAL(KIND=DP) :: TOL = 0.001_DP
  
  DO WHILE(.NOT. CONVERGED)
      CONVERGED = .TRUE.
      DO I = 1, NUM_POINTS
          NEW POTENTIAL = !blah blah blah
          IF (CONVERGED) THEN
              IF (NEW_POTENTIAL &lt; OLD_POTENTIAL - TOL .OR. NEW_POTENTIAL &gt; OLD_POTENTIAL + TOL) THEN
                  CONVERGED = .FALSE.
              END IF
          END IF
          OLD_POTENTIAL = NEW POTENTIAL
      END DO  
  END DO
  </code></pre>
  
  <p>I'm thinking that many IF statements can't be too great for performance. I thought about checking for convergence at the end; finding the average value (summing the whole grid, divide by num_points), and checking if that has converged in the same way as above, but I'm not convinced this will always be accurate.</p>
  
  <p>What is the best way of doing this?</p>