我想知道以下类型之间的区别是什么nan
。除了(与 just 相比,NAN_macro
其评估结果为)的视觉差异外,它们的行为似乎都相同(根据下面的示例脚本)。-nan(ind)
nan
我看了一些其他的答案,例如安静的 NaN 和信令 NaN 有什么区别?. 但我仍然不太明白为什么NAN
is -nan(ind)
while std::numeric_limits<double>::quiet_NaN()
is nan
,或者为什么我们有std::nan("")
,std::nan("1")
如果在一天结束时它们似乎都是同一个概念。
任何解释或澄清链接都会很棒。
#include <cmath>
#include <limits>
int main()
{
using num_lim = std::numeric_limits<double>;
const double NAN_macro = static_cast<double>(NAN); // -nan(ind)
const double NAN_quiet = num_lim::quiet_NaN(); // nan
const double NAN_sig = num_lim::signaling_NaN(); // nan
const double NAN_str = std::nan(""); // nan
const double NAN_str1 = std::nan("1"); // nan
const double NAN_strLong = std::nan("some string"); // nan
const bool isnan_macro = std::isnan(NAN_macro); // true
const bool isnan_quiet = std::isnan(NAN_quiet); // true
const bool isnan_sig = std::isnan(NAN_sig); // true
const bool isnan_str = std::isnan(NAN_str); // true
const bool isnan_str1 = std::isnan(NAN_str1); // true
const bool isnan_strLong = std::isnan(NAN_strLong); // true
const bool not_equal_macro = (NAN_macro != NAN_macro); // true
const bool not_equal_quiet = (NAN_quiet != NAN_quiet); // true
const bool not_equal_sig = (NAN_sig != NAN_sig); // true
const bool not_equal_str = (NAN_str != NAN_str); // true
const bool not_equal_str1 = (NAN_str1 != NAN_str1); // true
const bool not_equal_strLong = (NAN_strLong != NAN_strLong); // true
const double sum_macro = 123.456 + NAN_macro; // -nan(ind)
const double sum_quiet = 123.456 + NAN_quiet; // nan
const double sum_sig = 123.456 + NAN_sig; // nan
const double sum_str = 123.456 + NAN_str; // nan
const double sum_str1 = 123.456 + NAN_str1; // nan
const double sum_strLong = 123.456 + NAN_strLong; // nan
}