From 253bb287ab03c5e9caa24cfa66b5edd87144347d Mon Sep 17 00:00:00 2001 From: Priyansh Jain Date: Fri, 21 Nov 2025 14:02:05 +0530 Subject: [PATCH 1/3] 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 --- .../terms/fpclassify/fpclassify.md | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 content/cpp/concepts/math-functions/terms/fpclassify/fpclassify.md diff --git a/content/cpp/concepts/math-functions/terms/fpclassify/fpclassify.md b/content/cpp/concepts/math-functions/terms/fpclassify/fpclassify.md new file mode 100644 index 00000000000..c48eb4a5ec5 --- /dev/null +++ b/content/cpp/concepts/math-functions/terms/fpclassify/fpclassify.md @@ -0,0 +1,127 @@ +--- +Title: 'fpclassify()' +Description: 'Classifies a floating-point value into specific categories such as zero, normal, subnormal, infinite, or NaN.' +Subjects: + - 'Computer Science' + - 'Data Science' + - 'Game Development' +Tags: + - 'Functions' + - 'Math' + - 'Classification' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`fpclassify()`** [function](https://www.codecademy.com/resources/docs/cpp/functions) 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 `` header. + +## Syntax + +```pseudo +fpclassify(x) +``` + +**Parameters:** + +- `x`: A floating-point value (can be `float`, `double`, or `long double`). + +**Return value:** + +The `fpclassify()` function returns one of the following integer constants: + +- `FP_INFINITE`: The value is positive or negative infinity. +- `FP_NAN`: The value is Not-a-Number. +- `FP_ZERO`: The value is zero. +- `FP_SUBNORMAL`: The value is a subnormal (denormalized) number. +- `FP_NORMAL`: The value is a normal finite non-zero number. + +## Example + +The following example demonstrates various classifications using `fpclassify()`: + +```cpp +#include +#include +#include + +using namespace std; + +int main() { + double normal = 1.5; + double zero = 0.0; + double inf = INFINITY; + double nan = NAN; + + cout << "Classification of " << normal << ": "; + if (fpclassify(normal) == FP_NORMAL) { + cout << "Normal" << endl; + } + + cout << "Classification of " << zero << ": "; + if (fpclassify(zero) == FP_ZERO) { + cout << "Zero" << endl; + } + + cout << "Classification of inf: "; + if (fpclassify(inf) == FP_INFINITE) { + cout << "Infinite" << endl; + } + + cout << "Classification of nan: "; + if (fpclassify(nan) == FP_NAN) { + cout << "NaN" << endl; + } + + return 0; +} +``` + +The output of this code is: + +```shell +Classification of 1.5: Normal +Classification of 0: Zero +Classification of inf: Infinite +Classification of nan: NaN +``` + +## Codebyte Example + +The following runnable example shows how to use `fpclassify()` with different values: + +```codebyte/cpp +#include +#include + +using namespace std; + +int main() { + double values[] = {1.0, 0.0, INFINITY, NAN, -5.5}; + + for (double val : values) { + cout << "fpclassify(" << val << ") = "; + + switch(fpclassify(val)) { + case FP_INFINITE: + cout << "Infinite"; + break; + case FP_NAN: + cout << "NaN"; + break; + case FP_ZERO: + cout << "Zero"; + break; + case FP_SUBNORMAL: + cout << "Subnormal"; + break; + case FP_NORMAL: + cout << "Normal"; + break; + } + cout << endl; + } + + return 0; +} +``` From f4debfcf468af6b7cdea6e4e0084684be51afa84 Mon Sep 17 00:00:00 2001 From: Priyansh Jain Date: Fri, 21 Nov 2025 14:17:15 +0530 Subject: [PATCH 2/3] fix: format fpclassify.md to pass Prettier check --- .../terms/fpclassify/fpclassify.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/content/cpp/concepts/math-functions/terms/fpclassify/fpclassify.md b/content/cpp/concepts/math-functions/terms/fpclassify/fpclassify.md index c48eb4a5ec5..97599aa91e7 100644 --- a/content/cpp/concepts/math-functions/terms/fpclassify/fpclassify.md +++ b/content/cpp/concepts/math-functions/terms/fpclassify/fpclassify.md @@ -1,6 +1,6 @@ --- Title: 'fpclassify()' -Description: 'Classifies a floating-point value into specific categories such as zero, normal, subnormal, infinite, or NaN.' +Description: 'Classifies a floating-point value into categories such as zero, normal, subnormal, infinite, or NaN.' Subjects: - 'Computer Science' - 'Data Science' @@ -52,27 +52,27 @@ int main() { double zero = 0.0; double inf = INFINITY; double nan = NAN; - + cout << "Classification of " << normal << ": "; if (fpclassify(normal) == FP_NORMAL) { cout << "Normal" << endl; } - + cout << "Classification of " << zero << ": "; if (fpclassify(zero) == FP_ZERO) { cout << "Zero" << endl; } - + cout << "Classification of inf: "; if (fpclassify(inf) == FP_INFINITE) { cout << "Infinite" << endl; } - + cout << "Classification of nan: "; if (fpclassify(nan) == FP_NAN) { cout << "NaN" << endl; } - + return 0; } ``` @@ -98,10 +98,10 @@ using namespace std; int main() { double values[] = {1.0, 0.0, INFINITY, NAN, -5.5}; - + for (double val : values) { cout << "fpclassify(" << val << ") = "; - + switch(fpclassify(val)) { case FP_INFINITE: cout << "Infinite"; @@ -121,7 +121,7 @@ int main() { } cout << endl; } - + return 0; } ``` From d86a58ff76607d8c2756c8f611dad56a48c0d679 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 22 Nov 2025 13:39:13 +0530 Subject: [PATCH 3/3] minor fixes in the example code and phrasing --- .../cpp/concepts/math-functions/terms/fpclassify/fpclassify.md | 1 - 1 file changed, 1 deletion(-) diff --git a/content/cpp/concepts/math-functions/terms/fpclassify/fpclassify.md b/content/cpp/concepts/math-functions/terms/fpclassify/fpclassify.md index 97599aa91e7..6bcf4fa4989 100644 --- a/content/cpp/concepts/math-functions/terms/fpclassify/fpclassify.md +++ b/content/cpp/concepts/math-functions/terms/fpclassify/fpclassify.md @@ -43,7 +43,6 @@ The following example demonstrates various classifications using `fpclassify()`: ```cpp #include #include -#include using namespace std;