What is the rationale for all comparisons returning false for IEEE754 NaN values?

后端 未结 11 2130
耶瑟儿~
耶瑟儿~ 2020-11-21 07:04

Why do comparisons of NaN values behave differently from all other values? That is, all comparisons with the operators ==, <=, >=, <, > where one or both values is NaN

11条回答
  •  春和景丽
    2020-11-21 07:29

    It only looks peculiar because most programming environments that allow NaNs do not also allow 3-valued logic. If you throw 3-valued logic into the mix, it becomes consistent:

    • (2.7 == 2.7) = true
    • (2.7 == 2.6) = false
    • (2.7 == NaN) = unknown
    • (NaN == NaN) = unknown

    Even .NET does not provide a bool? operator==(double v1, double v2) operator, so you are still stuck with the silly (NaN == NaN) = false result.

提交回复
热议问题