Difference between revisions of "cpp/numeric/math/fpclassify"
From cppreference.com
(+integral type overloads) |
(Applied P1467R9.) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 2: | Line 2: | ||
{{cpp/numeric/math/navbar}} | {{cpp/numeric/math/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header | cmath}} | + | {{dcl header|cmath}} |
− | {{dcl | | + | {{dcl rev multi|num=1|since1=c++11|dcl1= |
− | int fpclassify( | + | int fpclassify( float num ); |
+ | int fpclassify( double num ); | ||
+ | int fpclassify( long double num ); | ||
+ | |since2=c++23|dcl2= | ||
+ | constexpr int fpclassify( /* floating-point-type */ num ); | ||
}} | }} | ||
− | {{dcl | | + | {{dcl h|[[#Notes|Additional overloads]]}} |
− | + | {{dcl header|cmath}} | |
− | }} | + | {{dcl|num=A|since=c++11|notes={{mark|constexpr since C++23}}| |
− | {{dcl | since=c++11 | | + | template< class Integer > |
− | + | int fpclassify( Integer num ); | |
− | + | ||
− | {{ | + | |
− | int fpclassify( | + | |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | @1 | + | @1@ Categorizes floating point value {{c|num}} into the following categories: zero, subnormal, normal, infinite, NAN, or implementation-defined category.{{rev inl|since=c++23| The library provides overloads of {{tt|std::fpclassify}} for all cv-unqualified floating-point types as the type of the parameter {{c|num}}.}} |
− | + | @A@ Additional overloads are provided for all integer types, which are treated as {{c/core|double}}. | |
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | | + | {{par|num|floating-point or integer value}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
+ | one of {{lc|FP_INFINITE}}, {{lc|FP_NAN}}, {{lc|FP_NORMAL}}, {{lc|FP_SUBNORMAL}}, {{lc|FP_ZERO}} or implementation-defined type, specifying the category of {{c|num}}. | ||
− | + | ===Notes=== | |
+ | {{cpp/numeric/math/additional integer overload note|fpclassify}} | ||
===Example=== | ===Example=== | ||
− | {{example | | + | {{example| |
− | + | |code= | |
− | #include < | + | #include <cfloat> |
#include <cmath> | #include <cmath> | ||
− | #include < | + | #include <iostream> |
− | + | auto show_classification(double x) | |
− | switch(std::fpclassify(x)) { | + | { |
− | case FP_INFINITE: | + | switch (std::fpclassify(x)) |
− | case FP_NAN: | + | { |
− | case FP_NORMAL: | + | case FP_INFINITE: |
− | case FP_SUBNORMAL: return "subnormal"; | + | return "Inf"; |
− | case FP_ZERO: | + | case FP_NAN: |
− | default: | + | return "NaN"; |
+ | case FP_NORMAL: | ||
+ | return "normal"; | ||
+ | case FP_SUBNORMAL: | ||
+ | return "subnormal"; | ||
+ | case FP_ZERO: | ||
+ | return "zero"; | ||
+ | default: | ||
+ | return "unknown"; | ||
} | } | ||
} | } | ||
+ | |||
int main() | int main() | ||
{ | { | ||
− | std::cout << "1.0/0.0 is " << show_classification(1/0.0) << '\n' | + | std::cout << "1.0/0.0 is " << show_classification(1 / 0.0) << '\n' |
− | << "0.0/0.0 is " << show_classification(0.0/0.0) << '\n' | + | << "0.0/0.0 is " << show_classification(0.0 / 0.0) << '\n' |
− | << "DBL_MIN/2 is " << show_classification(DBL_MIN/2) << '\n' | + | << "DBL_MIN/2 is " << show_classification(DBL_MIN / 2) << '\n' |
<< "-0.0 is " << show_classification(-0.0) << '\n' | << "-0.0 is " << show_classification(-0.0) << '\n' | ||
<< "1.0 is " << show_classification(1.0) << '\n'; | << "1.0 is " << show_classification(1.0) << '\n'; | ||
} | } | ||
− | + | |output= | |
1.0/0.0 is Inf | 1.0/0.0 is Inf | ||
0.0/0.0 is NaN | 0.0/0.0 is NaN | ||
Line 63: | Line 75: | ||
===See also=== | ===See also=== | ||
− | |||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/numeric/math/dsc isfinite}} | + | {{dsc inc|cpp/numeric/math/dsc isfinite}} |
− | {{dsc inc | cpp/numeric/math/dsc isinf}} | + | {{dsc inc|cpp/numeric/math/dsc isinf}} |
− | {{dsc inc | cpp/numeric/math/dsc isnan}} | + | {{dsc inc|cpp/numeric/math/dsc isnan}} |
− | {{dsc inc | cpp/numeric/math/dsc isnormal}} | + | {{dsc inc|cpp/numeric/math/dsc isnormal}} |
− | {{dsc inc | cpp/types/dsc numeric_limits}} | + | {{dsc inc|cpp/types/dsc numeric_limits}} |
− | {{dsc see c | c/numeric/math/fpclassify}} | + | {{dsc see c|c/numeric/math/fpclassify}} |
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 01:24, 20 March 2023
Defined in header <cmath>
|
||
(1) | ||
int fpclassify( float num ); int fpclassify( double num ); |
(since C++11) (until C++23) |
|
constexpr int fpclassify( /* floating-point-type */ num ); |
(since C++23) | |
Defined in header <cmath>
|
||
template< class Integer > int fpclassify( Integer num ); |
(A) | (since C++11) (constexpr since C++23) |
1) Categorizes floating point value num into the following categories: zero, subnormal, normal, infinite, NAN, or implementation-defined category. The library provides overloads of
std::fpclassify
for all cv-unqualified floating-point types as the type of the parameter num.(since C++23)A) Additional overloads are provided for all integer types, which are treated as double.
Contents |
[edit] Parameters
num | - | floating-point or integer value |
[edit] Return value
one of FP_INFINITE, FP_NAN, FP_NORMAL, FP_SUBNORMAL, FP_ZERO or implementation-defined type, specifying the category of num.
[edit] Notes
The additional overloads are not required to be provided exactly as (A). They only need to be sufficient to ensure that for their argument num of integer type, std::fpclassify(num) has the same effect as std::fpclassify(static_cast<double>(num)).
[edit] Example
Run this code
#include <cfloat> #include <cmath> #include <iostream> auto show_classification(double x) { switch (std::fpclassify(x)) { case FP_INFINITE: return "Inf"; case FP_NAN: return "NaN"; case FP_NORMAL: return "normal"; case FP_SUBNORMAL: return "subnormal"; case FP_ZERO: return "zero"; default: return "unknown"; } } int main() { std::cout << "1.0/0.0 is " << show_classification(1 / 0.0) << '\n' << "0.0/0.0 is " << show_classification(0.0 / 0.0) << '\n' << "DBL_MIN/2 is " << show_classification(DBL_MIN / 2) << '\n' << "-0.0 is " << show_classification(-0.0) << '\n' << "1.0 is " << show_classification(1.0) << '\n'; }
Output:
1.0/0.0 is Inf 0.0/0.0 is NaN DBL_MIN/2 is subnormal -0.0 is zero 1.0 is normal
[edit] See also
(C++11) |
checks if the given number has finite value (function) |
(C++11) |
checks if the given number is infinite (function) |
(C++11) |
checks if the given number is NaN (function) |
(C++11) |
checks if the given number is normal (function) |
provides an interface to query properties of all fundamental numeric types (class template) | |
C documentation for fpclassify
|