std::isinf(3)
β¨ 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:
- This one determines if the given floating-point number
numis 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 parameternum. (since C++23, slay) - 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-mathoption (additionally implied by-ffast-math), which allows the respective compiler to assume the nonexistence of special IEEE-754 floating point values such asNaN, infinity, or negative zero. In other words,std::isinfis assumed to always returnfalseunder 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 ofisinf. π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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms