|
| 1 | +--- |
| 2 | +Title: 'isnormal()' |
| 3 | +Description: 'Checks whether a floating-point value is a normal number (not zero, subnormal, infinite, or NaN).' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Game Development' |
| 7 | +Tags: |
| 8 | + - 'Arithmetic' |
| 9 | + - 'Functions' |
| 10 | + - 'Math' |
| 11 | + - 'Numbers' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-plus-plus' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +In C++, the **`isnormal()`** function determines whether a floating-point number is normal, meaning it is non-zero, not subnormal, not infinite, and not NaN. It is useful when performing numerical computations that require standard floating-point numbers. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +bool isnormal(double x); |
| 23 | +``` |
| 24 | + |
| 25 | +Or, alternatively: |
| 26 | + |
| 27 | +```pseudo |
| 28 | +bool isnormal(float x); |
| 29 | +``` |
| 30 | + |
| 31 | +Or, alternatively: |
| 32 | + |
| 33 | +```pseudo |
| 34 | +bool isnormal(long double x); |
| 35 | +``` |
| 36 | + |
| 37 | +**Parameters:** |
| 38 | + |
| 39 | +- `x` : The floating-point number (`float`, `double`, or `long double`) to be checked. |
| 40 | + |
| 41 | +**Return value:** |
| 42 | + |
| 43 | +- `true` if `x` is a normal floating-point number. |
| 44 | +- `false` if `x` is zero, subnormal, infinite, or NaN. |
| 45 | + |
| 46 | +## Example 1: Checking normal and zero numbers |
| 47 | + |
| 48 | +In this example, a normal number and zero are checked using `isnormal()`. The normal number returns `true`, while zero returns `false`: |
| 49 | + |
| 50 | +```cpp |
| 51 | +#include <iostream> |
| 52 | +#include <cmath> |
| 53 | + |
| 54 | +int main() { |
| 55 | + double a = 5.0; |
| 56 | + double b = 0.0; |
| 57 | + |
| 58 | + std::cout << std::boolalpha; |
| 59 | + std::cout << "isnormal(a): " << std::isnormal(a) << "\n"; |
| 60 | + std::cout << "isnormal(b): " << std::isnormal(b) << "\n"; |
| 61 | + |
| 62 | + return 0; |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +The output of this code is: |
| 67 | + |
| 68 | +```shell |
| 69 | +isnormal(a): true |
| 70 | +isnormal(b): false |
| 71 | +``` |
| 72 | + |
| 73 | +## Codebyte Example: Checking subnormal, infinite, and NaN numbers |
| 74 | + |
| 75 | +In this example, subnormal, infinite, and NaN numbers are checked. All of these are not considered normal, so `isnormal()` returns `false` for each: |
| 76 | + |
| 77 | +```codebyte/cpp |
| 78 | +#include <iostream> |
| 79 | +#include <cmath> |
| 80 | +#include <limits> |
| 81 | +
|
| 82 | +int main() { |
| 83 | + double subnormal = std::numeric_limits<double>::denorm_min(); |
| 84 | + double inf = std::numeric_limits<double>::infinity(); |
| 85 | + double nanVal = std::nan(""); |
| 86 | +
|
| 87 | + std::cout << std::boolalpha; |
| 88 | + std::cout << "isnormal(subnormal): " << std::isnormal(subnormal) << "\n"; |
| 89 | + std::cout << "isnormal(inf): " << std::isnormal(inf) << "\n"; |
| 90 | + std::cout << "isnormal(nanVal): " << std::isnormal(nanVal) << "\n"; |
| 91 | +
|
| 92 | + return 0; |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## Frequently Asked Questions |
| 97 | + |
| 98 | +### 1. How to use not equal in C++? |
| 99 | + |
| 100 | +The not equal operator in C++ is written as `!=`. It compares two values and returns `true` if they are not equal, otherwise `false`. Example: |
| 101 | + |
| 102 | +```cpp |
| 103 | +#include <iostream> |
| 104 | +int main() { |
| 105 | + int a = 5, b = 10; |
| 106 | + if (a != b) { |
| 107 | + std::cout << "a and b are not equal\n"; // outputs: a and b are not equal |
| 108 | + |
| 109 | + } |
| 110 | + return 0; |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +### 2. What is `isdigit()` in C++? |
| 115 | + |
| 116 | +The `isdigit()` function checks if a character is a decimal digit (`'0'`–`'9'`). It is declared in `<cctype>` or `<ctype.h>` and returns a non-zero value (`true`) if the character is a digit, otherwise 0 (`false`). |
| 117 | + |
| 118 | +### 3. What are the four types of functions in C++? |
| 119 | + |
| 120 | +The four types of functions in C++ based on parameters and return type are: |
| 121 | + |
| 122 | +1. Function with no arguments and no return value |
| 123 | +2. Function with arguments and no return value |
| 124 | +3. Function with no arguments but returns a value |
| 125 | +4. Function with arguments and returns a value |
0 commit comments