Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

Commit 359115f

Browse files
[Term Entry] C++ Math-functions: fpclassify()
* docs: add C++ fpclassify() documentation Added comprehensive documentation for the fpclassify() math function. Includes: - Description and syntax - Return value details (FP_INFINITE, FP_NAN, FP_ZERO, FP_SUBNORMAL, FP_NORMAL) - Example with multiple classifications - Runnable codebyte example Closes #7990 * fix: format fpclassify.md to pass Prettier check * minor fixes in the example code and phrasing * Update fpclassify.md ---------
1 parent 8d21ec3 commit 359115f

1 file changed

Lines changed: 125 additions & 0 deletions

File tree

  • content/cpp/concepts/math-functions/terms/fpclassify
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
Title: 'fpclassify()'
3+
Description: 'Classifies a floating-point value into categories such as zero, normal, subnormal, infinite, or NaN.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Classification'
9+
- 'Functions'
10+
- 'Math'
11+
CatalogContent:
12+
- 'learn-c-plus-plus'
13+
- 'paths/computer-science'
14+
---
15+
16+
The **`fpclassify()`** [function](https://www.codecademy.com/resources/docs/cpp/functions) in C++ returns an integer value indicating the classification of a floating-point number. It categorizes values as normal, subnormal, zero, infinite, or NaN (Not-a-Number). The function is available through the `<cmath>` header.
17+
18+
## Syntax
19+
20+
```pseudo
21+
fpclassify(x)
22+
```
23+
24+
**Parameters:**
25+
26+
- `x`: A floating-point value (can be `float`, `double`, or `long double`).
27+
28+
**Return value:**
29+
30+
The `fpclassify()` function returns one of the following integer constants:
31+
32+
- `FP_INFINITE`: The value is positive or negative infinity.
33+
- `FP_NAN`: The value is NaN (Not-a-Number).
34+
- `FP_ZERO`: The value is zero.
35+
- `FP_SUBNORMAL`: The value is a subnormal (denormalized) number.
36+
- `FP_NORMAL`: The value is a normal finite non-zero number.
37+
38+
## Example
39+
40+
The following example demonstrates various classifications using `fpclassify()`:
41+
42+
```cpp
43+
#include <iostream>
44+
#include <cmath>
45+
46+
using namespace std;
47+
48+
int main() {
49+
double normal = 1.5;
50+
double zero = 0.0;
51+
double inf = INFINITY;
52+
double nan = NAN;
53+
54+
cout << "Classification of " << normal << ": ";
55+
if (fpclassify(normal) == FP_NORMAL) {
56+
cout << "Normal" << endl;
57+
}
58+
59+
cout << "Classification of " << zero << ": ";
60+
if (fpclassify(zero) == FP_ZERO) {
61+
cout << "Zero" << endl;
62+
}
63+
64+
cout << "Classification of inf: ";
65+
if (fpclassify(inf) == FP_INFINITE) {
66+
cout << "Infinite" << endl;
67+
}
68+
69+
cout << "Classification of nan: ";
70+
if (fpclassify(nan) == FP_NAN) {
71+
cout << "NaN" << endl;
72+
}
73+
74+
return 0;
75+
}
76+
```
77+
78+
The output of this code is:
79+
80+
```shell
81+
Classification of 1.5: Normal
82+
Classification of 0: Zero
83+
Classification of inf: Infinite
84+
Classification of nan: NaN
85+
```
86+
87+
## Codebyte Example
88+
89+
The following runnable example shows how to use `fpclassify()` with different values:
90+
91+
```codebyte/cpp
92+
#include <iostream>
93+
#include <cmath>
94+
95+
using namespace std;
96+
97+
int main() {
98+
double values[] = {1.0, 0.0, INFINITY, NAN, -5.5};
99+
100+
for (double val : values) {
101+
cout << "fpclassify(" << val << ") = ";
102+
103+
switch(fpclassify(val)) {
104+
case FP_INFINITE:
105+
cout << "Infinite";
106+
break;
107+
case FP_NAN:
108+
cout << "NaN";
109+
break;
110+
case FP_ZERO:
111+
cout << "Zero";
112+
break;
113+
case FP_SUBNORMAL:
114+
cout << "Subnormal";
115+
break;
116+
case FP_NORMAL:
117+
cout << "Normal";
118+
break;
119+
}
120+
cout << endl;
121+
}
122+
123+
return 0;
124+
}
125+
```

0 commit comments

Comments
 (0)