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
else
arr(i) = i/1.2
endif
end do
sum = 0
do i=1,100
if (.not.ieee_is_nan(arr(i))) then
sum = sum+arr(i)
endif
enddo
print *, sum
end program sumnan
% pgf90 sumnan.f90 ; a.out
NaN
3750.000
Hope this helps,
Mat