Để kiểm tra xem một dấu phẩy động hoặc số kép có phải là NaN (Không phải là số) trong C ++ hay không, chúng ta có thể sử dụng hàm isnan (). Hàm isnan () có trong thư viện cmath. Hàm này được giới thiệu trong C ++ phiên bản 11. Vì vậy, từ C ++ 11 tiếp theo, chúng ta có thể sử dụng hàm này.
Ví dụ
#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;
}
} Đầu ra
Square root of 30 is a number Square root of -30 is not a number