fdiv
fdiv(float $num1, float $num2): float
Safe float division (PHP 8.0+): division by zero and invalid input don't throw DivisionByZeroError — IEEE semantics return INF/-INF/NAN.
C C23
#include <math.h>
#include <stdio.h>
int main(void) {
/* C follows IEEE: float division by zero yields INF/NAN outright; integer division by zero is undefined behavior (UB) */
double inf = 1.0 / 0.0;
double ninf = -1.0 / 0.0;
double nan_v = 0.0 / 0.0;
printf("%d %d %d\n", isinf(inf) != 0, isinf(ninf) != 0, isnan(nan_v) != 0);
printf("%d\n", 10.0 / 4.0 == 2.5);
return 0;
}
1 1 1 1
C++ C++26
#include <cmath>
#include <iostream>
int main() {
// C++ follows IEEE: float division by zero yields INF/NAN; integer division by zero is UB too
double inf = 1.0 / 0.0;
double ninf = -1.0 / 0.0;
double nan_v = 0.0 / 0.0;
std::cout << std::boolalpha;
std::cout << std::isinf(inf) << " " << std::isinf(ninf) << " " << std::isnan(nan_v) << "\n";
std::cout << (10.0 / 4.0 == 2.5) << "\n";
return 0;
}
true true true true
C# v11.0.100
// C# splits into two worlds: float division by zero yields INF/NAN (IEEE), integer division by zero throws DivideByZeroException
double zeroF = 0.0;
Console.WriteLine($"{double.IsInfinity(1.0 / zeroF)} {double.IsNaN(0.0 / zeroF)}");
Console.WriteLine(10.0 / 4.0);
int zeroI = 0;
try
{
_ = 1 / zeroI;
}
catch (DivideByZeroException e)
{
Console.WriteLine(e.GetType().Name);
}
True True 2.5 DivideByZeroException
Go v1.27.4
package main
import (
"fmt"
"math"
)
func main() {
// Go splits into two worlds: float division by zero yields INF/NAN (IEEE), integer division by zero panics at runtime
var zeroF float64
fmt.Println(math.IsInf(1.0/zeroF, 1), math.IsNaN(0.0/zeroF))
fmt.Println(10.0 / 4.0)
var zeroI int
func() {
defer func() {
if p := recover(); p != nil {
fmt.Println("panic:", p)
}
}()
_ = 1 / zeroI
}()
}
true true 2.5 panic: runtime error: integer divide by zero
Java v27.0.1
public class Main {
public static void main(String[] args) {
// Java splits into two worlds: float division by zero yields INF/NAN (IEEE), integer division by zero throws ArithmeticException
System.out.println(Double.isInfinite(1.0 / 0.0) + " " + Double.isNaN(0.0 / 0.0));
System.out.println(10.0 / 4.0);
try {
int zero = 0;
int boom = 1 / zero;
System.out.println(boom);
} catch (ArithmeticException e) {
System.out.println(e.getClass().getSimpleName());
}
}
}
true true 2.5 ArithmeticException
JavaScript v26.13.1
// JS is pure IEEE camp: even integers are floats — 1/0 gives Infinity, 0/0 gives NaN, nothing ever throws
console.log(1 / 0 === Infinity, Number.isNaN(0 / 0));
console.log(10 / 4);
true true 2.5
PHP v8.6.0
// fdiv (PHP 8.0+): safe division — zero/invalid divisors never throw DivisionByZeroError, returning INF/-INF/NAN per IEEE
var_dump(fdiv(10, 4));
var_dump(fdiv(1, 0)); // +INF
var_dump(fdiv(-1, 0)); // -INF
var_dump(fdiv(0, 0)); // NAN
var_dump(is_infinite(fdiv(1, 0)));
var_dump(is_nan(fdiv(0, 0)));
// For contrast: the / operator sides with the throwing camp — fdiv is the compromise entry point between the two
try {
1 / 0;
} catch (DivisionByZeroError $e) {
echo get_class($e), "\n";
}
float(2.5) float(INF) float(-INF) float(NAN) bool(true) bool(true) DivisionByZeroError
Python v3.15.2
import math
# Python is the most thorough member of the throwing camp: even float division by zero raises ZeroDivisionError (for INF you must build float("inf") explicitly)
try:
_ = 1.0 / 0.0
except ZeroDivisionError as e:
print(type(e).__name__)
inf = float("inf")
print("isinf:", math.isinf(inf), "isnan:", math.isnan(float("nan")))
print(10 / 4)
ZeroDivisionError isinf: True isnan: True 2.5
Rust v1.99.1
fn main() {
// Rust splits into two worlds: float division by zero yields INF/NAN (IEEE), integer division by zero panics at runtime;
// the literal 1.0/0.0 is a compile-time error — use a variable (fdiv is exactly PHP's compromise entry between the two camps)
let zero_f = 0.0f64;
println!("{} {}", (1.0 / zero_f).is_infinite(), (0.0 / zero_f).is_nan());
println!("{}", 10.0 / 4.0);
let zero_i: i32 = 0;
std::panic::set_hook(Box::new(|_| {})); // Silence the default panic output to keep stdout clean
let panicked = std::panic::catch_unwind(|| {
let _ = 1i32 / zero_i;
})
.is_err();
println!("integer div panicked: {}", panicked);
}
true true 2.5 integer div panicked: true
TypeScript v26.13.1
// TS is pure IEEE camp: even integers are floats — 1/0 gives Infinity, 0/0 gives NaN, nothing ever throws
console.log(1 / 0 === Infinity, Number.isNaN(0 / 0));
console.log(10 / 4);
true true 2.5
Language differences
C: IEEE camp — float division by zero yields INF/NAN directly; integer division by zero is UB (not an exception, not a panic — the compiler may optimize arbitrarily), so guard the denominator yourself.
C++: same as C (IEEE + integer-div-by-zero UB).
C#: floats follow IEEE to INF/NAN (1.0/0 is INF, 0.0/0 is NAN); integer division throws DivideByZeroException — two behaviors side by side, no fdiv-style unified entry.
Go: floats follow IEEE to INF/NAN; integer division panics at runtime (runtime error: integer divide by zero); literal constant division by zero is a compile error.
Java: floats follow IEEE to INF/NAN; integer division throws ArithmeticException — a dual world mirroring C#.
JavaScript: pure IEEE — ints are floats too; 1/0 gives Infinity, 0/0 gives NaN, nothing ever throws (closest in behavior to fdiv, though with no dedicated entry).
PHP: the / operator (since PHP 8) takes the exception side — division by zero throws DivisionByZeroError; fdiv is the compromise entry point, returning INF/-INF/NAN per IEEE (pair with the is_infinite/is_nan entries) — the safe option for tolerance-critical numeric/crypto code.
Python: the most thorough exception camp — even float division by zero throws ZeroDivisionError; INF only via explicit float("inf")/math.inf.
Rust: floats follow IEEE to INF/NAN (but literal 1.0/0.0 is a compile error — go through a variable); integer division panics at runtime.
TypeScript: same as JavaScript (1/0 === Infinity, nothing ever throws).