Integer Overflow

The outlier that promotes to float — PHP integer overflow surveyed against overflow strategies in 10 languages.

C C23

C23
#include <limits.h>
#include <stdio.h>

/* signed overflow in C is undefined behavior, so the demo wraps an
   unsigned value, which is defined as modulo 2^32 */
int main(void) {
    printf("%d\n", INT_MAX);
    unsigned int u = UINT_MAX;
    printf("%u\n", u + 1);
    return 0;
}
Output
2147483647
0

C++ C++26

C++26
#include <climits>
#include <cstdio>

// signed overflow is undefined behavior in C++ as well, so the demo
// wraps an unsigned value instead, which is well-defined
int main() {
    std::printf("%d\n", INT_MAX);
    unsigned int u = UINT_MAX;
    std::printf("%u\n", u + 1);
}
Output
2147483647
0

C# v11.0.100

v11.0.100
Console.WriteLine(int.MaxValue);
var max = int.MaxValue;
int over = unchecked(max + 1); // wraparound needs an explicit unchecked here:
                               // file-based apps compile checked by default
Console.WriteLine(over);
try {
    int boom = checked(max + 1); // opt in to the runtime check
    Console.WriteLine(boom);
} catch (OverflowException) {
    Console.WriteLine("checked: OverflowException");
}
Output
2147483647
-2147483648
checked: OverflowException

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"math"
)

// Go ints wrap around silently at runtime; note that a constant overflow
// like math.MaxInt + 1 is rejected at compile time, so a variable is used
func main() {
	fmt.Println(math.MaxInt)
	maxInt := math.MaxInt
	wrapped := maxInt + 1 // runtime: silent wraparound
	fmt.Println(wrapped)
	fmt.Println(wrapped == math.MinInt)
}
Output
9223372036854775807
-9223372036854775808
true

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        int over = Integer.MAX_VALUE + 1; // silent wraparound, no error
        System.out.println(over);
        try {
            Math.addExact(1, Integer.MAX_VALUE); // opt-in checked math
        } catch (ArithmeticException e) {
            System.out.println("addExact: ArithmeticException");
        }
    }
}
Output
2147483647
-2147483648
addExact: ArithmeticException

JavaScript v26.13.1

v26.13.1
console.log(Number.MAX_SAFE_INTEGER); // 2^53 - 1
// beyond the safe range the float64 format silently loses precision
console.log(Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2);
const big = BigInt(Number.MAX_SAFE_INTEGER) + 2n; // arbitrary precision
console.log(big.toString());
Output
9007199254740991
true
9007199254740993

PHP v8.6.0

v8.6.0
// PHP is the outlier: instead of wrapping or trapping, an overflow
// silently promotes the value to float — the int is gone
$max = PHP_INT_MAX;
var_dump($max);                    // int(...9223372036854775807)
$over = $max + 1;
var_dump($over);                   // float — the type itself changed

echo get_debug_type($over), "\n";

var_dump(PHP_INT_SIZE);            // int(8): 64-bit platform word size
Output
int(9223372036854775807)
float(9.223372036854776E+18)
float
int(8)

Python v3.15.2

v3.15.2
import sys
print(sys.maxsize)    # the platform word size, not a hard limit
big = sys.maxsize + 1 # ints grow without bound: no overflow exists
print(big)
print(big.bit_length())
Output
9223372036854775807
9223372036854775808
64

Rust v1.99.1

v1.99.1
// release builds wrap, debug builds panic; the explicit API makes the
// choice a property of the code instead of the build profile
fn main() {
    let max = i64::MAX;
    let wrapped = max.wrapping_add(1); // defined in both profiles
    println!("{}", wrapped);
    println!("{:?}", max.checked_add(1)); // overflow surfaced as a value
}
Output
-9223372036854775808
None

TypeScript v26.13.1

v26.13.1
console.log(Number.MAX_SAFE_INTEGER); // 2^53 - 1
// beyond the safe range the float64 format silently loses precision
console.log(Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2);
const big: bigint = BigInt(Number.MAX_SAFE_INTEGER) + 2n; // arbitrary precision
console.log(big.toString());
Output
9007199254740991
true
9007199254740993

Language differences

C: signed overflow is UB (example): the compiler may assume signed math never overflows and optimize on that, so the actual behavior is not portable; unsigned arithmetic wraps modulo 2^N by definition; the practical exit is check-before-compute (the __builtin_add_overflow family is a GCC/Clang extension).

C++: the same UB as C (example): unsigned wraparound is well-defined; the standard library ships no checked integers through C++17; safe exits are the C builtins or third-party libraries (safeNumerics); compiler switches (-ftrapv) are debugging aids only.

C#: checked/unchecked dual mode (example): the language defaults to unchecked (wraparound) and checked expressions or blocks throw OverflowException; a project property flips the default globally; .NET 10+ file-based apps (dotnet run file.cs) compile checked by default — the example needs an explicit unchecked to wrap; behavior as measured on this machine.

Go: silent runtime wraparound (example): int overflow wraps to MinInt with no diagnostic; but constant overflow is a compile error (the example had to use a variable) — Go constants are arbitrary-precision and only land in an int at assignment; the standard library has no checked math, so bounds checks are hand-rolled.

Java: silent wraparound plus opt-in checks (example): int overflow wraps and the JLS guarantees it (two's complement), while Math.addExact* throws ArithmeticException on overflow; long likewise; no overflow traps at the bytecode level.

JavaScript: no integer type (example): Number is float64, safe integers stop at 2^53-1, and precision silently degrades beyond that (the example asserts MAX_SAFE_INTEGER+1 === MAX_SAFE_INTEGER+2); BigInt (ES2020) adds arbitrary precision — an explicit n suffix, never auto-mixed with Number.

PHP: overflow promotes to float (example): the outlier of the ten — an int past its bounds neither wraps nor errors but silently promotes to float (the example asserts the type changed from int to float); the consequences are a precision cliff (float64 carries roughly 15-16 significant digits) and type-sensitive code (a strict int parameter will reject the value); PHP_INT_MIN behaves likewise; arbitrary precision goes through the GMP/BCMath extensions; on 32-bit platforms PHP_INT_MAX is 2147483647 with the same promotion.

Python: arbitrary precision (example): int is unbounded (memory permitting) and sys.maxsize is a word-size hint, not a boundary — the concept of overflow does not exist (the control group); the price is multi-precision arithmetic, slower than fixed-width integers.

Rust: profile-dependent plus explicit API (example): release builds wrap and debug builds panic (overflow-checks), so the same code behaves differently per profile — as of 1.80 the Cargo profile still decides; wrapping_add/checked_add/saturating_add put the policy in the code (the example asserts None from checked_add); conversions like u64::try_from a negative have try/wrapping variants too.

TypeScript: at the language level the same as JS (example): number is float64 and bigint is arbitrary-precision; the type system adds one layer — tsc errors on bigint literals beyond float64's safe range (since 4.1), while number literals are unchecked.