kahan_sum_real_1d Function

private pure function kahan_sum_real_1d(array) result(the_result)

Simple Kahan summation designed to limit the error arising from summing a large collection of numbers by taking care to keep track of the required compensation.

Arguments

Type IntentOptional Attributes Name
real, intent(in), dimension(:) :: array

Return Value real


Contents

Source Code


Source Code

  pure real function kahan_sum_real_1d(array) result(the_result)
    implicit none
    real, dimension(:), intent(in) :: array
    integer :: i
    real :: compensation, compensated_element, current_total
    the_result = 0.0
    compensation = 0.0
    do i = 1, size(array)
       compensated_element = array(i) - compensation
       current_total = the_result + compensated_element
       compensation = (current_total - the_result) - compensated_element
       the_result = current_total
    end do
  end function kahan_sum_real_1d