Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/math/nan"

From cppreference.com
< cpp‎ | numeric‎ | math
(std::nan(nullptr) is not defined by the standard and in fact produces a crash on linux gcc 6.3.0)
(follow the grammar of n-char-sequence in N1570 §7.22.1.3)
Line 16: Line 16:
 
Converts the implementation-defined character string {{tt|arg}} into the corresponding quiet NaN value, as if by calling {{lc|std::strtod}}, {{lc|std::strtof}}, or {{lc|std::strtold}}, respectively, as follows:
 
Converts the implementation-defined character string {{tt|arg}} into the corresponding quiet NaN value, as if by calling {{lc|std::strtod}}, {{lc|std::strtof}}, or {{lc|std::strtold}}, respectively, as follows:
  
The call {{c|std::nan("string")}} is equivalent to the call {{c|std::strtod("NAN(string)", (char**)nullptr);}}.
+
The call {{c|std::nan("n-char-sequence")}}, where {{c|n-char-sequence}} is a sequence of digits, Latin letters, and underscores, is equivalent to the call {{c|std::strtod("NAN(n-char-sequence)", (char**)nullptr);}}.
  
 
The call {{c|std::nan("")}} is equivalent to the call {{c|std::strtod("NAN()", (char**)nullptr);}}.
 
The call {{c|std::nan("")}} is equivalent to the call {{c|std::strtod("NAN()", (char**)nullptr);}}.
 +
 +
The call {{c|std::nan("string")}}, where {{c|string}} is neither an n-char-sequence nor an empty string, is equivalent to the call {{c|std::strtod("NAN", (char**)nullptr);}}.
  
 
===Parameters===
 
===Parameters===

Revision as of 03:25, 19 May 2017

 
 
 
 
Defined in header <cmath>
float       nanf( const char* arg );
(since C++11)
double      nan( const char* arg );
(since C++11)
long double nanl( const char* arg );
(since C++11)

Converts the implementation-defined character string arg into the corresponding quiet NaN value, as if by calling std::strtod, std::strtof, or std::strtold, respectively, as follows:

The call std::nan("n-char-sequence"), where n-char-sequence is a sequence of digits, Latin letters, and underscores, is equivalent to the call std::strtod("NAN(n-char-sequence)", (char**)nullptr);.

The call std::nan("") is equivalent to the call std::strtod("NAN()", (char**)nullptr);.

The call std::nan("string"), where string is neither an n-char-sequence nor an empty string, is equivalent to the call std::strtod("NAN", (char**)nullptr);.

Contents

Parameters

arg - narrow character string identifying the contents of a NaN

Return value

The quiet NaN value that corresponds to the identifying string arg or zero if the implementation does not support quiet NaNs.

Example

#include <iostream>
#include <cmath>
#include <cstdint>
#include <cstring>
 
int main()
{
    double f1 = std::nan("1");
    std::uint64_t f1n; std::memcpy(&f1n, &f1, sizeof f1);
    std::cout << "nan(\"1\") = " << f1 << " (" << std::hex << f1n << ")\n";
 
    double f2 = std::nan("2");
    std::uint64_t f2n; std::memcpy(&f2n, &f2, sizeof f2);
    std::cout << "nan(\"2\") = " << f2 << " (" << std::hex << f2n << ")\n";
}

Possible output:

nan("1") = nan (7ff0000000000001)
nan("2") = nan (7ff0000000000002)

See also

(C++11)
checks if the given number is NaN
(function) [edit]
(C++11)
evaluates to a quiet NaN of type float
(macro constant) [edit]
identifies floating-point types that can represent the special value "quiet not-a-number" (NaN)
(public static member constant of std::numeric_limits<T>) [edit]
identifies floating-point types that can represent the special value "signaling not-a-number" (NaN)
(public static member constant of std::numeric_limits<T>) [edit]
[static]
returns a quiet NaN value of the given floating-point type
(public static member function of std::numeric_limits<T>) [edit]
returns a signaling NaN value of the given floating-point type
(public static member function of std::numeric_limits<T>) [edit]
C documentation for nanf, nan, nanl