Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | numeric‎ | math
m (Minor misspelling)
m (Minor tweak.)
 
(19 intermediate revisions by 13 users not shown)
Line 1: Line 1:
 
{{cpp/title|nan|nanf|nanl}}
 
{{cpp/title|nan|nanf|nanl}}
 
{{cpp/numeric/math/navbar}}
 
{{cpp/numeric/math/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | cmath}}
+
{{dcl header|cmath}}
{{ddcl list item | notes={{mark since c++11}} |
+
{{dcl|since=c++11|num=1|
float nanf( const char* arg );
+
float       nanf( const char* arg );
 
}}
 
}}
{{ddcl list item | notes={{mark since c++11}} |
+
{{dcl|since=c++11|num=2|
double nan( const char* arg );
+
double     nan ( const char* arg );
 
}}
 
}}
{{ddcl list item | notes={{mark since c++11}} |
+
{{dcl|since=c++11|num=3|
 
long double nanl( const char* arg );
 
long double nanl( const char* arg );
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
Converts the implementation-defined character string {{tt|arg}} into the corresponding quiet NaN value. The call {{c|std::nan("string")}} is equivalent to the call {{c|std::strtod("NAN(string)", (char**)NULL);}}.
+
Converts the character string {{c|arg}} into the corresponding quiet NaN value, as if by calling {{lc|std::strtof}}, {{lc|std::strtod}}, or {{lc|std::strtold}}, respectively.
 +
 
 +
@1@ The call {{box|{{c/core|std::nanf("}}{{spar|n-char-sequence}}{{c/core|")}}}}, where {{spar|n-char-sequence}} is a sequence of digits, ASCII letters, and underscores, is equivalent to the call {{box|{{c/core|std::strtof("NAN(}}{{spar|n-char-sequence}}{{c/core|)"}}{{c/core|, (char**)nullptr);}}}}.
 +
 
 +
@@ The call {{c|std::nanf("")}} is equivalent to the call {{c|std::strtof("NAN()", (char**)nullptr);}}.
 +
 
 +
@@ The call {{box|{{c/core|std::nanf("}}{{spar|string}}{{c/core|")}}}}, where {{spar|string}} is neither an {{spar|n-char-sequence}} nor an empty string, is equivalent to the call {{c|std::strtof("NAN", (char**)nullptr);}}.
 +
 
 +
@2@ Same as {{v|1}}, but calls {{lc|std::strtod}} instead of {{lc|std::strtof}}.
 +
@3@ Same as {{v|1}}, but calls {{lc|std::strtold}} instead of {{lc|std::strtof}}.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | arg | narrow character string identifying the contents of a NaN, or an empty string}}
+
{{par|arg|narrow character string identifying the contents of a NaN}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
 +
The quiet NaN value that corresponds to the identifying string {{c|arg}} or zero if the implementation does not support quiet NaNs.
  
The NaN value that corresponds to the identifying string {{tt|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.
 +
 
 +
===Error handling===
 +
This function is not subject to any of the error conditions specified in {{ltt|cpp/numeric/math/math_errhandling}}.
 +
 
 +
===Example===
 +
{{example
 +
|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";
 +
}
 +
|p=true
 +
|output=
 +
nan("1") = nan (7ff0000000000001)
 +
nan("2") = nan (7ff0000000000002)
 +
}}
  
 
===See also===
 
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/numeric/math/dsc isnan}}
 +
{{dsc inc|cpp/numeric/math/dsc NAN}}}
 +
{{dsc inc|cpp/types/numeric_limits/dsc has_quiet_NaN}}
 +
{{dsc inc|cpp/types/numeric_limits/dsc has_signaling_NaN}}
 +
{{dsc inc|cpp/types/numeric_limits/dsc quiet_NaN}}
 +
{{dsc inc|cpp/types/numeric_limits/dsc signaling_NaN}}
 +
{{dsc see c|c/numeric/math/nan|nanf|nan|nanl}}
 +
{{dsc end}}
  
{{dcl list begin}}
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
{{dcl list template | cpp/numeric/math/dcl list isnan}}
+
{{dcl list template | cpp/types/numeric_limits/dcl list quiet_NaN}}
+
{{dcl list end}}
+

Latest revision as of 01:28, 14 March 2023

 
 
 
 
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);.
2) Same as (1), but calls std::strtod instead of std::strtof.
3) Same as (1), but calls std::strtold instead of std::strtof.

Contents

[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

#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

(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