F90, how to calculate sum of a vector with NaN values

Hi, I am reading a set of values from a file into a vector, and a few of them are NaN. I applied the intrinsic function ‘SUM’ to calculate sum of these values but due to NaN the sum of the values are not calculated. I want to calculate sum of the values excluding NaNs, any help is greatly appreciated.
Thanks much,

Hi satishr,

One method would be to use the ieee_is_nan intrinsic to test each value before it’s summed.

% cat sumnan.f90 

program sumnan

   use ieee_arithmetic

   real, dimension(100) :: arr
   real*4 a,sum
   integer i
   a = sqrt(-1.0)
   print *, a

   do i=1,100
      if (mod(i,10).eq.0) then
        arr(i) = a 
        arr(i) = i/1.2
   end do
   sum = 0
   do i=1,100
      if (.not.ieee_is_nan(arr(i))) then
        sum = sum+arr(i)
   print *, sum        

end program sumnan
% pgf90 sumnan.f90 ; a.out

Hope this helps,