# Returning Inf of specific type

**URL:** <https://discourse.julialang.org/t/returning-inf-of-specific-type/17138>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 4, 2018, 2:56pm UTC](https://discourse.julialang.org/t/returning-inf-of-specific-type/17138 "2018-11-04T14:56:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)\
**Post date:** [November 4, 2018, 2:56pm UTC](https://discourse.julialang.org/t/returning-inf-of-specific-type/17138/1 "2018-11-04T14:56:11Z")

</div>

I would like for a function to return `Inf` in some cases. However, the type of `Inf` should depend on the floating type of the function arguments. That is, I would like the following

```julia
function f(x::R) where {R <: Real}
  if # some condition
    return Inf
  end
end

```

to return `Inf16` if `R == Float16`, `Inf32` if `R == Float32`, `Inf64` if `R == Float64`. However, there seem not to be a way of constructing `Inf` of a given type `R <: Real`, unlike e.g. `zero`. Is there a way to do this?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 4, 2018, 2:59pm UTC](https://discourse.julialang.org/t/returning-inf-of-specific-type/17138/2 "2018-11-04T14:59:01Z")

</div>

You can write `R(Inf)` or `convert(R, Inf)`.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [November 4, 2018, 3:47pm UTC](https://discourse.julialang.org/t/returning-inf-of-specific-type/17138/3 "2018-11-04T15:47:50Z")

</div>

R(Inf) doesn’t work for integers. Consider restricting R to AbstractFloat

```julia
function f(x::R) where {R <: AbstractFloat}
  if # some condition
    return R(Inf)
  end
end

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 4, 2018, 4:14pm UTC](https://discourse.julialang.org/t/returning-inf-of-specific-type/17138/4 "2018-11-04T16:14:26Z")

</div>

Right, depending on what you want you may want to restrict the method or call `typemax` which will be a typed infinity for float types.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 4, 2018, 5:58pm UTC](https://discourse.julialang.org/t/returning-inf-of-specific-type/17138/5 "2018-11-04T17:58:20Z")

</div>

You can also use

```julia
function f(x)
    if stuff
        return oftype(x, Inf)
    end
    ...
end

```

which should lead to equivalent code, but is more compact if you don’t need `R`.

---

<div class="post-metadata">

**Author:** ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)\
**Post date:** [November 4, 2018, 8:37pm UTC](https://discourse.julialang.org/t/returning-inf-of-specific-type/17138/6 "2018-11-04T20:37:46Z")

</div>

Wow, thanks a lot, all viable solutions. I marked Stefan’s as solution just because it is closest to what I was trying to do. But it’s all stuff to consider. Thanks again.
