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