# What is the fastest way to obtain an infinity of the correct type?

**URL:** https://discourse.julialang.org/t/what-is-the-fastest-way-to-obtain-an-infinity-of-the-correct-type/88579
**Category:** General Usage
**Tags:** math
**Created:** [October 11, 2022, 2:48pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-way-to-obtain-an-infinity-of-the-correct-type/88579 "2022-10-11T14:48:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [October 11, 2022, 2:48pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-way-to-obtain-an-infinity-of-the-correct-type/88579/1 "2022-10-11T14:48:19Z")

</div>

This is related to [Analogue of zero(T) for infinitiy - #5 by rdeits](https://discourse.julialang.org/t/analogue-of-zero-t-for-infinitiy/7027/5), but the suggestion

```julia
T(Inf)

```

isn’t as fast as expected. In particular,

```julia
julia> @btime convert(Float16, $(Ref(Inf))[]);
  9.205 ns (0 allocations: 0 bytes)

```

but I know statically that the answer must be `Inf16`, so ideally this shouldn’t carry out any conversion at all. Is there a faster way to compute this result?

---

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [October 11, 2022, 2:56pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-way-to-obtain-an-infinity-of-the-correct-type/88579/2 "2022-10-11T14:56:16Z")

</div>

Is that really testing what you want? I’ve never used Refs in timing, have seen it recommended, but I’m not sure exactly what happens behind the scenes. I would do

```julia
julia> @btime convert(Float16, x) setup=(x=Inf)
  1.691 ns (0 allocations: 0 bytes)
Inf16

julia> @code_llvm convert(Float16, Inf)
; @ number.jl:7 within `convert`
define half @julia_convert_992(double %0) #0 {
top:
; ┌ @ float.jl:232 within `Float16`
   %1 = fptrunc double %0 to half
; └
  ret half %1
}

```

which seems fast and comes down to a single llvm instruction.

Wouldn’t `typemax(T)` be a good way to get infinity for the types that support it, and for those that don’t you get the closest approximation?

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [October 11, 2022, 3:03pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-way-to-obtain-an-infinity-of-the-correct-type/88579/3 "2022-10-11T15:03:06Z")

</div>

In general, const prop would handle this.

```julia
julia> inf(::Type{T}) where {T} = convert(T, Inf)
inf (generic function with 1 method)

julia> @code_typed inf(Float16)
CodeInfo(
1 ─ return Inf16
) => Float16

```

but yeah, I’d use `typemax(Float16)`.

```julia
julia> @code_typed typemax(Float16)
CodeInfo(
1 ─ return Inf16
) => Float16

```

given it’s already defined and does what you want.
