std::isinf(3)

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

✨ std::isinf(3) ✨ β€” the C++ Standard Library's infinity detector πŸ’…

Okay so bestie, let me introduce you to std::isinf β€” it's literally the glow-up function that tells you if a floating-point number is living its best infinite life (positive or negative). No cap, this is the C++ Standard Library we're talking about, so you know it's iconic. πŸŽ€


🌸 NAME 🌸

std::isinf β€” the vibe check for infinity, periodt. πŸ’–


πŸ’– SYNOPSIS πŸ’–

She's defined in the header <cmath>, and here's the main character energy right here:

bool isinf( float num );                                    (since C++11)
bool isinf( double num );                                   (until C++23)

bool isinf( long double num );
constexpr bool isinf( /* floating-point-type */ num );      (1) (since C++23)

✨ Additional overloads ✨ β€” also defined in header <cmath>:

template< class Integer >
bool isinf( Integer num );                                  (A) (since C++11)

(constexpr since C++23) for overload (A) too, iykyk. πŸ¦‹

Okay so let me break down the two numbers up there:

  1. This one determines if the given floating-point number num is a positive or negative infinity. Also β€” and this is important β€” the library provides overloads for all cv-unqualified floating-point types as the type of the parameter num. (since C++23, slay)
  2. A) Additional overloads are provided for all integer types, which are treated as double. Not me giving integers the double treatment 😭

πŸŽ€ PARAMETERS πŸŽ€

  • num β€” floating-point or integer value. That's it, that's the whole parameter list. Minimalist queen. πŸ’…

✨ RETURN VALUE ✨

true if num is infinite, false otherwise. Simple as that, fr fr. 🫢


πŸ’€ NOTES πŸ’€

Okay so this is the part where I need you to pay attention, bestie, because it's giving "important caveat" energy:

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::isinf is assumed to always return false under this option. πŸ’€

Like, literally the compiler is out here assuming infinity doesn't exist. The audacity. So if you're using -ffinite-math or -ffast-math, std::isinf is gonna be like "infinite? never heard of her" and always return false. Be aware of that, okay? πŸ™ˆ

Also, 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::isinf(num) has the same effect as std::isinf(static_cast<double>(num)). So the vibe is the same, even if the implementation isn't copy-paste identical. πŸ’œ


πŸ“ EXAMPLE πŸ“

Bestie, run this code and watch the magic happen:

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

int main()
{
    const double max = std::numeric_limits<double>::max();
    const double inf = std::numeric_limits<double>::infinity();

    std::cout << std::boolalpha
              << "isinf(NaN) = " << std::isinf(NAN) << '\n'
              << "isinf(Inf) = " << std::isinf(INFINITY) << '\n'
              << "isinf(max) = " << std::isinf(max) << '\n'
              << "isinf(inf) = " << std::isinf(inf) << '\n'
              << "isinf(0.0) = " << std::isinf(0.0) << '\n'
              << "isinf(exp(800)) = " << std::isinf(std::exp(800)) << '\n'
              << "isinf(DBL_MIN/2.0) = " << std::isinf(DBL_MIN / 2.0) << '\n';
}

And the output? Obsessed with this honestly:

isinf(NaN) = false
isinf(Inf) = true
isinf(max) = false
isinf(inf) = true
isinf(0.0) = false
isinf(exp(800)) = true
isinf(DBL_MIN/2.0) = false

See how isinf(Inf) and isinf(inf) are true? That's the infinity detection working exactly as intended. And isinf(NaN) is false because NaN is not infinity β€” she's a different kind of special. 🌈


πŸ”₯ SEE ALSO πŸ”₯

These are the other functions you need to know about, bestie β€” they're all part of the "what kind of number is this even" squad:

  • fpclassify β€” categorizes the given floating-point value (C++11) (function). She's the sorting hat of floating-point values. 🧚
  • isfinite β€” checks if the given number has finite value (C++11) (function). The literal opposite vibe of isinf. 😭
  • isnan β€” checks if the given number is NaN (C++11) (function). For when your number is Not A Number. Not me, not you, literally NaN. πŸ’€
  • isnormal β€” checks if the given number is normal (C++11) (function). No cap, normal is a specific thing here. πŸ‘‘
  • C documentation for isinf β€” because sometimes you need the C version too. πŸ’œ

Okay so bestie, that's std::isinf for you β€” the function that checks if your number is infinite, no cap. Remember the -ffinite-math / -ffast-math caveat and you're golden. ✨

This page came from cppreference.com, dated 2024.06.10, and it's the std::isinf(3) manual page. Slay. πŸ’…πŸŒΈπŸ’–

πŸ“¦ where this page comes from