    PROGRAM c06bafe

!      C06BAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : c06baf, nag_wp, x01aaf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: lwork = 16, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: abserr, ans, error, pi, r, result,   &
                                          seqn, sig
       INTEGER                         :: i, ifail, ncall
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: work(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          real
!      .. Executable Statements ..
       WRITE (nout,*) 'C06BAF Example Program Results'
       WRITE (nout,*)

       ALLOCATE (work(lwork))

       pi = x01aaf(pi)
       ans = pi**2/12.0_nag_wp
       ncall = 0
       sig = 1.0_nag_wp
       seqn = 0.0_nag_wp
       WRITE (nout,99999) 'Estimated       Actual'
       WRITE (nout,99998) 'I       SEQN       RESULT', &
          'abs error        error'
       WRITE (nout,*)
       DO i = 1, 10
          r = real(i,kind=nag_wp)
          seqn = seqn + sig/(r**2)

!         ifail: behaviour on error exit   
!                =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
          ifail = 0
          CALL c06baf(seqn,ncall,result,abserr,work,lwork,ifail)

          error = result - ans
          sig = -sig
          IF (i<=3) THEN
!            First three calls of C06BAF return no error estimate
             WRITE (nout,99997) i, seqn, result, error
          ELSE
             WRITE (nout,99996) i, seqn, result, abserr, error
          END IF
       END DO

99999  FORMAT (36X,A)
99998  FORMAT (3X,A25,8X,A)
99997  FORMAT (1X,I4,2F12.4,3X,10X,'-   ',E14.2)
99996  FORMAT (1X,I4,2F12.4,3X,2E14.2)
    END PROGRAM c06bafe
