You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[C++] Should NaN comparison return false or NaN/NA? #29038
In working on ARROW-12964 we ran into some corner behaviors with NaN that don't match our (and R's) expectations. It appears that (any?) comparison with NaN results in false:
>Scalar$create(NaN) >5Scalarfalse
though at least in R this would result in an NA value:
Eduardo Ponce / @edponce: EqualOptions has a nans_equal member to control the behavior of comparisons between NaNs. I assume this was included to satisfy the behavior of different tools.
IEEE 754 states that all logical operations with a NaN should always result in false, except for NaN != x. For a discussion on this topic, refer to the first answer of this stack overflow question and wikipedia NaN page.
My opinion for Arrow is that all logical comparisons with a NaN value should return false except for:
NaN != x
NaN == NaN
this would eliminate the use of the nans_equal option.
Eduardo Ponce / @edponce:
R uses NA to represent a missing value, equivalent to having a NULL bit set in Arrow.
Coercing NaN to logical or integer type gives an NA of the appropriate type, but coercion to character gives the string "NaN". NaN values are incomparable so tests of equality or collation involving NaN will result in NA.
w.r.t. R's behavior for
>NaN>5
[1] NA
it does not seems to conform strictly to IEEE 754. My speculation is that internally the result is NaN but when coerced as a logical type becomes NA.
EqualOptions has a nans_equal member to control the behavior of comparisons between NaNs. I assume this was included to satisfy the behavior of different tools.
Note this is for a different operation: for a "full array, data-structure equality" (arr1.equals(arr2) = True or False), and the option is added here mainly for convenience (as often you want to regard NaNs in the same location as equal when it comes to full array equality, and writing this out manually is rather verbose, i.e. something like ((a == b) | (a.isnan() & b.isnan()).all()).
We don't have such an option for element-wise comparisons (which is the type of equality/comparison that is discussed in this issue)
This issue has been marked as stale because it has had no activity in the past 365 days. Please remove the stale label or comment below, or this issue will be closed in 14 days. If this improvement is still desired but has no current owner, please add the 'Status: needs champion' label.
Activity
asfimport commentedon Jul 17, 2021
Eduardo Ponce / @edponce:
EqualOptions has a nans_equal member to control the behavior of comparisons between NaNs. I assume this was included to satisfy the behavior of different tools.
IEEE 754 states that all logical operations with a NaN should always result in false, except for NaN != x. For a discussion on this topic, refer to the first answer of this stack overflow question and wikipedia NaN page.
My opinion for Arrow is that all logical comparisons with a NaN value should return false except for:
NaN != x
NaN == NaN
this would eliminate the use of the nans_equal option.
asfimport commentedon Jul 17, 2021
Eduardo Ponce / @edponce:
R uses NA to represent a missing value, equivalent to having a NULL bit set in Arrow.
Coercing NaN to logical or integer type gives an NA of the appropriate type, but coercion to character gives the string "NaN". NaN values are incomparable so tests of equality or collation involving NaN will result in NA.
w.r.t. R's behavior for
it does not seems to conform strictly to IEEE 754. My speculation is that internally the result is NaN but when coerced as a logical type becomes NA.
asfimport commentedon Jul 17, 2021
Joris Van den Bossche / @jorisvandenbossche:
Note this is for a different operation: for a "full array, data-structure equality" (
arr1.equals(arr2) = True or False), and the option is added here mainly for convenience (as often you want to regard NaNs in the same location as equal when it comes to full array equality, and writing this out manually is rather verbose, i.e. something like((a == b) | (a.isnan() & b.isnan()).all()).We don't have such an option for element-wise comparisons (which is the type of equality/comparison that is discussed in this issue)
github-actions commentedon Feb 4, 2026
This issue has been marked as stale because it has had no activity in the past 365 days. Please remove the stale label or comment below, or this issue will be closed in 14 days. If this improvement is still desired but has no current owner, please add the 'Status: needs champion' label.