is_infinite
is_infinite(float $num): bool
Checks whether a value is infinite (INF or -INF).
C C23
#include <math.h>
#include <stdio.h>
int main(void) {
double inf = INFINITY; /* from math.h */
printf("%d %d %d\n", isinf(inf) != 0, isinf(-inf) != 0, isinf(1.0) != 0); /* C99 macros: only nonzero/zero guaranteed; sign is libc-specific */
printf("%d\n", isfinite(inf) != 0); /* the complementary check */
return 0;
}
1 1 0 0
C++ C++26
#include <cmath>
#include <iostream>
#include <limits>
int main() {
double inf = std::numeric_limits<double>::infinity(); // <cmath> helpers, C++11 onward
std::cout << std::boolalpha;
std::cout << std::isinf(inf) << " " << std::isinf(-inf) << " " << std::isinf(1.0) << "\n";
std::cout << std::isfinite(inf) << "\n";
return 0;
}
true true false false
C# v11.0.100
double inf = double.PositiveInfinity;
Console.WriteLine($"{double.IsPositiveInfinity(inf)} {double.IsNegativeInfinity(-inf)} {double.IsInfinity(1.0)}");
Console.WriteLine(double.IsFinite(inf)); // .NET Core 3.0+ companion to IsInfinity
True True False False
Go v1.27.4
package main
import (
"fmt"
"math"
)
func main() {
inf := math.Inf(1) // positive; -1 gives -INF, sign argument filters direction
fmt.Println(math.IsInf(inf, 1), math.IsInf(-inf, -1), math.IsInf(inf, 0), math.IsInf(1.0, 0))
// note: Go has no IsFinite — finiteness is !IsInf && !IsNaN
fmt.Println(!math.IsInf(inf, 0) && !math.IsNaN(inf))
}
true true true false false
Java v27.0.1
public class Main {
public static void main(String[] args) {
double inf = Double.POSITIVE_INFINITY; // float /0 yields INF (int /0 throws)
System.out.println(Double.isInfinite(inf) + " " + Double.isInfinite(-inf) + " " + Double.isInfinite(1.0));
System.out.println(Double.isFinite(inf));
}
}
true true false false
JavaScript v26.13.1
const inf = Number.POSITIVE_INFINITY;
console.log(inf === Number.POSITIVE_INFINITY, -inf === Number.NEGATIVE_INFINITY);
// Number.isFinite is the safe check: the legacy global isFinite("1") coerces strings
console.log(Number.isFinite(inf), Number.isFinite(1.0));
true true false true
PHP v8.6.0
var_dump(is_infinite(INF));
var_dump(is_infinite(-INF));
var_dump(is_infinite(1.0));
var_dump(is_finite(INF)); // the complementary check
bool(true) bool(true) bool(false) bool(false)
Python v3.15.2
import math
inf = float("inf") # no INF literal in Python: float("inf") is the idiom
print(math.isinf(inf), math.isinf(-inf), math.isinf(1.0))
print(math.isfinite(inf))
True True False False
Rust v1.99.1
fn main() {
let inf = f64::INFINITY; // overflow and runtime 1.0/0.0 land here
println!(
"{} {} {}",
inf.is_infinite(),
(-inf).is_infinite(),
1.0f64.is_infinite()
);
println!("{}", inf.is_finite());
}
true true false false
TypeScript v26.13.1
const inf: number = Number.POSITIVE_INFINITY;
// Number.isFinite is the safe check: the legacy global isFinite("1") coerces strings
console.log(`${inf === Number.POSITIVE_INFINITY} ${-inf === Number.NEGATIVE_INFINITY}`);
console.log(`${Number.isFinite(inf)} ${Number.isFinite(1.0)}`);
true true false true
Language differences
Where INF comes from: IEEE 754 overflow (1e308*10, 1.0/0.0) yields ±INF; the key difference is who allows division by zero — C/C++/Go/JS/Rust/Java/C# follow IEEE and get INF directly, PHP 8's / throws DivisionByZeroError (use fdiv(1.0, 0.0) to get INF), and Python throws ZeroDivisionError on 1.0/0 (explicit float("inf") is the only source).
C: isinf/isfinite are C99 macros (example) guaranteed only nonzero/zero — glibc returns -1 for -inf while BSD returns 1, so the example normalizes with != 0 (the portable idiom); the INFINITY macro comes from math.h; telling directions requires signbit alongside.
C++: std::isinf/std::isfinite (example) + numeric_limits' infinity(); the only differences from C are the namespace and type safety.
C#: IsInfinity ignores direction (true for both ±INF — third item on line 1 of the example); direction needs IsPositiveInfinity/IsNegativeInfinity; double.IsFinite only exists since .NET Core 3.0.
Go: math.IsInf(v, sign) has a unique sign argument (example) — 0 both ways, 1 positive, -1 negative; Go has no IsFinite, so finiteness is hand-assembled as !IsInf && !IsNaN.
Java: Double.isInfinite (example) is true for both ±INF; integer division by zero throws ArithmeticException while float division by zero yields INF — Java draws two separate worlds for ints and floats.
JavaScript: INF is simply Number.POSITIVE_INFINITY (example); 1/0 gives Infinity with no error; the global isFinite coerces strings (isFinite("1") === true), while Number.isFinite (example) never converts and is the real test.
PHP: is_infinite (example) with the complementary is_finite; since PHP 8 both 1/0 and 1.0/0 throw DivisionByZeroError, and the only safe INF gateway is fdiv (its own entry on this site) — which makes PHP's INF show up with fewer surprises than elsewhere.
Python: math.isinf/math.isfinite (example); Python ints have unbounded precision and never overflow to INF (only floats do), and division by zero throws — INF must be constructed explicitly; the math.inf constant equals float("inf").
Rust: the f64::INFINITY constant plus is_infinite/is_finite (example); 1.0/0.0 is a compile-time constant-expression error (in literal contexts), while runtime variable division yields INF — the language catches half of it at compile time.
TypeScript: same as JS (example); Number.POSITIVE_INFINITY === Infinity is true, and the Infinity literal is directly usable.