在C++中檢查double(或float)是否是NaN
為了檢查浮點數或雙精度數是否是C++中的NaN(非數字),我們可以使用isnan()函式。isnan()函式存在於cmath庫中。此函式在C++版本11中引入。因此,從C++11開始,我們可以使用此函式。
示例
#include <cmath>
#include <iostream>
using namespace std;
main() {
if(isnan(sqrt(30))) { //square root of 30 is a floating point number
cout << "Square root of 30 is not a number" <<endl;
} else {
cout << "Square root of 30 is a number" <<endl;
}
if(isnan(sqrt(-30))) { //square root of -30 is an imaginary number
cout << "Square root of -30 is not a number" <<endl;
} else {
cout << "Square root of -30 is a number" <<endl;
}
}輸出
Square root of 30 is a number Square root of -30 is not a number
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP