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