Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | numeric‎ | math
(+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 | since=c++11 | num=1 |
+
{{dcl rev multi|num=1|since1=c++11|dcl1=
int fpclassify( float arg );
+
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 | since=c++11 | num=2 |
+
{{dcl h|[[#Notes|Additional overloads]]}}
int fpclassify( double arg );
+
{{dcl header|cmath}}
}}
+
{{dcl|num=A|since=c++11|notes={{mark|constexpr since C++23}}|
{{dcl | since=c++11 | num=3 |
+
template< class Integer >
int fpclassify( long double arg );
+
int fpclassify( Integer num );
}}
+
{{dcl | since=c++11 | num=4 |
+
int fpclassify( Integral arg );
+
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
@1-3@ Categorizes floating point value {{tt|arg}} into the following categories: zero, subnormal, normal, infinite, NAN, or implementation-defined category.  
+
@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}}.}}
@4@ A set of overloads or a function template accepting the {{tt|from}} argument of any [[cpp/types/is_integral|integral type]]. Equivalent to {{v|2}} (the argument is cast to {{c|double}}).
+
@A@ Additional overloads are provided for all integer types, which are treated as {{c/core|double}}.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | arg | floating point value}}
+
{{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}}.
  
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 {{tt|arg}}.
+
===Notes===
 +
{{cpp/numeric/math/additional integer overload note|fpclassify}}
  
 
===Example===
 
===Example===
{{example |
+
{{example|
| code=
+
|code=
#include <iostream>
+
#include <cfloat>
 
#include <cmath>
 
#include <cmath>
#include <cfloat>
+
#include <iostream>
  
const char* show_classification(double x) {
+
auto show_classification(double x)
     switch(std::fpclassify(x)) {
+
{
         case FP_INFINITE: return "Inf";
+
     switch (std::fpclassify(x))
         case FP_NAN:       return "NaN";
+
    {
         case FP_NORMAL:   return "normal";
+
         case FP_INFINITE:
         case FP_SUBNORMAL: return "subnormal";
+
            return "Inf";
         case FP_ZERO:     return "zero";
+
         case FP_NAN:
         default:           return "unknown";
+
            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=
+
|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}}
  
[[de:cpp/numeric/math/fpclassify]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/numeric/math/fpclassify]]
+
[[fr:cpp/numeric/math/fpclassify]]
+
[[it:cpp/numeric/math/fpclassify]]
+
[[ja:cpp/numeric/math/fpclassify]]
+
[[pt:cpp/numeric/math/fpclassify]]
+
[[ru:cpp/numeric/math/fpclassify]]
+
[[zh:cpp/numeric/math/fpclassify]]
+

Latest revision as of 01:24, 20 March 2023

 
 
 
 
Defined in header <cmath>
(1)
int fpclassify( float num );

int fpclassify( double num );

int fpclassify( long 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

#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) [edit]
(C++11)
checks if the given number is infinite
(function) [edit]
(C++11)
checks if the given number is NaN
(function) [edit]
(C++11)
checks if the given number is normal
(function) [edit]
provides an interface to query properties of all fundamental numeric types
(class template) [edit]
C documentation for fpclassify