C++ Standard Library C++ STL Library

C++ <cmath> - isnan() Function



The C++ <cmath> isnan() function returns true if the given argument is a NaN (Not-A-Number) value, else returns false. A NaN value is used to identify undefined or non-representable values for floating-point elements, for example - the result of 0/0.

Syntax

bool isnan (float x);
bool isnan (double x);
bool isnan (long double x);             

Parameters

x Specify a floating-point value.

Return Value

Returns true (non-zero value) if the argument is an infinity value, else returns false (zero value).

Example:

The example below shows the usage of isnan() function.

#include <iostream>
#include <cmath>

using namespace std;
 
int main (){

  cout<<boolalpha;
  cout<<"isnan(10.5): "<<isnan(10.5)<<"\n";
  cout<<"isnan(1.0/0.0): "<<isnan(1.0/0.0)<<"\n";
  cout<<"isnan(0.0/0.0): "<<isnan(0.0/0.0)<<"\n";
  cout<<"isnan(sqrt(-1.0)): "<<isnan(sqrt(-1.0))<<"\n";
  return 0;
}

The output of the above code will be:

isnan(10.5): false
isnan(1.0/0.0): false
isnan(0.0/0.0): true
isnan(sqrt(-1.0)): true

❮ C++ <cmath> Library

5