# Typemax for T \<: Integer if T can be BigInt?

**URL:** https://discourse.julialang.org/t/typemax-for-t-integer-if-t-can-be-bigint/57001
**Category:** General Usage
**Created:** [March 12, 2021, 6:55am UTC](https://discourse.julialang.org/t/typemax-for-t-integer-if-t-can-be-bigint/57001 "2021-03-12T06:55:53Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![wherrera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wherrera/32/5288_2.png) [@wherrera](https://discourse.julialang.org/u/wherrera)
#### Post date: [March 12, 2021, 6:55am UTC](https://discourse.julialang.org/t/typemax-for-t-integer-if-t-can-be-bigint/57001/1 "2021-03-12T06:55:53Z")

</div>

I am trying to keep a function generic with regard to its integer arguments. One of the function portions is

```
if n > typemax(T) ÷ k
    break
end

```

and since T is Integer and could be BigInt, I would like a suggestion what to do about

```
julia> typemax(BigInt)
ERROR: MethodError: no method matching typemax(::Type{BigInt})

```

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [March 12, 2021, 7:47am UTC](https://discourse.julialang.org/t/typemax-for-t-integer-if-t-can-be-bigint/57001/2 "2021-03-12T07:47:16Z")

</div>

There are at least two approaches. One will be more appropriate. To write the function so BigInts are not accepted but all other built-in Integer types (or so all other built-in Signed Integer types) are accepted:

```julia
function for_builtin_fixedsize_integers(x::Base.BitInteger) ...
function for_builtin_fixedsuze_signedintegers(x::Base.BitSigned) ...

```

To accept BigInts and avoid the error that `typemax(BigInt)` triggers  
(a) rewrite your function so `typemax(T)` is not used  
or  
(b) rewrite your function so `typemax(BigInt)` is avoided  
consider using

```julia
const MaxBigInt = # whatever you choose
function avoids_error(x) # or (x::Signed) or (x::Integer)
   # code
   maxint = x isa BigInt ? MaxBigInt : typemax(typeof(x))
   # code
end

```

or an approach similar to

```julia
function avoids_error(x) # or (x::Signed) or (x::Integer)
   # code
   maxint = x isa BigInt ? x^2: typemax(typeof(x))
   # code
end

```

---

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [March 12, 2021, 8:00am UTC](https://discourse.julialang.org/t/typemax-for-t-integer-if-t-can-be-bigint/57001/3 "2021-03-12T08:00:04Z")

</div>

You can use the `Base.hastypemax` trait, in order to conditionally make a check only for bounded integer types, e.g.

```julia
if hastypemax(T) && n > typemax(T) ÷ k
    break
end

```

Note that `Base.hastypemax` is currently not public API, there is an issue on this: [Rational of `Base.hastypemax(BigInt)` (not exported) vs `typemax(BigInt) = Inf` · Issue #39344 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/39344).
