intdiv
intdiv(int $num1, int $num2): int
Integer division: the quotient truncates toward zero; dividing by zero throws DivisionByZeroError.
C C23
#include <stdio.h>
int main(void) {
// Since C99, / on integers is truncating division (toward zero)
printf("%d %d\n", 7 / 2, -7 / 2);
printf("%g\n", 7.0 / 2);
return 0;
}
3 -3 3.5
C++ C++26
#include <iostream>
int main() {
// Since C++11, / on integers is truncating division (toward zero)
std::cout << 7 / 2 << " " << -7 / 2 << "\n";
std::cout << 7.0 / 2 << "\n";
return 0;
}
3 -3 3.5
C# v11.0.100
// Integer / is truncating division (toward zero)
Console.WriteLine($"{7 / 2} {-7 / 2}");
Console.WriteLine(7 / 2.0);
3 -3 3.5
Go v1.27.4
package main
import "fmt"
func main() {
// Go's / on integers is truncating division (toward zero)
fmt.Println(7/2, -7/2)
// Float division needs explicit conversion
fmt.Println(7.0 / 2)
}
3 -3 3.5
Java v27.0.1
public class Main {
public static void main(String[] args) {
// Integer / is truncating division (toward zero)
System.out.println(7 / 2 + " " + -7 / 2);
// Math.floorDiv is floor semantics (different results for negatives)
System.out.println(Math.floorDiv(-7, 2));
System.out.println(7 / 2.0);
}
}
3 -3 -4 3.5
JavaScript v26.13.1
// JS has only float division; truncate with Math.trunc
console.log(Math.trunc(7 / 2), Math.trunc(-7 / 2));
// Math.floor is floor semantics (different results for negatives)
console.log(Math.floor(-7 / 2));
console.log(7 / 2);
3 -3 -4 3.5
PHP v8.6.0
// intdiv: integer division, quotient truncated toward zero
var_dump(intdiv(7, 2), intdiv(-7, 2));
// / is float division: 7/2 gives 3.5
var_dump(7 / 2);
int(3) int(-3) float(3.5)
Python v3.15.2
# // is floor division: negative quotients round down (not truncation!)
print(7 // 2, -7 // 2)
# int() truncation emulates intdiv: int(-7 / 2) gives -3
print(int(-7 / 2))
print(7 / 2)
3 -4 -3 3.5
Rust v1.99.1
fn main() {
// Integer / is truncating division (toward zero); dividing by 0 panics
println!("{} {}", 7 / 2, -7 / 2);
// div_euclid is Euclidean division (non-negative remainder) for contrast
println!("{}", (-7i32).div_euclid(2));
println!("{}", 7.0 / 2.0);
}
3 -3 -4 3.5
TypeScript v26.13.1
// TS has only float division; truncate with Math.trunc
const q: number = Math.trunc(-7 / 2);
console.log(Math.trunc(7 / 2), q);
console.log(Math.floor(-7 / 2));
console.log(7 / 2);
3 -3 -4 3.5
Language differences
C: / with integer operands truncates — C99 mandates truncation toward zero; math.h's div returns the quotient-remainder pair.
C++: same truncation as C (mandatory since C++11); div_t (std::div) returns the quotient-remainder pair.
C#: integer / truncates; Math.DivRem returns the quotient-remainder pair.
Go: integer / truncates (toward zero); there is no separate intdiv — just /, and floating-point division needs an explicit conversion.
Java: integer / truncates; Math.floorDiv/floorMod offer the floor semantics (different results for negatives).
JavaScript: there is no integer type — / is always floating point; truncate with Math.trunc (avoid | 0, a 32-bit overflow trap).
PHP: / is always floating point (7/2 yields 3.5), so intdiv is the only integer-division entry — negative quotients truncate toward zero (-3) and a zero divisor throws DivisionByZeroError; %'s remainder takes the sign of the dividend.
Python: an outlier — // is floor division, so -7 // 2 yields -4 (rounded down) rather than -3; for truncation use int(-7 / 2) or math.trunc; a zero divisor raises ZeroDivisionError.
Rust: integer / truncates (toward zero); div_euclid provides Euclidean semantics (non-negative remainder); division by zero panics (checked_div probes safely).
TypeScript: same as JavaScript's Math.trunc.