Can Std: :Isfinite(Integraltype) Ever Return False?

According to the documentation of std::isfinite, the overload bool isfinite( IntegralType arg ) always casts arg to double and calls bool isfinite( double arg ).

How would this be different from saying "bool isfinite( IntegralType arg ) always returns true"? In other words, under what scenario would bool isfinite( IntegralType arg ) return false?

2

1 Answer

Most (all?) other functions in <cmath> also accept integers and automatically convert them to doubles. std::isfinite likely does it for consistency.

In theory it could return false if the conversion to double overflowed, but in practice we don't have large enough integral types. In fact, GCC & Clang with -O3 (targeting x86-64) appear to replace the call with true (even with [unsigned]long long arguments).

Such an overflow could happen if the integral type is 128 bits wide and the floating-point type is single-precision (32 bits wide), for example std::isfinite(float(__uint128_t(-1))) evaluates to false, but since double is not allowed to be 32-bit (see comments), this doesn't matter.

11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest