Special Floating Point Quantities

Special Floating Point Quantities#

IEEE 754 defines a few special quantities:

  • NaN (not a number) is the result of 0.0/0.0 or std::sqrt(-1.0)

  • Inf (infinity) is the result of 1.0/0.0

  • -0 is a valid number and the standard says that -0 is equivalent to 0

We can test on these values using std::isnan and std::isinf. For instance, here we generate a NaN:

Listing 17 nan.cpp#
#include <iostream>
#include <cmath>

int main() {

    double x{-1.0};

    double y = std::sqrt(x);

    std::cout << y << std::endl;
    std::cout << std::isnan(y) << std::endl;

}