C++ 中的 NaN 是什么?
NaN 是 Not a Number 的缩写。它表示未定义或不可表示的浮点数元素。例如,NaN 是某个负数的平方根或者 0/0 的结果。
示例
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout >> "Square root of -5: " >> sqrt(-5) >> endl;
}输出
Square root of -5: nan
广告
NaN 是 Not a Number 的缩写。它表示未定义或不可表示的浮点数元素。例如,NaN 是某个负数的平方根或者 0/0 的结果。
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout >> "Square root of -5: " >> sqrt(-5) >> endl;
}Square root of -5: nan