In C++, std::isnormal(x) is the strictest of the classification checks. It returns true only for ordinary numbers that are stored with full precision. Zero is not normal, and neither are infinity, NaN, and the tiny subnormal numbers.
A subnormal number is a value between zero and the smallest normal double, which is std::numeric_limits<double>::min(), about 2.2e-308. Subnormal numbers still work, but they lose digits of precision as they get smaller.
std::isnormal() is declared in <cmath> and is part of the group Classification and comparison. These tools ask what kind of number you have: normal, zero, infinite, or NaN. In C++ they are ordinary overloaded functions in std, not macros.
Syntax
Include <cmath> and call std::isnormal. Start with these lines.
C++
bool a = std::isnormal(1.5); // true
bool b = std::isnormal(0.0); // false (zero is not normal)
bool c = std::isnormal(1e-310); // false (subnormal)
Call
What it does
std::isnormal(1.5)
An ordinary number. Returns true.
std::isnormal(0.0)
Zero is not normal. Returns false.
std::isnormal(1e-310)
Smaller than the smallest normal double, so subnormal. Returns false.
Write std:: in front, and include <cmath>. The function returns a bool, so use std::boolalpha to print true or false.
numeric_limits<double>::min() is the smallest positive normal double, 2.22507e-308.
Half of it is 1.11254e-308. That is still a real number, and it is not zero, but it is below the normal range.
So isnormal() is true for the first value and false for the half. This is the exact border of the normal range.
Example 3 — Halve a Number Until It Is Not Normal
Start at 1.0 and divide by 2 each time. Count the steps until the value stops being normal.
C++
#include <cmath>
#include <iostream>
int main() {
double x = 1.0;
int halvings = 0;
while (std::isnormal(x)) {
x /= 2;
++halvings;
}
std::cout << "Halvings until not normal: " << halvings << '\n';
std::cout << "Value now: " << x << '\n';
std::cout << "Is it zero? " << (x == 0.0) << '\n';
return 0;
}
Output
Halvings until not normal: 1023
Value now: 1.11254e-308
Is it zero? 0
How It Works
After n halvings x equals 2^-n. The smallest normal value is 2^-1022.
The loop stops at the first value below that, which is 2^-1023. That needs 1023 halvings, and it is about 1.11254e-308.
The value is not zero. It is a subnormal number: the numbers get smaller more slowly and lose precision before they finally reach zero. This is called gradual underflow.
Frequently Asked Questions
std::isnormal(x) returns true only if x is a normal number. It returns false for zero, subnormal numbers, infinity, and NaN.
true if x is a normal finite number, not zero. false if x is zero, subnormal, infinity, or NaN.
Zero has no normal form. A normal number is stored with a leading 1 bit and a non-zero exponent, and zero is stored specially. Use std::isfinite() if you want zero to count as valid.
std::numeric_limits<double>::min(), which is about 2.2250738585072014e-308. Anything smaller but not zero is subnormal.
Include <cmath> and write std::isnormal(). That always works and picks the C++ overloads. The global name ::isnormal often works too, but the standard does not promise it, and it may only take a double.