std::isnan(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::isnan β€” the std::isnan(3) glow-up

okay so bestie, welcome to the C++ Standard Library corner of the manual, and today's main character is std::isnan. yes she's a tiny little boolean function and yes she's absolutely iconic. grab your iced coffee, we're learning her whole lore ✨


🌸 NAME 🌸

std::isnan β€” like, literally std::isnan. that's the name. she's not complicated and honestly? respect 🫢


✨ Synopsis ✨

she lives in <cmath>, as all the cool math girlies do πŸ’–

Defined in header <cmath>
bool isnan( float num );
                                                              (since C++11)
bool isnan( double num );                                     (until C++23)

bool isnan( long double num );
constexpr bool isnan( /* floating-point-type */ num  (1)      (since C++23)
);
Additional overloads
Defined in header <cmath>
template< class Integer >                             (A)     (since C++11)
bool isnan( Integer num );
(constexpr since C++23)

so what's the vibe here, you ask? let me break it down πŸŽ€

  1. she determines if the given floating point number num is a not-a-number (NaN) value. that's the whole job. that's her purpose. queen of one (1) task πŸ‘‘

and β€” this is the slay part β€” the library provides overloads for all cv-unqualified floating-point types as the type of the parameter num. (since C++23, bestie, keep up πŸ’…)

A) also!! additional overloads are provided for all integer types, and those get treated as double. no cap.


πŸ¦‹ Parameters πŸ¦‹

  • num β€” a floating-point or integer value. that's it. that's the parameter. she's low maintenance πŸ’œ

πŸ”₯ Return value πŸ”₯

  • true if num is a NaN
  • false otherwise

binary queen. no notes. periodt. πŸ’€


🌈 Notes 🌈

okay this is the part where I need you to lock in, because the notes are kind of a lot and they're all important 🧚

  • there are many different NaN values with different sign bits and payloads β€” see std::nan and std::numeric_limits::quiet_NaN. so she's not one girl, she's a whole friend group πŸ™ˆ
  • NaN values never compare equal to themselves or to other NaN values. not even to herself. truly the "I'm not like other girls" of floating point 😭
  • copying a NaN is not required, by IEEE-754, to preserve its bit representation (sign and payload), though most implementation do. so like... it's giving "undefined but everyone's chill about it" πŸ’€
  • another way to test if a floating-point value is NaN is to compare it with itself:
bool is_nan(double x) { return x != x; }

her little self-comparison trick. honestly? iconic. she is her own worst enemy and her own best test 🫢

  • GCC and Clang support a -ffinite-math option (additionally implied by -ffast-math), which allows the respective compiler to assume the nonexistence of special IEEE-754 floating point values such as NaN, infinity, or negative zero. in other words, std::isnan is assumed to always return false under this option.
⚠️ okay so that's a big deal, bestie. flip that flag on and std::isnan basically gets gaslit into always saying false. she's not lying, she's just been told NaN doesn't exist. be careful out there πŸ’€
  • the additional overloads are not required to be provided exactly as (A). they only need to be sufficient to ensure that for their argument num of integer type, std::isnan(num) has the same effect as std::isnan(static_cast<double>(num)). implementation girlies have wiggle room πŸ’…

πŸ’– Example πŸ’–

// Run this code

#include <cfloat>
#include <cmath>
#include <iostream>

int main()
{
    std::cout << std::boolalpha
              << "isnan(NaN) = " << std::isnan(NAN) << '\n'
              << "isnan(Inf) = " << std::isnan(INFINITY) << '\n'
              << "isnan(0.0) = " << std::isnan(0.0) << '\n'
              << "isnan(DBL_MIN/2.0) = " << std::isnan(DBL_MIN / 2.0) << '\n'
              << "isnan(0.0 / 0.0)   = " << std::isnan(0.0 / 0.0) << '\n'
              << "isnan(Inf - Inf)   = " << std::isnan(INFINITY - INFINITY) << '\n';
}

⭐ Output ⭐

isnan(NaN) = true
isnan(Inf) = false
isnan(0.0) = false
isnan(DBL_MIN/2.0) = false
isnan(0.0 / 0.0)   = true
isnan(Inf - Inf)   = true

look at her go. 0.0 / 0.0 and Inf - Inf both ate and left no crumbs πŸ”₯


πŸ‘‘ See also πŸ‘‘

the whole squad, fr fr:

  • nan, nanf, nanl β€” not-a-number (NaN) (C++11) (function)
  • fpclassify β€” categorizes the given floating-point value (C++11) (function)
  • isfinite β€” checks if the given number has finite value (C++11) (function)
  • isinf β€” checks if the given number is infinite (C++11) (function)
  • isnormal β€” checks if the given number is normal (C++11) (function)
  • isunordered β€” checks if two floating-point values are unordered (C++11) (function)
  • C documentation for isnan

πŸŽ€ that's the whole page bestie, you understood it perfectly, I'm so proud of you. now go check your NaNs. iykyk πŸ’…

mwah πŸ’‹ β€” this page came from http://cppreference.com, version 2024.06.10, dated the very cute 2024.06.10. go drink some water and don't forget -ffast-math is a menace 🌸

πŸ“¦ where this page comes from