# Determinant of an integer matrix is float

**URL:** https://discourse.julialang.org/t/determinant-of-an-integer-matrix-is-float/111107
**Category:** General Usage
**Tags:** linearalgebra
**Created:** [March 3, 2024, 10:26pm UTC](https://discourse.julialang.org/t/determinant-of-an-integer-matrix-is-float/111107 "2024-03-03T22:26:12Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [March 3, 2024, 10:26pm UTC](https://discourse.julialang.org/t/determinant-of-an-integer-matrix-is-float/111107/1 "2024-03-03T22:26:12Z")

</div>

Why isn’t it an int?

```julia
julia> a, c, b, d = M = [10 20; 30 40]; a*d - b*c , det(M)
(-200, -200.00000000000003)

```

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [March 3, 2024, 10:52pm UTC](https://discourse.julialang.org/t/determinant-of-an-integer-matrix-is-float/111107/2 "2024-03-03T22:52:49Z")

</div>

> [@Possible bug in determinant calculation](https://discourse.julialang.org/t/possible-bug-in-determinant-calculation/20323/8):
>
> You can do this without overflowing by using Rational{BigInt}, although this can get expensive quickly. I don’t think it should use BigInt unless you explicitly request this. There are specialized algorithms for integer determinants (e.g. [Dodgeson condensation](https://en.wikipedia.org/wiki/Dodgson_condensation)), but in general doing computational number theory efficiently is probably outside the scope of the standard LinearAlgebra library and is more something for packages like Nemo.jl.

---

<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: [March 4, 2024, 12:33am UTC](https://discourse.julialang.org/t/determinant-of-an-integer-matrix-is-float/111107/3 "2024-03-04T00:33:24Z")

</div>

Nowadays, Julia includes an integer-determinant algorithm (the [Bareiss algorithm](https://en.wikipedia.org/wiki/Bareiss_algorithm)), but only uses it by default for `BigInt` matrices (since for fixed-width integer types like the default `Int` a determinant will quickly overflow unless the matrix is tiny).

```julia
julia> using LinearAlgebra

julia> M = BigInt[10 20; 30 40]
2×2 Matrix{BigInt}:
 10 20
 30 40

julia> det(M) # exact BigInt determinant
-200

```

You can invoke the Bareiss algorithm explicitly for other integer types by calling `LinearAlgebra.det_bareiss`, but beware that it can easily overflow for larger matrices:

```julia
julia> LinearAlgebra.det_bareiss([10 20; 30 40]) # exact Int determinant
-200

```

For example, with a 100 \times 100 matrix of random ±1 entries:

```julia
julia> A = rand([-1,1], 100,100);

julia> det(A) # floating-point approximation: fast and reasonably accurate
7.014043363270618e77

julia> det(BigInt.(A)) # exact
701404336326996708202480358186962679200983525458711992094310103304386652405760

julia> LinearAlgebra.det_bareiss(A) # overflows — bogus answer
746

```

PS. I just pushed a PR to clarify the `det` documentation: [document exact BigInt determinants by stevengj · Pull Request #53579 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/53579)

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [March 4, 2024, 1:23am UTC](https://discourse.julialang.org/t/determinant-of-an-integer-matrix-is-float/111107/4 "2024-03-04T01:23:48Z")

</div>

Using the wikipedia link in stevengj’s answer, the following function might be useful:

```julia
bareiss_bits(M) = (n = size(M,1); 
  ndigits(maximum(abs,M); base=2)*n+ceil(Int,log2(n)*n/2))

```

It calculates a bound on the bits needed to calculate `det_bareiss` and thus, if it is less than 64 or 128, Int64 and Int128 can be used, respectively (not exact, but hopefullly a conservative evaluation and thus safe).

---

<div class="post-metadata">

### Author: ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)
#### Post date: [March 4, 2024, 4:42am UTC](https://discourse.julialang.org/t/determinant-of-an-integer-matrix-is-float/111107/5 "2024-03-04T04:42:13Z")

</div>

The problem/concern I think with that is that it makes the return type of `det` depend on not just the type but also the values of the arguments.
