Recently, when debugging with Visual Studio 2019, I noticed that a variable had the unusual value of 6.324...e-322#DEN. This is "almost" equal to zero. In Visual Studio's debugger, the #DEN label refers to a denormalized (or subnormal) floating-point number. This occurs when the value of a floating-point variable is so close to zero that it cannot be represented in the normalized format of the IEEE 754 standard.
A floating-point number is typically represented as:
This is called a normalized number because the leading digit of the fraction (mantissa) is assumed to be 1.When the exponent is at its smallest possible value (the minimum exponent), and the number is still too small to be represented in the normalized form, it is stored as a denormalized number. For denormalized numbers, the leading digit is 0 instead of 1, so the representation becomes:
Denormalized numbers fill the gap between the smallest normalized number and zero.//Investigate denormalized numbers. | |
//Şamil Korkmaz, November 2024 | |
#include <iostream> | |
#include <iomanip> | |
#include <limits> | |
#include <cmath> | |
bool isDenormalized(double value) { | |
return value != 0.0 && std::abs(value) < std::numeric_limits<double>::min(); | |
} | |
int main() { | |
double normalValue = 1.0; | |
double denormalValueMin = std::numeric_limits<double>::denorm_min(); | |
//double val = 2e-308; | |
double val1 = std::numeric_limits<double>::min(); | |
double val2 = val1 - 1e-323; | |
std::cout << "min double: " << std::numeric_limits<double>::min() << std::endl; | |
std::cout << "normalValue " << normalValue << " is " << (isDenormalized(normalValue) ? "denormalized" : "normalized") << '\n'; | |
std::cout << "denormalValueMin " << denormalValueMin << " is " << (isDenormalized(denormalValueMin) ? "denormalized" : "normalized") << '\n'; | |
std::cout << std::fixed << std::setprecision(324); // Up to 324 decimal places | |
std::cout << "val1 " << val1 << " is " << (isDenormalized(val1) ? "denormalized" : "normalized") << '\n'; | |
std::cout << "val2 " << val2 << " is " << (isDenormalized(val2) ? "denormalized" : "normalized") << '\n'; | |
return 0; | |
} |