IDL Q: How do I check for an NaN in an array, in a loop?

  • Thread starter Thread starter jenny.genders
  • Start date Start date
  • Tags Tags
    Array Loop
Click For Summary

Discussion Overview

The discussion revolves around checking for NaN (Not a Number) values in an array during a loop in IDL (Interactive Data Language). Participants are addressing a specific coding issue related to conditional calculations based on the presence of NaN values in a dataset.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a loop intended to perform calculations only when the elements in the array 'sfc_data' are not NaN, but reports that the calculations are occurring regardless of the NaN values.
  • Another participant references a method for checking NaN values in programming, stating that a float is considered NaN if it is not equal to itself (float != float).
  • A further comment emphasizes the importance of ensuring that compilers do not optimize out the NaN check, suggesting the use of the "volatile" qualifier in variable declarations.

Areas of Agreement / Disagreement

Participants express differing views on the implementation of NaN checks and the potential pitfalls of compiler optimizations, indicating that there is no consensus on the best approach to resolve the issue presented.

Contextual Notes

Participants do not fully resolve the issue of checking for NaN values, and there are assumptions regarding compiler behavior and the specifics of the IDL environment that remain unaddressed.

jenny.genders
Messages
1
Reaction score
0
So, I'm doing a loop through 612 rows of data, each row having 2592 numbers. If the number I'm looking at is NOT equal to NaN, I want to do a calculation using a different array. This way, if the element in sfc_data is not a number, the number in lat_matrix in that same column is ignored, and if the element in sfc_data IS a number, the matching number from lat_matrix is added to the weights array. However when I do the following loop, it's doing the calculation regardless of whether the number in the first array equals NaN.

for i = 0,611 do begin
for j = 0, 2591 do begin
if sfc_data[j,i] ne 'NaN' then weights = weights + lat_matrix[j,0]
endfor
endfor

The loop is doing the full number of calculations as if none of the numbers in sfc_data are NaN (there are definitely many NaN's in sfc_data). So how do I check if sfc_data[j,i] is equal to the string NaN and isn't actually a number?
 
Technology news on Phys.org
TylerH said:
In short, float != float, iff float is NaN.

But make sure that a not-very-clever compiler doesn't optimize out the test x != x, because it thinks the result will always be false :rolleyes:
 
Good point. Add the "volatile" qualifier to the variables declaration.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K