# Why is Julia the only one that doesn't overflow?

**URL:** <https://discourse.julialang.org/t/why-is-julia-the-only-one-that-doesnt-overflow/135758>\
**Category:** General Usage\
**Tags:** question, integer-overflow, recursion\
**Created:** [February 20, 2026, 7:21pm UTC](https://discourse.julialang.org/t/why-is-julia-the-only-one-that-doesnt-overflow/135758 "2026-02-20T19:21:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cocoa1231](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cocoa1231/32/22215_2.png) [@cocoa1231](https://discourse.julialang.org/u/cocoa1231)\
**Post date:** [February 20, 2026, 7:21pm UTC](https://discourse.julialang.org/t/why-is-julia-the-only-one-that-doesnt-overflow/135758/1 "2026-02-20T19:21:05Z")

</div>

I have the following tail recursive fibonacci function in zig, rust, julia and c. For some reason, only the Julia version doesn’t overflow. Why is that?

```rust
fn fib(n: usize, p1: usize, p2: usize) -> usize {
    if n < 2 {
        return p1 + p2;
    }
    return fib(n - 1, p1 + p2, p1);
}

fn main() {
    dbg!(fib(100, 1, 1));
}

```

* * *

```zig
const std = @import("std");

fn fib(n: usize, p1: usize, p2: usize) usize {
    if (n < 2) {
        return p1 + p2;
    }
    return fib(n - 1, p1 + p2, p1);
}

pub fn main() !void {
    std.debug.print("fib(100, 1, 1) = {d}", .{fib(100, 1, 1)});
}

```

* * *

```C
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

uint64_t fib(uint64_t n, uint64_t p1, uint64_t p2) {
    if (n < 2)
        return p1 + p2;
    return fib(n - 1, p1 + p2, p1);
}

int main() {
    printf("fib(100, 1, 1) = %" PRIu64 "\n", fib(100, 1, 1));
    return 0;
}

```

* * *

```julia
function fib(n::UInt64, p1::UInt64, p2::UInt64) ::UInt64
    if (n < 2)
        return p1 + p2
    end
    return fib(n - 1, p1 + p2, p1)
end

@show fib(UInt64(100), UInt64(1), UInt64(1))

```

And running all of them:

```sh
$ rustc -o fibrs fib.rs && ./fibrs

thread 'main' (10899) panicked at fib.rs:5:23:
attempt to add with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
$ zig build-exe --name fibz fib.zig && ./fibz
thread 10921 panic: integer overflow
...
$ gcc -o fibc fib.c && ./fibc
fib(100, 1, 1) = 101
$ julia fib.jl
fib(UInt64(100), UInt64(1), UInt64(1)) = 0x45e1a61e5624f888

```

Why does Julia not overflow? Am I missing something? I think my implementations are all the same. The actual code I’ve uploaded to my GitHub

> **[GitHub - cocoa1231/fib\_uint\_overflow: Why is julia the only one that doesn't overflow?](https://github.com/cocoa1231/fib_uint_overflow)**
>
> Why is julia the only one that doesn't overflow?

* * *

Edit: I’m a dummy and my C code was buggy. Both julia and C overflow

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 20, 2026, 7:33pm UTC](https://discourse.julialang.org/t/why-is-julia-the-only-one-that-doesnt-overflow/135758/2 "2026-02-20T19:33:59Z")

</div>

> [@cocoa1231](#):
>
> Why does Julia not overflow?

It does overflow. It just doesn’t throw an error on overflow.

```julia-auto
julia> 0xff * 0xff
0x01

```

In particular, let me rewrite your `fib` function to work for any integer type:

```julia-auto
fib(n, p1, p2) = n < 2 ? p1 + p2 : fib(n - one(n), p1 + p2, p1)

```

(using `one(n)` to avoid accidentally using `1`, which is an `Int`). Then we can compute the exact result using `BigInt` and compare to the `UInt64` result:

```julia-auto
julia> exact = fib(big(100), big(1), big(1))
927372692193078999176

julia> uint64 = fib(UInt64(100), UInt64(1), UInt64(1))
0x45e1a61e5624f888

julia> BigInt(uint64)
5035488507601418376

julia> uint64 == exact
false

```

and we see that the `UInt64` calculation gave a different result due to overflow.

---

<div class="post-metadata">

**Author:** ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)\
**Post date:** [February 20, 2026, 7:34pm UTC](https://discourse.julialang.org/t/why-is-julia-the-only-one-that-doesnt-overflow/135758/3 "2026-02-20T19:34:35Z")

</div>

> [@cocoa1231](#):
>
> Why does Julia not overflow?

It seems like it does overflow anyway, but doesn’t tell you about it.

First, your C code has 2 bugs: you’re calling `fib(n - 1, p1 + p2, 1)` instead of `fib(n - 1, p1 + p2, p1)` and your format specifier `%d` is for `int`s. Apparently you need a different specifier: [https://stackoverflow.com/a/9225648](https://stackoverflow.com/a/9225648).

Putting this together, I get:

```julia-auto
> cat fib.c
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

uint64_t fib(uint64_t n, uint64_t p1, uint64_t p2) {
    if (n < 2)
        return p1 + p2;
    return fib(n - 1, p1 + p2, p1);
}

int main() {
    printf("fib(100, 1, 1) = %" PRIu64 "\n", fib(100, 1, 1));
    return 0;
}
> clang fib.c -o fib && ./fib
fib(100, 1, 1) = 5035488507601418376

```

Julia actually returns the same number:

```julia-auto
>>> 0x45e1a61e5624f888
5035488507601418376

```

Using Python’s unbounded integers, we get the correct answer:

```python
>>> def fib(n, p1, p2):
... if n < 2: return p1 + p2
... return fib(n-1, p1+p2, p1)
...     
>>> fib(100,1,1)
927372692193078999176

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [February 20, 2026, 7:35pm UTC](https://discourse.julialang.org/t/why-is-julia-the-only-one-that-doesnt-overflow/135758/4 "2026-02-20T19:35:33Z")

</div>

It is a feature of Julia. Relevant section of the manual:

> **[Overflow behavior - Integers and Floating-Point Numbers · The Julia Language](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#Overflow-behavior)**
>
> Integers and floating-point values are the basic building blocks of arithmetic and computation. Built-in representations of such values are called numeric primitives, while representations of integers and floating-point numbers as immediate values in...

I don’t know exactly the motivation of this feature, but I can imagine that since arithmetic operations on integers are everywhere in any program, not having to check overflow allows faster computations. If you want safe integers there is a very lightweight package that provides them:

> **[GitHub - JeffreySarnoff/SaferIntegers.jl: These integer types use checked arithmetic,...](https://github.com/JeffreySarnoff/SaferIntegers.jl)**
>
> These integer types use checked arithmetic, otherwise they are as system types.

---

<div class="post-metadata">

**Author:** ![cocoa1231](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cocoa1231/32/22215_2.png) [@cocoa1231](https://discourse.julialang.org/u/cocoa1231)\
**Post date:** [February 20, 2026, 7:35pm UTC](https://discourse.julialang.org/t/why-is-julia-the-only-one-that-doesnt-overflow/135758/5 "2026-02-20T19:35:54Z")

</div>

Oops mystery solved. Tysm!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 20, 2026, 7:42pm UTC](https://discourse.julialang.org/t/why-is-julia-the-only-one-that-doesnt-overflow/135758/6 "2026-02-20T19:42:37Z")

</div>

Note also that Rust also [allows overflow if you compile with the `--release` flag](https://doc.rust-lang.org/book/ch03-02-data-types.html#integer-overflow).
