std::nan, std::nanf, std::nanl
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Numerics library
Mathematical special functions (C++17)
Mathematical constants (C++20)
Basic linear algebra algorithms (C++26)
Data-parallel types (SIMD) (C++26)
Floating-point environment (C++11)
Bit manipulation (C++20)
Saturation arithmetic (C++26)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
Common mathematical functions
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
(C++11)
Defined in header
<cmath>
float nanf( const char* arg );
(1)
(since C++11)
double nan ( const char* arg );
(2)
(since C++11)
long double nanl( const char* arg );
(3)
(since C++11)
Converts the character string arg into the corresponding quiet NaN value, as if by calling std::strtof , std::strtod , or std::strtold , respectively.
1) The call std::nanf("n-char-sequence"), where n-char-sequence is a sequence of digits, ASCII letters, and underscores, is equivalent to the call std::strtof ("NAN(n-char-sequence)", (char**)nullptr);.
The call std::nanf("") is equivalent to the call std::strtof ("NAN()", (char**)nullptr);.
The call std::nanf("string"), where string is neither an n-char-sequence nor an empty string, is equivalent to the call std::strtof ("NAN", (char**)nullptr);.
[edit] Parameters
arg
-
narrow character string identifying the contents of a NaN
[edit] Return value
The quiet NaN value that corresponds to the identifying string arg or zero if the implementation does not support quiet NaNs.
If the implementation supports IEEE floating-point arithmetic (IEC 60559), it also supports quiet NaNs.
[edit] Error handling
This function is not subject to any of the error conditions specified in math_errhandling.
[edit] Example
Run this code
#include <cmath> #include <cstdint> #include <cstring> #include <iostream> 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)
[edit] See also
[static]
(public static member constant of
std::numeric_limits<T>
) [edit]
[static]
(public static member constant of
std::numeric_limits<T>
) [edit]
[static]
(public static member function of
std::numeric_limits<T>
) [edit]
[static]
(public static member function of
std::numeric_limits<T>
) [edit]
C documentation for nanf, nan, nanl